raylibstarter 0.1.0
sprite_animated.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5#include <raylib.h>
6
7#include "texture2d.h"
8#include "sprite.h"
9
10namespace game::core {
11 /**
12 * The position within the spritesheet from which the individual frames are taken, as well as the number of frames
13 * in the animation, the size of an individual frame and the speed of the animation can be specified via the
14 * constructor parameters.
15 *
16 * The sprite animation class also allows you to change the animation (called animation state) by selecting a new
17 * section from the spritesheet texture using the AddState method. You can switch between the individual animation
18 * states using the nextState function.
19 *
20 * Same as Sprite objects also animated sprites can be drawn using the game::core::Renderer::DrawTexture function.
21 * However, the preferred way is to create Actor objects. These contain a sprite (or animated sprite) and are
22 * automatically drawn if they are referenced in the actors map of a Scene object.
23 *
24 * Note that the frame-animation is only automatically updated when the SpriteAnimated object is referenced in
25 * the actors-map of a scene.
26 *
27 * @see Sprite
28 *
29 * @brief The SpriteAnimated class enables frame-by-frame animations by displaying only a certain section of a spritesheet texture at a time.
30 */
32 public:
33 SpriteAnimated() = delete;
34
35 /**
36 * This constructor requires as parameters a Texture2D object, the height and width specification of an animation
37 * frame, an y-offset specified as a row number (the x-offset is always 0), the number of animation steps and
38 * the desired speed of the animation.
39 *
40 * In addition, the rotation origin is set to its center and rotation angle = 0. The default position is {0, 0}.
41 * @brief Constructor
42 * @param texture Shared pointer to a Texture2D object.
43 * @param frame_width Width of a single animation frame
44 * @param frame_height Height of a single animation frame
45 * @param row y-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.).
46 * @param steps Number of steps of the animation.
47 * @param speed Speed of the frame-by-frame animation
48 */
49 SpriteAnimated(std::shared_ptr<game::core::Texture2D> texture, float frame_width, float frame_height,
50 int row,
51 int steps,
52 int speed);
53
54 /**
55 * This constructor requires as parameters a Texture2D object, the height and width specification of an animation
56 * frame, an y-offset specified as a row number (the x-offset is always 0), the number of animation steps and
57 * the desired speed of the animation. Also the position on the screen can be specified.
58 *
59 * In addition, the rotation origin is set to its center and rotation angle = 0.
60 * @brief Constructor
61 * @param texture Shared pointer to a Texture2D object.
62 * @param frame_width Width of a single animation frame
63 * @param frame_height Height of a single animation frame
64 * @param row y-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.).
65 * @param steps Number of steps of the animation.
66 * @param speed Speed of the frame-by-frame animation
67 * @param pos_x The sprites x position.
68 * @param pos_y The sprites y position.
69 */
70 SpriteAnimated(std::shared_ptr<game::core::Texture2D> texture, float frame_width, float frame_height,
71 int row,
72 int steps,
73 int speed,
74 int pos_x,
75 int pos_y);
76
77 /**
78 * This constructor requires as parameters a Texture2D object, the height and width specification of an animation
79 * frame, an y-offset specified as a row number (the x-offset is always 0), the number of animation steps and
80 * the desired speed of the animation. Also the position on the screen and the sprite rotation angle can be specified.
81 *
82 * @brief Constructor
83 * @param texture Shared pointer to a Texture2D object.
84 * @param frame_width Width of a single animation frame
85 * @param frame_height Height of a single animation frame
86 * @param row y-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.).
87 * @param steps Number of steps of the animation.
88 * @param speed Speed of the frame-by-frame animation
89 * @param pos_x The sprites x position.
90 * @param pos_y The sprites y position.
91 * @param rotation The sprites rotation angle.
92 */
93 SpriteAnimated(std::shared_ptr<game::core::Texture2D> texture, float frame_width, float frame_height,
94 int row,
95 int steps,
96 int speed,
97 int pos_x,
98 int pos_y,
99 float rotation);
100
101 /**
102 * @return The sprites current animation state number.
103 */
104 [[nodiscard]] int state() const;
105
106 /**
107 * The method checks whether the next animation frame should be displayed based on the game time game::core::Store::ticks
108 * and the set animation speed. The prerequisite for this is that the sprite is visible, or for invisible sprites
109 * that update_if_invisible is set to true.
110 *
111 * @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.
112 *
113 * @brief Updates the animation state of the sprite.
114 */
115 void Update() override;
116
117 /**
118 * Creates a new animation state. To do this, a new section of the sprite texture must be selected via the row
119 * parameter (y-offset). Furthermore, the number of animation steps of the new animation state must be specified
120 * as well as the desired speed. Since the new animation may have a different frame size, this is also specified.
121 * Important: The y-offset refers to the new frame height set here.
122 *
123 * To switch to the new animation state call the nextState method.
124 *
125 * @brief Adds a new animation state
126 * @param row y-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.).
127 * @param steps Number of steps of the animation.
128 * @param speed Speed of the frame-by-frame animation.
129 * @param frame_width Width of a single animation frame.
130 * @param frame_height Height of a single animation frame.
131 */
132 void AddState(int row, int steps, int speed, float frame_width,float frame_height);
133
134 /**
135 * @brief Switch to another animation state by its id. The switch will happen after the last animation cycle is completed.
136 * @param nextState id of the animation state to switch to.
137 */
138 void nextState(int nextState);
139
140 /// Defines whether the animation should be updated also if the sprite is not visible. Default is false.
142
143 private:
144 /**
145 * @brief This structure contains all data necessary for the description of the frame-by-frame animation.
146 */
147 struct anim_state {
148 /// y-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.).
149 int row;
150 /// Number of steps of the animation.
151 int steps;
152 /// Speed of the frame-by-frame animation.
153 int speed;
154 /// Width of a single animation frame.
156 /// Height of a single animation frame.
158 };
159
160 /// Stores the id of the current state
161 int state_ = 0;
162
163 /// The id of the next state. The switch will happen after the current animation cycle is completed.
164 int next_state_ = 0;
165
166 /// Internal animation cycle step counter
168
169 /// Vector that holds the individual state structs.
170 std::vector<anim_state> states_;
171
172 /**
173 * The method is called internally by Update() and replaces the animation state with the one specified via this->next_state_.
174 * The call is made by Update() only when the current animation cycle has finished.
175 */
176 void SwitchState();
177 };
178}
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.
int state_
Stores the id of the current state.
std::vector< anim_state > states_
Vector that holds the individual state structs.
int current_step_
Internal animation cycle step counter.
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...
bool update_if_invisible
Defines whether the animation should be updated also if the sprite is not visible....
int next_state_
The id of the next state. The switch will happen after the current animation cycle is completed.
The Sprite class specifies position and degree of rotation on the screen for an associated VRAM textu...
Definition: sprite.h:21
const ::Texture2D & texture() const
Definition: sprite.cpp:63
float rotation
The sprites rotation angle. Default is 0.
Definition: sprite.h:40
int pos_x
The sprites x position.
Definition: sprite.h:43
int pos_y
The sprites y position.
Definition: sprite.h:46
This structure contains all data necessary for the description of the frame-by-frame animation.
float frame_height
Height of a single animation frame.
int row
y-offset measured in rows (depending on the frame height, i.e. row 1 has a y-offset of 0px,...
int steps
Number of steps of the animation.
float frame_width
Width of a single animation frame.
int speed
Speed of the frame-by-frame animation.