ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
sprite_sheet.cpp
Go to the documentation of this file.
1
8
10
11#include <algorithm>
12#include <cmath>
13
14#include "asw/modules/draw.h"
15#include "asw/modules/util.h"
16
17namespace asw {
18
19SpriteSheet::SpriteSheet(const Texture& texture, const Vec2<float>& frame_size, int frame_count)
20 : texture(texture)
21 , frame_size(frame_size)
22{
23 if (texture == nullptr || frame_size.x <= 0.0F || frame_size.y <= 0.0F) {
24 return;
25 }
26
27 const auto texture_size = util::get_texture_size(texture);
28 columns = std::max(1, static_cast<int>(std::floor(texture_size.x / frame_size.x)));
29 const int rows = std::max(1, static_cast<int>(std::floor(texture_size.y / frame_size.y)));
30 const int fits = columns * rows;
31
32 this->frame_count = frame_count > 0 ? std::min(frame_count, fits) : fits;
33}
34
36{
37 return texture;
38}
39
44
46{
47 return frame_count;
48}
49
51{
52 if (frame_count <= 0) {
53 return { 0.0F, 0.0F, frame_size.x, frame_size.y };
54 }
55
56 // Wrap, negative indexes count back from the end
57 const int frame = ((index % frame_count) + frame_count) % frame_count;
58 const auto column = static_cast<float>(frame % columns);
59 const auto row = static_cast<float>(frame / columns);
60
61 return { column * frame_size.x, row * frame_size.y, frame_size.x, frame_size.y };
62}
63
64void SpriteSheet::draw_frame(int index, const Quad<float>& dest) const
65{
66 if (texture == nullptr) {
67 return;
68 }
69
71}
72
73Animation::Animation(int frame_count, float frame_duration, bool loop)
74 : frame_count(std::max(1, frame_count))
75 , frame_duration(std::max(0.0001F, frame_duration))
76 , loop(loop)
77{
78}
79
80void Animation::update(float dt)
81{
82 elapsed += dt;
83
84 // Keep time small so float precision holds up over long runs, and so the
85 // frame index never overflows
86 const float length = frame_duration * static_cast<float>(frame_count);
87 if (elapsed >= length) {
88 elapsed = loop ? std::fmod(elapsed, length) : length;
89 }
90}
91
93{
94 elapsed = 0.0F;
95}
96
97void Animation::set_frame_duration(float frame_duration)
98{
99 this->frame_duration = std::max(0.0001F, frame_duration);
100}
101
103{
104 const int frame = static_cast<int>(elapsed / frame_duration);
105
106 if (loop) {
107 return frame % frame_count;
108 }
109
110 return std::min(frame, frame_count - 1);
111}
112
114{
115 return !loop && elapsed >= frame_duration * static_cast<float>(frame_count);
116}
117
118} // namespace asw
bool is_finished() const
Check if a non looping animation has reached its last frame.
void update(float dt)
Advance the animation.
int get_frame() const
Get the frame to draw.
void set_frame_duration(float frame_duration)
Set seconds each frame is shown.
Animation()=default
Create an animation with a single frame.
void reset()
Go back to the first frame.
A 2D rectangle in space.
Definition geometry.h:504
int get_frame_count() const
Get the number of frames.
Quad< float > get_frame(int index) const
Get the area of the texture a frame covers.
SpriteSheet()=default
Create an empty sprite sheet.
Vec2< float > frame_size
Vec2< float > get_frame_size() const
Get the size of one frame.
const Texture & get_texture() const
Get the texture.
void draw_frame(int index, const Quad< float > &dest) const
Draw a frame stretched to an area of the screen.
A 2D vector in space.
Definition geometry.h:27
T y
The y component of the vector.
Definition geometry.h:280
T x
The x component of the vector.
Definition geometry.h:277
Routines for drawing sprites and primitives to the screen.
void stretch_sprite_blit(const asw::Texture &tex, const asw::Quad< float > &source, const asw::Quad< float > &dest)
Draw a sprite with the option to stretch a portion of it.
Definition draw.cpp:300
asw::Vec2< float > get_texture_size(const asw::Texture &tex)
Get texture size.
Definition util.cpp:74
Definition camera.cpp:16
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:76
Sprite sheets and frame animation.
General utility functions.