class SF::RenderWindow

Overview

Window that can serve as a target for 2D drawing

SF::RenderWindow is the main class of the Graphics module. It defines an OS window that can be painted using the other classes of the graphics module.

SF::RenderWindow is derived from SF::Window, thus it inherits all its features: events, window management, OpenGL rendering, etc. See the documentation of SF::Window for a more complete description of all these features, as well as code examples.

On top of that, SF::RenderWindow adds more features related to 2D drawing with the graphics module (see its base module SF::RenderTarget for more details). Here is a typical rendering and event loop with a SF::RenderWindow:

# Declare and create a new render-window
window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "SFML window")

# Limit the framerate to 60 frames per second (this step is optional)
window.framerate_limit = 60

# The main loop - ends as soon as the window is closed
while window.open?
  # Event processing
  while (event = window.poll_event)
    # Request for closing the window
    if event.is_a? SF::Event::Closed
      window.close
    end
  end

  # Clear the whole window before rendering a new frame
  window.clear

  # Draw some graphical entities
  window.draw sprite
  window.draw circle
  window.draw text

  # End the current frame and display its contents on screen
  window.display
end

Like SF::Window, SF::RenderWindow is still able to render direct OpenGL stuff. It is even possible to mix together OpenGL calls and regular SFML drawing commands.

# Create the render window
window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "SFML OpenGL")

# Create a sprite and a text to display
sprite = SF::Sprite.new
text = SF::Text.new
# [...]

# Perform OpenGL initializations
glMatrixMode(GL_PROJECTION)
# [...]

# Start the rendering loop
while window.open?
  # Process events
  # [...]

  # Draw a background sprite
  window.push_gl_states
  window.draw sprite
  window.pop_gl_states

  # Draw a 3D object using OpenGL
  glBegin(GL_QUADS)
  glVertex3f(...)
  # [...]
  glEnd

  # Draw text on top of the 3D object
  window.push_gl_states
  window.draw text
  window.pop_gl_states

  # Finally, display the rendered frame on screen
  window.display
end

See also: SF::Window, SF::RenderTarget, SF::RenderTexture, SF::View

Included Modules

Defined in:

graphics/graphics.cr
graphics/obj.cr

Constructors

Instance Method Summary

Instance methods inherited from module SF::RenderTarget

active=(active : Bool = true) : Bool active=, clear(color : Color = Color.new(0, 0, 0, 255)) clear, default_view : View default_view, draw(vertices : Array(Vertex) | Slice(Vertex), type : PrimitiveType, states : RenderStates = RenderStates::Default)
draw(vertex_buffer : VertexBuffer, first_vertex : Int, vertex_count : Int, states : RenderStates = RenderStates::Default)
draw(vertex_buffer : VertexBuffer, states : RenderStates = RenderStates::Default)
draw(drawable : Drawable, states : RenderStates = RenderStates::Default)
draw
, get_viewport(view : View) : IntRect get_viewport, map_coords_to_pixel(point : Vector2 | Tuple, view : View) : Vector2i
map_coords_to_pixel(point : Vector2 | Tuple) : Vector2i
map_coords_to_pixel
, map_pixel_to_coords(point : Vector2 | Tuple, view : View) : Vector2f
map_pixel_to_coords(point : Vector2 | Tuple) : Vector2f
map_pixel_to_coords
, pop_gl_states pop_gl_states, push_gl_states push_gl_states, reset_gl_states reset_gl_states, size : Vector2u size, view : View view, view=(view : View) view=

Instance methods inherited from class SF::Window

active=(active : Bool = true) : Bool active=, close close, create(mode : VideoMode, title : String, style : Style = Style::Default, settings : ContextSettings = ContextSettings.new())
create(handle : WindowHandle, settings : ContextSettings = ContextSettings.new())
create
, display display, finalize finalize, focus? : Bool focus?, framerate_limit=(limit : Int) framerate_limit=, joystick_threshold=(threshold : Number) joystick_threshold=, key_repeat_enabled=(enabled : Bool) key_repeat_enabled=, mouse_cursor=(cursor : Cursor) mouse_cursor=, mouse_cursor_grabbed=(grabbed : Bool) mouse_cursor_grabbed=, mouse_cursor_visible=(visible : Bool) mouse_cursor_visible=, open? : Bool open?, poll_event : Event | Nil poll_event, position : Vector2i position, position=(position : Vector2 | Tuple) position=, request_focus request_focus, set_icon(width : Int, height : Int, pixels : Pointer(UInt8)) set_icon, settings : ContextSettings settings, size : Vector2u size, size=(size : Vector2 | Tuple) size=, system_handle : WindowHandle system_handle, title=(title : String) title=, vertical_sync_enabled=(enabled : Bool) vertical_sync_enabled=, visible=(visible : Bool) visible=, wait_event : Event | Nil wait_event

Constructor methods inherited from class SF::Window

new(mode : VideoMode, title : String, style : Style = Style::Default, settings : ContextSettings = ContextSettings.new())
new(handle : WindowHandle, settings : ContextSettings = ContextSettings.new())
new
new(*args, **kwargs) : self
new

Constructor Detail

def self.new(mode : VideoMode, title : String, style : Style = Style::Default, settings : ContextSettings = ContextSettings.new()) #

Construct a new window

This constructor creates the window with the size and pixel depth defined in mode. An optional style can be passed to customize the look and behavior of the window (borders, title bar, resizable, closable, ...).

The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. You shouldn't care about these parameters for a regular usage of the graphics module.

  • mode - Video mode to use (defines the width, height and depth of the rendering area of the window)
  • title - Title of the window
  • style - Window style, a bitwise OR combination of SF::Style enumerators
  • settings - Additional settings for the underlying OpenGL context

[View source]
def self.new(handle : WindowHandle, settings : ContextSettings = ContextSettings.new()) #

Construct the window from an existing control

Use this constructor if you want to create an SFML rendering area into an already existing control.

The second parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. You shouldn't care about these parameters for a regular usage of the graphics module.

  • handle - Platform-specific handle of the control (hwnd on Windows, %window on Linux/FreeBSD, ns_window on OS X)

  • settings - Additional settings for the underlying OpenGL context


[View source]
def self.new #

Default constructor

This constructor doesn't actually create the window, use the other constructors or call create() to do so.


[View source]
def self.new(*args, **kwargs) : self #

Shorthand for render_window = RenderWindow.new; render_window.create(...); render_window


[View source]

Instance Method Detail

def active=(active : Bool = true) : Bool #

Activate or deactivate the window as the current target for OpenGL rendering

A window is active only on the current thread, if you want to make it active on another thread you have to deactivate it on the previous thread first if it was active. Only one window can be active on a thread at a time, thus the window previously active (if any) automatically gets deactivated. This is not to be confused with request_focus().

  • active - True to activate, false to deactivate

Returns: True if operation was successful, false otherwise


[View source]
def capture : Image #

Copy the current contents of the window to an image

DEPRECATED Use a SF::Texture and its SF::Texture#update(window) method and copy its contents into an SF::Image instead.

texture = SF::Texture.new(window.size.x, window.size.y)
texture.update(window)
screenshot = texture.copy_to_image

This is a slow operation, whose main purpose is to make screenshots of the application. If you want to update an image with the contents of the window and then use it for drawing, you should rather use a SF::Texture and its update(window) method. You can also draw things directly to a texture with the SF::RenderTexture class.

Returns: Image containing the captured contents


[View source]
def finalize #

Destructor

Closes the window and frees all the resources attached to it.


[View source]
def size : Vector2u #

Get the size of the rendering region of the window

The size doesn't include the titlebar and borders of the window.

Returns: Size in pixels


[View source]