raylibstarter 0.1.0
sprite_animated.cpp
Go to the documentation of this file.
1#include <raylib.h>
2
3#include <utility>
4
5#include "store.h"
6#include "config.h"
7
8#include "sprite_animated.h"
9
10game::core::SpriteAnimated::SpriteAnimated(std::shared_ptr<game::core::Texture2D> texture, float frame_width, float frame_height, int row, int steps, int speed)
11 : SpriteAnimated(std::move(texture), frame_width, frame_height, row, steps, speed, 0, 0, 0.0) {
12}
13
14game::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)
15 : SpriteAnimated(std::move(texture), frame_width, frame_height, row, steps, speed, pos_x, pos_y, 0.0) {
16}
17
18game::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)
19 : game::core::Sprite(std::move(texture), pos_x, pos_y, {frame_width/2, frame_height/2}, rotation, {0, static_cast<float>(row - 1) * frame_height, frame_width, frame_height}) {
20 this->states_.push_back({row, steps, speed, frame_width, frame_height});
21}
22
24 if(!visible && !update_if_invisible)
25 return;
26
27 if(this->states_[state_].steps <= 1)
28 return;
29
30 if (game::core::Store::ticks % this->states_[this->state_].speed == 0) {
31 this->current_step_++;
32
33 if (this->current_step_ >= this->states_[this->state_].steps) {
34 this->current_step_ = 0;
35 this->SwitchState();
36 }
37 else
38 this->frame_.x += this->states_[this->state_].frame_width;
39 }
40}
41
42void game::core::SpriteAnimated::AddState(int row, int steps, int speed, float frame_width, float frame_height) {
43 this->states_.push_back({row, steps, speed, frame_width, frame_height});
44}
45
47 this->state_ = this->next_state_;
48 this->frame_ = {0, static_cast<float>(this->states_[this->state_].row - 1) * this->states_[this->state_].frame_height, this->states_[this->state_].frame_width, this->states_[this->state_].frame_height};
49}
50
52 return this->state_;
53}
54
56 this->next_state_ = nextState;
57}
The SpriteAnimated class enables frame-by-frame animations by displaying only a certain section of a ...
void Update() override
Updates the animation state of the sprite.
void AddState(int row, int steps, int speed, float frame_width, float frame_height)
Adds a new animation state.
void nextState(int nextState)
Switch to another animation state by its id. The switch will happen after the last animation cycle is...
The Sprite class specifies position and degree of rotation on the screen for an associated VRAM textu...
Definition: sprite.h:21
Definition: actor.h:5
static long long int ticks
Global game counter. Can be used e.g. for the calculation of animation times.
Definition: store.h:23