gdk-graphics 0a0aef92e917cfbac53dffbc32c295e4de914093
Loading...
Searching...
No Matches
sprite_animation.h
1// © Joseph Cameron - MIT License
2
3#ifndef GDK_GRAPHICS_EXT_SPRITE_ANIMATION_H
4#define GDK_GRAPHICS_EXT_SPRITE_ANIMATION_H
5
6#include <gdk/graphics/data_types.h>
7
8#include <cstddef>
9#include <span>
10#include <vector>
11
12namespace gdk::graphics {
14 class sprite_animation final {
15 public:
16 using normalized_texture_coordinate_type = float;
17 using time_type = float;
18 using texel_coordinate_type = int;
19
21 enum class play_mode {
24 };
25
27 struct frame {
28 texel_coordinate_type x;
29 texel_coordinate_type y;
30 texel_coordinate_type w;
31 texel_coordinate_type h;
32 time_type length;
33 };
34
37 normalized_texture_coordinate_type u;
38 normalized_texture_coordinate_type v;
39 normalized_texture_coordinate_type w;
40 normalized_texture_coordinate_type h;
41 };
42
49 [[nodiscard]] normalized_frame at(time_type aTime) const;
50
56 [[nodiscard]] bool has_finished(time_type aTime) const;
57
59 [[nodiscard]] time_type length() const;
60
62 [[nodiscard]] std::size_t frame_count() const;
63
74 sprite_animation(std::span<const frame> aFrameData,
75 texel_coordinate_type aTextureWidth,
76 texel_coordinate_type aTextureHeight,
77 play_mode aPlayMode = play_mode::loop);
78
79 private:
81 std::vector<double> m_Starts;
82
84 std::vector<normalized_frame> m_Frames;
85
86 double m_Length = 0;
87
88 play_mode m_PlayMode = play_mode::loop;
89 };
90}
91
92#endif
sprite_animation(std::span< const frame > aFrameData, texel_coordinate_type aTextureWidth, texel_coordinate_type aTextureHeight, play_mode aPlayMode=play_mode::loop)
builds an animation from frames cut out of a sprite sheet.
std::size_t frame_count() const
how many frames the animation has
bool has_finished(time_type aTime) const
whether a once animation has played out by the given time.
play_mode
what happens when the animation reaches its end
Definition sprite_animation.h:21
@ once
stop on the last frame. has_finished() then reports true.
Definition sprite_animation.h:23
@ loop
start again. The default, and what an idle or walk cycle wants.
Definition sprite_animation.h:22
time_type length() const
how long one cycle of the animation lasts, the total of its frames' lengths
normalized_frame at(time_type aTime) const
the frame showing at the given time, in normalized texture coordinates.
animation frame position and size in texels
Definition sprite_animation.h:27
animation frame position and size in normalized texture coordinate space
Definition sprite_animation.h:36