View on GitHub

DSFML

dsfml.graphics.rendertexture



class RenderTexture: dsfml.graphics.rendertarget.RenderTarget;
Target for off-screen 2D rendering into a texture.

RenderTexture is the little brother of RenderWindow.

It implements the same 2D drawing and OpenGL-related functions (see their base class 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.

Authors:
Laurent Gomila, Jeremy DeHaan

See Also:


http:
//www.sfml-dev.org/documentation/2.0/classsf_1_1RenderTexture.php#details

void create(uint width, uint height, bool depthBuffer = false);
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).

Params:
uint width Width of the render-texture
uint height Height of the render-texture
bool depthBuffer Do you want this render-texture to have a depth buffer?

@property bool smooth(bool newSmooth);
Enable or disable texture smoothing.

@property bool smooth();
Enable or disable texture smoothing.

@property const(View) view(const(View) newView);
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.

const @property 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.

const const(View) getDefaultView();
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.

const Vector2u getSize();
Return the size of the rendering region of the target.

Returns:
Size in pixels

const IntRect getViewport(const(View) view);
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.

Params:
const(View) view The view for which we want to compute the viewport

Returns:
Viewport rectangle, expressed in pixels

const(Texture) getTexture();
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 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.

void setActive(bool active = true);
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.

Params:
bool active True to activate, false to deactivate

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

Params:
Color color Fill color to use to clear the render target

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

void draw(Drawable drawable, RenderStates states = RenderStates.Default);
Draw a drawable object to the render target.

Params:
Drawable drawable Object to draw
RenderStates states Render states to use for drawing

void draw(const(Vertex)[] vertices, PrimitiveType type, RenderStates states = RenderStates.Default);
Draw primitives defined by an array of vertices.

Params:
const(Vertex)[] vertices Array of vertices to draw
PrimitiveType type Type of primitives to draw
RenderStates states Render states to use for drawing

const Vector2f mapPixelToCoords(Vector2i point);
Convert a point fom target coordinates to world coordinates, using the current view.

This function is an overload of the mapPixelToCoords function that implicitely uses the current view.

Params:
Vector2i point Pixel to convert

Returns:
The converted point, in "world" coordinates.

const Vector2f mapPixelToCoords(Vector2i point, const(View) view);
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.

Params:
Vector2i point Pixel to convert
const(View) view The view to use for converting the point

Returns:
The converted point, in "world" coordinates.

const Vector2i mapCoordsToPixel(Vector2f point);
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.

Params:
Vector2f point Point to convert

The converted point, in "world" coordinates

const Vector2i mapCoordsToPixel(Vector2f point, const(View) view);
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.

Params:
Vector2f point Point to convert
const(View) view The view to use for converting the point

Returns:
The converted point, in target coordinates (pixels)

void popGLStates();
Restore the previously saved OpenGL render states and matrices.

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

void 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 an SFML function

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

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.

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