Skip to content

class SF::Sprite
inherits SF::Transformable #

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:

# Declare and load a texture
texture = SF::Texture.from_file("texture.png")

# Create a sprite
sprite = SF::Sprite.new
sprite.texture = texture
sprite.texture_rect = SF.int_rect(10, 10, 50, 30)
sprite.color = SF.color(255, 255, 255, 200)
sprite.position = {100, 25}

# Draw it
window.draw sprite

See also: SF::Texture, SF::Transformable

Included modules

SF::Drawable

Constructors#

.new(texture : Texture, rectangle : IntRect)#

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

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

See also: texture=, texture_rect=

View source

.new(texture : Texture)#

Construct the sprite from a source texture

  • texture - Source texture

See also: texture=

View source

.new#

Default constructor

Creates an empty sprite with no source texture.

View source

Methods#

#color : Color#

Get the global color of the sprite

Returns: Global color of the sprite

See also: color=

View source

#color=(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.

  • color - New color of the sprite

See also: color

View source

#dup : Sprite#

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.

View source

#finalize#

Virtual destructor

View source

#global_bounds : FloatRect#

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

View source

#local_bounds : FloatRect#

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

View source

#set_texture(texture : Texture, reset_rect : Bool = 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 reset_rect 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.

  • texture - New texture
  • reset_rect - Should the texture rect be reset to the size of the new texture?

See also: texture, texture_rect=

View source

#texture=(texture : Texture)#

Shorthand for #set_texture

View source

#texture_rect : IntRect#

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

Returns: Texture rectangle of the sprite

See also: texture_rect=

View source

#texture_rect=(rectangle : IntRect)#

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.

  • rectangle - Rectangle defining the region of the texture to display

See also: texture_rect, texture=

View source