![]() |
Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
|
A basic sprite. More...
#include <gf/BasicSprite.h>
Public Member Functions | |
BasicSprite () | |
Default constructor. More... | |
BasicSprite (const Texture &texture) | |
Construct the sprite from a source texture. More... | |
BasicSprite (const Texture &texture, const RectF &textureRect) | |
Construct the sprite from a sub-rectangle of a source texture. More... | |
void | setTexture (const Texture &texture, bool resetRect) |
Change the source texture of the sprite. More... | |
void | setTexture (const Texture &texture, const RectF &textureRect=RectF::fromSize({ 1.0f, 1.0f })) |
Change the source texture of the sprite. More... | |
const Texture & | getTexture () const |
Get the source texture of the sprite. More... | |
bool | hasTexture () const |
Check if a texture is set. More... | |
void | unsetTexture () |
Unset the source texture of the sprite. More... | |
void | setTextureRect (const RectF &rect) |
Set the sub-rectangle of the texture that the sprite will display. More... | |
const RectF & | getTextureRect () const |
Get the sub-rectangle of the texture displayed by the sprite. More... | |
RectF | getLocalBounds () const |
Get the local bounding rectangle of the entity. More... | |
void | updateGeometry (Span< Vertex > vertices) |
gf::BasicSprite::BasicSprite | ( | ) |
Default constructor.
Creates an empty sprite with no source texture.
gf::BasicSprite::BasicSprite | ( | const Texture & | texture | ) |
Construct the sprite from a sub-rectangle of a source texture.
texture | Source texture |
textureRect | Sub-rectangle of the texture to assign to the sprite |
RectF gf::BasicSprite::getLocalBounds | ( | ) | const |
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.
|
inline |
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.
|
inline |
Get the sub-rectangle of the texture displayed by the sprite.
|
inline |
Check if a texture is set.
void gf::BasicSprite::setTexture | ( | const Texture & | texture, |
bool | resetRect | ||
) |
Change the source texture of the sprite.
The texture 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 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.
texture | New texture |
resetRect | Should the texture rect be reset to the size of the new texture? |
void gf::BasicSprite::setTexture | ( | const Texture & | texture, |
const RectF & | textureRect = RectF::fromSize({ 1.0f, 1.0f }) |
||
) |
Change the source texture of the sprite.
The texture 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.
By default, the sub-rectangle will be reset to the full size of the new texture.
texture | New texture |
textureRect | Sub-rectangle of the new texture to assign to the sprite |
void gf::BasicSprite::setTextureRect | ( | const RectF & | rect | ) |
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.
The rectangle is given in texture coordinates, meaning that \((0,0)\) is the top left corner and \((1,1)\) is the bottom right corner.
rect | Rectangle defining the region of the texture to display |
void gf::BasicSprite::unsetTexture | ( | ) |
Unset the source texture of the sprite.
After a call to this function, the sprite has no source texture.