Public Member Functions | Protected Member Functions | Protected Attributes | Static Private Member Functions | List of all members
cpp3ds::Window Class Reference

Window that serves as a target for OpenGL rendering. More...

#include <Window.hpp>

Inheritance diagram for cpp3ds::Window:
cpp3ds::RenderTarget cpp3ds::GlResource cpp3ds::NonCopyable

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 ContextSettingsgetSettings () 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 ViewgetView () const
 Get the view currently in use in the render target. More...
 
const ViewgetDefaultView () 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...
 

Detailed Description

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:

// Declare and create a new window
cpp3ds::Window window(cpp3ds::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 (window.pollEvent(event))
{
// Request for closing the window
if (event.type == cpp3ds::Event::Closed)
window.close();
}
// Activate the window for OpenGL rendering
window.setActive();
// OpenGL drawing commands go here...
// End the current frame and display its contents on screen
window.display();
}

Definition at line 47 of file Window/Window.hpp.

Constructor & Destructor Documentation

cpp3ds::Window::Window ( )

Default constructor.

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

virtual cpp3ds::Window::~Window ( )
virtual

Destructor.

Closes the window and frees all the resources attached to it.

Member Function Documentation

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.

Returns
Image containing the captured contents
void cpp3ds::RenderTarget::clear ( const Color color = Color(0, 0, 0, 255))
inherited

Clear the entire target with a single color.

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

Parameters
colorFill 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.

Parameters
settingsAdditional 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.

void cpp3ds::RenderTarget::draw ( const Drawable drawable,
const RenderStates states = RenderStates::Default 
)
inherited

Draw a drawable object to the render-target.

Parameters
drawableObject to draw
statesRender states to use for drawing
void cpp3ds::RenderTarget::draw ( const Vertex vertices,
unsigned int  vertexCount,
PrimitiveType  type,
const RenderStates states = RenderStates::Default 
)
inherited

Draw primitives defined by an array of vertices.

Parameters
verticesPointer to the vertices
vertexCountNumber of vertices in the array
typeType of primitives to draw
statesRender states to use for drawing
const View& cpp3ds::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.

Returns
The default view of the render target
See also
setView, getView
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.

Returns
Structure containing the OpenGL context settings
virtual Vector2u cpp3ds::Window::getSize ( ) const
virtual

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
See also
setSize

Implements cpp3ds::RenderTarget.

const View& cpp3ds::RenderTarget::getView ( ) const
inherited

Get the view currently in use in the render target.

Returns
The view object that is currently used
See also
setView, getDefaultView
IntRect cpp3ds::RenderTarget::getViewport ( const View view) const
inherited

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.

Parameters
viewThe view for which we want to compute the viewport
Returns
Viewport rectangle, expressed in pixels
void cpp3ds::Window::initialize ( )
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).

Returns
True if the window is open, false if it has been closed
Vector2i cpp3ds::RenderTarget::mapCoordsToPixel ( const Vector2f point) const
inherited

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:

target.mapCoordsToPixel(point, target.getView());
Parameters
pointPoint to convert
Returns
The converted point, in target coordinates (pixels)
See also
mapPixelToCoords
Vector2i cpp3ds::RenderTarget::mapCoordsToPixel ( const Vector2f point,
const View view 
) const
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.

Parameters
pointPoint to convert
viewThe view to use for converting the point
Returns
The converted point, in target coordinates (pixels)
See also
mapPixelToCoords
Vector2f cpp3ds::RenderTarget::mapPixelToCoords ( const Vector2i point) const
inherited

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:

target.mapPixelToCoords(point, target.getView());
Parameters
pointPixel to convert
Returns
The converted point, in "world" coordinates
See also
mapCoordsToPixel
Vector2f cpp3ds::RenderTarget::mapPixelToCoords ( const Vector2i point,
const View view 
) const
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.

Parameters
pointPixel to convert
viewThe view to use for converting the point
Returns
The converted point, in "world" units
See also
mapCoordsToPixel
void cpp3ds::RenderTarget::popGLStates ( )
inherited

Restore the previously saved OpenGL render states and matrices.

See the description of pushGLStates to get a detailed description of these functions.

See also
pushGLStates
void cpp3ds::RenderTarget::pushGLStates ( )
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:

  • SFML's internal states are not messed up by your OpenGL code
  • your OpenGL states are not modified by a call to a SFML function

More specifically, it must be used around code that calls Draw functions. Example:

// OpenGL code here...
window.pushGLStates();
window.draw(...);
window.draw(...);
window.popGLStates();
// OpenGL code here...

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.

See also
popGLStates
void cpp3ds::RenderTarget::resetGLStates ( )
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:

// OpenGL code here...
glPushAttrib(...);
window.resetGLStates();
window.draw(...);
window.draw(...);
glPopAttrib(...);
// OpenGL code here...
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.

Parameters
activeTrue to activate, false to deactivate
Returns
True if operation was successful, false otherwise
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).

Parameters
limitFramerate 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.

Parameters
enabledTrue to enable v-sync, false to deactivate it
void cpp3ds::RenderTarget::setView ( const View view)
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.

Parameters
viewNew view to use
See also
getView, getDefaultView

Member Data Documentation

Clock cpp3ds::Window::m_clock
protected

Clock for measuring the elapsed time between frames.

Definition at line 231 of file Window/Window.hpp.

priv::GlContext* cpp3ds::Window::m_context
protected

Platform-specific implementation of the OpenGL context.

Definition at line 230 of file Window/Window.hpp.

Time cpp3ds::Window::m_frameTimeLimit
protected

Current framerate limit.

Definition at line 232 of file Window/Window.hpp.

Vector2u cpp3ds::Window::m_size
protected

Current size of the window.

Definition at line 233 of file Window/Window.hpp.


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