-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrail.c
55 lines (47 loc) · 1.06 KB
/
trail.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <assert.h>
#include "util.h"
#include "trail.h"
enum {
KEYFRAMECHUNK_LEN = 32,
};
struct KeyframeChunk {
KeyframeChunk *next, *prev;
unsigned len;
Keyframe frames[];
};
static void trail_next(Trail *self) {
if (++self->index >= self->chunk->len) {
self->chunk = self->chunk->next;
self->index = 0;
}
}
static void trail_prev(Trail *self) {
if (self->index <= 0) {
self->chunk = self->chunk->prev;
self->index = self->chunk->len;
}
self->index--;
}
static Keyframe *trail_get(const Trail *self) {
return &self->chunk->frames[self->index];
}
Keyframe *trail_search(Trail *self, int t) {
while (t >= trail_get(self)->t) trail_next(self);
while (t < trail_get(self)->t) trail_prev(self);
return trail_get(self);
}
Keyframe *trail_add(Trail *self) {
KeyframeChunk *prev = self->chunk;
trail_next(self);
if (!self->chunk) {
self->chunk = malloc(
sizeof(KeyframeChunk) + sizeof(Keyframe) * KEYFRAMECHUNK_LEN
);
assert(self->chunk);
*self->chunk = (KeyframeChunk){
.prev = prev,
.len = KEYFRAMECHUNK_LEN
};
}
return trail_get(self);
}