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. | |
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 | GetEvent (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 | UseVerticalSync (bool enabled) |
Enable or disable vertical synchronization. | |
void | ShowMouseCursor (bool show) |
Show or hide the mouse cursor. | |
void | SetCursorPosition (unsigned int left, unsigned int top) |
Change the position of the mouse cursor. | |
void | SetPosition (int left, int top) |
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 | 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. | |
const Input & | GetInput () const |
Get the input manager attached the window. | |
void | SetFramerateLimit (unsigned int limit) |
Limit the framerate to a maximum fixed frequency. | |
float | GetFrameTime () const |
Get the time elapsed since the last frame. | |
void | SetJoystickThreshold (float threshold) |
Change the joystick threshold. | |
void | Clear (const Color &color=Color(0, 0, 0)) |
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.GetEvent(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.
Definition at line 34 of file RenderWindow.cpp.
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 |
Definition at line 41 of file RenderWindow.cpp.
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 |
Definition at line 49 of file RenderWindow.cpp.
sf::RenderWindow::~RenderWindow | ( | ) | [virtual] |
Destructor.
Closes the window and free all the resources attached to it.
Definition at line 57 of file RenderWindow.cpp.
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 |
Definition at line 57 of file RenderTarget.cpp.
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 GetEvent() or Display() will still work (i.e. you don't have to test IsOpened() every time), and will have no effect on closed windows.
Definition at line 157 of file Window.cpp.
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 |
Definition at line 169 of file RenderTarget.cpp.
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 |
Definition at line 162 of file RenderTarget.cpp.
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 |
Definition at line 140 of file Window.cpp.
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 |
Definition at line 96 of file Window.cpp.
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.
Definition at line 330 of file Window.cpp.
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 |
Definition at line 95 of file RenderTarget.cpp.
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 |
Definition at line 65 of file RenderTarget.cpp.
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 |
Definition at line 292 of file Window.cpp.
const View & sf::RenderTarget::GetDefaultView | ( | ) | const [inherited] |
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.
Definition at line 141 of file RenderTarget.cpp.
bool sf::Window::GetEvent | ( | 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 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.GetEvent(event)) { // process event... }
event | Event to be returned |
Definition at line 210 of file Window.cpp.
float sf::Window::GetFrameTime | ( | ) | const [inherited] |
Get the time elapsed since the last frame.
This function returns the time elapsed during the last frame. This can be useful for calculating the framerate, or for updating the application's objects.
Definition at line 365 of file Window.cpp.
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.
Definition at line 78 of file RenderWindow.cpp.
const Input & sf::Window::GetInput | ( | ) | const [inherited] |
Get the input manager attached the window.
This input gives access to the real-time state of keyboard, mouse and joysticks for this window.
Definition at line 351 of file Window.cpp.
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.
Definition at line 201 of file Window.cpp.
const View & sf::RenderTarget::GetView | ( | ) | const [inherited] |
Retrieve the view currently in use in the render target.
Definition at line 134 of file RenderTarget.cpp.
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 |
Definition at line 148 of file RenderTarget.cpp.
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.
Definition at line 71 of file RenderWindow.cpp.
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.
Definition at line 212 of file RenderTarget.cpp.
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.
Definition at line 180 of file Window.cpp.
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.
Definition at line 198 of file RenderTarget.cpp.
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).
Definition at line 183 of file RenderTarget.cpp.
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 |
Definition at line 308 of file Window.cpp.
void sf::Window::SetCursorPosition | ( | unsigned int | left, | |
unsigned int | top | |||
) | [inherited] |
Change the position of the mouse cursor.
left | Left coordinate of the cursor, relative to the window | |
top | Top coordinate of the cursor, relative to the window |
Definition at line 254 of file Window.cpp.
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) |
Definition at line 358 of file Window.cpp.
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 |
Definition at line 300 of file Window.cpp.
void sf::Window::SetJoystickThreshold | ( | float | threshold | ) | [inherited] |
Change the joystick threshold.
Ths 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] |
Definition at line 372 of file Window.cpp.
void sf::Window::SetPosition | ( | int | left, | |
int | top | |||
) | [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).
left | Left position | |
top | Top position |
Definition at line 268 of file Window.cpp.
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 |
Definition at line 276 of file Window.cpp.
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 |
Definition at line 125 of file RenderTarget.cpp.
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 |
Definition at line 284 of file Window.cpp.
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 |
Definition at line 246 of file Window.cpp.
void sf::Window::UseVerticalSync | ( | 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 |
Definition at line 238 of file Window.cpp.
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 |
Definition at line 224 of file Window.cpp.