Drawable representation of a texture, with its own transformations, color, etc.
// Declare and load a texture auto texture = new Texture(); texture.loadFromFile("texture.png"); // Create a sprite auto sprite = new Sprite(); sprite.setTexture(texture); sprite.textureRect = IntRect(10, 10, 50, 30); sprite.color = Color(255, 255, 255, 200); sprite.position = Vector2f(100, 25); // Draw it window.draw(sprite);
$(TEXTURE_LINK), $(TRANSFORMABLE_LINK)
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.
Sprite works in combination with the $(TEXTURE_LINK) class, which loads and provides the pixel data of a given texture.
The separation of 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 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 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 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).