Initial commit
This commit is contained in:
49
src/engine/animation.c
Normal file
49
src/engine/animation.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "engine/animation.h"
|
||||
|
||||
void animation_set(Animation *a, const AnimDef *def) {
|
||||
if (a->def == def) return; /* don't restart same anim */
|
||||
a->def = def;
|
||||
a->current_frame = 0;
|
||||
a->timer = 0.0f;
|
||||
a->finished = false;
|
||||
}
|
||||
|
||||
void animation_update(Animation *a, float dt) {
|
||||
if (!a->def || a->finished) return;
|
||||
|
||||
a->timer += dt;
|
||||
while (a->timer >= a->def->frames[a->current_frame].duration) {
|
||||
float dur = a->def->frames[a->current_frame].duration;
|
||||
if (dur <= 0.0f) break; /* avoid infinite loop on zero-duration frames */
|
||||
a->timer -= dur;
|
||||
a->current_frame++;
|
||||
|
||||
if (a->current_frame >= a->def->frame_count) {
|
||||
if (a->def->looping) {
|
||||
a->current_frame = 0;
|
||||
} else {
|
||||
a->current_frame = a->def->frame_count - 1;
|
||||
a->finished = true;
|
||||
a->timer = 0.0f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Rect animation_current_rect(const Animation *a) {
|
||||
if (!a->def || a->def->frame_count == 0) {
|
||||
return (SDL_Rect){0, 0, 0, 0};
|
||||
}
|
||||
return a->def->frames[a->current_frame].src;
|
||||
}
|
||||
|
||||
bool animation_is_last_frame(const Animation *a) {
|
||||
if (!a->def) return true;
|
||||
return a->current_frame >= a->def->frame_count - 1;
|
||||
}
|
||||
|
||||
SDL_Texture *animation_texture(const Animation *a) {
|
||||
if (!a->def) return NULL;
|
||||
return a->def->texture;
|
||||
}
|
||||
Reference in New Issue
Block a user