SFML 3.0.1
Loading...
Searching...
No Matches
Public Member Functions | List of all members
sf::Sprite Class Reference
Graphics module

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

`#include <SFML/Graphics/Sprite.hpp>`

Inheritance diagram for sf::Sprite:

## Public Member Functions
| Sprite (const Texture &texture)
| Construct the sprite from a source texture.
| Sprite (const Texture &&texture)=delete
| Disallow construction from a temporary texture.
| Sprite (const Texture &texture, const IntRect &rectangle)
| Construct the sprite from a sub-rectangle of a source texture.
| Sprite (const Texture &&texture, const IntRect &rectangle)=delete
| Disallow construction from a temporary texture.
void | setTexture (const Texture &texture, bool resetRect=false)
| Change the source texture of the sprite.
void | setTexture (const Texture &&texture, bool resetRect=false)=delete
| Disallow setting from a temporary texture.
void | setTextureRect (const IntRect &rectangle)
| Set the sub-rectangle of the texture that the sprite will display.
void | setColor (Color color)
| Set the global color of the sprite.
const Texture & | getTexture () const
| Get the source texture of the sprite.
const IntRect & | getTextureRect () const
| Get the sub-rectangle of the texture displayed by the sprite.
Color | getColor () const
| Get the global color of the sprite.
FloatRect | getLocalBounds () const
| Get the local bounding rectangle of the entity.
FloatRect | getGlobalBounds () const
| Get the global bounding rectangle of the entity.
void | setPosition (Vector2f position)
| set the position of the object
void | setRotation (Angle angle)
| set the orientation of the object
void | setScale (Vector2f factors)
| set the scale factors of the object
void | setOrigin (Vector2f origin)
| set the local origin of the object
Vector2f | getPosition () const
| get the position of the object
Angle | getRotation () const
| get the orientation of the object
Vector2f | getScale () const
| get the current scale of the object
Vector2f | getOrigin () const
| get the local origin of the object
void | move (Vector2f offset)
| Move the object by a given offset.
void | rotate (Angle angle)
| Rotate the object.
void | scale (Vector2f factor)
| Scale the object.
const Transform & | getTransform () const
| get the combined transform of the object
const Transform & | getInverseTransform () const
| get the inverse of the combined transform of the object

## Detailed Description

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

sf::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 sf::Transformable: 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.

sf::Sprite works in combination with the sf::Texture class, which loads and provides the pixel data of a given texture.

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

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

See also the note on coordinates and undistorted rendering in sf::Transformable.

Usage example:

// Load a texture
const sf::Texture texture("texture.png");
// Create a sprite
sf::Sprite sprite(texture);
sprite.setTextureRect({{10, 10}, {50, 30}});
sprite.setColor({255, 255, 255, 200});
sprite.setPosition({100.f, 25.f});
// Draw it
window.draw(sprite);
sf::Sprite
Drawable representation of a texture, with its own transformations, color, etc.
Definition Sprite.hpp:51
sf::Texture
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56

See also
  sf::Texture, sf::Transformable

Definition at line 50 of file Sprite.hpp.

## Constructor & Destructor Documentation

## ◆ Sprite() [1/4]

sf::Sprite::Sprite | ( | const Texture & | texture | ) |
| explicit

Construct the sprite from a source texture.

Parameters

  texture | Source texture

See also
  setTexture

## ◆ Sprite() [2/4]

sf::Sprite::Sprite | ( | const Texture && | texture | ) |
| explicit delete

Disallow construction from a temporary texture.

## ◆ Sprite() [3/4]

sf::Sprite::Sprite | ( | const Texture & | texture,
|  | const IntRect & | rectangle )

Construct the sprite from a sub-rectangle of a source texture.

Parameters

  texture | Source texture
  rectangle | Sub-rectangle of the texture to assign to the sprite

See also
  setTexture, setTextureRect

## ◆ Sprite() [4/4]

sf::Sprite::Sprite | ( | const Texture && | texture,
|  | const IntRect & | rectangle )
| delete

Disallow construction from a temporary texture.

## Member Function Documentation

## ◆ getColor()

Color sf::Sprite::getColor | ( |  | ) | const
| nodiscard

Get the global color of the sprite.

Returns
  Global color of the sprite

See also
  setColor

## ◆ getGlobalBounds()

FloatRect sf::Sprite::getGlobalBounds | ( |  | ) | const
| nodiscard

Get the global bounding rectangle of the entity.

The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the sprite in the global 2D world's coordinate system.

Returns
  Global bounding rectangle of the entity

## ◆ getInverseTransform()

const Transform & sf::Transformable::getInverseTransform | ( |  | ) | const
| nodiscard inherited

get the inverse of the combined transform of the object

Returns
  Inverse of the combined transformations applied to the object

See also
  getTransform

## ◆ getLocalBounds()

FloatRect sf::Sprite::getLocalBounds | ( |  | ) | const
| nodiscard

Get the local bounding rectangle of the entity.

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.

Returns
  Local bounding rectangle of the entity

## ◆ getOrigin()

Vector2f sf::Transformable::getOrigin | ( |  | ) | const
| nodiscard inherited

get the local origin of the object

Returns
  Current origin

See also
  setOrigin

## ◆ getPosition()

Vector2f sf::Transformable::getPosition | ( |  | ) | const
| nodiscard inherited

get the position of the object

Returns
  Current position

See also
  setPosition

## ◆ getRotation()

Angle sf::Transformable::getRotation | ( |  | ) | const
| nodiscard inherited

get the orientation of the object

The rotation is always in the range [0, 360].

Returns
  Current rotation

See also
  setRotation

## ◆ getScale()

Vector2f sf::Transformable::getScale | ( |  | ) | const
| nodiscard inherited

get the current scale of the object

Returns
  Current scale factors

See also
  setScale

## ◆ getTexture()

const Texture & sf::Sprite::getTexture | ( |  | ) | const
| nodiscard

Get the source texture of the sprite.

The returned reference is const, which means that you can't modify the texture when you retrieve it with this function.

Returns
  Reference to the sprite's texture

See also
  setTexture

## ◆ getTextureRect()

const IntRect & sf::Sprite::getTextureRect | ( |  | ) | const
| nodiscard

Get the sub-rectangle of the texture displayed by the sprite.

Returns
  Texture rectangle of the sprite

See also
  setTextureRect

## ◆ getTransform()

const Transform & sf::Transformable::getTransform | ( |  | ) | const
| nodiscard inherited

get the combined transform of the object

Returns
  Transform combining the position/rotation/scale/origin of the object

See also
  getInverseTransform

## ◆ move()

void sf::Transformable::move | ( | Vector2f | offset | ) |
| inherited

Move the object by a given offset.

This function adds to the current position of the object, unlike setPosition which overwrites it. Thus, it is equivalent to the following code:

object.setPosition(object.getPosition() + offset);
sf::Transformable::getPosition
Vector2f getPosition() const
get the position of the object

Parameters

  offset | Offset

See also
  setPosition

## ◆ rotate()

void sf::Transformable::rotate | ( | Angle | angle | ) |
| inherited

Rotate the object.

This function adds to the current rotation of the object, unlike setRotation which overwrites it. Thus, it is equivalent to the following code:

object.setRotation(object.getRotation() + angle);
sf::Transformable::getRotation
Angle getRotation() const
get the orientation of the object

Parameters

  angle | Angle of rotation

## ◆ scale()

void sf::Transformable::scale | ( | Vector2f | factor | ) |
| inherited

Scale the object.

This function multiplies the current scale of the object, unlike setScale which overwrites it. Thus, it is equivalent to the following code:

sf::Vector2f scale = object.getScale();
object.setScale(scale.x * factor.x, scale.y * factor.y);
sf::Transformable::scale
void scale(Vector2f factor)
Scale the object.
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:210

Parameters

  factor | Scale factors

See also
  setScale

## ◆ setColor()

void sf::Sprite::setColor | ( | Color | color | ) |

Set the global color of the sprite.

This color is modulated (multiplied) with the sprite's texture. It can be used to colorize the sprite, or change its global opacity. By default, the sprite's color is opaque white.

Parameters

  color | New color of the sprite

See also
  getColor

## ◆ setOrigin()

void sf::Transformable::setOrigin | ( | Vector2f | origin | ) |
| inherited

set the local origin of the object

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a transformable object is (0, 0).

Parameters

  origin | New origin

See also
  getOrigin

## ◆ setPosition()

void sf::Transformable::setPosition | ( | Vector2f | position | ) |
| inherited

set the position of the object

This function completely overwrites the previous position. See the move function to apply an offset based on the previous position instead. The default position of a transformable object is (0, 0).

Parameters

  position | New position

See also
  move, getPosition

## ◆ setRotation()

void sf::Transformable::setRotation | ( | Angle | angle | ) |
| inherited

set the orientation of the object

This function completely overwrites the previous rotation. See the rotate function to add an angle based on the previous rotation instead. The default rotation of a transformable object is 0.

Parameters

  angle | New rotation

See also
  rotate, getRotation

## ◆ setScale()

void sf::Transformable::setScale | ( | Vector2f | factors | ) |
| inherited

set the scale factors of the object

This function completely overwrites the previous scale. See the scale function to add a factor based on the previous scale instead. The default scale of a transformable object is (1, 1).

Parameters

  factors | New scale factors

See also
  scale, getScale

## ◆ setTexture() [1/2]

void sf::Sprite::setTexture | ( | const Texture && | texture,
|  | bool | resetRect = false )
| delete

Disallow setting from a temporary texture.

## ◆ setTexture() [2/2]

void sf::Sprite::setTexture | ( | const Texture & | texture,
|  | bool | resetRect = false )

Change the source texture of the sprite.

The texture argument refers to a texture that must exist as long as the sprite uses it. Indeed, the sprite doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the sprite tries to use it, the behavior is undefined. If resetRect is true, the TextureRect property of the sprite is automatically adjusted to the size of the new texture. If it is false, the texture rect is left unchanged.

Parameters

  texture | New texture
  resetRect | Should the texture rect be reset to the size of the new texture?

See also
  getTexture, setTextureRect

## ◆ setTextureRect()

void sf::Sprite::setTextureRect | ( | const IntRect & | rectangle | ) |

Set the sub-rectangle of the texture that the sprite will display.

The texture rect is useful when you don't want to display the whole texture, but rather a part of it. By default, the texture rect covers the entire texture.

Parameters

  rectangle | Rectangle defining the region of the texture to display

See also
  getTextureRect, setTexture

The documentation for this class was generated from the following file:

- Sprite.hpp