From 68b9ade03e574a27a240aec0b24519bab6fa7993 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 21 Jan 2024 21:18:09 -0500 Subject: [PATCH] Keep track of animation state --- include/hexagram/anim.h | 6 ++++++ src/anim.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/include/hexagram/anim.h b/include/hexagram/anim.h index 910b010..e5c735c 100644 --- a/include/hexagram/anim.h +++ b/include/hexagram/anim.h @@ -13,6 +13,11 @@ typedef enum { HEXAGRAM_ANIM_RADIUS = 1 << 2 } hexagram_anim_flags; +typedef enum { + HEXAGRAM_ANIM_STOPPED, + HEXAGRAM_ANIM_PLAYING +} hexagram_anim_state; + typedef struct _hexagram_anim_stop hexagram_anim_stop; typedef double (hexagram_anim_progress_fn)(hexagram_anim_stop *, double); @@ -45,6 +50,7 @@ struct _hexagram_anim_stop { typedef struct _hexagram_anim { hexagram_anim_stop *stops; size_t count; + hexagram_anim_state state; struct timeval start, now; } hexagram_anim; diff --git a/src/anim.c b/src/anim.c index 978cbb7..9250b8a 100644 --- a/src/anim.c +++ b/src/anim.c @@ -4,6 +4,7 @@ #include void hexagram_anim_init(hexagram_anim *anim) { + anim->state = HEXAGRAM_ANIM_PLAYING; gettimeofday(&anim->start, NULL); } @@ -27,6 +28,10 @@ int hexagram_anim_step(hexagram_anim *anim) { double offset = 0.0, interval, progress; size_t i, a; + if (anim->state == HEXAGRAM_ANIM_STOPPED) { + return 0; + } + gettimeofday(&anim->now, NULL); timersub(&anim->now, &anim->start, &tv); @@ -53,6 +58,7 @@ int hexagram_anim_step(hexagram_anim *anim) { if (i == anim->count) { i--; progress = 1.0; + anim->state = HEXAGRAM_ANIM_STOPPED; } else if (anim->stops[i].fn) { progress = anim->stops[i].fn(&anim->stops[i], (interval - offset) / anim->stops[i].duration);