dungeon_crawler

annotate prototype/anim/track.c @ 80:a373b36ffc17

better
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Oct 2012 01:59:39 +0300
parents
children
rev   line source
nuclear@67 1 #include <stdlib.h>
nuclear@67 2 #include <string.h>
nuclear@67 3 #include <assert.h>
nuclear@67 4 #include "track.h"
nuclear@67 5 #include "dynarr.h"
nuclear@67 6
nuclear@67 7 static int keycmp(const void *a, const void *b);
nuclear@67 8 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm);
nuclear@67 9
nuclear@67 10 static float interp_step(float v0, float v1, float v2, float v3, float t);
nuclear@67 11 static float interp_linear(float v0, float v1, float v2, float v3, float t);
nuclear@67 12 static float interp_cubic(float v0, float v1, float v2, float v3, float t);
nuclear@67 13
nuclear@67 14 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end);
nuclear@67 15 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end);
nuclear@67 16 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end);
nuclear@67 17
nuclear@67 18 /* XXX keep this in sync with enum anm_interpolator at track.h */
nuclear@67 19 static float (*interp[])(float, float, float, float, float) = {
nuclear@67 20 interp_step,
nuclear@67 21 interp_linear,
nuclear@67 22 interp_cubic,
nuclear@67 23 0
nuclear@67 24 };
nuclear@67 25
nuclear@67 26 /* XXX keep this in sync with enum anm_extrapolator at track.h */
nuclear@67 27 static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = {
nuclear@67 28 remap_extend,
nuclear@67 29 remap_clamp,
nuclear@67 30 remap_repeat,
nuclear@67 31 0
nuclear@67 32 };
nuclear@67 33
nuclear@67 34 int anm_init_track(struct anm_track *track)
nuclear@67 35 {
nuclear@67 36 memset(track, 0, sizeof *track);
nuclear@67 37
nuclear@67 38 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
nuclear@67 39 return -1;
nuclear@67 40 }
nuclear@67 41 track->interp = ANM_INTERP_LINEAR;
nuclear@67 42 track->extrap = ANM_EXTRAP_CLAMP;
nuclear@67 43 return 0;
nuclear@67 44 }
nuclear@67 45
nuclear@67 46 void anm_destroy_track(struct anm_track *track)
nuclear@67 47 {
nuclear@67 48 dynarr_free(track->keys);
nuclear@67 49 }
nuclear@67 50
nuclear@67 51 struct anm_track *anm_create_track(void)
nuclear@67 52 {
nuclear@67 53 struct anm_track *track;
nuclear@67 54
nuclear@67 55 if((track = malloc(sizeof *track))) {
nuclear@67 56 if(anm_init_track(track) == -1) {
nuclear@67 57 free(track);
nuclear@67 58 return 0;
nuclear@67 59 }
nuclear@67 60 }
nuclear@67 61 return track;
nuclear@67 62 }
nuclear@67 63
nuclear@67 64 void anm_free_track(struct anm_track *track)
nuclear@67 65 {
nuclear@67 66 anm_destroy_track(track);
nuclear@67 67 free(track);
nuclear@67 68 }
nuclear@67 69
nuclear@67 70 int anm_set_track_name(struct anm_track *track, const char *name)
nuclear@67 71 {
nuclear@67 72 char *tmp;
nuclear@67 73
nuclear@67 74 if(!(tmp = malloc(strlen(name) + 1))) {
nuclear@67 75 return -1;
nuclear@67 76 }
nuclear@67 77 free(track->name);
nuclear@67 78 track->name = tmp;
nuclear@67 79 return 0;
nuclear@67 80 }
nuclear@67 81
nuclear@67 82 const char *anm_get_track_name(struct anm_track *track)
nuclear@67 83 {
nuclear@67 84 return track->name;
nuclear@67 85 }
nuclear@67 86
nuclear@67 87 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
nuclear@67 88 {
nuclear@67 89 track->interp = in;
nuclear@67 90 }
nuclear@67 91
nuclear@67 92 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
nuclear@67 93 {
nuclear@67 94 track->extrap = ex;
nuclear@67 95 }
nuclear@67 96
nuclear@67 97 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@67 98 {
nuclear@67 99 return remap_time[track->extrap](tm, start, end);
nuclear@67 100 }
nuclear@67 101
nuclear@67 102 void anm_set_track_default(struct anm_track *track, float def)
nuclear@67 103 {
nuclear@67 104 track->def_val = def;
nuclear@67 105 }
nuclear@67 106
nuclear@67 107 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
nuclear@67 108 {
nuclear@67 109 int idx = anm_get_key_interval(track, key->time);
nuclear@67 110
nuclear@67 111 /* if we got a valid keyframe index, compare them... */
nuclear@67 112 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
nuclear@67 113 /* ... it's the same key, just update the value */
nuclear@67 114 track->keys[idx].val = key->val;
nuclear@67 115 } else {
nuclear@67 116 /* ... it's a new key, add it and re-sort them */
nuclear@67 117 void *tmp;
nuclear@67 118 if(!(tmp = dynarr_push(track->keys, key))) {
nuclear@67 119 return -1;
nuclear@67 120 }
nuclear@67 121 track->keys = tmp;
nuclear@67 122 /* TODO lazy qsort */
nuclear@67 123 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
nuclear@67 124 }
nuclear@67 125 return 0;
nuclear@67 126 }
nuclear@67 127
nuclear@67 128 static int keycmp(const void *a, const void *b)
nuclear@67 129 {
nuclear@67 130 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
nuclear@67 131 }
nuclear@67 132
nuclear@67 133 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
nuclear@67 134 {
nuclear@67 135 if(idx < 0 || idx >= track->count) {
nuclear@67 136 return 0;
nuclear@67 137 }
nuclear@67 138 return track->keys + idx;
nuclear@67 139 }
nuclear@67 140
nuclear@67 141 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
nuclear@67 142 {
nuclear@67 143 int last;
nuclear@67 144
nuclear@67 145 if(!track->count || tm < track->keys[0].time) {
nuclear@67 146 return -1;
nuclear@67 147 }
nuclear@67 148
nuclear@67 149 last = track->count - 1;
nuclear@67 150 if(tm > track->keys[last].time) {
nuclear@67 151 return last;
nuclear@67 152 }
nuclear@67 153
nuclear@67 154 return find_prev_key(track->keys, 0, last, tm);
nuclear@67 155 }
nuclear@67 156
nuclear@67 157 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
nuclear@67 158 {
nuclear@67 159 int mid;
nuclear@67 160
nuclear@67 161 if(end - start <= 1) {
nuclear@67 162 return start;
nuclear@67 163 }
nuclear@67 164
nuclear@67 165 mid = (start + end) / 2;
nuclear@67 166 if(tm < arr[mid].time) {
nuclear@67 167 return find_prev_key(arr, start, mid, tm);
nuclear@67 168 }
nuclear@67 169 if(tm > arr[mid].time) {
nuclear@67 170 return find_prev_key(arr, mid, end, tm);
nuclear@67 171 }
nuclear@67 172 return mid;
nuclear@67 173 }
nuclear@67 174
nuclear@67 175 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
nuclear@67 176 {
nuclear@67 177 struct anm_keyframe key;
nuclear@67 178 key.time = tm;
nuclear@67 179 key.val = val;
nuclear@67 180
nuclear@67 181 return anm_set_keyframe(track, &key);
nuclear@67 182 }
nuclear@67 183
nuclear@67 184 float anm_get_value(struct anm_track *track, anm_time_t tm)
nuclear@67 185 {
nuclear@67 186 int idx0, idx1, last_idx;
nuclear@67 187 anm_time_t tstart, tend;
nuclear@67 188 float t, dt;
nuclear@67 189 float v0, v1, v2, v3;
nuclear@67 190
nuclear@67 191 if(!track->count) {
nuclear@67 192 return track->def_val;
nuclear@67 193 }
nuclear@67 194
nuclear@67 195 last_idx = track->count - 1;
nuclear@67 196
nuclear@67 197 tstart = track->keys[0].time;
nuclear@67 198 tend = track->keys[last_idx].time;
nuclear@67 199
nuclear@67 200 if(tstart == tend) {
nuclear@67 201 return track->keys[0].val;
nuclear@67 202 }
nuclear@67 203
nuclear@67 204 tm = remap_time[track->extrap](tm, tstart, tend);
nuclear@67 205
nuclear@67 206 idx0 = anm_get_key_interval(track, tm);
nuclear@67 207 assert(idx0 >= 0 && idx0 < track->count);
nuclear@67 208 idx1 = idx0 + 1;
nuclear@67 209
nuclear@67 210 if(idx0 == last_idx) {
nuclear@67 211 return track->keys[idx0].val;
nuclear@67 212 }
nuclear@67 213
nuclear@67 214 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
nuclear@67 215 t = (float)(tm - track->keys[idx0].time) / dt;
nuclear@67 216
nuclear@67 217 v1 = track->keys[idx0].val;
nuclear@67 218 v2 = track->keys[idx1].val;
nuclear@67 219
nuclear@67 220 /* get the neigboring values to allow for cubic interpolation */
nuclear@67 221 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
nuclear@67 222 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
nuclear@67 223
nuclear@67 224 return interp[track->interp](v0, v1, v2, v3, t);
nuclear@67 225 }
nuclear@67 226
nuclear@67 227
nuclear@67 228 static float interp_step(float v0, float v1, float v2, float v3, float t)
nuclear@67 229 {
nuclear@67 230 return v1;
nuclear@67 231 }
nuclear@67 232
nuclear@67 233 static float interp_linear(float v0, float v1, float v2, float v3, float t)
nuclear@67 234 {
nuclear@67 235 return v1 + (v2 - v1) * t;
nuclear@67 236 }
nuclear@67 237
nuclear@67 238 static float interp_cubic(float a, float b, float c, float d, float t)
nuclear@67 239 {
nuclear@67 240 float x, y, z, w;
nuclear@67 241 float tsq = t * t;
nuclear@67 242
nuclear@67 243 x = -a + 3.0 * b - 3.0 * c + d;
nuclear@67 244 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
nuclear@67 245 z = c - a;
nuclear@67 246 w = 2.0 * b;
nuclear@67 247
nuclear@67 248 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
nuclear@67 249 }
nuclear@67 250
nuclear@67 251 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@67 252 {
nuclear@67 253 return remap_repeat(tm, start, end);
nuclear@67 254 }
nuclear@67 255
nuclear@67 256 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@67 257 {
nuclear@67 258 return tm < start ? start : (tm >= end ? end - 1 : tm);
nuclear@67 259 }
nuclear@67 260
nuclear@67 261 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@67 262 {
nuclear@67 263 anm_time_t interv = end - start;
nuclear@67 264
nuclear@67 265 if(tm < start) {
nuclear@67 266 while(tm < start) {
nuclear@67 267 tm += interv;
nuclear@67 268 }
nuclear@67 269 return tm;
nuclear@67 270 }
nuclear@67 271 return (tm - start) % interv + start;
nuclear@67 272 }