104 lines
2.6 KiB
C
104 lines
2.6 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <hexagram/anim.h>
|
|
|
|
void hexagram_anim_init(hexagram_anim *anim) {
|
|
anim->state = HEXAGRAM_ANIM_PLAYING;
|
|
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;
|
|
}
|
|
|
|
int hexagram_anim_step(hexagram_anim *anim) {
|
|
struct timeval tv;
|
|
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);
|
|
|
|
/* Determine the interval between initialisation and now */
|
|
interval = tv_seconds(&tv);
|
|
|
|
/* 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 (offset + duration >= interval) {
|
|
break;
|
|
}
|
|
|
|
offset += duration;
|
|
}
|
|
|
|
/* Determine progress through current stop */
|
|
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);
|
|
} else {
|
|
progress = (interval - offset) / anim->stops[i].duration;
|
|
}
|
|
|
|
for (a=0; a<anim->stops[i].count; a++) {
|
|
hexagram_anim_action *action = &anim->stops[i].actions[a];
|
|
|
|
if (action->flags & HEXAGRAM_ANIM_MOVE) {
|
|
double x = progress * (action->to.x - action->from.x) + action->from.x,
|
|
y = progress * (action->to.y - action->from.y) + action->from.y;
|
|
|
|
hexagram_gauge_move(action->gauge, x, y);
|
|
}
|
|
|
|
if (action->flags & HEXAGRAM_ANIM_ALPHA) {
|
|
double alpha = progress * (action->to.alpha - action->from.alpha) + action->from.alpha;
|
|
|
|
hexagram_gauge_set_alpha(action->gauge, alpha);
|
|
}
|
|
|
|
if (action->flags & HEXAGRAM_ANIM_RADIUS) {
|
|
double radius = progress * (action->to.radius - action->from.radius) + action->from.radius;
|
|
|
|
hexagram_dial_resize(action->dial, radius);
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
double hexagram_anim_duration(hexagram_anim *anim) {
|
|
double ret = 0.0;
|
|
size_t i;
|
|
|
|
for (i=0; i<anim->count; i++) {
|
|
ret += anim->stops[i].duration;
|
|
}
|
|
|
|
return ret;
|
|
}
|