SFML 3.0.1
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
sf::RenderTexture Class Reference
Graphics module

Target for off-screen 2D rendering into a texture. More...

`#include <SFML/Graphics/RenderTexture.hpp>`

Inheritance diagram for sf::RenderTexture:

## Public Member Functions
| RenderTexture ()
| Default constructor.
| RenderTexture (Vector2u size, const ContextSettings &settings={})
| Construct a render-texture.
| ~RenderTexture () override
| Destructor.
| RenderTexture (const RenderTexture &)=delete
| Deleted copy constructor.
RenderTexture & | operator= (const RenderTexture &)=delete
| Deleted copy assignment.
| RenderTexture (RenderTexture &&) noexcept
| Move constructor.
RenderTexture & | operator= (RenderTexture &&) noexcept
| Move assignment operator.
bool | resize (Vector2u size, const ContextSettings &settings={})
| Resize the render-texture.
void | setSmooth (bool smooth)
| Enable or disable texture smoothing.
bool | isSmooth () const
| Tell whether the smooth filtering is enabled or not.
void | setRepeated (bool repeated)
| Enable or disable texture repeating.
bool | isRepeated () const
| Tell whether the texture is repeated or not.
bool | generateMipmap ()
| Generate a mipmap using the current texture data.
bool | setActive (bool active=true) override
| Activate or deactivate the render-texture for rendering.
void | display ()
| Update the contents of the target texture.
Vector2u | getSize () const override
| Return the size of the rendering region of the texture.
bool | isSrgb () const override
| Tell if the render-texture will use sRGB encoding when drawing on it.
const Texture & | getTexture () const
| Get a read-only reference to the target texture.
void | clear (Color color=Color::Black)
| Clear the entire target with a single color.
void | clear (Color color, StencilValue stencilValue)
| Clear the entire target with a single color and stencil value.
void | clearStencil (StencilValue stencilValue)
| Clear the stencil buffer to a specific value.
void | setView (const View &view)
| Change the current active view.
const View & | getView () const
| Get 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.
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.
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.

## Static Public Member Functions
static unsigned int | getMaximumAntiAliasingLevel ()
| Get the maximum anti-aliasing level supported by the system.

## Protected Member Functions
void | initialize ()
| Performs the common initialization step after creation.

## Detailed Description

Target for off-screen 2D rendering into a texture.

sf::RenderTexture is the little brother of sf::RenderWindow.

It implements the same 2D drawing and OpenGL-related functions (see their base class sf::RenderTarget for more details), the difference is that the result is stored in an off-screen texture rather than being show in a window.

Rendering to a texture can be useful in a variety of situations:

- precomputing a complex static texture (like a level's background from multiple tiles)
- applying post-effects to the whole scene with shaders
- creating a sprite from a 3D object rendered with OpenGL
- etc.

Usage example:

// Create a new render-window
sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML window");
// Create a new render-texture
sf::RenderTexture texture({500, 500});
// The main loop
while (window.isOpen())
{
// Event processing
// ...
// Clear the whole texture with red color
texture.clear(sf::Color::Red);
// Draw stuff to the texture
texture.draw(sprite); // sprite is a sf::Sprite
texture.draw(shape); // shape is a sf::Shape
texture.draw(text); // text is a sf::Text
// We're done drawing to the texture
texture.display();
// Now we start rendering to the window, clear it first
window.clear();
// Draw the texture
sf::Sprite sprite(texture.getTexture());
window.draw(sprite);
// End the current frame and display its contents on screen
window.display();
}
sf::Color::Red
static const Color Red
Red predefined color.
Definition Color.hpp:84
sf::RenderTarget::draw
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
Draw a drawable object to the render target.
sf::RenderTarget::clear
void clear(Color color=Color::Black)
Clear the entire target with a single color.
sf::RenderTexture
Target for off-screen 2D rendering into a texture.
Definition RenderTexture.hpp:54
sf::RenderWindow
Window that can serve as a target for 2D drawing.
Definition RenderWindow.hpp:55
sf::Sprite
Drawable representation of a texture, with its own transformations, color, etc.
Definition Sprite.hpp:51
sf::VideoMode
VideoMode defines a video mode (size, bpp)
Definition VideoMode.hpp:44
sf::WindowBase::isOpen
bool isOpen() const
Tell whether or not the window is open.
sf::Window::display
void display()
Display on screen what has been rendered to the window so far.

Like sf::RenderWindow, sf::RenderTexture is still able to render direct OpenGL stuff. It is even possible to mix together OpenGL calls and regular SFML drawing commands. If you need a depth buffer for 3D rendering, don't forget to request it when calling RenderTexture::create.

See also
  sf::RenderTarget, sf::RenderWindow, sf::View, sf::Texture

Definition at line 53 of file RenderTexture.hpp.

## Constructor & Destructor Documentation

## ◆ RenderTexture() [1/4]

sf::RenderTexture::RenderTexture | ( |  | ) |

Default constructor.

Constructs a render-texture with width 0 and height 0.

See also
  resize

## ◆ RenderTexture() [2/4]

sf::RenderTexture::RenderTexture | ( | Vector2u | size,
|  | const ContextSettings & | settings = {} )

Construct a render-texture.

The last parameter, settings, is useful if you want to enable multi-sampling or use the render-texture for OpenGL rendering that requires a depth or stencil buffer. Otherwise it is unnecessary, and you should leave this parameter at its default value.

After creation, the contents of the render-texture are undefined. Call RenderTexture::clear first to ensure a single color fill.

Parameters

  size | Width and height of the render-texture
  settings | Additional settings for the underlying OpenGL texture and context

Exceptions

  sf::Exception | if creation was unsuccessful

## ◆ ~RenderTexture()

sf::RenderTexture::~RenderTexture | ( |  | ) |
| override

Destructor.

## ◆ RenderTexture() [3/4]

sf::RenderTexture::RenderTexture | ( | const RenderTexture & |  | ) |
| delete

Deleted copy constructor.

## ◆ RenderTexture() [4/4]

sf::RenderTexture::RenderTexture | ( | RenderTexture && |  | ) |
| noexcept

Move constructor.

## Member Function Documentation

## ◆ clear() [1/2]

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

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

  color | Fill color to use to clear the render target
  stencilValue | Stencil value to clear to

## ◆ clear() [2/2]

void sf::RenderTarget::clear | ( | Color | color = Color::Black | ) |
| 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

  color | Fill color to use to clear the render target

## ◆ clearStencil()

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

Clear the stencil buffer to a specific value.

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

Parameters

  stencilValue | Stencil value to clear to

## ◆ display()

void sf::RenderTexture::display | ( |  | ) |

Update the contents of the target texture.

This function updates the target texture with what has been drawn so far. Like for windows, calling this function is mandatory at the end of rendering. Not calling it may leave the texture in an undefined state.

## ◆ draw() [1/4]

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

Draw a drawable object to the render target.

Parameters

  drawable | Object to draw
  states | Render 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 )
| inherited

Draw primitives defined by an array of vertices.

Parameters

  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

## ◆ draw() [3/4]

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

Draw primitives defined by a vertex buffer.

Parameters

  vertexBuffer | Vertex buffer
  states | Render 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 )
| inherited

Draw primitives defined by a vertex buffer.

Parameters

  vertexBuffer | Vertex buffer
  firstVertex | Index of the first vertex to render
  vertexCount | Number of vertices to render
  states | Render states to use for drawing

## ◆ generateMipmap()

bool sf::RenderTexture::generateMipmap | ( |  | ) |
| nodiscard

Generate a mipmap using the current texture data.

This function is similar to Texture::generateMipmap and operates on the texture used as the target for drawing. Be aware that any draw operation may modify the base level image data. For this reason, calling this function only makes sense after all drawing is completed and display has been called. Not calling display after subsequent drawing will lead to undefined behavior if a mipmap had been previously generated.

Returns
  true if mipmap generation was successful, false if unsuccessful

## ◆ getDefaultView()

const View & sf::RenderTarget::getDefaultView | ( |  | ) | const
| nodiscard 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

## ◆ getMaximumAntiAliasingLevel()

unsigned int sf::RenderTexture::getMaximumAntiAliasingLevel | ( |  | ) |
| static nodiscard

Get the maximum anti-aliasing level supported by the system.

Returns
  The maximum anti-aliasing level supported by the system

## ◆ getScissor()

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

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

  view | The view for which we want to compute the scissor rectangle

Returns
  Scissor rectangle, expressed in pixels

## ◆ getSize()

Vector2u sf::RenderTexture::getSize | ( |  | ) | const
| nodiscard override virtual

Return the size of the rendering region of the texture.

The returned value is the size that you passed to the create function.

Returns
  Size in pixels

Implements sf::RenderTarget.

## ◆ getTexture()

const Texture & sf::RenderTexture::getTexture | ( |  | ) | const
| nodiscard

Get a read-only reference to the target texture.

After drawing to the render-texture and calling Display, you can retrieve the updated texture using this function, and draw it using a sprite (for example). The internal sf::Texture of a render-texture is always the same instance, so that it is possible to call this function once and keep a reference to the texture even after it is modified.

Returns
  Const reference to the texture

## ◆ getView()

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

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

  view | The view for which we want to compute the viewport

Returns
  Viewport rectangle, expressed in pixels

## ◆ initialize()

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.

## ◆ isRepeated()

bool sf::RenderTexture::isRepeated | ( |  | ) | const
| nodiscard

Tell whether the texture is repeated or not.

Returns
  true if texture is repeated

See also
  setRepeated

## ◆ isSmooth()

bool sf::RenderTexture::isSmooth | ( |  | ) | const
| nodiscard

Tell whether the smooth filtering is enabled or not.

Returns
  true if texture smoothing is enabled

See also
  setSmooth

## ◆ isSrgb()

bool sf::RenderTexture::isSrgb | ( |  | ) | const
| nodiscard override virtual

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

You can request sRGB encoding for a render-texture by having the sRgbCapable flag set for the context parameter of create() method

Returns
  true if the render-texture use sRGB encoding, false otherwise

Reimplemented from sf::RenderTarget.

## ◆ mapCoordsToPixel() [1/2]

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

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

  point | Point 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 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, 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

  point | Point to convert
  view | The 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 inherited

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

  point | Pixel 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 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, 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

  point | Pixel to convert
  view | The view to use for converting the point

Returns
  The converted point, in "world" units

See also
  mapCoordsToPixel

## ◆ operator=() [1/2]

RenderTexture & sf::RenderTexture::operator= | ( | const RenderTexture & |  | ) |
| delete

Deleted copy assignment.

## ◆ operator=() [2/2]

RenderTexture & sf::RenderTexture::operator= | ( | RenderTexture && |  | ) |
| noexcept

Move assignment operator.

## ◆ popGLStates()

void sf::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

## ◆ pushGLStates()

void sf::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

## ◆ resetGLStates()

void sf::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...

## ◆ resize()

bool sf::RenderTexture::resize | ( | Vector2u | size,
|  | const ContextSettings & | settings = {} )
| nodiscard

Resize the render-texture.

The last parameter, settings, is useful if you want to enable multi-sampling or use the render-texture for OpenGL rendering that requires a depth or stencil buffer. Otherwise it is unnecessary, and you should leave this parameter at its default value.

After resizing, the contents of the render-texture are undefined. Call RenderTexture::clear first to ensure a single color fill.

Parameters

  size | Width and height of the render-texture
  settings | Additional settings for the underlying OpenGL texture and context

Returns
  true if resizing has been successful, false if it failed

## ◆ setActive()

bool sf::RenderTexture::setActive | ( | bool | active = true | ) |
| nodiscard override virtual

Activate or deactivate the render-texture for rendering.

This function makes the render-texture's context current for future OpenGL rendering operations (so you shouldn't care about it if you're not doing direct OpenGL stuff). Only one context can be current in a thread, so if you want to draw OpenGL geometry to another render target (like a RenderWindow) don't forget to activate it again.

Parameters

  active | true to activate, false to deactivate

Returns
  true if operation was successful, false otherwise

Reimplemented from sf::RenderTarget.

## ◆ setRepeated()

void sf::RenderTexture::setRepeated | ( | bool | repeated | ) |

Enable or disable texture repeating.

This function is similar to Texture::setRepeated. This parameter is disabled by default.

Parameters

  repeated | true to enable repeating, false to disable it

See also
  isRepeated

## ◆ setSmooth()

void sf::RenderTexture::setSmooth | ( | bool | smooth | ) |

Enable or disable texture smoothing.

This function is similar to Texture::setSmooth. This parameter is disabled by default.

Parameters

  smooth | true to enable smoothing, false to disable it

See also
  isSmooth

## ◆ setView()

void sf::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

  view | New view to use

See also
  getView, getDefaultView

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

- RenderTexture.hpp