- cpp3ds
- Window
Window that serves as a target for OpenGL rendering. More...
#include <Window.hpp>
Public Member Functions | |
Window () | |
Default constructor. More... | |
virtual | ~Window () |
Destructor. More... | |
void | create (const ContextSettings &settings=ContextSettings()) |
Create (or recreate) the window. More... | |
void | close () |
Close the window and destroy all the attached resources. More... | |
bool | isOpen () const |
Tell whether or not the window is open. More... | |
const ContextSettings & | getSettings () const |
Get the settings of the OpenGL context of the window. More... | |
virtual Vector2u | getSize () const |
Get the size of the rendering region of the window. More... | |
void | setVerticalSyncEnabled (bool enabled) |
Enable or disable vertical synchronization. More... | |
void | setFramerateLimit (unsigned int limit) |
Limit the framerate to a maximum fixed frequency. More... | |
bool | setActive (bool active=true) const |
Activate or deactivate the window as the current target for OpenGL rendering. More... | |
void | display () |
Display on screen what has been rendered to the window so far. More... | |
Image | capture () const |
Copy the current contents of the window to an image. More... | |
void | clear (const Color &color=Color(0, 0, 0, 255)) |
Clear the entire target with a single color. More... | |
void | setView (const View &view) |
Change the current active view. More... | |
const View & | getView () const |
Get the view currently in use in the render target. More... | |
const View & | getDefaultView () const |
Get the default view of the render target. More... | |
IntRect | getViewport (const View &view) const |
Get the viewport of a view, applied to this render target. More... | |
Vector2f | mapPixelToCoords (const Vector2i &point) const |
Convert a point from target coordinates to world coordinates, using the current view. More... | |
Vector2f | mapPixelToCoords (const Vector2i &point, const View &view) const |
Convert a point from target coordinates to world coordinates. More... | |
Vector2i | mapCoordsToPixel (const Vector2f &point) const |
Convert a point from world coordinates to target coordinates, using the current view. More... | |
Vector2i | mapCoordsToPixel (const Vector2f &point, const View &view) const |
Convert a point from world coordinates to target coordinates. More... | |
void | draw (const Drawable &drawable, const RenderStates &states=RenderStates::Default) |
Draw a drawable object to the render-target. More... | |
void | draw (const Vertex *vertices, unsigned int vertexCount, PrimitiveType type, const RenderStates &states=RenderStates::Default) |
Draw primitives defined by an array of vertices. More... | |
void | pushGLStates () |
Save the current OpenGL render states and matrices. More... | |
void | popGLStates () |
Restore the previously saved OpenGL render states and matrices. More... | |
void | resetGLStates () |
Reset the internal OpenGL states so that the target is ready for drawing. More... | |
Protected Member Functions | |
void | initialize () |
Perform some common internal initializations. More... | |
Protected Attributes | |
priv::GlContext * | m_context |
Platform-specific implementation of the OpenGL context. More... | |
Clock | m_clock |
Clock for measuring the elapsed time between frames. More... | |
Time | m_frameTimeLimit |
Current framerate limit. More... | |
Vector2u | m_size |
Current size of the window. More... | |
Static Private Member Functions | |
static void | ensureGlContext () |
Make sure that a valid OpenGL context exists in the current thread. More... | |
Window that serves as a target for OpenGL rendering.
cpp3ds::Window is the main class of the Window module.
It defines an OS window that is able to receive an OpenGL rendering.
A cpp3ds::Window can create its own new window, or be embedded into an already existing control using the create(handle) function. This can be useful for embedding an OpenGL rendering area into a view which is part of a bigger GUI with existing windows, controls, etc. It can also serve as embedding an OpenGL rendering area into a window created by another (probably richer) GUI library like Qt or wxWidgets.
The cpp3ds::Window class provides a simple interface for manipulating the window: move, resize, show/hide, control mouse cursor, etc. It also provides event handling through its pollEvent() and waitEvent() functions.
Note that OpenGL experts can pass their own parameters (antialiasing level, bits for the depth and stencil buffers, etc.) to the OpenGL context attached to the window, with the cpp3ds::ContextSettings structure which is passed as an optional argument when creating the window.
Usage example:
Definition at line 47 of file Window/Window.hpp.
cpp3ds::Window::Window | ( | ) |
Default constructor.
This constructor doesn't actually create the window, use the other constructors or call create() to do so.
|
virtual |
Destructor.
Closes the window and frees all the resources attached to it.
Image cpp3ds::Window::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 cpp3ds::Window::close | ( | ) |
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 isOpen() every time), and will have no effect on closed windows.
void cpp3ds::Window::create | ( | const ContextSettings & | settings = ContextSettings() | ) |
Create (or recreate) the window.
If the window was already created, it closes it first.
The parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc.
settings | Additional settings for the underlying OpenGL context |
void cpp3ds::Window::display | ( | ) |
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.
|
inherited |
Draw a drawable object to the render-target.
drawable | Object to draw |
states | Render states to use for drawing |
|
inherited |
Draw primitives defined by an array of vertices.
vertices | Pointer to the vertices |
vertexCount | Number of vertices in the array |
type | Type of primitives to draw |
states | Render states to use for drawing |
|
inherited |
const ContextSettings& cpp3ds::Window::getSettings | ( | ) | const |
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.
|
virtual |
Get the size of the rendering region of the window.
The size doesn't include the titlebar and borders of the window.
Implements cpp3ds::RenderTarget.
|
inherited |
Get 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 |
|
protected |
Perform some common internal initializations.
bool cpp3ds::Window::isOpen | ( | ) | const |
Tell whether or not the window is open.
This function returns whether or not the window exists. Note that a hidden window (setVisible(false)) is open (therefore this function would return true).
Convert a point from world coordinates to target coordinates, using the current view.
This function is an overload of the mapCoordsToPixel function that implicitely uses the current view. It is equivalent to:
point | Point to convert |
|
inherited |
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, ie. 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.
point | Point to convert |
view | The view to use for converting the point |
Convert a point from target coordinates to world coordinates, using the current view.
This function is an overload of the mapPixelToCoords function that implicitely uses the current view. It is equivalent to:
point | Pixel to convert |
|
inherited |
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, ie. 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.
point | Pixel to convert |
view | The view to use for converting the point |
|
inherited |
Restore the previously saved OpenGL render states and matrices.
See the description of pushGLStates to get a detailed description of these functions.
|
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 PopGLStates, it ensures that:
More specifically, it must be used around code that calls Draw functions. Example:
Note that this function is quite expensive: it saves all the possible OpenGL states and matrices, even the ones you don't care about. Therefore it should be used wisely. It is provided for convenience, but the best results will be achieved if you handle OpenGL states yourself (because you know which states have really changed, and need to be saved and restored). Take a look at the ResetGLStates function if you do so.
|
inherited |
Reset the internal OpenGL states so that the target is ready for drawing.
This function can be used when you mix SFML drawing and direct OpenGL rendering, if you choose not to use pushGLStates/popGLStates. It makes sure that all OpenGL states needed by SFML are set, so that subsequent draw() calls will work as expected.
Example:
bool cpp3ds::Window::setActive | ( | bool | active = true | ) | const |
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 cpp3ds::Window::setFramerateLimit | ( | unsigned int | limit | ) |
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 cpp3ds::sleep, whose precision depends on the underlying OS, the results may be a little unprecise as well (for example, you can get 65 FPS when requesting 60).
limit | Framerate limit, in frames per seconds (use 0 to disable limit) |
void cpp3ds::Window::setVerticalSyncEnabled | ( | bool | enabled | ) |
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 it |
|
inherited |
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 getDefaultView() to this function.
view | New view to use |
|
protected |
Clock for measuring the elapsed time between frames.
Definition at line 231 of file Window/Window.hpp.
|
protected |
Platform-specific implementation of the OpenGL context.
Definition at line 230 of file Window/Window.hpp.
|
protected |
Current framerate limit.
Definition at line 232 of file Window/Window.hpp.
|
protected |
Current size of the window.
Definition at line 233 of file Window/Window.hpp.