dsys2

view src/dsys.c @ 7:3258d163cfbc

hurray it seems like working for the most part
author John Tsiombikas <nuclear@siggraph.org>
date Fri, 02 Sep 2011 08:14:40 +0300
parents 80f86f0f67ec
children 4ad7a01c4ff5
line source
1 #include <stdio.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include "dsys2.h"
8 #include "dsys_impl.h"
10 static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname);
11 static char *strip_ws(char *buf);
12 static void dbg_print_events(struct dsys_event *ev);
14 static void proc_event(struct dsys_event *ev, demotime_t tm);
15 static void link_callback(struct dsys_event *ev, void *cls);
16 static void free_event(struct dsys_event *ev);
18 static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev);
19 static struct dsys_event *merge_evlists(struct dsys_event *list1, struct dsys_event *list2);
22 struct dsys_demo *dsys_open(const char *fname)
23 {
24 FILE *fp;
25 struct dsys_demo *demo;
27 if(!(fp = fopen(fname, "r"))) {
28 fprintf(stderr, "failed to open demoscript: %s: %s\n", fname, strerror(errno));
29 return 0;
30 }
32 if(!(demo = malloc(sizeof *demo))) {
33 perror("failed to allocate memory");
34 fclose(fp);
35 return 0;
36 }
37 memset(demo, 0, sizeof *demo);
39 demo->src_tm = demo->start_tm = -1;
41 if(read_script(demo, fp, fname) == -1) {
42 free(demo);
43 fclose(fp);
44 return 0;
45 }
47 fclose(fp);
48 return demo;
49 }
51 struct dsys_demo *dsys_open_stream(FILE *fp)
52 {
53 struct dsys_demo *demo;
55 if(!(demo = malloc(sizeof *demo))) {
56 perror("failed to allocate memory");
57 return 0;
58 }
59 memset(demo, 0, sizeof *demo);
61 demo->src_tm = demo->start_tm = -1;
63 if(read_script(demo, fp, 0) == -1) {
64 free(demo);
65 return 0;
66 }
68 return demo;
69 }
71 void dsys_close(struct dsys_demo *demo)
72 {
73 while(demo->evlist) {
74 struct dsys_event *ev = demo->evlist;
75 demo->evlist = demo->evlist->next;
76 free_event(ev);
77 }
79 free(demo);
80 }
83 #define SEP " \t\n\r"
85 static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname)
86 {
87 int nline = 0;
88 char buf[512], *line, *tok, *endp;
89 unsigned int t0, t1;
90 struct dsys_event *ev;
92 if(!fname) {
93 fname = "<unknown>";
94 }
96 demo->duration = dsys_msec_to_dtime(0);
98 while(fgets(buf, sizeof buf, fp)) {
99 nline++;
101 line = strip_ws(buf);
103 if(!line || !*line) {
104 continue;
105 }
107 if(!(tok = strtok(line, SEP)) || (t0 = strtol(tok, &endp, 10), endp == tok)) {
108 fprintf(stderr, "%s line: %d, error: expected timestamp t0\n", fname, nline);
109 return -1;
110 }
112 if(!(tok = strtok(0, SEP))) {
113 fprintf(stderr, "%s line: %d, error: expected second timestamp or event name\n", fname, nline);
114 return -1;
115 }
117 t1 = strtol(tok, &endp, 10);
118 if(endp == tok) {
119 t1 = t0;
120 } else {
121 if(!(tok = strtok(0, SEP))) {
122 fprintf(stderr, "%s line: %d, error: expected event name\n", fname, nline);
123 return -1;
124 }
125 }
127 if(!(ev = malloc(sizeof *ev))) {
128 perror("read_script: failed to allocate memory for an event\n");
129 return -1;
130 }
131 ev->t0 = dsys_msec_to_dtime(t0);
132 ev->t1 = dsys_msec_to_dtime(t1);
134 if(!(ev->name = malloc(strlen(tok) + 1))) {
135 free(ev);
136 fprintf(stderr, "read_script: failed to allocate memory for the event name: %s\n", tok);
137 return -1;
138 }
139 strcpy(ev->name, tok);
141 ev->eval = t0 == t1 ? dsys_eval_step : dsys_eval_lerp;
143 ev->next = demo->evlist;
144 ev->prev = 0;
145 if(demo->evlist) {
146 demo->evlist->prev = ev;
147 }
148 demo->evlist = ev;
149 demo->num_ev++;
151 if(ev->t1 > demo->duration) {
152 demo->duration = ev->t1;
153 }
154 }
156 demo->evlist = sort_evlist(demo->evlist, demo->num_ev);
158 dbg_print_events(demo->evlist);
160 return 0;
161 }
163 static char *strip_ws(char *buf)
164 {
165 char *ptr;
167 while(isspace(*buf)) {
168 buf++;
169 }
171 ptr = buf;
172 while(*ptr) {
173 if(*ptr == '\n' || *ptr == '\r' || *ptr == '#') {
174 *ptr = 0;
175 break;
176 }
177 ptr++;
178 }
180 return buf;
181 }
183 static void dbg_print_events(struct dsys_event *ev)
184 {
185 int i;
187 for(i=0; ev; i++) {
188 printf("%02d - %s (%f -> %f) [%s]\n", i, ev->eval == dsys_eval_step ? "step" : "lerp",
189 ev->t0, ev->t1, ev->name);
190 ev = ev->next;
191 }
192 }
194 void dsys_update(struct dsys_demo *demo, demotime_t tm)
195 {
196 struct dsys_event *ev;
198 demo->src_tm = tm;
200 if(demo->start_tm == -1) {
201 dsys_start(demo);
202 }
204 if(!demo->running) {
205 return; /* nothing changes */
206 }
208 demo->tm = tm - demo->start_tm - demo->stoppage_tm;
210 if(demo->tm < 0) {
211 demo->tm = 0;
212 }
213 if(demo->tm > demo->duration) {
214 demo->tm = demo->duration;
215 }
217 while(demo->active->t1 < demo->tm) {
218 proc_event(demo->active, demo->tm);
219 demo->active = demo->active->next;
220 }
222 ev = demo->active;
223 while(ev && ev->t0 <= demo->tm) {
224 proc_event(ev, demo->tm);
225 ev = ev->next;
226 }
227 demo->nextev = ev;
230 if(demo->tm >= demo->duration) {
231 dsys_stop(demo);
232 }
233 }
235 static void proc_event(struct dsys_event *ev, demotime_t tm)
236 {
237 float val = ev->eval(ev, tm);
239 if(ev->val != val) {
240 struct callback *cb = ev->cblist;
242 while(cb) {
243 cb->func(ev, cb->cls);
244 cb = cb->next;
245 }
246 ev->val = val;
247 }
248 }
250 void dsys_start(struct dsys_demo *demo)
251 {
252 if(demo->running) {
253 return;
254 }
256 if(demo->start_tm == -1) {
257 demo->start_tm = demo->src_tm;
258 demo->nextev = demo->active = demo->evlist;
259 } else {
260 demo->stoppage_tm += demo->src_tm - demo->stop_tm;
261 }
263 demo->running = 1;
264 }
266 void dsys_stop(struct dsys_demo *demo)
267 {
268 if(!demo->running) {
269 return;
270 }
272 demo->stop_tm = demo->src_tm;
273 demo->running = 0;
274 }
276 int dsys_is_running(struct dsys_demo *demo)
277 {
278 return demo->running;
279 }
282 demotime_t dsys_duration(struct dsys_demo *demo)
283 {
284 return demo->duration;
285 }
287 demotime_t dsys_time(struct dsys_demo *demo)
288 {
289 return demo->tm;
290 }
292 float dsys_progress(struct dsys_demo *demo)
293 {
294 return demo->tm / demo->duration;
295 }
297 /* seek without continuity */
298 void dsys_seek(struct dsys_demo *demo, demotime_t tm)
299 {
300 if(tm < 0) {
301 tm = 0;
302 }
303 if(tm > demo->duration) {
304 tm = demo->duration;
305 }
307 demo->start_tm = demo->src_tm - tm;
308 demo->stoppage_tm = 0;
309 }
311 void dsys_seek_norm(struct dsys_demo *demo, float t)
312 {
313 dsys_seek(demo, t * demo->duration);
314 }
316 /* seek by accelerating time */
317 void dsys_warp(struct dsys_demo *demo, demotime_t tm)
318 {
319 fprintf(stderr, "dsys_warp not implemented yet\n");
320 }
322 void dsys_warp_norm(struct dsys_demo *demo, float t)
323 {
324 dsys_warp(demo, t * demo->duration);
325 }
328 /* events */
329 struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name)
330 {
331 struct dsys_event *iter = demo->evlist;
333 while(iter) {
334 if(strcmp(iter->name, name) == 0) {
335 return iter;
336 }
337 iter = iter->next;
338 }
339 return 0;
340 }
342 enum dsys_evtype dsys_event_type(struct dsys_event *ev)
343 {
344 return ev->type;
345 }
347 float dsys_event_value(struct dsys_event *ev)
348 {
349 return ev->val;
350 }
352 int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls)
353 {
354 struct callback *cb;
356 if(!(cb = malloc(sizeof *cb))) {
357 perror("failed to allocate memory");
358 return -1;
359 }
360 cb->func = func;
361 cb->cls = cls;
362 cb->next = ev->cblist;
363 ev->cblist = cb;
364 return 0;
365 }
367 int dsys_event_link(struct dsys_event *ev, float *link)
368 {
369 return dsys_event_callback(ev, link_callback, link);
370 }
372 static void link_callback(struct dsys_event *ev, void *cls)
373 {
374 *(float*)cls = ev->val;
375 }
378 /* time conversion */
379 demotime_t dsys_sec_to_dtime(float sec)
380 {
381 return sec;
382 }
384 demotime_t dsys_msec_to_dtime(unsigned long msec)
385 {
386 return (demotime_t)msec / 1000.0;
387 }
389 float dsys_dtime_to_sec(demotime_t tm)
390 {
391 return tm;
392 }
394 unsigned long dsys_dtime_to_msec(demotime_t tm)
395 {
396 return (unsigned long)(tm * 1000.0);
397 }
400 float dsys_eval_step(struct dsys_event *ev, demotime_t t)
401 {
402 return t >= ev->t1 ? 1.0 : 0.0;
403 }
405 #define CLAMP(x, low, high) ((x) < (low) ? (low) : ((x) > (high) ? (high) : (x)))
407 float dsys_eval_lerp(struct dsys_event *ev, demotime_t t)
408 {
409 float res = (t - ev->t0) / (ev->t1 - ev->t0);
410 return CLAMP(res, 0.0, 1.0);
411 }
413 float dsys_eval_sigmoid(struct dsys_event *ev, demotime_t t)
414 {
415 t = dsys_eval_lerp(ev, t);
416 return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
417 }
419 static void free_event(struct dsys_event *ev)
420 {
421 while(ev->cblist) {
422 struct callback *cb = ev->cblist;
423 ev->cblist = ev->cblist->next;
424 free(cb);
425 }
426 }
428 static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev)
429 {
430 int i, num_left, num_right;
431 struct dsys_event *left, *right, *node = list;
433 if(num_ev < 2) {
434 return list;
435 }
437 num_left = num_ev / 2;
438 num_right = num_ev - num_left;
440 for(i=0; i<num_ev/2; i++) {
441 node = node->next;
442 }
444 if(node->prev) {
445 node->prev->next = 0;
446 node->prev = 0;
447 }
449 left = sort_evlist(list, num_left);
450 right = sort_evlist(node, num_right);
452 return merge_evlists(left, right);
453 }
455 static struct dsys_event *merge_evlists(struct dsys_event *list1, struct dsys_event *list2)
456 {
457 struct dsys_event *head, *tail, *node;
459 if(!list1) {
460 return list2;
461 }
462 if(!list2) {
463 return list1;
464 }
466 head = tail = 0;
468 while(list1 && list2) {
469 if(list1->t0 < list2->t0) {
470 node = list1;
471 list1 = list1->next;
472 } else {
473 node = list2;
474 list2 = list2->next;
475 }
477 node->next = 0;
478 node->prev = tail;
480 if(!head) {
481 head = node;
482 } else {
483 tail->next = node;
484 }
485 tail = node;
486 }
488 if(list1) {
489 tail->next = list1;
490 list1->prev = tail;
491 } else if(list2) {
492 tail->next = list2;
493 list2->prev = tail;
494 }
496 return head;
497 }