El Octavio  1.0
This is a video game about adventures.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sf::RenderTexture Class Reference

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

#include <RenderTexture.hpp>

+ Inheritance diagram for sf::RenderTexture:
+ Collaboration diagram for sf::RenderTexture:

Public Member Functions

 RenderTexture ()
 Default constructor. More...
 
virtual ~RenderTexture ()
 Destructor. More...
 
SFML_DEPRECATED bool create (unsigned int width, unsigned int height, bool depthBuffer)
 Create the render-texture. More...
 
bool create (unsigned int width, unsigned int height, const ContextSettings &settings=ContextSettings())
 Create the render-texture. More...
 
void setSmooth (bool smooth)
 Enable or disable texture smoothing. More...
 
bool isSmooth () const
 Tell whether the smooth filtering is enabled or not. More...
 
void setRepeated (bool repeated)
 Enable or disable texture repeating. More...
 
bool isRepeated () const
 Tell whether the texture is repeated or not. More...
 
bool generateMipmap ()
 Generate a mipmap using the current texture data. More...
 
bool setActive (bool active=true)
 Activate or deactivate the render-texture for rendering. More...
 
void display ()
 Update the contents of the target texture. More...
 
virtual Vector2u getSize () const
 Return the size of the rendering region of the texture. More...
 
const TexturegetTexture () const
 Get a read-only reference to the target texture. More...
 
- Public Member Functions inherited from sf::RenderTarget
virtual ~RenderTarget ()
 Destructor. 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, std::size_t vertexCount, PrimitiveType type, const RenderStates &states=RenderStates::Default)
 Draw primitives defined by an array of vertices. More...
 
void draw (const VertexBuffer &vertexBuffer, const RenderStates &states=RenderStates::Default)
 Draw primitives defined by a vertex buffer. More...
 
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. More...
 
virtual Vector2u getSize () const =0
 Return the size of the rendering region of the target. More...
 
virtual bool setActive (bool active=true)
 Activate or deactivate the render target for rendering. 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...
 

Static Public Member Functions

static unsigned int getMaximumAntialiasingLevel ()
 Get the maximum anti-aliasing level supported by the system. More...
 

Additional Inherited Members

- Protected Member Functions inherited from sf::RenderTarget
 RenderTarget ()
 Default constructor. More...
 
void initialize ()
 Performs the common initialization step after creation. More...
 

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
if (!texture.create(500, 500))
return -1;
// The main loop
while (window.isOpen())
{
// Event processing
// ...
// Clear the whole texture with red color
// 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();
}
static const Color Red
Red predefined color.
Definition: Color.hpp:85
Target for off-screen 2D rendering into a texture.
Window that can serve as a target for 2D drawing.
Drawable representation of a texture, with its own transformations, color, etc.
Definition: Sprite.hpp:48
VideoMode defines a video mode (width, height, bpp)
Definition: VideoMode.hpp:42
Texture texture
Definition: Menu.cpp:17

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 48 of file RenderTexture.hpp.

Constructor & Destructor Documentation

◆ RenderTexture()

sf::RenderTexture::RenderTexture ( )

Default constructor.

Constructs an empty, invalid render-texture. You must call create to have a valid render-texture.

See also
create

◆ ~RenderTexture()

virtual sf::RenderTexture::~RenderTexture ( )
virtual

Destructor.

Member Function Documentation

◆ create() [1/2]

SFML_DEPRECATED bool sf::RenderTexture::create ( unsigned int  width,
unsigned int  height,
bool  depthBuffer 
)

Create the render-texture.

Before calling this function, the render-texture is in an invalid state, thus it is mandatory to call it before doing anything with the render-texture. The last parameter, depthBuffer, is useful if you want to use the render-texture for 3D OpenGL rendering that requires a depth buffer. Otherwise it is unnecessary, and you should leave this parameter to false (which is its default value).

Parameters
widthWidth of the render-texture
heightHeight of the render-texture
depthBufferDo you want this render-texture to have a depth buffer?
Returns
True if creation has been successful
Deprecated:
Use create(unsigned int, unsigned int, const ContextSettings&) instead.

◆ create() [2/2]

bool sf::RenderTexture::create ( unsigned int  width,
unsigned int  height,
const ContextSettings settings = ContextSettings() 
)

Create the render-texture.

Before calling this function, the render-texture is in an invalid state, thus it is mandatory to call it before doing anything with 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.

Parameters
widthWidth of the render-texture
heightHeight of the render-texture
settingsAdditional settings for the underlying OpenGL texture and context
Returns
True if creation has been successful

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

◆ generateMipmap()

bool sf::RenderTexture::generateMipmap ( )

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

◆ getMaximumAntialiasingLevel()

static unsigned int sf::RenderTexture::getMaximumAntialiasingLevel ( )
static

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

Returns
The maximum anti-aliasing level supported by the system

◆ getSize()

virtual Vector2u sf::RenderTexture::getSize ( ) const
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

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

◆ isRepeated()

bool sf::RenderTexture::isRepeated ( ) const

Tell whether the texture is repeated or not.

Returns
True if texture is repeated
See also
setRepeated

◆ isSmooth()

bool sf::RenderTexture::isSmooth ( ) const

Tell whether the smooth filtering is enabled or not.

Returns
True if texture smoothing is enabled
See also
setSmooth

◆ setActive()

bool sf::RenderTexture::setActive ( bool  active = true)
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
activeTrue 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
repeatedTrue 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
smoothTrue to enable smoothing, false to disable it
See also
isSmooth

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