prosperon/source/engine/anim.c

61 lines
1.2 KiB
C
Raw Normal View History

2022-12-27 17:54:39 -06:00
#include "anim.h"
#include "log.h"
2023-05-12 13:22:05 -05:00
#include "stb_ds.h"
2022-12-27 17:54:39 -06:00
struct anim make_anim() {
2023-05-12 13:22:05 -05:00
struct anim a = {0};
a.interp = 1;
2022-12-27 17:54:39 -06:00
2023-05-12 13:22:05 -05:00
return a;
2022-12-27 17:54:39 -06:00
}
2022-12-28 16:50:54 -06:00
void free_anim(struct anim a) {
2023-05-12 13:22:05 -05:00
arrfree(a.frames);
2022-12-28 16:50:54 -06:00
}
2022-12-27 17:54:39 -06:00
struct anim anim_add_keyframe(struct anim a, struct keyframe key) {
2023-05-12 13:22:05 -05:00
arrput(a.frames, key);
2022-12-27 17:54:39 -06:00
2023-05-12 13:22:05 -05:00
return a;
2022-12-27 17:54:39 -06:00
}
double interval(struct keyframe a, struct keyframe b, double t) {
2023-05-12 13:22:05 -05:00
return (t - a.time) / (b.time - a.time);
2022-12-27 17:54:39 -06:00
}
double near_val(struct anim anim, double t) {
2023-05-12 13:22:05 -05:00
for (int i = 0; i < arrlen(anim.frames) - 1; i++) {
2022-12-27 17:54:39 -06:00
2023-05-12 13:22:05 -05:00
if (t > anim.frames[i + 1].time)
continue;
2022-12-27 17:54:39 -06:00
2023-05-12 13:22:05 -05:00
return (interval(anim.frames[i], anim.frames[i + 1], t) >= 0.5f ? anim.frames[i + 1].val : anim.frames[i].val);
}
2022-12-28 16:50:54 -06:00
2023-05-12 13:22:05 -05:00
return arrlast(anim.frames).val;
2022-12-27 17:54:39 -06:00
}
double lerp_val(struct anim anim, double t) {
2023-05-12 13:22:05 -05:00
for (int i = 0; i < arrlen(anim.frames) - 1; i++) {
if (t > anim.frames[i + 1].time)
continue;
2022-12-27 17:54:39 -06:00
2023-05-12 13:22:05 -05:00
double intv = interval(anim.frames[i], anim.frames[i + 1], t);
return ((1 - intv) * anim.frames[i].val) + (intv * anim.frames[i + 1].val);
}
2022-12-27 17:54:39 -06:00
2023-05-12 13:22:05 -05:00
return arrlast(anim.frames).val;
2022-12-27 17:54:39 -06:00
}
double cubic_val(struct anim anim, double t) {
return 0.0f;
2022-12-27 17:54:39 -06:00
}
double anim_val(struct anim anim, double t) {
2023-05-12 13:22:05 -05:00
if (anim.interp == 0)
return near_val(anim, t);
2022-12-27 17:54:39 -06:00
2023-05-12 13:22:05 -05:00
return lerp_val(anim, t);
}