RenderWindow

sfml.graphics.RenderWindow
See theRenderWindow companion object
trait RenderWindow(using ctor: ctor) extends Window, RenderTarget

Window that can serve as a target for 2D drawing.

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.

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

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

import sfml.window.{Event, VideoMode}

val sprite: Sprite = ???
val text: Text = ???

// Declare and create a new render-window
val window = RenderWindow(VideoMode(((800, 600))), "SFML window")

// Limit the framerate to 60 frames per second (this step is optional)
window.framerateLimit = 60;

// The main loop - ends as soon as the window is closed
while window.isOpen() do
   // Event processing
   for event <- window.pollEvent() do
       event match
           // Request for closing the window
           case Event.Closed() => window.close()
           case _              => ()

   // Clear the whole window before rendering a new frame
   window.clear()

   // Draw some graphical entities
   window.draw(sprite)
   window.draw(text)

   // End the current frame and display its contents on screen
   window.display()

Attributes

Companion
object
Graph
Supertypes
trait RenderTarget
trait Window
trait WindowBase
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

override def activate(active: Boolean = ...): Boolean

Activate or deactivate the render target for rendering.

Activate or deactivate the render target for rendering.

This function makes the render target's context current for future OpenGL rendering operations (so you shouldn't care about it if you're not doing direct OpenGL stuff). A render target's context 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 context can be current in a thread, so if you want to draw OpenGL geometry to another render target don't forget to activate it again. Activating a render target will automatically deactivate the previously active context (if any).

Value parameters

active

true to activate, false to deactivate

Attributes

Returns

true if operation was successful, false otherwise

Definition Classes
override def isSrgb: Boolean

Tell if the render target will use sRGB encoding when drawing on it.

Tell if the render target will use sRGB encoding when drawing on it.

Attributes

Returns

true if the render target use sRGB encoding, false otherwise

Definition Classes
override def size: Vector2[Int]

Get the size of the rendering region of the window.

Get the size of the rendering region of the window.

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

Attributes

Returns

Size in pixels

Definition Classes

Inherited methods

def clear(color: Immutable[Color], stencilValue: Immutable[StencilValue]): Unit

Clear the entire target with a single color and stencil value.

Clear the entire target with a single color and stencil value.

The specified stencil value is truncated to the bit width of the current stencil buffer.

Value parameters

color

Fill color to use to clear the render target

stencilValue

Stencil value to clear to

Attributes

Inherited from:
RenderTarget
def clear(stencilValue: Immutable[StencilValue]): Unit

Clear the stencil buffer to a specific value.

Clear the stencil buffer to a specific value.

The specified value is truncated to the bit width of the current stencil buffer.

Value parameters

stencilValue

Stencil value to clear to

Attributes

Inherited from:
RenderTarget
def clear(color: Immutable[Color] = ...): Unit

Clear the entire target with a single color.

Clear the entire target with a single color.

This function is usually called once every frame, to clear the previous contents of the target.

Value parameters

color

Fill color to use to clear the render target

Attributes

Inherited from:
RenderTarget
override def close(): Unit

Attributes

Definition Classes
Inherited from:
Window

Get the default view of the render target.

Get the default view of the render target.

The default view has the initial size of the render target, and never changes after the target has been created.

Attributes

Returns

The default view of the render target

See also
Inherited from:
RenderTarget
def display(): Unit

Display on screen what has been rendered to the window so far.

Display on screen what has been rendered to the window so far.

This function is typically called after all OpenGL rendering has been done for the current frame, in order to show it on screen.

Attributes

Inherited from:
Window
def draw(drawable: Immutable[Drawable], states: Immutable[RenderStates] = ...): Unit

Draw a drawable object to the render target.

Draw a drawable object to the render target.

Value parameters

drawable

Object to draw states Render states to use for drawing

Attributes

Inherited from:
RenderTarget
def framerateLimit: Unit

Attributes

Inherited from:
Window
def framerateLimit_=(limit: Int): Unit

Limit the framerate to a maximum fixed frequency.

Limit the framerate to a maximum fixed frequency.

If a limit is set, the window will use a small delay after each call to display to ensure that the current frame lasted long enough to match the framerate limit. SFML will try to match the given limit as much as it can, but since it internally uses sleep, whose precision depends on the underlying OS, the results may be a little imprecise as well (for example, you can get 65 FPS when requesting 60).

Value parameters

limit

Framerate limit, in frames per seconds (use 0 to disable limit)

Attributes

Inherited from:
Window
def hasFocus: Boolean

Attributes

Inherited from:
WindowBase
def isOpen(): Boolean

Attributes

Inherited from:
WindowBase
def mapCoordsToPixel(point: Immutable[Vector2[Float]], view: Immutable[View]): Vector2[Int]

Convert a point from world coordinates to target coordinates.

Convert a point from world coordinates to target coordinates.

This function finds the pixel of the render target that matches the given 2D point. In other words, it goes through the same process as the graphics card, to compute the final position of a rendered point.

Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render target, this assertion is not true anymore, i.e. a point located at (150, 75) in your 2D world may map to the pixel (10, 50) of your render target – if the view is translated by (140, 25).

This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render target.

Value parameters

point

Point to convert

view

The view to use for converting the point

Attributes

Returns

The converted point, in target coordinates (pixels)

See also
Inherited from:
RenderTarget
def mapCoordsToPixel(point: Immutable[Vector2[Float]]): Vector2[Int]

Convert a point from world coordinates to target coordinates, using the current view.

Convert a point from world coordinates to target coordinates, using the current view.

This function is an overload of the mapCoordsToPixel function that implicitly uses the current view. It is equivalent to:

import sfml.system.Vector2

val target: RenderTarget = ???
val point: Vector2[Int] = ???

target.mapCoordsToPixel(point, target.view)

Value parameters

point

Point to convert

Attributes

Returns

The converted point, in target coordinates (pixels)

See also
Inherited from:
RenderTarget
def mapPixelToCoords(point: Immutable[Vector2[Int]], view: Immutable[View]): Vector2[Float]

Convert a point from target coordinates to world coordinates.

Convert a point from target coordinates to world coordinates.

This function finds the 2D position that matches the given pixel of the render target. In other words, it does the inverse of what the graphics card does, to find the initial position of a rendered pixel.

Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render target, this assertion is not true anymore, i.e. a point located at (10, 50) in your render target may map to the point (150, 75) in your 2D world – if the view is translated by (140, 25).

For render-windows, this function is typically used to find which point (or object) is located below the mouse cursor.

This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render target.

Value parameters

point

Pixel to convert

view

The view to use for converting the point

Attributes

Returns

The converted point, in "world" units

See also
Inherited from:
RenderTarget
def mapPixelToCoords(point: Immutable[Vector2[Int]]): Vector2[Float]

Convert a point from target coordinates to world coordinates, using the current view.

Convert a point from target coordinates to world coordinates, using the current view.

This function is an overload of the mapPixelToCoords function that implicitly uses the current view. It is equivalent to:

import sfml.system.Vector2

val target: RenderTarget = ???
val point: Vector2[Int] = ???

target.mapPixelToCoords(point, target.view)

Value parameters

point

Pixel to convert

Attributes

Returns

The converted point, in "world" coordinates

See also
Inherited from:
RenderTarget
def pollEvent(): Iterator[Event]

Attributes

Inherited from:
WindowBase
def position: Vector2[Int]

Attributes

Inherited from:
WindowBase
def position_=(position: Immutable[Vector2[Int]]): Unit

Attributes

Inherited from:
WindowBase
def scissor(view: Immutable[View]): Rect[Int]

Get the scissor rectangle of a view, applied to this render target.

Get the scissor rectangle of a view, applied to this render target.

The scissor rectangle is defined in the view as a ratio. This function simply applies this ratio to the current dimensions of the render target to calculate the pixels rectangle that the scissor rectangle actually covers in the target.

Value parameters

view

The view for which we want to compute the scissor rectangle

Attributes

Returns

Scissor rectangle, expressed in pixels

Inherited from:
RenderTarget

Get the settings of the OpenGL context of the window.

Get the settings of the OpenGL context of the window.

Note that these settings may be different from what was passed to the constructor, if one or more settings were not supported. In this case, SFML chose the closest match.

Attributes

Returns

Structure containing the OpenGL context settings

Inherited from:
Window
def size_=(size: Immutable[Vector2[Int]]): Unit

Attributes

Inherited from:
WindowBase
def verticalSync: Unit

Attributes

Inherited from:
Window
def verticalSync_=(enabled: Boolean): Unit

Enable or disable vertical synchronization.

Enable or disable vertical synchronization.

Activating vertical synchronization will limit the number of frames displayed to the refresh rate of the monitor. This can avoid some visual artifacts, and limit the framerate to a good value (but not constant across different computers).

Vertical synchronization is disabled by default.

Value parameters

enabled

true to enable v-sync, false to deactivate it

Attributes

Inherited from:
Window

Get the view currently in use in the render target.

Get the view currently in use in the render target.

Attributes

Returns

The view object that is currently used

See also
Inherited from:
RenderTarget
def view_=(view: Immutable[View]): Unit

Change the current active view.

Change the current active view.

The view is like a 2D camera, it controls which part of the 2D scene is visible, and how it is viewed in the render target. The new view will affect everything that is drawn, until another view is set. The render target keeps its own copy of the view object, so it is not necessary to keep the original one alive after calling this function. To restore the original view of the target, you can pass the result of defaultView to this function.

Value parameters

view

New view to use

Attributes

See also
Inherited from:
RenderTarget
def viewport(view: Immutable[View]): Rect[Int]

Get the viewport of a view, applied to this render target.

Get the viewport of a view, applied to this render target.

The viewport is defined in the view as a ratio, this function simply applies this ratio to the current dimensions of the render target to calculate the pixels rectangle that the viewport actually covers in the target.

Value parameters

view

The view for which we want to compute the viewport

Attributes

Returns

Viewport rectangle, expressed in pixels

Inherited from:
RenderTarget