SFML
Simple and Fast Multimedia Library
Loading...
Searching...
No Matches
sf::RenderWindow Class Reference

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

#include <RenderWindow.hpp>

Inheritance diagram for sf::RenderWindow:
[legend]

Public Member Functions

 RenderWindow ()=default
 Default constructor.
 RenderWindow (VideoMode mode, const String &title, std::uint32_t style=Style::Default, State state=State::Windowed, const ContextSettings &settings={})
 Construct a new window.
 RenderWindow (VideoMode mode, const String &title, State state, const ContextSettings &settings={})
 Construct a new window.
 RenderWindow (WindowHandle handle, const ContextSettings &settings={})
 Construct the window from an existing control.
Vector2u getSize () const override
 Get the size of the rendering region of the window.
void setIcon (const Image &icon)
 Change the window's icon.
bool isSrgb () const override
 Tell if the window will use sRGB encoding when drawing on it.
bool setActive (bool active=true) override
 Activate or deactivate the window as the current target for OpenGL rendering.
Public Member Functions inherited from sf::Window
 Window ()
 Default constructor.
 Window (VideoMode mode, const String &title, std::uint32_t style=Style::Default, State state=State::Windowed, const ContextSettings &settings={})
 Construct a new window.
 Window (VideoMode mode, const String &title, State state, const ContextSettings &settings={})
 Construct a new window.
 Window (WindowHandle handle, const ContextSettings &settings={})
 Construct the window from an existing control.
 ~Window () override
 Destructor.
 Window (const Window &)=delete
 Deleted copy constructor.
Windowoperator= (const Window &)=delete
 Deleted copy assignment.
 Window (Window &&) noexcept
 Move constructor.
Windowoperator= (Window &&) noexcept
 Move assignment.
void create (VideoMode mode, const String &title, std::uint32_t style=Style::Default, State state=State::Windowed) override
 Create (or recreate) the window.
virtual void create (VideoMode mode, const String &title, std::uint32_t style, State state, const ContextSettings &settings)
 Create (or recreate) the window.
void create (WindowHandle handle) override
 Create (or recreate) the window from an existing control.
virtual void create (WindowHandle handle, const ContextSettings &settings)
 Create (or recreate) the window from an existing control.
void close () override
 Close the window and destroy all the attached resources.
const ContextSettingsgetSettings () const
 Get the settings of the OpenGL context of the window.
void setVerticalSyncEnabled (bool enabled)
 Enable or disable vertical synchronization.
void setFramerateLimit (unsigned int limit)
 Limit the framerate to a maximum fixed frequency.
bool setActive (bool active=true) const
 Activate or deactivate the window as the current target for OpenGL rendering.
void display ()
 Display on screen what has been rendered to the window so far.
Public Member Functions inherited from sf::WindowBase
 WindowBase ()
 Default constructor.
 WindowBase (VideoMode mode, const String &title, std::uint32_t style=Style::Default, State state=State::Windowed)
 Construct a new window.
 WindowBase (VideoMode mode, const String &title, State state)
 Construct a new window.
 WindowBase (WindowHandle handle)
 Construct the window from an existing control.
virtual ~WindowBase ()
 Destructor.
 WindowBase (const WindowBase &)=delete
 Deleted copy constructor.
WindowBaseoperator= (const WindowBase &)=delete
 Deleted copy assignment.
 WindowBase (WindowBase &&) noexcept
 Move constructor.
WindowBaseoperator= (WindowBase &&) noexcept
 Move assignment.
bool isOpen () const
 Tell whether or not the window is open.
std::optional< EventpollEvent ()
 Pop the next event from the front of the FIFO event queue, if any, and return it.
std::optional< EventwaitEvent (Time timeout=Time::Zero)
 Wait for an event and return it.
template<typename... Ts>
void handleEvents (Ts &&... handlers)
 Handle all pending events.
Vector2i getPosition () const
 Get the position of the window.
void setPosition (Vector2i position)
 Change the position of the window on screen.
Vector2u getSize () const
 Get the size of the rendering region of the window.
void setSize (Vector2u size)
 Change the size of the rendering region of the window.
void setMinimumSize (const std::optional< Vector2u > &minimumSize)
 Set the minimum window rendering region size.
void setMaximumSize (const std::optional< Vector2u > &maximumSize)
 Set the maximum window rendering region size.
void setTitle (const String &title)
 Change the title of the window.
void setIcon (Vector2u size, const std::uint8_t *pixels)
 Change the window's icon.
void setVisible (bool visible)
 Show or hide the window.
void setMouseCursorVisible (bool visible)
 Show or hide the mouse cursor.
void setMouseCursorGrabbed (bool grabbed)
 Grab or release the mouse cursor.
void setMouseCursor (const Cursor &cursor)
 Set the displayed cursor to a native system cursor.
void setKeyRepeatEnabled (bool enabled)
 Enable or disable automatic key-repeat.
void setJoystickThreshold (float threshold)
 Change the joystick threshold.
void requestFocus ()
 Request the current window to be made the active foreground window.
bool hasFocus () const
 Check whether the window has the input focus.
WindowHandle getNativeHandle () const
 Get the OS-specific handle of the window.
bool createVulkanSurface (const VkInstance &instance, VkSurfaceKHR &surface, const VkAllocationCallbacks *allocator=nullptr)
 Create a Vulkan rendering surface.
Public Member Functions inherited from sf::RenderTarget
virtual ~RenderTarget ()=default
 Destructor.
 RenderTarget (const RenderTarget &)=delete
 Deleted copy constructor.
RenderTargetoperator= (const RenderTarget &)=delete
 Deleted copy assignment.
 RenderTarget (RenderTarget &&) noexcept=default
 Move constructor.
RenderTargetoperator= (RenderTarget &&) noexcept=default
 Move assignment.
void clear (Color color=Color::Black)
 Clear the entire target with a single color.
void clearStencil (StencilValue stencilValue)
 Clear the stencil buffer to a specific value.
void clear (Color color, StencilValue stencilValue)
 Clear the entire target with a single color and stencil value.
void setView (const View &view)
 Change the current active view.
const ViewgetView () const
 Get the view currently in use in the render target.
const ViewgetDefaultView () const
 Get the default view of the render target.
IntRect getViewport (const View &view) const
 Get the viewport of a view, applied to this render target.
IntRect getScissor (const View &view) const
 Get the scissor rectangle of a view, applied to this render target.
Vector2f mapPixelToCoords (Vector2i point) const
 Convert a point from target coordinates to world coordinates, using the current view.
Vector2f mapPixelToCoords (Vector2i point, const View &view) const
 Convert a point from target coordinates to world coordinates.
Vector2i mapCoordsToPixel (Vector2f point) const
 Convert a point from world coordinates to target coordinates, using the current view.
Vector2i mapCoordsToPixel (Vector2f point, const View &view) const
 Convert a point from world coordinates to target coordinates.
void draw (const Drawable &drawable, const RenderStates &states=RenderStates::Default)
 Draw a drawable object to the render target.
void draw (const Vertex *vertices, std::size_t vertexCount, PrimitiveType type, const RenderStates &states=RenderStates::Default)
 Draw primitives defined by an array of vertices.
void draw (const VertexBuffer &vertexBuffer, const RenderStates &states=RenderStates::Default)
 Draw primitives defined by a vertex buffer.
void draw (const VertexBuffer &vertexBuffer, std::size_t firstVertex, std::size_t vertexCount, const RenderStates &states=RenderStates::Default)
 Draw primitives defined by a vertex buffer.
void pushGLStates ()
 Save the current OpenGL render states and matrices.
void popGLStates ()
 Restore the previously saved OpenGL render states and matrices.
void resetGLStates ()
 Reset the internal OpenGL states so that the target is ready for drawing.

Protected Member Functions

void onCreate () override
 Function called after the window has been created.
void onResize () override
 Function called after the window has been resized.
Protected Member Functions inherited from sf::RenderTarget
 RenderTarget ()=default
 Default constructor.
void initialize ()
 Performs the common initialization step after creation.

Detailed Description

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 class sf::RenderTarget for more details). Here is a typical rendering and event loop with a sf::RenderWindow:

// Declare and create a new render-window
sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML window");
// Limit the framerate to 60 frames per second (this step is optional)
window.setFramerateLimit(60);
// The main loop - ends as soon as the window is closed
while (window.isOpen())
{
// Event processing
while (const std::optional event = window.pollEvent())
{
// Request for closing the window
if (event->is<sf::Event::Closed>())
window.close();
}
// 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();
}
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
Draw a drawable object to the render target.
void clear(Color color=Color::Black)
Clear the entire target with a single color.
Window that can serve as a target for 2D drawing.
Definition RenderWindow.hpp:55
VideoMode defines a video mode (width, height, bpp).
Definition VideoMode.hpp:44
std::optional< Event > pollEvent()
Pop the next event from the front of the FIFO event queue, if any, and return it.
bool isOpen() const
Tell whether or not the window is open.
void close() override
Close the window and destroy all the attached resources.
void display()
Display on screen what has been rendered to the window so far.
void setFramerateLimit(unsigned int limit)
Limit the framerate to a maximum fixed frequency.
Closed event subtype.
Definition Event.hpp:54

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
sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML OpenGL");
// Create a sprite and a text to display
const sf::Texture texture("circle.png");
sf::Sprite sprite(texture);
const sf::Font font("arial.ttf");
sf::Text text(font);
...
// Perform OpenGL initializations
glMatrixMode(GL_PROJECTION);
...
// Start the rendering loop
while (window.isOpen())
{
// Process events
...
// Draw a background sprite
window.pushGLStates();
window.draw(sprite);
window.popGLStates();
// Draw a 3D object using OpenGL
glBegin(GL_TRIANGLES);
glVertex3f(...);
...
glEnd();
// Draw text on top of the 3D object
window.pushGLStates();
window.draw(text);
window.popGLStates();
// Finally, display the rendered frame on screen
window.display();
}
Class for loading and manipulating character fonts.
Definition Font.hpp:64
void pushGLStates()
Save the current OpenGL render states and matrices.
void popGLStates()
Restore the previously saved OpenGL render states and matrices.
Drawable representation of a texture, with its own transformations, color, etc.
Definition Sprite.hpp:51
Graphical text that can be drawn to a render target.
Definition Text.hpp:57
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56
See also
sf::Window, sf::RenderTarget, sf::RenderTexture, sf::View

Constructor & Destructor Documentation

◆ RenderWindow() [1/4]

sf::RenderWindow::RenderWindow ( )
default

Default constructor.

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

◆ RenderWindow() [2/4]

sf::RenderWindow::RenderWindow ( VideoMode mode,
const String & title,
std::uint32_t style = Style::Default,
State state = State::Windowed,
const ContextSettings & settings = {} )

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 last 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.

Parameters
modeVideo mode to use (defines the width, height and depth of the rendering area of the window)
titleTitle of the window
styleWindow style, a bitwise OR combination of sf::Style enumerators
stateWindow state
settingsAdditional settings for the underlying OpenGL context

◆ RenderWindow() [3/4]

sf::RenderWindow::RenderWindow ( VideoMode mode,
const String & title,
State state,
const ContextSettings & settings = {} )

Construct a new window.

This constructor creates the window with the size and pixel depth defined in mode. If state is State::Fullscreen, then mode must be a valid video mode.

The last parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc.

Parameters
modeVideo mode to use (defines the width, height and depth of the rendering area of the window)
titleTitle of the window
stateWindow state
settingsAdditional settings for the underlying OpenGL context

◆ RenderWindow() [4/4]

sf::RenderWindow::RenderWindow ( WindowHandle handle,
const ContextSettings & settings = {} )
explicit

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.

Parameters
handlePlatform-specific handle of the control (HWND on Windows, Window on Linux/FreeBSD, NSWindow on OS X)
settingsAdditional settings for the underlying OpenGL context

Member Function Documentation

◆ getSize()

Vector2u sf::RenderWindow::getSize ( ) const
nodiscardoverridevirtual

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

Implements sf::RenderTarget.

◆ isSrgb()

bool sf::RenderWindow::isSrgb ( ) const
nodiscardoverridevirtual

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

Reimplemented from sf::RenderTarget.

◆ onCreate()

void sf::RenderWindow::onCreate ( )
overrideprotectedvirtual

Function called after the window has been created.

This function is called so that derived classes can perform their own specific initialization as soon as the window is created.

Reimplemented from sf::WindowBase.

◆ onResize()

void sf::RenderWindow::onResize ( )
overrideprotectedvirtual

Function called after the window has been resized.

This function is called so that derived classes can perform custom actions when the size of the window changes.

Reimplemented from sf::WindowBase.

◆ setActive()

bool sf::RenderWindow::setActive ( bool active = true)
nodiscardoverridevirtual

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 requestFocus().

Parameters
activeTrue to activate, false to deactivate
Returns
True if operation was successful, false otherwise

Reimplemented from sf::RenderTarget.

◆ setIcon()

void sf::RenderWindow::setIcon ( const Image & icon)

Change the window's icon.

The OS default icon is used by default.

Parameters
iconImage to use as the icon. The image is copied, so you need not keep the source alive after calling this function.

The documentation for this class was generated from the following file: