struct CrystGLFW::Window

Overview

A Window represents a GLFW Window and its associated OpenGL context.

Defined in:

crystglfw/windows/cursor.cr
crystglfw/windows/image.cr
crystglfw/windows/window.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.current : Window #

Returns the window whose context is current.

current_window = CrystGLFW::Window.current

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def self.new(width = 640, height = 480, title = "", monitor = nil, sharing_window = nil, hints = nil) #

Creates a new GLFW window.

# Create a window that is 640x480, titled "My Test Window".
window = CrystGLFW::Window.new(width: 640, height: 480, title: "My Test Window")

This method accepts the following arguments:

  • width, the desired width of the window in screen coordinates.
  • height, the desired height of the window in screen coordinates.
  • title, the title of the window.
  • monitor, the monitor to use for full screen mode. If left blank, the context will run in windowed mode.
  • sharing_window, the window whose context to share resources with. If left blank, no resource sharing occurs.
  • hints, the desired window options. If left blank, GLFW defaults will be used.

There are several different kinds of hints that can be set, all of which can be found in the GLFW documentation. To specify hints for window creation, create a hash:

hints = {:client_api => :opengl_es_api, :context_version_major => 3}
window = CrystGLFW::Window.new(title: "My Window", monitor: CrystGLFW::Monitor.primary, hints: hints)

Hints are cleared after window creation, so they are only used when passed.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]

Instance Method Detail

def aspect_ratio=(ratio : NamedTuple()) #

Alternate syntax for #set_aspect_ratio.

# Sets the window's aspect ratio to 16:9.
window.aspect_ratio = {numerator: 16, denominator: 9}

This method accepts the following arguments:

  • ratio, the desired aspect ratio.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def clipboard : String #

Returns the contents of the system clipboard as a String.

puts window.clipboard # Prints the contents of the clipboard.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def clipboard=(clipboard_string : String) #

Alternate syntax for #set_clipboard.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def contains?(coordinates : NamedTuple()) : Bool #

Alternate syntax for #contains?.

window.position = {x: 100, y: 100}
window.size = {width: 100, height: 100}

# Check if the location (150, 150) is inside the window.
window.contains? {x: 150, y: 150} # => true

# Check if the location (1000, 1000) is inside the window.
window.contains? {x: 1000, y: 1000} # => false

This method accepts the following arguments: -coordinates, the coordinates to check.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def contains?(x : Number, y : Number) : Bool #

Returns true if the window contains the given coordinates. False otherwise.

window.position = {x: 100, y: 100}
window.size = {width: 100, height: 100}

# Check if the location (150, 150) is inside the window.
window.contains? 150, 150 # => true

# Check if the location (1000, 1000) is inside the window.
window.contains? 1000, 1000 # => false

This method accepts the following arguments:

  • x, the x-coordinate to check.
  • y, the y-coordinate to check.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def corners : NamedTuple() #

Returns the corners of the window.

window.position = {x: 100, y: 100}

# Retrieve the window's corners.
c = window.corners

# Get the top left corner coordinates.
tl = c[:top_left]

# Print the coordinates.
puts "Top Left: (#{tl[:x]}, #{tl[:y]})" # => "Top Left: (100, 100)"

Each corner is labeled accordingly: top_left, top_right, bottom_right, bottom_left

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def cursor(shape : Symbol) #

Creates a cursor for the window with the given shape.

# Creates a crosshair cursor
cursor = window.cursor :crosshair_cursor

# Creates an ibeam cursor
cursor = window.cursor :ibeam_cursor

This method accepts the following arguments:

  • shape, a symbol representing the desired shape of the cursor.

shape can be one of the following values:

  • :arrow_cursor
  • :ibeam_cursor
  • :crosshair_cursor
  • :vresize_cursor
  • :hresize_cursor
  • :hand_cursor

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def cursor(image : Image, x : Int32, y : Int32) #

Creates a cursor that looks like image with hotspot at (x, y).

# Create a cursor that looks like cool_image, with hotspot at (12, 12)
cursor = window.cursor cool_image, 12, 12

This method accepts the following arguments:

  • image, the desired cursor image.
  • x, the x-coordinate of the cursor hotspot, relative to the top-left corner of the image..
  • y, the y-coordinate of the cursor hotspot, relative to the top-left corner of the image.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def cursor #

Returns the window's current cursor or creates one if it does not exist.

cursor = window.cursor

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def cursor_disabled? #

Returns true if the window's cursor mode is disabled. False otherwise.

if window.cursor_disabled?
  puts "The window's cursor mode is disabled!"
end

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def cursor_hidden? #

Returns true if the window's cursor mode is hidden. False otherwise.

if window.cursor_hidden?
  puts "The window's cursor mode is hidden!"
end

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def cursor_normal? #

Returns true if the window's cursor mode is normal. False otherwise.

if window.cursor_normal?
  puts "The window's cursor mode is normal!"
end

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def decorated? : Bool #

Returns true if the window is decorated. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def destroy #

Destroys the underlying GLFW Window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def disable_cursor #

Sets the cursor mode to disabled.

window.disable_cursor

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def disable_sticky_keys #

Disables sticky keys.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def disable_sticky_mouse_buttons #

Disables sticky mouse buttons.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def enable_sticky_keys #

Enables sticky keys.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def enable_sticky_mouse_buttons #

Enables sticky mouse buttons.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def exit_full_screen(x : Int32 = 100, y : Int32 = 100, width : Int32 = 640, height : Int32 = 480) #

Exits full screen mode and enters windowed mode with the given settings.

if window.full_screen?
  # Enter windowed mode at (50, 50) with dimensions 800x600.
  window.exit_full_screen(x: 50, y: 50, width: 800, height: 600)
end

This method accepts the following arguments:

  • x, the x-coordinate of the window's desired position.
  • y, the y-coordinate of the window's desired position.
  • width, the width of the window.
  • height, the height of the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def floating? : Bool #

Returns true if the window is floating. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def focus #

Focuses the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def focused? : Bool #

Returns true if the window is focused. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def frame_size : NamedTuple() #

Returns the size of the window's frame at each of its edges in screen coordinates.

# Retrieve the size of the window's frame along the top edge.
title_bar_height = window.frame_size[:top]

# Print out the title bar's height.
puts "This window's title bar is #{title_bar_height} screen coordinates tall."

Each edge of the window is accessible: left, top, right, bottom.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def framebuffer_size : NamedTuple() #

Returns the size of the framebuffer.

# Retrieve the framebuffer's size.
fb_size = window.framebuffer_size

# Print the size of the framebuffer.
puts "The size of the framebuffer is #{fb_size[:width]} x #{fb_size[:height]}."

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def full_screen? : Bool #

Returns true if the window is full screened. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def hide #

Hides the window (makes it invisible). The exact opposite of #show.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def hide_cursor #

Sets the cursor mode to hidden.

window.hide_cursor

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def icon=(icon : Image) #

Alternate syntax for #set_icon

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def iconified? : Bool #

Returns true if the window is iconified (minimized). False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def iconify #

Iconifies (minimizes) the window. The exact opposite of #restore.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def key_pressed?(key : CrystGLFW::Key) #

Checks to see if the given key is pressed according to this window.

key = event.key
if window.key_pressed?(key)
  puts "key is pressed!"
end

The key's state is affected by sticky keys.

This method accepts the following arguments:

  • key, the key to check.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def make_context_current #

Sets the window to be the current OpenGL context.

window = Window.new(title: "Awesome window")
window.make_context_current
until window.should_close?
  window.wait_events
  window.swap_buffers
end

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def maximize #

Maximizes the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def maximized? : Bool #

Returns true if the window is maximized. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def monitor : Monitor #

Returns the window's monitor if the window is in fullscreen mode. Returns nil otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def monitor=(mon : CrystGLFW::Monitor) #

Alternate syntax for #set_monitor

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def mouse_button_pressed?(mouse_button : CrystGLFW::MouseButton) #

Checks to see if the given mouse button is pressed according to this window.

mouse_button = event.mouse_button
if window.mouse_button_pressed?(mouse_button)
  puts "mouse button is pressed!"
end

The mouse button's state is affected by sticky mouse buttons.

This method accepts the following arguments:

  • mouse_button, the mouse button to check.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def normalize_cursor #

Sets the cursor mode to normal.

window.normalize_cursor

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def on_char(&callback : CharCallback) #

def on_close(&callback : CloseCallback) #

def on_cursor_cross_threshold(&callback : CursorCrossThresholdCallback) #

def on_cursor_move(&callback : CursorMoveCallback) #

def on_file_drop(&callback : FileDropCallback) #

def on_framebuffer_resize(&callback : FramebufferResizeCallback) #

def on_key(&callback : KeyCallback) #

def on_mouse_button(&callback : MouseButtonCallback) #

def on_move(&callback : MoveCallback) #

def on_refresh(&callback : RefreshCallback) #

def on_resize(&callback : ResizeCallback) #

def on_scroll(&callback : ScrollCallback) #

def on_toggle_focus(&callback : ToggleFocusCallback) #

def on_toggle_iconification(&callback : ToggleIconificationCallback) #

def position : NamedTuple() #

Returns the position of the window, in screen coordinates, as a named tuple.

pos = window.position
puts "The window's top left corner is at (#{pos[:x]}, #{pos[:y]})."

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def position=(pos : NamedTuple()) #

Alternate syntax for #set_position.

# Set the window's top left corner at (150, 150).
window.position = {x: 150, y: 150}

This method accepts the following arguments:

  • pos, the desired position of the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def remove_cursor #

Removes the window's current cursor.

window.remove_cursor if window.should_close?

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def resizable? : Bool #

Returns true if the window is resizable. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def restore #

Restores (de-minimizes) the window. The exact opposite of #iconify.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def set_aspect_ratio(numerator : Int32 = CrystGLFW[:dont_care], denominator : Int32 = CrystGLFW[:dont_care]) #

Sets the aspect ratio of the window.

# Sets the window's aspect ratio to 16:9.
window.set_aspect_ratio 16, 9

This method accepts the following arguments:

  • numerator, the numerator of the aspect ratio.
  • denominator, the denominator of the aspect ratio.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def set_clipboard(clipboard_string : String) #

Sets the contents of the system clipboard.

window.set_clipboard "Hello from GLFW!"

This method accepts the following arguments:

  • clipboard_string, the desired contents of the clipboard.

[View source]
def set_icon(icon : Image) #

Sets the window's icon.

TODO Add an example here.

This method accepts the following arguments:

  • icon, the window's desired icon image.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def set_monitor(mon : CrystGLFW::Monitor) #

Sets the monitor used for full screen mode.

monitor = Monitor.primary
unless window.full_screen?
  window.set_monitor monitor
end

The monitor's current video mode is used when the window is made full-screen.

This method accepts the following arguments:

  • mon, the monitor that will be used for full screen mode.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def set_position(x : Int32, y : Int32) #

Sets the position of the window.

# Set the window's top left corner at (150, 150).
window.set_position 150, 150

This method accepts the following arguments:

  • x, the x-coordinate of the desired position.
  • y, the y-coordinate of the desired position.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def set_size(width : Int32, height : Int32) #

Sets the size of the window.

# Set the window's size to be 640x480.
window.set_size 640, 480

This method accepts the following arguments:

  • width, the desired width of the window.
  • height, the desired height of the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def set_size_limits(min_width : Int32 = CrystGLFW[:dont_care], min_height : Int32 = CrystGLFW[:dont_care], max_width : Int32 = CrystGLFW[:dont_care], max_height : Int32 = CrystGLFW[:dont_care]) #

Sets the size limits of the window.

# Disallows the window to be resized smaller than 100x100.
window.set_size_limits min_width: 100, min_height: 100

# Disallows the window to be resized larger than 640x480.
window.set_size_limits max_width: 640, max_height: 480

This method accepts the following arguments:

  • min_width, the minimum width of the window, in screen coordinates.
  • min_height, the minimum height of the window, in screen coordinates.
  • max_width, the maximum width of the window, in screen coordinates.
  • max_height, the maximum height of the window, in screen coordinates.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def set_title(title : String) #

Sets the window's title.

window.on_key |key_event|
  # If the key is printable, then set the window title to the name of the key.
  window.set_title key_event.key.name if key_event.key.printable? && key_event.press?
end

This method accepts the following arguments:

  • title, the desired title of the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def should_close #

Marks this window for closing.

window.on_key do |key_event|
  # if the 'q' key is held down, then mark the window for closing.
  if key_event.key.is? :key_q && key_event.repeat?
    window.should_close
  end
end

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def should_close? : Bool #

Returns true if the window is currently marked for closing. False otherwise.

until window.should_close?
  window.wait_events
  window.swap_buffers
end

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def should_not_close #

The exact opposite of #should_close.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def show #

Shows the window (makes it visible). The exact opposite of #hide.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def size : NamedTuple() #

Returns the size of the window, in screen coordinates, as a named tuple.

size = window.size
puts "The window is currently #{size[:width]} x #{size[:height]}"

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def size=(s : NamedTuple()) #

Alternate syntax for #set_size.

# Set the window's size to be 640x480.
window.size = {width: 640, height: 480}

This method accepts the following arguments:

  • s, the desired size of the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def size_limits=(limits : NamedTuple()) #

Alternate syntax for #set_size_limits.

# Disallow the window to be resized smaller than 100x100 or larger than 640x480.
limits = {min_width: 100, min_height: 100, max_width: 640, max_height: 480}
window.size_limits = limits

This method accepts the following arguments:

  • limits, the desired size limits to impose on the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def sticky_keys? : Bool #

Returns true if sticky keys are enabled. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def sticky_mouse_buttons? : Bool #

Returns true if sticky mouse buttons are enabled. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def swap_buffers #

Swaps the front and back buffers.

window = Window.new(title: "Awesome window")
window.make_context_current
until window.should_close?
  window.wait_events
  window.swap_buffers
end

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def title : String #

Returns the window's title.

window.title = "Test Title"
window.title # => "Test Title"

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def title=(title : String) #

Alternate syntax for #set_title.

window.on_key do |key_event|
  # If the key is printable, then set the window title to the name of the key.
  window.title = key_event.key.name if key_event.key.printable? && key_event.press?
end

This method accepts the following arguments:

  • title, the desired title of the window.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]
def visible? : Bool #

Returns true if the window is visible. False otherwise.

NOTE This method must be called from within a CrystGLFW#run block definition.


[View source]

Macro Detail

macro define_callback(symbol, camel) #

Defines a callback method.


[View source]
macro define_callbacks(pairs) #

Defines all of the callback methods.


[View source]