Public Member Functions | Protected Member Functions
sf::Sprite Class Reference

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

#include <Sprite.hpp>

Inheritance diagram for sf::Sprite:
sf::Drawable

List of all members.

Public Member Functions

 Sprite ()
 Default constructor.
 Sprite (const Texture &texture)
 Construct the sprite from a source texture.
void SetTexture (const Texture &texture, bool adjustToNewSize=false)
 Change the source texture of the sprite.
void SetSubRect (const IntRect &rectangle)
 Set the part of the texture that the sprite will display.
void Resize (float width, float height)
 Change the size of the sprite.
void Resize (const Vector2f &size)
 Change the size of the sprite.
void FlipX (bool flipped)
 Flip the sprite horizontally.
void FlipY (bool flipped)
 Flip the sprite vertically.
const TextureGetTexture () const
 Get the source texture of the sprite.
const IntRectGetSubRect () const
 Get the region of the texture displayed by the sprite.
Vector2f GetSize () const
 Get the global size of the sprite.
void SetPosition (float x, float y)
 Set the position of the object.
void SetPosition (const Vector2f &position)
 Set the position of the object.
void SetX (float x)
 Set the X position of the object.
void SetY (float y)
 Set the Y position of the object.
void SetScale (float factorX, float factorY)
 Set the scale factors of the object.
void SetScale (const Vector2f &factors)
 Set the scale factors of the object.
void SetScaleX (float factor)
 Set the X scale factor of the object.
void SetScaleY (float factor)
 Set the Y scale factor of the object.
void SetOrigin (float x, float y)
 Set the local origin of the object.
void SetOrigin (const Vector2f &origin)
 Set the local origin of the object.
void SetRotation (float angle)
 Set the orientation of the object.
void SetColor (const Color &color)
 Set the global color of the object.
void SetBlendMode (Blend::Mode mode)
 Set the blending mode of the object.
const Vector2fGetPosition () const
 Get the position of the object.
const Vector2fGetScale () const
 Get the current scale of the object.
const Vector2fGetOrigin () const
 Get the local origin of the object.
float GetRotation () const
 Get the orientation of the object.
const ColorGetColor () const
 Get the color of the object.
Blend::Mode GetBlendMode () const
 Get the blend mode of the object.
void Move (float offsetX, float offsetY)
 Move the object by a given offset.
void Move (const Vector2f &offset)
 Move the object by a given offset.
void Scale (float factorX, float factorY)
 Scale the object.
void Scale (const Vector2f &factor)
 Scale the object.
void Rotate (float angle)
 Rotate the object.
Vector2f TransformToLocal (const Vector2f &point) const
 Transform a point in object local coordinates.
Vector2f TransformToGlobal (const Vector2f &point) const
 Transform a local point in global coordinates.

Protected Member Functions

virtual void Render (RenderTarget &target, Renderer &renderer) const
 Draw the object to a render target.
const Matrix3GetMatrix () const
 Get the transform matrix of the object.
const Matrix3GetInverseMatrix () const
 Get the inverse transform matrix of the object.

Detailed Description

Drawable representation of a texture, with its own transformations, color, blend mode, 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::Drawable: position, rotation, scale, origin, global color and blend mode. It also adds sprite-specific properties such as the texture to use, the part of it to display, and some convenience functions to flip or resize the sprite.

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 destructed 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).

Usage example:

 // Declare and load a texture
 sf::Texture texture;
 texture.LoadFromFile("texture.png");
 
 // Create a sprite
 sf::Sprite sprite;
 sprite.SetTexture(texture);
 sprite.SetSubRect(sf::IntRect(10, 10, 50, 30));
 sprite.Resize(100, 60);

 // Display it
 window.Draw(sprite); // window is a sf::RenderWindow
See also:
sf::Texture

Definition at line 45 of file Sprite.hpp.


Constructor & Destructor Documentation

sf::Sprite::Sprite ( )

Default constructor.

Creates an empty sprite with no source texture.

sf::Sprite::Sprite ( const Texture texture) [explicit]

Construct the sprite from a source texture.

See also:
SetTexture

Member Function Documentation

void sf::Sprite::FlipX ( bool  flipped)

Flip the sprite horizontally.

Parameters:
flippedTrue to flip the sprite
See also:
FlipY
void sf::Sprite::FlipY ( bool  flipped)

Flip the sprite vertically.

Parameters:
flippedTrue to flip the sprite
See also:
FlipX
Blend::Mode sf::Drawable::GetBlendMode ( ) const [inherited]

Get the blend mode of the object.

Returns:
Current blend mode
See also:
SetBlendMode
const Color& sf::Drawable::GetColor ( ) const [inherited]

Get the color of the object.

Returns:
Current color
See also:
SetColor
const Matrix3& sf::Drawable::GetInverseMatrix ( ) const [protected, inherited]

Get the inverse transform matrix of the object.

Returns:
Inverse transform matrix
See also:
GetMatrix
const Matrix3& sf::Drawable::GetMatrix ( ) const [protected, inherited]

Get the transform matrix of the object.

Returns:
Transform matrix
See also:
GetInverseMatrix
const Vector2f& sf::Drawable::GetOrigin ( ) const [inherited]

Get the local origin of the object.

Returns:
Current origin
See also:
SetOrigin
const Vector2f& sf::Drawable::GetPosition ( ) const [inherited]

Get the position of the object.

Returns:
Current position
See also:
SetPosition
float sf::Drawable::GetRotation ( ) const [inherited]

Get the orientation of the object.

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

Returns:
Current rotation, in degrees
See also:
SetRotation
const Vector2f& sf::Drawable::GetScale ( ) const [inherited]

Get the current scale of the object.

Returns:
Current scale factors
See also:
SetScale
Vector2f sf::Sprite::GetSize ( ) const

Get the global size of the sprite.

This function is a shortcut that multiplies the size of the subrect by the scale factors.

Returns:
Size of the sprite
See also:
Resize
const IntRect& sf::Sprite::GetSubRect ( ) const

Get the region of the texture displayed by the sprite.

Returns:
Rectangle defining the region of the texture
See also:
SetSubRect
const Texture* sf::Sprite::GetTexture ( ) const

Get the source texture of the sprite.

If the sprite has no source texture, or if the texture doesn't exist anymore, a NULL pointer is returned. The returned pointer is const, which means that you can't modify the texture when you retrieve it with this function.

Returns:
Pointer to the sprite's texture
See also:
SetTexture
void sf::Drawable::Move ( float  offsetX,
float  offsetY 
) [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:

 sf::Vector2f pos = object.GetPosition();
 object.SetPosition(pos.x + offsetX, pos.y + offsetY);
Parameters:
offsetXX offset
offsetYY offset
See also:
SetPosition
void sf::Drawable::Move ( const 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);
Parameters:
offsetOffset
See also:
SetPosition
virtual void sf::Sprite::Render ( RenderTarget target,
Renderer renderer 
) const [protected, virtual]

Draw the object to a render target.

Parameters:
targetRender target
rendererRenderer providing low-level rendering commands

Implements sf::Drawable.

void sf::Sprite::Resize ( float  width,
float  height 
)

Change the size of the sprite.

This function is just a shortcut that calls SetScale with the proper values, calculated from the size of the current subrect. If width or height is not strictly positive, this functions does nothing.

Parameters:
widthNew width of the sprite
heightNew height of the sprite
See also:
GetSize
void sf::Sprite::Resize ( const Vector2f size)

Change the size of the sprite.

This function is just a shortcut that calls SetScale with the proper values, calculated from the size of the current subrect. If size.x or size.y is not strictly positive, this functions does nothing.

Parameters:
sizeNew size of the sprite
See also:
GetSize
void sf::Drawable::Rotate ( float  angle) [inherited]

Rotate the object.

This function ads 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);
Parameters:
angleAngle of rotation, in degrees
void sf::Drawable::Scale ( float  factorX,
float  factorY 
) [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 * factorX, scale.y * factorY);
Parameters:
factorXHorizontal scale factor
factorYVertical scale factor
See also:
SetScale
void sf::Drawable::Scale ( const 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);
Parameters:
factorScale factors
See also:
SetScale
void sf::Drawable::SetBlendMode ( Blend::Mode  mode) [inherited]

Set the blending mode of the object.

This property defines how the pixels of an object are blended with the pixels of the render target to which it is drawn. To know more about the blending modes available, see the sf::Blend::Mode enum. The default blend mode is Blend::Alpha.

Parameters:
modeNew blending mode
See also:
GetBlendMode
void sf::Drawable::SetColor ( const Color color) [inherited]

Set the global color of the object.

This global color affects the entire object, and modulates (multiplies) its original pixels. The default color is white.

Parameters:
colorNew color
See also:
GetColor
void sf::Drawable::SetOrigin ( float  x,
float  y 
) [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 drawable object is (0, 0).

Parameters:
xX coordinate of the new origin
yY coordinate of the new origin
See also:
GetOrigin
void sf::Drawable::SetOrigin ( const 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 drawable object is (0, 0).

Parameters:
originNew origin
See also:
GetOrigin
void sf::Drawable::SetPosition ( float  x,
float  y 
) [inherited]

Set the position of the object.

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

Parameters:
xX coordinate of the new position
yY coordinate of the new position
See also:
Move, SetX, SetY, GetPosition
void sf::Drawable::SetPosition ( const Vector2f position) [inherited]

Set the position of the object.

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

Parameters:
positionNew position
See also:
Move, SetX, SetY, GetPosition
void sf::Drawable::SetRotation ( float  angle) [inherited]

Set the orientation of the object.

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

Parameters:
angleNew rotation, in degrees
See also:
Rotate, GetRotation
void sf::Drawable::SetScale ( float  factorX,
float  factorY 
) [inherited]

Set the scale factors of the object.

factorX and factorY must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).

Parameters:
factorXNew horizontal scale factor
factorYNew vertical scale factor
See also:
Scale, SetScaleX, SetScaleY, GetScale
void sf::Drawable::SetScale ( const Vector2f factors) [inherited]

Set the scale factors of the object.

scale.x and scale.y must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).

Parameters:
factorsNew scale factors
See also:
Scale, SetScaleX, SetScaleY, GetScale
void sf::Drawable::SetScaleX ( float  factor) [inherited]

Set the X scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factorNew horizontal scale factor
See also:
SetScaleY, SetScale, GetScale
void sf::Drawable::SetScaleY ( float  factor) [inherited]

Set the Y scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factorNew vertical scale factor
See also:
SetScaleX, SetScale, GetScale
void sf::Sprite::SetSubRect ( const IntRect rectangle)

Set the part of the texture that the sprite will display.

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

Parameters:
rectangleRectangle defining the region of the texture to display
See also:
GetSubRect, SetTexture
void sf::Sprite::SetTexture ( const Texture texture,
bool  adjustToNewSize = 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, it may appear as a white rectangle. If adjustToNewSize is true, the SubRect property of the sprite is adjusted to the size of the new texture. If it is false, the SubRect is unchanged.

Parameters:
textureNew texture
adjustToNewSizeShould the sub-rect be adjusted to the size of the new texture?
See also:
GetTexture, SetSubRect
void sf::Drawable::SetX ( float  x) [inherited]

Set the X position of the object.

Parameters:
xNew X coordinate
See also:
SetY, SetPosition, GetPosition
void sf::Drawable::SetY ( float  y) [inherited]

Set the Y position of the object.

Parameters:
yNew Y coordinate
See also:
SetX, SetPosition, GetPosition
Vector2f sf::Drawable::TransformToGlobal ( const Vector2f point) const [inherited]

Transform a local point in global coordinates.

This function takes a point in local coordinates, and transforms it in global coordinates. In other words, it applies the same transformations that are applied to the object (origin, translation, rotation and scale).

Parameters:
pointPoint to transform
Returns:
The transformed point
See also:
TransformToLocal
Vector2f sf::Drawable::TransformToLocal ( const Vector2f point) const [inherited]

Transform a point in object local coordinates.

This function takes a point in global coordinates, and transforms it in coordinates local to the object. In other words, it applies the inverse of all the transformations applied to the object (origin, translation, rotation and scale).

Parameters:
pointPoint to transform
Returns:
The transformed point
See also:
TransformToGlobal

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