Window that can serve as a target for 2D drawing. More...
#include <RenderWindow.hpp>
Public Member Functions | |
RenderWindow () | |
Default constructor. | |
RenderWindow (VideoMode mode, const std::string &title, unsigned long style=Style::Default, const ContextSettings &settings=ContextSettings()) | |
Construct a new window. | |
RenderWindow (WindowHandle handle, const ContextSettings &settings=ContextSettings()) | |
Construct the window from an existing control. | |
virtual | ~RenderWindow () |
Destructor. | |
virtual unsigned int | GetWidth () const |
Get the width of the rendering region of the window. | |
virtual unsigned int | GetHeight () const |
Get the height of the rendering region of the window. | |
Image | Capture () const |
Copy the current contents of the window to an image. | |
void | Create (VideoMode mode, const std::string &title, unsigned long style=Style::Default, const ContextSettings &settings=ContextSettings()) |
Create (or recreate) the window. | |
void | Create (WindowHandle handle, const ContextSettings &settings=ContextSettings()) |
Create (or recreate) the window from an existing control. | |
void | Close () |
Close the window and destroy all the attached resources. | |
bool | IsOpened () const |
Tell whether or not the window is opened. | |
const ContextSettings & | GetSettings () const |
Get the settings of the OpenGL context of the window. | |
bool | PollEvent (Event &event) |
Pop the event on top of events stack, if any, and return it. | |
bool | WaitEvent (Event &event) |
Wait for an event and return it. | |
void | EnableVerticalSync (bool enabled) |
Enable or disable vertical synchronization. | |
void | ShowMouseCursor (bool show) |
Show or hide the mouse cursor. | |
void | SetPosition (int x, int y) |
Change the position of the window on screen. | |
void | SetSize (unsigned int width, unsigned int height) |
Change the size of the rendering region of the window. | |
void | SetTitle (const std::string &title) |
Change the title of the window. | |
void | Show (bool show) |
Show or hide the window. | |
void | EnableKeyRepeat (bool enabled) |
Enable or disable automatic key-repeat. | |
void | SetIcon (unsigned int width, unsigned int height, const Uint8 *pixels) |
Change the window's icon. | |
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. | |
void | SetFramerateLimit (unsigned int limit) |
Limit the framerate to a maximum fixed frequency. | |
Uint32 | GetFrameTime () const |
Get the duration of the last frame. | |
void | SetJoystickThreshold (float threshold) |
Change the joystick threshold. | |
WindowHandle | GetSystemHandle () const |
Get the OS-specific handle of the window. | |
void | Clear (const Color &color=Color(0, 0, 0, 255)) |
Clear the entire target with a single color. | |
void | Draw (const Drawable &object) |
Draw an object into the target. | |
void | Draw (const Drawable &object, const Shader &shader) |
Draw an object into the target with a shader. | |
void | SetView (const View &view) |
Change the current active view. | |
const View & | GetView () const |
Retrieve the view currently in use in the render target. | |
const View & | GetDefaultView () 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. | |
Vector2f | ConvertCoords (unsigned int x, unsigned int y) const |
Convert a point from target coordinates to view coordinates. | |
Vector2f | ConvertCoords (unsigned int x, unsigned int y, const View &view) const |
Convert a point from target coordinates to view coordinates. | |
void | SaveGLStates () |
Save the current OpenGL render states and matrices. | |
void | RestoreGLStates () |
Restore the previously saved OpenGL render states and matrices. | |
Protected Member Functions | |
void | Initialize () |
Performs the common initialization step after creation. |
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: mouse/keyboard/joystick input, events, window handling, OpenGL rendering, etc. See the documentation of sf::Window for a more complete description of all these features and code samples.
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 / 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.IsOpened()) { // Event processing sf::Event event; while (window.PollEvent(event)) { // Request for closing the window if (event.Type == sf::Event::Closed) window.Close(); } // Clear the whole window before rendering a new frame window.Clear(); // Draw some sprites / shapes / texts window.Draw(sprite); // sprite is a sf::Sprite window.Draw(shape); // shape is a sf::Shape window.Draw(text); // text is a sf::Text // End the current frame and display its contents on screen window.Display(); }
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. When doing so, make sure that OpenGL states are not messed up by calling the SaveGLStates / RestoreGLStates functions.
// Create the render window sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL"); // Create a sprite and a text to display sf::Sprite sprite; sf::Text text; ... // Perform OpenGL initializations glMatrixMode(GL_PROJECTION); ... // Start the rendering loop while (window.IsOpened()) { // Process events ... // Draw a background sprite window.SaveGLStates(); window.Draw(sprite); window.RestoreGLStates(); // Draw a 3D object using OpenGL glBegin(GL_QUADS); glVertex3f(...); ... glEnd(); // Draw text on top of the 3D object window.SaveGLStates(); window.Draw(text); window.RestoreGLStates(); // Finally, display the rendered frame on screen window.Display(); }
Definition at line 43 of file RenderWindow.hpp.
sf::RenderWindow::RenderWindow | ( | ) |
Default constructor.
This constructor doesn't actually create the window, use the other constructors or call Create to do so.
sf::RenderWindow::RenderWindow | ( | VideoMode | mode, |
const std::string & | title, | ||
unsigned long | style = Style::Default , |
||
const ContextSettings & | settings = ContextSettings() |
||
) |
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 behaviour 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 |
settings | Additional settings for the underlying OpenGL context |
sf::RenderWindow::RenderWindow | ( | WindowHandle | handle, |
const ContextSettings & | settings = ContextSettings() |
||
) | [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 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.
handle | Platform-specific handle of the control |
settings | Additional settings for the underlying OpenGL context |
virtual sf::RenderWindow::~RenderWindow | ( | ) | [virtual] |
Destructor.
Closes the window and free all the resources attached to it.
Image sf::RenderWindow::Capture | ( | ) | const |
Copy the current contents of the window to an 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&) function. You can also draw things directly to a texture with the sf::RenderTexture class.
Clear the entire target with a single color.
This function is usually called once every frame, to clear the previous contents of the target.
color | Fill color to use to clear the render target |
void sf::Window::Close | ( | ) | [inherited] |
Close the window and destroy all the attached resources.
After calling this function, the sf::Window instance remains valid and you can call Create() to recreate the window. All other functions such as PollEvent() or Display() will still work (i.e. you don't have to test IsOpened() every time), and will have no effect on closed windows.
Vector2f sf::RenderTarget::ConvertCoords | ( | unsigned int | x, |
unsigned int | y | ||
) | const [inherited] |
Convert a point from target coordinates to view coordinates.
Initially, a unit of the 2D world matches a pixel of the render target. But if you define a custom view, this assertion is not true anymore, ie. a point located at (10, 50) in your render target (for example a window) may map to the point (150, 75) in your 2D world -- for example 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 the current view of the render target. See the other overload to specify a custom view.
x | X coordinate of the point to convert, relative to the render target |
y | Y coordinate of the point to convert, relative to the render target |
Vector2f sf::RenderTarget::ConvertCoords | ( | unsigned int | x, |
unsigned int | y, | ||
const View & | view | ||
) | const [inherited] |
Convert a point from target coordinates to view coordinates.
Initially, a unit of the 2D world matches a pixel of the render target. But if you define a custom view, this assertion is not true anymore, ie. a point located at (10, 50) in your render target (for example a window) may map to the point (150, 75) in your 2D world -- for example 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 to use the current view of the render target.
x | X coordinate of the point to convert, relative to the render target |
y | Y coordinate of the point to convert, relative to the render target |
view | The view to use for converting the point |
void sf::Window::Create | ( | VideoMode | mode, |
const std::string & | title, | ||
unsigned long | style = Style::Default , |
||
const ContextSettings & | settings = ContextSettings() |
||
) | [inherited] |
Create (or recreate) the window.
If the window was already created, it closes it first. If style contains Style::Fullscreen, then mode must be a valid video mode.
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 |
settings | Additional settings for the underlying OpenGL context |
void sf::Window::Create | ( | WindowHandle | handle, |
const ContextSettings & | settings = ContextSettings() |
||
) | [inherited] |
Create (or recreate) the window from an existing control.
Use this function if you want to create an OpenGL rendering area into an already existing control. If the window was already created, it closes it first.
handle | Platform-specific handle of the control |
settings | Additional settings for the underlying OpenGL context |
void sf::Window::Display | ( | ) | [inherited] |
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.
void sf::RenderTarget::Draw | ( | const Drawable & | object | ) | [inherited] |
Draw an object into the target.
This function draws anything that inherits from the sf::Drawable base class (sf::Sprite, sf::Shape, sf::Text, or even your own derived classes).
object | Object to draw |
Draw an object into the target with a shader.
This function draws anything that inherits from the sf::Drawable base class (sf::Sprite, sf::Shape, sf::Text, or even your own derived classes). The shader alters the way that the pixels are processed right before being written to the render target.
object | Object to draw |
shader | Shader to use for drawing the object |
void sf::Window::EnableKeyRepeat | ( | bool | enabled | ) | [inherited] |
Enable or disable automatic key-repeat.
If key repeat is enabled, you will receive repeated KeyPress events while keeping a key pressed. If it is disabled, you will only get a single event when the key is pressed.
Key repeat is enabled by default.
enabled | True to enable, false to disable |
void sf::Window::EnableVerticalSync | ( | bool | enabled | ) | [inherited] |
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.
enabled | True to enable v-sync, false to deactivate |
const View& sf::RenderTarget::GetDefaultView | ( | ) | const [inherited] |
Uint32 sf::Window::GetFrameTime | ( | ) | const [inherited] |
Get the duration of the last frame.
This function returns the time elapsed between the last two calls to Display(). This can be useful for calculating the framerate, or for updating the application's objects.
virtual unsigned int sf::RenderWindow::GetHeight | ( | ) | const [virtual] |
Get the height of the rendering region of the window.
The height doesn't include the titlebar and borders of the window.
Implements sf::RenderTarget.
const ContextSettings& sf::Window::GetSettings | ( | ) | const [inherited] |
Get the settings of the OpenGL context of the window.
Note that these settings may be different from what was passed to the constructor or the Create() function, if one or more settings were not supported. In this case, SFML chose the closest match.
WindowHandle sf::Window::GetSystemHandle | ( | ) | const [inherited] |
Get the OS-specific handle of the window.
The type of the returned handle is sf::WindowHandle, which is a typedef to the handle type defined by the OS. You shouldn't need to use this function, unless you have very specific stuff to implement that SFML doesn't support, or implement a temporary workaround until a bug is fixed.
const View& sf::RenderTarget::GetView | ( | ) | const [inherited] |
Retrieve the view currently in use in the 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.
view | The view for which we want to compute the viewport |
virtual unsigned int sf::RenderWindow::GetWidth | ( | ) | const [virtual] |
Get the width of the rendering region of the window.
The width doesn't include the titlebar and borders of the window.
Implements sf::RenderTarget.
void sf::RenderTarget::Initialize | ( | ) | [protected, inherited] |
Performs the common initialization step after creation.
The derived classes must call this function after the target is created and ready for drawing.
bool sf::Window::IsOpened | ( | ) | const [inherited] |
Tell whether or not the window is opened.
This function returns whether or not the window exists. Note that a hidden window (Show(false)) will return true.
bool sf::Window::PollEvent | ( | Event & | event | ) | [inherited] |
Pop the event on top of events stack, if any, and return it.
This function is not blocking: if there's no pending event then it will return false and leave event unmodified. Note that more than one event may be present in the events stack, thus you should always call this function in a loop to make sure that you process every pending event.
sf::Event event; while (window.PollEvent(event)) { // process event... }
event | Event to be returned |
void sf::RenderTarget::RestoreGLStates | ( | ) | [inherited] |
Restore the previously saved OpenGL render states and matrices.
See the description of SaveGLStates to get a detailed description of these functions.
void sf::RenderTarget::SaveGLStates | ( | ) | [inherited] |
Save the current OpenGL render states and matrices.
This function can be used when you mix SFML drawing and direct OpenGL rendering. Combined with RestoreGLStates, it ensures that:
More specifically, it must be used around code that calls Draw functions. Example:
// OpenGL code here... window.SaveGLStates(); window.Draw(...); window.Draw(...); window.RestoreGLStates(); // OpenGL code here...
Note that this function is quite expensive and should be used wisely. It is provided for convenience, and the best results will be achieved if you handle OpenGL states yourself (because you really know which states have really changed, and need to be saved / restored).
bool sf::Window::SetActive | ( | bool | active = true | ) | const [inherited] |
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.
active | True to activate, false to deactivate |
void sf::Window::SetFramerateLimit | ( | unsigned int | limit | ) | [inherited] |
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.
limit | Framerate limit, in frames per seconds (use 0 to disable limit) |
void sf::Window::SetIcon | ( | unsigned int | width, |
unsigned int | height, | ||
const Uint8 * | pixels | ||
) | [inherited] |
Change the window's icon.
pixels must be an array of width x height pixels in 32-bits RGBA format.
The OS default icon is used by default.
width | Icon's width, in pixels |
height | Icon's height, in pixels |
pixels | Pointer to the array of pixels in memory |
void sf::Window::SetJoystickThreshold | ( | float | threshold | ) | [inherited] |
Change the joystick threshold.
The joystick threshold is the value below which no JoyMoved event will be generated.
The threshold value is 0.1 by default.
threshold | New threshold, in the range [0, 100] |
void sf::Window::SetPosition | ( | int | x, |
int | y | ||
) | [inherited] |
Change the position of the window on screen.
This function only works for top-level windows (i.e. it will be ignored for windows created from the handle of a child window/control).
x | Left position |
y | Top position |
void sf::Window::SetSize | ( | unsigned int | width, |
unsigned int | height | ||
) | [inherited] |
Change the size of the rendering region of the window.
width | New width, in pixels |
height | New height, in pixels |
void sf::Window::SetTitle | ( | const std::string & | title | ) | [inherited] |
Change the title of the window.
title | New title |
void sf::RenderTarget::SetView | ( | const View & | view | ) | [inherited] |
Change the current active view.
The new view will affect everything that is drawn, until another view is activated. The render target keeps its own copy of the view object, so it is not necessary to keep the original one alive as long as it is in use. To restore the original view of the target, you can pass the result of GetDefaultView() to this function.
view | New view to use |
void sf::Window::Show | ( | bool | show | ) | [inherited] |
Show or hide the window.
The window is shown by default.
show | True to show, false to hide |
void sf::Window::ShowMouseCursor | ( | bool | show | ) | [inherited] |
Show or hide the mouse cursor.
The mouse cursor is shown by default.
show | True to show, false to hide |
bool sf::Window::WaitEvent | ( | Event & | event | ) | [inherited] |
Wait for an event and return it.
This function is blocking: if there's no pending event then it will wait until an event is received. After this function returns (and no error occured), the event object is always valid and filled properly. This function is typically used when you have a thread that is dedicated to events handling: you want to make this thread sleep as long as no new event is received.
sf::Event event; if (window.WaitEvent(event)) { // process event... }
event | Event to be returned |