dsfml.graphics.sprite

$(U Sprite) is a drawable class that allows to easily display a texture (or a part of it) on a render target.

It inherits all the functions from $(TRANSFORMABLE_LINK): position, rotation, scale, origin. It also adds sprite-specific properties such as the texture to use, the part of it to display, and some convenience functions to change the overall color of the sprite, or to get its bounding rectangle.

$(U Sprite) works in combination with the $(TEXTURE_LINK) class, which loads and provides the pixel data of a given texture.

The separation of $(U Sprite) and $(TEXTURE_LINK) allows more flexibility and better performances: indeed a $(TEXTURE_LINK) is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a $(U Sprite) is a lightweight object which can use the pixel data of a $(TEXTURE_LINK) and draw it with its own transformation/color/blending attributes.

It is important to note that the $(U Sprite) instance doesn't copy the texture that it uses, it only keeps a reference to it. Thus, a $(TEXTURE_LINK) must not be destroyed while it is used by a $(U Sprite) (i.e. never write a function that uses a local Texture instance for creating a sprite).

See also the note on coordinates and undistorted rendering in $(TRANSFORMABLE_LINK).

Members

Classes

Sprite
class Sprite

Drawable representation of a texture, with its own transformations, color, etc.

Examples

1 // Declare and load a texture
2 auto texture = new Texture();
3 texture.loadFromFile("texture.png");
4 
5 // Create a sprite
6 auto sprite = new Sprite();
7 sprite.setTexture(texture);
8 sprite.textureRect = IntRect(10, 10, 50, 30);
9 sprite.color = Color(255, 255, 255, 200);
10 sprite.position = Vector2f(100, 25);
11 
12 // Draw it
13 window.draw(sprite);

See Also

$(TEXTURE_LINK), $(TRANSFORMABLE_LINK)

Meta