dsys2

view src/dsys.c @ 9:61cc1a525023

ha!
author John Tsiombikas <nuclear@siggraph.org>
date Fri, 02 Sep 2011 19:12:12 +0300
parents 4ad7a01c4ff5
children 5ba9dd6742a0
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 demo->tm = tm - demo->start_tm - demo->stoppage_tm;
206 if(demo->tm < 0) {
207 demo->tm = 0;
208 }
209 if(demo->tm > demo->duration) {
210 demo->tm = demo->duration;
211 }
213 if(!demo->running) {
214 return; /* nothing changes */
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 struct dsys_event *ev;
302 if(tm < 0) {
303 tm = 0;
304 }
305 if(tm > demo->duration) {
306 tm = demo->duration;
307 }
309 demo->start_tm = demo->src_tm - tm;
310 demo->stoppage_tm = 0;
312 demo->nextev = demo->active = demo->evlist;
314 /* recalculate events */
315 ev = demo->evlist;
316 while(ev) {
317 proc_event(ev, tm);
318 ev = ev->next;
319 }
320 }
322 void dsys_seek_norm(struct dsys_demo *demo, float t)
323 {
324 dsys_seek(demo, t * demo->duration);
325 }
327 /* seek by accelerating time */
328 void dsys_warp(struct dsys_demo *demo, demotime_t tm)
329 {
330 fprintf(stderr, "dsys_warp not implemented yet\n");
331 }
333 void dsys_warp_norm(struct dsys_demo *demo, float t)
334 {
335 dsys_warp(demo, t * demo->duration);
336 }
339 /* events */
340 struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name)
341 {
342 struct dsys_event *iter = demo->evlist;
344 while(iter) {
345 if(strcmp(iter->name, name) == 0) {
346 return iter;
347 }
348 iter = iter->next;
349 }
350 return 0;
351 }
353 enum dsys_evtype dsys_event_type(struct dsys_event *ev)
354 {
355 return ev->type;
356 }
358 float dsys_event_value(struct dsys_event *ev)
359 {
360 return ev->val;
361 }
363 int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls)
364 {
365 struct callback *cb;
367 if(!(cb = malloc(sizeof *cb))) {
368 perror("failed to allocate memory");
369 return -1;
370 }
371 cb->func = func;
372 cb->cls = cls;
373 cb->next = ev->cblist;
374 ev->cblist = cb;
375 return 0;
376 }
378 int dsys_event_link(struct dsys_event *ev, float *link)
379 {
380 return dsys_event_callback(ev, link_callback, link);
381 }
383 static void link_callback(struct dsys_event *ev, void *cls)
384 {
385 *(float*)cls = ev->val;
386 }
389 /* time conversion */
390 demotime_t dsys_sec_to_dtime(float sec)
391 {
392 return sec;
393 }
395 demotime_t dsys_msec_to_dtime(unsigned long msec)
396 {
397 return (demotime_t)msec / 1000.0;
398 }
400 float dsys_dtime_to_sec(demotime_t tm)
401 {
402 return tm;
403 }
405 unsigned long dsys_dtime_to_msec(demotime_t tm)
406 {
407 return (unsigned long)(tm * 1000.0);
408 }
411 float dsys_eval_step(struct dsys_event *ev, demotime_t t)
412 {
413 return t >= ev->t1 ? 1.0 : 0.0;
414 }
416 #define CLAMP(x, low, high) ((x) < (low) ? (low) : ((x) > (high) ? (high) : (x)))
418 float dsys_eval_lerp(struct dsys_event *ev, demotime_t t)
419 {
420 float res = (t - ev->t0) / (ev->t1 - ev->t0);
421 return CLAMP(res, 0.0, 1.0);
422 }
424 float dsys_eval_sigmoid(struct dsys_event *ev, demotime_t t)
425 {
426 t = dsys_eval_lerp(ev, t);
427 return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
428 }
430 static void free_event(struct dsys_event *ev)
431 {
432 while(ev->cblist) {
433 struct callback *cb = ev->cblist;
434 ev->cblist = ev->cblist->next;
435 free(cb);
436 }
437 }
439 static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev)
440 {
441 int i, num_left, num_right;
442 struct dsys_event *left, *right, *node = list;
444 if(num_ev < 2) {
445 return list;
446 }
448 num_left = num_ev / 2;
449 num_right = num_ev - num_left;
451 for(i=0; i<num_ev/2; i++) {
452 node = node->next;
453 }
455 if(node->prev) {
456 node->prev->next = 0;
457 node->prev = 0;
458 }
460 left = sort_evlist(list, num_left);
461 right = sort_evlist(node, num_right);
463 return merge_evlists(left, right);
464 }
466 static struct dsys_event *merge_evlists(struct dsys_event *list1, struct dsys_event *list2)
467 {
468 struct dsys_event *head, *tail, *node;
470 if(!list1) {
471 return list2;
472 }
473 if(!list2) {
474 return list1;
475 }
477 head = tail = 0;
479 while(list1 && list2) {
480 if(list1->t0 < list2->t0) {
481 node = list1;
482 list1 = list1->next;
483 } else {
484 node = list2;
485 list2 = list2->next;
486 }
488 node->next = 0;
489 node->prev = tail;
491 if(!head) {
492 head = node;
493 } else {
494 tail->next = node;
495 }
496 tail = node;
497 }
499 if(list1) {
500 tail->next = list1;
501 list1->prev = tail;
502 } else if(list2) {
503 tail->next = list2;
504 list2->prev = tail;
505 }
507 return head;
508 }