Drawable representation of a texture, with its own transformations, color, etc.
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: 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 class, which loads and provides the pixel data of a given texture.
The separation of Sprite and Texture allows more flexibility and better performances: indeed a Texture 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 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 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.
import sfml.window.VideoMode
val window: RenderWindow = ???
val texture = Texture()
texture.loadFromFile("texture.png")
val sprite = Sprite()
sprite.texture = texture
sprite.textureRect = Rect(10, 10, 50, 30)
sprite.color = Color(255, 255, 255, 200)
sprite.position = (100, 25)
window.draw(sprite)
Attributes
- See also
- Companion
- object
- Graph
-
- Supertypes
Members list
Value members
Concrete methods
Get the global color of the sprite.
Set the global color of the sprite.
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.
Value parameters
- color
-
New color of the sprite
Attributes
- See also
Draw the object to a render target.
Draw the object to a render target.
This is a function that has to be implemented by the derived class to define how the drawable should be drawn.
Value parameters
- states
-
Current render states
- target
-
Render target to draw to
Attributes
- Definition Classes
Get the global bounding rectangle of the entity.
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.
Attributes
- Returns
-
Global bounding rectangle of the entity
Get the local bounding rectangle of the entity.
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.
Attributes
- Returns
-
Local bounding rectangle of the entity
Get the sub-rectangle of the texture displayed by the sprite.
Get the sub-rectangle of the texture displayed by the sprite.
Attributes
- Returns
-
Texture rectangle of the sprite
- See also
Set the sub-rectangle of the texture that the sprite will display.
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.
Value parameters
- rectangle
-
Rectangle defining the region of the texture to display
Attributes
- See also
Change the source texture of the sprite.
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.
Value parameters
- resetRect
-
Should the texture rect be reset to the size of the new texture?
- texture
-
New texture
Attributes
- See also
Inherited methods
get the inverse of the combined transform of the object
get the inverse of the combined transform of the object
Attributes
- Returns
-
Inverse of the combined transformations applied to the object
- See also
- Inherited from:
- Transformable
Move the object by a given offset.
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:
import sfml.system.Vector2
val obj: Transformable = ???
val offset: Vector2[Float] = ???
obj.position = obj.position + offset
Value parameters
- offset
-
Offset
Attributes
- See also
- Inherited from:
- Transformable
Move the object by a given offset.
Move the object by a given offset.
This function adds to the current position of the object, unlike position_= which overwrites it. Thus, it is equivalent to the following code:
import sfml.system.Vector2
val obj: Transformable = ???
val offsetX, offsetY: Float = ???
val pos: Vector2[Float] = obj.position
obj.position = (pos.x + offsetX, pos.y + offsetY)
Value parameters
- offsetX
-
X offset
- offsetY
-
Y offset
Attributes
- See also
- Inherited from:
- Transformable
get the local origin of the object
get the local origin of the object
Attributes
- Returns
-
Current origin
- See also
- Inherited from:
- Transformable
set the local origin of the object
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).
Value parameters
- origin
-
New origin
Attributes
- See also
- Inherited from:
- Transformable
set the local origin of the object
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).
Value parameters
- x
-
X coordinate of the new origin
- y
-
Y coordinate of the new origin
Attributes
- See also
- Inherited from:
- Transformable
get the position of the object
get the position of the object
Attributes
- Returns
-
Current position
- See also
- Inherited from:
- Transformable
set the position of the object
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).
Value parameters
- position
-
New position
Attributes
- See also
- Inherited from:
- Transformable
set the position of the object
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).
Value parameters
- x
-
X coordinate of the new position
- y
-
Y coordinate of the new position
Attributes
- See also
- Inherited from:
- Transformable
Rotate the object.
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:
val obj: Transformable = ???
val angle: Float = ???
obj.rotation = obj.rotation + angle
Value parameters
- angle
-
Angle of rotation, in degrees
Attributes
- Inherited from:
- Transformable
get the orientation of the object
get the orientation of the object
The rotation is always in the range [0, 360].
Attributes
- Returns
-
Current rotation, in degrees
- See also
- Inherited from:
- Transformable
set the orientation of the object
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.
Value parameters
- angle
-
New rotation, in degrees
Attributes
- See also
- Inherited from:
- Transformable
get the current scale of the object
get the current scale of the object
Attributes
- Returns
-
Current scale factors
- See also
- Inherited from:
- Transformable
Scale the object.
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:
import sfml.system.Vector2
val obj: Transformable = ???
val factor: Vector2[Float] = ???
val scale: Vector2[Float] = obj.scale
obj.scale = (scale.x * factor.x, scale.y * factor.y)
Value parameters
- factor
-
Scale factors
Attributes
- See also
- Inherited from:
- Transformable
Scale the object.
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:
import sfml.system.Vector2
val obj: Transformable = ???
val factorX, factorY: Float = ???
val scale: Vector2[Float] = obj.scale
obj.scale = (scale.x + factorX, scale.y + factorY)
Value parameters
- factorX
-
Horizontal scale factor
- factorY
-
Vertical scale factor
Attributes
- See also
- Inherited from:
- Transformable
set the scale factors of the object
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).
Value parameters
- factors
-
New scale factors
Attributes
- See also
- Inherited from:
- Transformable
set the scale factors of the object
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).
Value parameters
- factorX
-
New horizontal scale factor
- factorY
-
New vertical scale factor
Attributes
- See also
- Inherited from:
- Transformable
get the combined transform of the object
get the combined transform of the object
Attributes
- Returns
-
Transform combining the position/rotation/scale/origin of the object
- See also
- Inherited from:
- Transformable