Keep track of animation state

This commit is contained in:
XANTRONIX Development 2024-01-21 21:18:09 -05:00
parent 0d6bcc7db2
commit 68b9ade03e
2 changed files with 12 additions and 0 deletions

View file

@ -13,6 +13,11 @@ typedef enum {
HEXAGRAM_ANIM_RADIUS = 1 << 2 HEXAGRAM_ANIM_RADIUS = 1 << 2
} hexagram_anim_flags; } hexagram_anim_flags;
typedef enum {
HEXAGRAM_ANIM_STOPPED,
HEXAGRAM_ANIM_PLAYING
} hexagram_anim_state;
typedef struct _hexagram_anim_stop hexagram_anim_stop; typedef struct _hexagram_anim_stop hexagram_anim_stop;
typedef double (hexagram_anim_progress_fn)(hexagram_anim_stop *, double); typedef double (hexagram_anim_progress_fn)(hexagram_anim_stop *, double);
@ -45,6 +50,7 @@ struct _hexagram_anim_stop {
typedef struct _hexagram_anim { typedef struct _hexagram_anim {
hexagram_anim_stop *stops; hexagram_anim_stop *stops;
size_t count; size_t count;
hexagram_anim_state state;
struct timeval start, now; struct timeval start, now;
} hexagram_anim; } hexagram_anim;

View file

@ -4,6 +4,7 @@
#include <hexagram/anim.h> #include <hexagram/anim.h>
void hexagram_anim_init(hexagram_anim *anim) { void hexagram_anim_init(hexagram_anim *anim) {
anim->state = HEXAGRAM_ANIM_PLAYING;
gettimeofday(&anim->start, NULL); gettimeofday(&anim->start, NULL);
} }
@ -27,6 +28,10 @@ int hexagram_anim_step(hexagram_anim *anim) {
double offset = 0.0, interval, progress; double offset = 0.0, interval, progress;
size_t i, a; size_t i, a;
if (anim->state == HEXAGRAM_ANIM_STOPPED) {
return 0;
}
gettimeofday(&anim->now, NULL); gettimeofday(&anim->now, NULL);
timersub(&anim->now, &anim->start, &tv); timersub(&anim->now, &anim->start, &tv);
@ -53,6 +58,7 @@ int hexagram_anim_step(hexagram_anim *anim) {
if (i == anim->count) { if (i == anim->count) {
i--; i--;
progress = 1.0; progress = 1.0;
anim->state = HEXAGRAM_ANIM_STOPPED;
} else if (anim->stops[i].fn) { } else if (anim->stops[i].fn) {
progress = anim->stops[i].fn(&anim->stops[i], progress = anim->stops[i].fn(&anim->stops[i],
(interval - offset) / anim->stops[i].duration); (interval - offset) / anim->stops[i].duration);