Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | List of all members
gf::RenderTexture Class Reference

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

#include <gf/RenderTexture.h>

Inheritance diagram for gf::RenderTexture:
Inheritance graph
[legend]

Public Member Functions

 RenderTexture ()
 Default constructor. More...
 
 ~RenderTexture ()
 Destructor. More...
 
bool create (Vector2u size)
 Create the render-texture. More...
 
void setSmooth (bool smooth=true)
 Enable or disable texture smoothing. More...
 
bool isSmooth () const
 Check if the smooth filtering is enabled or not. More...
 
void setRepeated (bool repeated=true)
 Enable or disable texture repeating. More...
 
bool isRepeated () const
 Check if the texture is repeated or not. More...
 
virtual Vector2u getSize () const override
 Return the size of the rendering region of the target. More...
 
void setActive ()
 Activate the render-texture for rendering. More...
 
void display ()
 Update the contents of the target texture. More...
 
Image capture () const
 Copy the current contents of the render texture to an image. More...
 
const TexturegetTexture () const
 Get a read-only reference to the target texture. More...
 
- Public Member Functions inherited from gf::RenderTarget
 RenderTarget ()=default
 Default constructor. More...
 
virtual ~RenderTarget ()
 Destructor. More...
 
 RenderTarget (const RenderTarget &)=delete
 Deleted copy constructor. More...
 
RenderTargetoperator= (const RenderTarget &)=delete
 Deleted copy assignment. More...
 
bool getScissorTest ()
 Tell if the scissor test is enabled. More...
 
void setScissorTest (bool scissor=true)
 Enable or disable the scissor test. More...
 
RectI getScissoBox ()
 Get the current scissor box. More...
 
void setScissorBox (const RectI &box)
 Define the scissor box. More...
 
void clear (const Color4f &color)
 Clear the entire target with a single color. More...
 
void clear ()
 Clear the entire target. More...
 
RangeF getAliasedLineWidthRange () const
 Get the range for aliased line width. More...
 
float getLineWidth () const
 Get the line width. More...
 
void draw (const Vertex *vertices, std::size_t count, PrimitiveType type, const RenderStates &states=RenderStates())
 Draw primitives defined by an array of vertices. More...
 
void draw (const Vertex *vertices, const uint16_t *indices, std::size_t count, PrimitiveType type, const RenderStates &states=RenderStates())
 Draw primitives defined by an array of vertices and their indices. More...
 
void draw (const Vertex *vertices, int *first, const std::size_t *count, std::size_t primcount, PrimitiveType type, const RenderStates &states=RenderStates())
 Draw primitives defined by an array of vertices. More...
 
void draw (const Vertex *vertices, const uint16_t **indices, const std::size_t *count, std::size_t primcount, PrimitiveType type, const RenderStates &states=RenderStates())
 Draw primitives defined by an array of vertices and their indices. More...
 
void draw (const VertexBuffer &buffer, const RenderStates &states=RenderStates())
 Draw a vertex buffer to the render target. More...
 
void draw (Drawable &drawable, const RenderStates &states=RenderStates())
 Draw a drawable object to the render target. 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...
 
RectI getViewport (const View &view) const
 Get the viewport of a view, applied to this render target. More...
 
Vector2f mapPixelToCoords (Vector2i point, const View &view) const
 Convert a point from target coordinates to world coordinates. More...
 
Vector2f mapPixelToCoords (Vector2i point) const
 Convert a point from target coordinates to world coordinates, using the current view. More...
 
Vector2i mapCoordsToPixel (Vector2f point, const View &view) const
 Convert a point from world coordinates to target coordinates. More...
 
Vector2i mapCoordsToPixel (Vector2f point) const
 Convert a point from world coordinates to target coordinates, using the current view. More...
 

Additional Inherited Members

- Protected Member Functions inherited from gf::RenderTarget
void initialize ()
 Performs the common initialization step after creation. More...
 
Image captureFramebuffer (unsigned name) const
 Capture the given framebuffer. More...
 

Detailed Description

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

gf::RenderTexture is the little brother of gf::RenderWindow. It implements the same 2D drawing and OpenGL-related functions (see their base class gf::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:

Usage example:

// Create a new render-window
gf::RenderWindow renderer(window);
// Create a new render-texture
gf::RenderTexture textureRenderer;
if (!textureRenderer.create(500, 500)) {
return -1;
}
// The main loop
while (window.isOpen()) {
// Event processing
// ...
// Activate the texture
textureRenderer.setActive();
// Clear the whole texture with red color
textureRenderer.clear(gf::Color::Red);
// Draw stuff to the texture
textureRenderer.draw(sprite); // sprite is a gf::Sprite
textureRenderer.draw(shape); // shape is a gf::Shape
textureRenderer.draw(text); // text is a gf::Text
// We're done drawing to the texture
textureRenderer.display();
// Now we start rendering to the window
// Activate it first and then clear it
renderer.setActive();
renderer.clear();
// Draw the texture
gf::Sprite sprite(textureRenderer.getTexture());
renderer.draw(sprite);
// End the current frame and display its contents on screen
renderer.display();
}
See also
gf::RenderTarget, gf::RenderWindow, gf::View, gf::Texture

Constructor & Destructor Documentation

gf::RenderTexture::RenderTexture ( )

Default constructor.

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

See also
create()
gf::RenderTexture::~RenderTexture ( )

Destructor.

Member Function Documentation

Image gf::RenderTexture::capture ( ) const

Copy the current contents of the render texture to an image.

This is a slow operation, whose main purpose is to make screenshots of the application.

bool gf::RenderTexture::create ( Vector2u  size)

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.

Parameters
sizeSize of the render-texture
Returns
True if creation has been successful
void gf::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.

virtual Vector2u gf::RenderTexture::getSize ( ) const
overridevirtual

Return the size of the rendering region of the target.

Returns
Size in pixels

Implements gf::RenderTarget.

const Texture& gf::RenderTexture::getTexture ( ) const
inline

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
bool gf::RenderTexture::isRepeated ( ) const
inline

Check if the texture is repeated or not.

Returns
True if texture is repeated
See also
setRepeated()
bool gf::RenderTexture::isSmooth ( ) const
inline

Check if the smooth filtering is enabled or not.

Returns
True if texture smoothing is enabled
See also
setSmooth()
void gf::RenderTexture::setActive ( )

Activate the render-texture for rendering.

This function activates the render-texture so that all draw calls are targeted to the texture. You should call this function before you want to draw something to the target.

void gf::RenderTexture::setRepeated ( bool  repeated = true)
inline

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()
void gf::RenderTexture::setSmooth ( bool  smooth = true)
inline

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