hexagram/src/anim.c

68 lines
1.5 KiB
C
Raw Normal View History

2024-01-19 16:33:54 -05:00
#include <stdlib.h>
#include <string.h>
2024-01-19 16:33:54 -05:00
#include <hexagram/anim.h>
void hexagram_anim_init(hexagram_anim *anim) {
anim->current = 0;
gettimeofday(&anim->start, NULL);
}
static inline double tv_seconds(struct timeval *tv) {
return (double)tv->tv_sec + (double)tv->tv_usec / 1000000.0;
}
static inline double stop_offset(hexagram_anim *anim, size_t index) {
double offset = 0.0;
size_t i;
for (i=0; i<index; i++) {
offset += anim->stops[i].duration;
}
return offset;
}
static inline size_t stop_index(hexagram_anim *anim, double offset) {
return 0;
}
2024-01-19 16:33:54 -05:00
int hexagram_anim_step(hexagram_anim *anim, cairo_t *bg, cairo_t *fg) {
struct timeval tv;
2024-01-19 20:47:07 -05:00
double offset = 0.0, interval, progress;
size_t i;
gettimeofday(&anim->now, NULL);
timersub(&anim->now, &anim->start, &tv);
2024-01-19 20:47:07 -05:00
/* Determine the interval between initialisation and now */
interval = tv_seconds(&tv);
2024-01-19 20:47:07 -05:00
/* Determine current animation stop index and time offset */
for (i=0; i<anim->count; i++) {
double duration = anim->stops[i].duration;
if (duration <= 0) {
continue;
}
if (interval < offset + duration) {
break;
}
offset += duration;
}
if (i == anim->count) {
/* We are apparently at the end. */
return 0;
}
/* Determine current progress through current stop */
progress = (interval - offset) / anim->stops[i].duration;
2024-01-19 16:33:54 -05:00
return 1;
2024-01-19 16:33:54 -05:00
}