Game Development Reference
In-Depth Information
Rendering shapes with textures
Let's start with something important, which isn't always completely obvious to everyone.
Textures cannot be rendered on their own. They need a surface to be mapped to and then
that surface can be rendered. Textures, as we mentioned in the beginning of the chapter, are
just a collection of pixels, which cannot be rendered directly on the screen without having
some sort of reference (such as a position, rotation, and so on). However, in SFML, there
are renderable classes which can use a texture for their surface. In fact, we used one of
those classes in the previous chapter—the shape.
Apart from a fill color and an outline color, every Shape object can have a texture as well.
We can apply a texture to a shape by calling Shape::setTexture() and passing a
pointer to a texture. The last thing we need to do is render the shape in a window:
The first important thing that stands out is that Shape::setTexture() takes a pointer
rather than a reference. That's why we pass the address of the texture with &texture . The
shape then stores that pointer locally and uses it when it needs to be rendered. This means
that the address, which we pass to the function, has to hold a valid texture throughout the
lifetime of the shape. Moving the texture in memory or destroying it will lead to a dangling
pointer inside the Shape object, resulting in an undefined behavior. That is why we always
need to make sure that a texture does not get out of the scope of the object which uses it.
We will explore resource management techniques in the last section of this chapter.
Search WWH ::




Custom Search