dsfml.graphics.texture

$(U Texture) stores pixels that can be drawn, with a sprite for example. A texture lives in the graphics card memory, therefore it is very fast to draw a texture to a render target, or copy a render target to a texture (the graphics card can access both directly). * Being stored in the graphics card memory has some drawbacks. A texture cannot be manipulated as freely as a $(IMAGE_LINK), you need to prepare the pixels first and then upload them to the texture in a single operation (see Texture.update). * $(U Texture) makes it easy to convert from/to Image, but keep in mind that these calls require transfers between the graphics card and the central memory, therefore they are slow operations. * A texture can be loaded from an image, but also directly from a file/memory/stream. The necessary shortcuts are defined so that you don't need an image first for the most common cases. However, if you want to perform some modifications on the pixels before creating the final texture, you can load your file to a $(IMAGE_LINK), do whatever you need with the pixels, and then call Texture.loadFromImage. * Since they live in the graphics card memory, the pixels of a texture cannot be accessed without a slow copy first. And they cannot be accessed individually. Therefore, if you need to read the texture's pixels (like for pixel-perfect collisions), it is recommended to store the collision information separately, for example in an array of booleans. * Like $(IMAGE_LINK), $(U Texture) can handle a unique internal representation of pixels, which is RGBA 32 bits. This means that a pixel must be composed of 8 bits red, green, blue and alpha channels – just like a $(COLOR_LINK). *

Members

Classes

Texture
class Texture

Image living on the graphics card that can be used for drawing.

Examples

1 // This example shows the most common use of Texture:
2 // drawing a sprite
3  *
4 // Load a texture from a file
5 auto texture = new Texture();
6 if (!texture.loadFromFile("texture.png"))
7    return -1;
8  *
9 // Assign it to a sprite
10 auto sprite = new Sprite();
11 sprite.setTexture(texture);
12  *
13 // Draw the textured sprite
14 window.draw(sprite);

*

1 // This example shows another common use of Texture:
2 // streaming real-time data, like video frames
3  *
4 // Create an empty texture
5 auto texture = new Texture();
6 if (!texture.create(640, 480))
7    return -1;
8  *
9 // Create a sprite that will display the texture
10 auto sprite = new Sprite(texture);
11  *
12 while (...) // the main loop
13 {
14    ...
15  *
16    // update the texture
17  *
18    // get a fresh chunk of pixels (the next frame of a movie, for example)
19    ubyte[] pixels = ...;
20    texture.update(pixels);
21  *
22    // draw it
23    window.draw(sprite);
24  *
25    ...
26 }
27  *

* $(PARA Like $(SHADER_LINK) that can be used as a raw OpenGL shader, $(U Texture) can also be used directly as a raw texture for custom OpenGL geometry.)

Texture.bind(texture);
... render OpenGL geometry ...
Texture.bind(null);

*

See Also

$(SPRITE_LINK), $(IMAGE_LINK), $(RENDERTEXTURE_LINK)

Meta