pub struct Sprite<'s> { /* private fields */ }
Expand description

Drawable representation of a texture

Sprite is a drawable type that allows to easily display a Texture (or a part of it) on a render target.

Note: Currently, it is not feasible to store sprites long term. A common pattern with rust-sfml is to create a Sprite right before you start drawing, and draw all the sprites you want with it. You can change its properties using set_texture, set_position, etc., before drawing it, as many times as you need to.

Implementations

Create a new sprite

Create a new sprite with a texture

Create a new sprite with a texture and a source rectangle

Change the source texture of a 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 behaviour is undefined. If reset_rect is true, the texture_rect property of the sprite is automatically adjusted to the size of the new texture. If it is false, the texture rect is left unchanged.

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

Disable Texturing

Disable the current texture and reset the texture rect

Set the global color of a 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.

Arguments
  • color - New color of the sprite

Get the source texture of a sprite

If the sprite has no source texture, None is returned. You can’t modify the texture when you retrieve it with this function.

Return an Option to the sprite’s texture

Get the global color of a sprite

Return the global color of the sprite

Get the local bounding rectangle of a sprite

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.

Return the local bounding rectangle of the entity

Get the global bounding rectangle of a sprite

The returned rectangle is in global coordinates, which means that it takes in 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.

Return the global bounding rectangle of the entity

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

Return the texture rectangle of the sprite

Set the sub-rectangle of the texture that a 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.

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

Trait Implementations

Return a new Sprite or panic! if there is not enough memory

Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Draws into target with RenderStates states.
Executes the destructor for this type. Read more
Sets the position of the object. Read more
Set the orientation of the object. Read more
Sets the scale factors of the object. Read more
Sets the local origin of the object. Read more
Gets the position of the object.
Gets the rotation of the object. Read more
Gets the current scale of the object.
Gets the local origin of the object.
Moves the object by a given offset. Read more
Rotates the object. Read more
Scales the object. Read more
Gets the combined transform of the object.
Gets the inverse combined transform of the object.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.