Loading...
Searching...
No Matches

Base class for all render targets (window, texture, ...) More...

#include <SFML/Graphics/RenderTarget.hpp>

Inheritance diagram for sf::RenderTarget:
sf::RenderTexture sf::RenderWindow

Public Member Functions

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.
 
virtual Vector2u getSize () const =0
 Return the size of the rendering region of the target.
 
virtual bool isSrgb () const
 Tell if the render target will use sRGB encoding when drawing on it.
 
virtual bool setActive (bool active=true)
 Activate or deactivate the render target for rendering.
 
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

 RenderTarget ()=default
 Default constructor.
 
void initialize ()
 Performs the common initialization step after creation.
 

Detailed Description

Base class for all render targets (window, texture, ...)

sf::RenderTarget defines the common behavior of all the 2D render targets usable in the graphics module.

It makes it possible to draw 2D entities like sprites, shapes, text without using any OpenGL command directly.

A sf::RenderTarget is also able to use views (sf::View), which are a kind of 2D cameras. With views you can globally scroll, rotate or zoom everything that is drawn, without having to transform every single entity. See the documentation of sf::View for more details and sample pieces of code about this class.

On top of that, render targets are 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 pushGLStates/popGLStates functions.

While render targets are moveable, it is not valid to move them between threads. This will cause your program to crash. The problem boils down to OpenGL being limited with regard to how it works in multithreaded environments. Please ensure you only move render targets within the same thread.

See also
sf::RenderWindow, sf::RenderTexture, sf::View

Definition at line 62 of file RenderTarget.hpp.

Constructor & Destructor Documentation

◆ ~RenderTarget()

virtual sf::RenderTarget::~RenderTarget ( )
virtualdefault

Destructor.

◆ RenderTarget() [1/3]

sf::RenderTarget::RenderTarget ( const RenderTarget & )
delete

Deleted copy constructor.

◆ RenderTarget() [2/3]

sf::RenderTarget::RenderTarget ( RenderTarget && )
defaultnoexcept

Move constructor.

◆ RenderTarget() [3/3]

sf::RenderTarget::RenderTarget ( )
protecteddefault

Default constructor.

Member Function Documentation

◆ clear() [1/2]

void sf::RenderTarget::clear ( Color color,
StencilValue stencilValue )

Clear the entire target with a single color and stencil value.

The specified stencil value is truncated to the bit width of the current stencil buffer.

Parameters
colorFill color to use to clear the render target
stencilValueStencil value to clear to

◆ clear() [2/2]

void sf::RenderTarget::clear ( Color color = Color::Black)

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

◆ clearStencil()

void sf::RenderTarget::clearStencil ( StencilValue stencilValue)

Clear the stencil buffer to a specific value.

The specified value is truncated to the bit width of the current stencil buffer.

Parameters
stencilValueStencil value to clear to

◆ draw() [1/4]

void sf::RenderTarget::draw ( const Drawable & drawable,
const RenderStates & states = RenderStates::Default )

Draw a drawable object to the render target.

Parameters
drawableObject to draw
statesRender states to use for drawing

◆ draw() [2/4]

void sf::RenderTarget::draw ( const Vertex * vertices,
std::size_t vertexCount,
PrimitiveType type,
const RenderStates & states = RenderStates::Default )

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

◆ draw() [3/4]

void sf::RenderTarget::draw ( const VertexBuffer & vertexBuffer,
const RenderStates & states = RenderStates::Default )

Draw primitives defined by a vertex buffer.

Parameters
vertexBufferVertex buffer
statesRender states to use for drawing

◆ draw() [4/4]

void sf::RenderTarget::draw ( const VertexBuffer & vertexBuffer,
std::size_t firstVertex,
std::size_t vertexCount,
const RenderStates & states = RenderStates::Default )

Draw primitives defined by a vertex buffer.

Parameters
vertexBufferVertex buffer
firstVertexIndex of the first vertex to render
vertexCountNumber of vertices to render
statesRender states to use for drawing

◆ getDefaultView()

const View & sf::RenderTarget::getDefaultView ( ) const
nodiscard

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

◆ getScissor()

IntRect sf::RenderTarget::getScissor ( const View & view) const
nodiscard

Get the scissor rectangle of a view, applied to this render target.

The scissor rectangle 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 scissor rectangle actually covers in the target.

Parameters
viewThe view for which we want to compute the scissor rectangle
Returns
Scissor rectangle, expressed in pixels

◆ getSize()

virtual Vector2u sf::RenderTarget::getSize ( ) const
nodiscardpure virtual

Return the size of the rendering region of the target.

Returns
Size in pixels

Implemented in sf::RenderTexture, and sf::RenderWindow.

◆ getView()

const View & sf::RenderTarget::getView ( ) const
nodiscard

Get the view currently in use in the render target.

Returns
The view object that is currently used
See also
setView, getDefaultView

◆ getViewport()

IntRect sf::RenderTarget::getViewport ( const View & view) const
nodiscard

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

◆ initialize()

void sf::RenderTarget::initialize ( )
protected

Performs the common initialization step after creation.

The derived classes must call this function after the target is created and ready for drawing.

◆ isSrgb()

virtual bool sf::RenderTarget::isSrgb ( ) const
nodiscardvirtual

Tell if the render target will use sRGB encoding when drawing on it.

Returns
true if the render target use sRGB encoding, false otherwise

Reimplemented in sf::RenderTexture, and sf::RenderWindow.

◆ mapCoordsToPixel() [1/2]

Vector2i sf::RenderTarget::mapCoordsToPixel ( Vector2f point) const
nodiscard

Convert a point from world coordinates to target coordinates, using the current view.

This function is an overload of the mapCoordsToPixel function that implicitly 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

◆ mapCoordsToPixel() [2/2]

Vector2i sf::RenderTarget::mapCoordsToPixel ( Vector2f point,
const View & view ) const
nodiscard

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, i.e. 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

◆ mapPixelToCoords() [1/2]

Vector2f sf::RenderTarget::mapPixelToCoords ( Vector2i point) const
nodiscard

Convert a point from target coordinates to world coordinates, using the current view.

This function is an overload of the mapPixelToCoords function that implicitly 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

◆ mapPixelToCoords() [2/2]

Vector2f sf::RenderTarget::mapPixelToCoords ( Vector2i point,
const View & view ) const
nodiscard

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, i.e. 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

◆ operator=() [1/2]

RenderTarget & sf::RenderTarget::operator= ( const RenderTarget & )
delete

Deleted copy assignment.

◆ operator=() [2/2]

RenderTarget & sf::RenderTarget::operator= ( RenderTarget && )
defaultnoexcept

Move assignment.

◆ popGLStates()

void sf::RenderTarget::popGLStates ( )

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

◆ pushGLStates()

void sf::RenderTarget::pushGLStates ( )

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

◆ resetGLStates()

void sf::RenderTarget::resetGLStates ( )

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

◆ setActive()

virtual bool sf::RenderTarget::setActive ( bool active = true)
nodiscardvirtual

Activate or deactivate the render target for rendering.

This function makes the render target's context current for future OpenGL rendering operations (so you shouldn't care about it if you're not doing direct OpenGL stuff). A render target's context 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 context can be current in a thread, so if you want to draw OpenGL geometry to another render target don't forget to activate it again. Activating a render target will automatically deactivate the previously active context (if any).

Parameters
activetrue to activate, false to deactivate
Returns
true if operation was successful, false otherwise

Reimplemented in sf::RenderTexture, and sf::RenderWindow.

◆ setView()

void sf::RenderTarget::setView ( const View & view)

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

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