Skip to content

class SF::RenderWindow
inherits SF::Window #

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

SF::RenderTarget

Constructors#

.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

.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

.new#

Default constructor

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

View source

.new(*args, **kwargs) : self#

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

View source

Methods#

#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

#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

#finalize#

Destructor

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

View source

#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

#srgb? : Bool#

Tell if the window will use sRGB encoding when drawing on it

You can request sRGB encoding for a window by having the sRgbCapable flag set in the ContextSettings

Returns: True if the window use sRGB encoding, false otherwise

View source