Game Development Reference
In-Depth Information
After we declare a
RenderTexture
variable, we have to call the
RenderTex-
ture::create()
method to actually make a valid render texture. The method takes
width and height parameters as well as an indicator if the render texture uses a depth buf-
fer. In this case, we are only rendering a single circle shape on it, so no depth buffer is re-
quired. The depth buffer allows us to discard any fragments (pixels) that are rendered be-
hind already existing fragments. As such, if we were rendering 3D models to a texture
(using OpenGL) we would probably want the depth buffer enabled. In this example our
single circle shape is with radius 16, so we want a texture with a width and height of
2xRadius = 32.
After we create our circle shape, we render it onto the texture.
RenderTex-
ture::clear()
resets each texel of the texture to the given color. The color argument
is optional though, with
Color::Black
being the default color. Next, we draw our
shape onto the texture with the
RenderTexture::draw()
method and finally display
everything that has been drawn so far. The
RenderTexture::display()
method
updates each pixel of the texture with everything that has been done to it since the last
time it was called.
Now that we have our texture ready, we can use it by getting the underlying
Texture
object with
RenderTexture::getTexture()
. The method returns a reference to
the texture object stored as a field inside
RenderTexture
. In this case we opt to create
a sprite out of it, which we can use later on for any purpose that a sprite can be used for.