raylibstarter 0.1.0
game::core::SpriteAnimated Class Reference

The SpriteAnimated class enables frame-by-frame animations by displaying only a certain section of a spritesheet texture at a time. More...

#include <sprite_animated.h>

Inheritance diagram for game::core::SpriteAnimated:
Collaboration diagram for game::core::SpriteAnimated:

Classes

struct  anim_state
 This structure contains all data necessary for the description of the frame-by-frame animation. More...
 

Public Member Functions

 SpriteAnimated ()=delete
 
 SpriteAnimated (std::shared_ptr< game::core::Texture2D > texture, float frame_width, float frame_height, int row, int steps, int speed)
 Constructor. More...
 
 SpriteAnimated (std::shared_ptr< game::core::Texture2D > texture, float frame_width, float frame_height, int row, int steps, int speed, int pos_x, int pos_y)
 Constructor. More...
 
 SpriteAnimated (std::shared_ptr< game::core::Texture2D > texture, float frame_width, float frame_height, int row, int steps, int speed, int pos_x, int pos_y, float rotation)
 Constructor. More...
 
int state () const
 
void Update () override
 Updates the animation state of the sprite. More...
 
void AddState (int row, int steps, int speed, float frame_width, float frame_height)
 Adds a new animation state. More...
 
void nextState (int nextState)
 Switch to another animation state by its id. The switch will happen after the last animation cycle is completed. More...
 
- Public Member Functions inherited from game::core::Sprite
 Sprite ()=delete
 
 Sprite (std::shared_ptr< game::core::Texture2D > texture)
 Constructor. More...
 
 Sprite (std::shared_ptr< game::core::Texture2D > texture, int pos_x, int pos_y)
 Constructor. More...
 
 Sprite (std::shared_ptr< game::core::Texture2D > texture, int pos_x, int pos_y, float rotation)
 Constructor. More...
 
 Sprite (std::shared_ptr< game::core::Texture2D > texture, int pos_x, int pos_y, Vector2 rotation_origin, float rotation, Rectangle frame)
 Constructor. More...
 
virtual ~Sprite ()
 
void texture (const std::shared_ptr< game::core::Texture2D > &texture)
 Replaces the Texture2D object. More...
 
const std::shared_ptr< game::core::Texture2D > & texture_object () const
 
const ::Texture2D & texture () const
 
Vector2 position () const
 
const Rectangle & frame () const
 
void frame (const Rectangle &frame)
 Sets the section of the associated VRAM texture to be displayed. More...
 
virtual void Update ()
 

Public Attributes

bool update_if_invisible = false
 Defines whether the animation should be updated also if the sprite is not visible. Default is false. More...
 
- Public Attributes inherited from game::core::Sprite
bool visible = true
 Visibility of the Sprite. More...
 
Color tint = WHITE
 The Raylib tint applied to the VRAM texture when drawing. Defulat ist Raylib WHITE. More...
 
Vector2 rotation_origin
 The sprites rotation origin, centered by default. More...
 
float rotation = 0.0f
 The sprites rotation angle. Default is 0. More...
 
int pos_x = 0
 The sprites x position. More...
 
int pos_y = 0
 The sprites y position. More...
 

Private Member Functions

void SwitchState ()
 

Private Attributes

int state_ = 0
 Stores the id of the current state. More...
 
int next_state_ = 0
 The id of the next state. The switch will happen after the current animation cycle is completed. More...
 
int current_step_ = 0
 Internal animation cycle step counter. More...
 
std::vector< anim_statestates_
 Vector that holds the individual state structs. More...
 

Additional Inherited Members

- Protected Attributes inherited from game::core::Sprite
std::shared_ptr< game::core::Texture2Dtexture_
 The sprites Texture2D. More...
 
Rectangle frame_
 The section of the associated VRAM texture to be displayed as Raylib Rectangle structure. More...
 

Detailed Description

The SpriteAnimated class enables frame-by-frame animations by displaying only a certain section of a spritesheet texture at a time.

The position within the spritesheet from which the individual frames are taken, as well as the number of frames in the animation, the size of an individual frame and the speed of the animation can be specified via the constructor parameters.

The sprite animation class also allows you to change the animation (called animation state) by selecting a new section from the spritesheet texture using the AddState method. You can switch between the individual animation states using the nextState function.

Same as Sprite objects also animated sprites can be drawn using the game::core::Renderer::DrawTexture function. However, the preferred way is to create Actor objects. These contain a sprite (or animated sprite) and are automatically drawn if they are referenced in the actors map of a Scene object.

Note that the frame-animation is only automatically updated when the SpriteAnimated object is referenced in the actors-map of a scene.

See also
Sprite

Definition at line 31 of file sprite_animated.h.

Constructor & Destructor Documentation

◆ SpriteAnimated() [1/4]

game::core::SpriteAnimated::SpriteAnimated ( )
delete

◆ SpriteAnimated() [2/4]

game::core::SpriteAnimated::SpriteAnimated ( std::shared_ptr< game::core::Texture2D texture,
float  frame_width,
float  frame_height,
int  row,
int  steps,
int  speed 
)

Constructor.

This constructor requires as parameters a Texture2D object, the height and width specification of an animation frame, an y-offset specified as a row number (the x-offset is always 0), the number of animation steps and the desired speed of the animation.

In addition, the rotation origin is set to its center and rotation angle = 0. The default position is {0, 0}.

Parameters
textureShared pointer to a Texture2D object.
frame_widthWidth of a single animation frame
frame_heightHeight of a single animation frame
rowy-offset measured in rows (depending on the frame height, i.e. row 1 has a y-offset of 0px, row 2 has a y-offset of the height of an animation frame, etc.).
stepsNumber of steps of the animation.
speedSpeed of the frame-by-frame animation

Definition at line 10 of file sprite_animated.cpp.

◆ SpriteAnimated() [3/4]

game::core::SpriteAnimated::SpriteAnimated ( std::shared_ptr< game::core::Texture2D texture,
float  frame_width,
float  frame_height,
int  row,
int  steps,
int  speed,
int  pos_x,
int  pos_y 
)

Constructor.

This constructor requires as parameters a Texture2D object, the height and width specification of an animation frame, an y-offset specified as a row number (the x-offset is always 0), the number of animation steps and the desired speed of the animation. Also the position on the screen can be specified.

In addition, the rotation origin is set to its center and rotation angle = 0.

Parameters
textureShared pointer to a Texture2D object.
frame_widthWidth of a single animation frame
frame_heightHeight of a single animation frame
rowy-offset measured in rows (depending on the frame height, i.e. row 1 has a y-offset of 0px, row 2 has a y-offset of the height of an animation frame, etc.).
stepsNumber of steps of the animation.
speedSpeed of the frame-by-frame animation
pos_xThe sprites x position.
pos_yThe sprites y position.

Definition at line 14 of file sprite_animated.cpp.

◆ SpriteAnimated() [4/4]

game::core::SpriteAnimated::SpriteAnimated ( std::shared_ptr< game::core::Texture2D texture,
float  frame_width,
float  frame_height,
int  row,
int  steps,
int  speed,
int  pos_x,
int  pos_y,
float  rotation 
)

Constructor.

This constructor requires as parameters a Texture2D object, the height and width specification of an animation frame, an y-offset specified as a row number (the x-offset is always 0), the number of animation steps and the desired speed of the animation. Also the position on the screen and the sprite rotation angle can be specified.

Parameters
textureShared pointer to a Texture2D object.
frame_widthWidth of a single animation frame
frame_heightHeight of a single animation frame
rowy-offset measured in rows (depending on the frame height, i.e. row 1 has a y-offset of 0px, row 2 has a y-offset of the height of an animation frame, etc.).
stepsNumber of steps of the animation.
speedSpeed of the frame-by-frame animation
pos_xThe sprites x position.
pos_yThe sprites y position.
rotationThe sprites rotation angle.

Definition at line 18 of file sprite_animated.cpp.

Member Function Documentation

◆ AddState()

void game::core::SpriteAnimated::AddState ( int  row,
int  steps,
int  speed,
float  frame_width,
float  frame_height 
)

Adds a new animation state.

Creates a new animation state. To do this, a new section of the sprite texture must be selected via the row parameter (y-offset). Furthermore, the number of animation steps of the new animation state must be specified as well as the desired speed. Since the new animation may have a different frame size, this is also specified. Important: The y-offset refers to the new frame height set here.

To switch to the new animation state call the nextState method.

Parameters
rowy-offset measured in rows (depending on the frame height, i.e. row 1 has a y-offset of 0px, row 2 has a y-offset of the height of an animation frame, etc.).
stepsNumber of steps of the animation.
speedSpeed of the frame-by-frame animation.
frame_widthWidth of a single animation frame.
frame_heightHeight of a single animation frame.

Definition at line 42 of file sprite_animated.cpp.

◆ nextState()

void game::core::SpriteAnimated::nextState ( int  nextState)

Switch to another animation state by its id. The switch will happen after the last animation cycle is completed.

Parameters
nextStateid of the animation state to switch to.

Definition at line 55 of file sprite_animated.cpp.

◆ state()

int game::core::SpriteAnimated::state ( ) const
Returns
The sprites current animation state number.

Definition at line 51 of file sprite_animated.cpp.

◆ SwitchState()

void game::core::SpriteAnimated::SwitchState ( )
private

The method is called internally by Update() and replaces the animation state with the one specified via this->next_state_. The call is made by Update() only when the current animation cycle has finished.

Definition at line 46 of file sprite_animated.cpp.

◆ Update()

void game::core::SpriteAnimated::Update ( )
overridevirtual

Updates the animation state of the sprite.

The method checks whether the next animation frame should be displayed based on the game time game::core::Store::ticks and the set animation speed. The prerequisite for this is that the sprite is visible, or for invisible sprites that update_if_invisible is set to true.

Note
If the animated sprite is used over an actor object and is in the actors map of a scene, this method is called automatically.

Reimplemented from game::core::Sprite.

Definition at line 23 of file sprite_animated.cpp.

Member Data Documentation

◆ current_step_

int game::core::SpriteAnimated::current_step_ = 0
private

Internal animation cycle step counter.

Definition at line 167 of file sprite_animated.h.

◆ next_state_

int game::core::SpriteAnimated::next_state_ = 0
private

The id of the next state. The switch will happen after the current animation cycle is completed.

Definition at line 164 of file sprite_animated.h.

◆ state_

int game::core::SpriteAnimated::state_ = 0
private

Stores the id of the current state.

Definition at line 161 of file sprite_animated.h.

◆ states_

std::vector<anim_state> game::core::SpriteAnimated::states_
private

Vector that holds the individual state structs.

Definition at line 170 of file sprite_animated.h.

◆ update_if_invisible

bool game::core::SpriteAnimated::update_if_invisible = false

Defines whether the animation should be updated also if the sprite is not visible. Default is false.

Definition at line 141 of file sprite_animated.h.


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