Game Development Reference
In-Depth Information
Creating textures
The
Texture
class shares a lot of methods with the
Image
class. For example, we can
load textures from files in the same way we did with images:
Texture::loadFromFile()
offers a bit more functionality though. When loading a
texture from a file, we can opt to load only a small section of the image. There is an option-
al argument in
Texture::loadFromFile()
, which allows us to do that. In the fol-
lowing code, we will only load a 32 x 32 pixel square from the original image, beginning at
the top-left corner:
The preceding code loads the whole image and only then creates a texture from the speci-
fied rectangle. This method becomes inefficient if we want to use the same image multiple
times. The alternative is to load the image file once (in
Image
), and use that to create our
textures. Here's how to create textures directly from an image:
Like the
Image
class, textures can be created by calling
Texture::create()
.
However, we have to be careful with the dimensions we provide for the texture, since all
existing graphics cards have limits for the texture size. Fortunately, there is a static func-