dbf-udg

view libs/dsys2/dsys.c @ 12:1abbed71e9c9

cleanup, copyright statements and notices, readme files
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 20 Feb 2013 05:45:27 +0200
parents 5f99c4c7a9fe
children
line source
1 /*
2 New demosystem by Nuclear / Mindlapse
3 Copyright (C) 2011-2013 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <math.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include "dsys.h"
25 #include "dsys_impl.h"
27 static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname);
28 static char *strip_ws(char *buf);
29 static void dbg_print_events(struct dsys_event *ev);
31 static void proc_event(struct dsys_event *ev, demotime_t tm);
32 static void link_callback(struct dsys_event *ev, void *cls);
33 static void free_event(struct dsys_event *ev);
35 static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev);
36 static struct dsys_event *merge_evlists(struct dsys_event *list1, struct dsys_event *list2);
39 struct dsys_demo *dsys_open(const char *fname)
40 {
41 FILE *fp;
42 struct dsys_demo *demo;
44 if(!(fp = fopen(fname, "r"))) {
45 fprintf(stderr, "failed to open demoscript: %s: %s\n", fname, strerror(errno));
46 return 0;
47 }
49 if(!(demo = malloc(sizeof *demo))) {
50 perror("failed to allocate memory");
51 fclose(fp);
52 return 0;
53 }
54 memset(demo, 0, sizeof *demo);
56 demo->src_tm = demo->start_tm = -1;
58 if(read_script(demo, fp, fname) == -1) {
59 free(demo);
60 fclose(fp);
61 return 0;
62 }
64 fclose(fp);
65 return demo;
66 }
68 struct dsys_demo *dsys_open_stream(FILE *fp)
69 {
70 struct dsys_demo *demo;
72 if(!(demo = malloc(sizeof *demo))) {
73 perror("failed to allocate memory");
74 return 0;
75 }
76 memset(demo, 0, sizeof *demo);
78 demo->src_tm = demo->start_tm = -1;
80 if(read_script(demo, fp, 0) == -1) {
81 free(demo);
82 return 0;
83 }
85 return demo;
86 }
88 void dsys_close(struct dsys_demo *demo)
89 {
90 while(demo->evlist) {
91 struct dsys_event *ev = demo->evlist;
92 demo->evlist = demo->evlist->next;
93 free_event(ev);
94 }
96 free(demo);
97 }
100 #define SEP " \t\n\r"
102 static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname)
103 {
104 int nline = 0;
105 char buf[512], *line, *tok, *endp;
106 unsigned int t0, t1;
107 struct dsys_event *ev;
109 if(!fname) {
110 fname = "<unknown>";
111 }
113 demo->duration = dsys_msec_to_dtime(0);
115 while(fgets(buf, sizeof buf, fp)) {
116 nline++;
118 line = strip_ws(buf);
120 if(!line || !*line) {
121 continue;
122 }
124 if(!(tok = strtok(line, SEP)) || (t0 = strtol(tok, &endp, 10), endp == tok)) {
125 fprintf(stderr, "%s line: %d, error: expected timestamp t0\n", fname, nline);
126 return -1;
127 }
129 if(!(tok = strtok(0, SEP))) {
130 fprintf(stderr, "%s line: %d, error: expected second timestamp or event name\n", fname, nline);
131 return -1;
132 }
134 t1 = strtol(tok, &endp, 10);
135 if(endp == tok) {
136 t1 = t0;
137 } else {
138 if(!(tok = strtok(0, SEP))) {
139 fprintf(stderr, "%s line: %d, error: expected event name\n", fname, nline);
140 return -1;
141 }
142 }
144 if(!(ev = malloc(sizeof *ev))) {
145 perror("read_script: failed to allocate memory for an event\n");
146 return -1;
147 }
148 memset(ev, 0, sizeof *ev);
149 ev->t0 = dsys_msec_to_dtime(t0);
150 ev->t1 = dsys_msec_to_dtime(t1);
152 if(!(ev->name = malloc(strlen(tok) + 1))) {
153 free(ev);
154 fprintf(stderr, "read_script: failed to allocate memory for the event name: %s\n", tok);
155 return -1;
156 }
157 strcpy(ev->name, tok);
159 ev->eval = t0 == t1 ? dsys_eval_step : dsys_eval_lerp;
161 ev->next = demo->evlist;
162 ev->prev = 0;
163 if(demo->evlist) {
164 demo->evlist->prev = ev;
165 }
166 demo->evlist = ev;
167 demo->num_ev++;
169 if(ev->t1 > demo->duration) {
170 demo->duration = ev->t1;
171 }
172 }
174 demo->evlist = sort_evlist(demo->evlist, demo->num_ev);
176 /*dbg_print_events(demo->evlist);*/
178 return 0;
179 }
181 static char *strip_ws(char *buf)
182 {
183 char *ptr;
185 while(isspace(*buf)) {
186 buf++;
187 }
189 ptr = buf;
190 while(*ptr) {
191 if(*ptr == '\n' || *ptr == '\r' || *ptr == '#') {
192 *ptr = 0;
193 break;
194 }
195 ptr++;
196 }
198 return buf;
199 }
201 static void dbg_print_events(struct dsys_event *ev)
202 {
203 int i;
205 for(i=0; ev; i++) {
206 printf("%02d - %s (%f -> %f) [%s]\n", i, ev->eval == dsys_eval_step ? "step" : "lerp",
207 ev->t0, ev->t1, ev->name);
208 ev = ev->next;
209 }
210 }
212 void dsys_update(struct dsys_demo *demo, demotime_t tm)
213 {
214 struct dsys_event *ev;
216 demo->src_tm = tm;
218 if(demo->start_tm == -1) {
219 dsys_start(demo);
220 }
222 if(!demo->running) {
223 return; /* nothing changes */
224 }
226 demo->tm = tm - demo->start_tm - demo->stoppage_tm;
228 if(demo->tm < 0) {
229 demo->tm = 0;
230 }
231 if(demo->tm > demo->duration) {
232 demo->tm = demo->duration;
233 }
235 while(demo->active && demo->active->t1 <= demo->tm) {
236 proc_event(demo->active, demo->tm);
237 demo->active = demo->active->next;
238 }
240 ev = demo->active;
241 while(ev && ev->t0 <= demo->tm) {
242 proc_event(ev, demo->tm);
243 ev = ev->next;
244 }
245 demo->nextev = ev;
248 if(demo->tm >= demo->duration) {
249 dsys_stop(demo);
250 }
251 }
253 static void proc_event(struct dsys_event *ev, demotime_t tm)
254 {
255 float val = ev->eval(ev, tm);
257 if(ev->val != val) {
258 struct callback *cb = ev->cblist;
260 ev->val = val;
262 while(cb) {
263 cb->func(ev, cb->cls);
264 cb = cb->next;
265 }
266 }
267 }
269 void dsys_start(struct dsys_demo *demo)
270 {
271 if(demo->running) {
272 return;
273 }
275 if(demo->start_tm == -1) {
276 demo->start_tm = demo->src_tm;
277 demo->nextev = demo->active = demo->evlist;
278 } else {
279 demo->stoppage_tm += demo->src_tm - demo->stop_tm;
280 }
282 demo->running = 1;
283 }
285 void dsys_stop(struct dsys_demo *demo)
286 {
287 if(!demo->running) {
288 return;
289 }
291 demo->stop_tm = demo->src_tm;
292 demo->running = 0;
293 }
295 int dsys_is_running(struct dsys_demo *demo)
296 {
297 return demo->running;
298 }
301 demotime_t dsys_duration(struct dsys_demo *demo)
302 {
303 return demo->duration;
304 }
306 demotime_t dsys_time(struct dsys_demo *demo)
307 {
308 return demo->tm;
309 }
311 float dsys_progress(struct dsys_demo *demo)
312 {
313 return demo->tm / demo->duration;
314 }
316 /* seek without continuity */
317 void dsys_seek(struct dsys_demo *demo, demotime_t tm)
318 {
319 struct dsys_event *ev;
321 if(tm < 0) {
322 tm = 0;
323 }
324 if(tm > demo->duration) {
325 tm = demo->duration;
326 }
328 if(tm < demo->tm) {
329 /* on backwards seek, invalidate the sliding window */
330 demo->nextev = demo->active = demo->evlist;
331 }
333 demo->start_tm = demo->src_tm - tm;
334 demo->stoppage_tm = 0;
335 demo->stop_tm = demo->src_tm;
336 demo->tm = tm;
338 /* recalculate events */
339 ev = demo->evlist;
340 while(ev) {
341 proc_event(ev, tm);
342 ev = ev->next;
343 }
344 }
346 void dsys_seek_norm(struct dsys_demo *demo, float t)
347 {
348 dsys_seek(demo, t * demo->duration);
349 }
351 /* seek by accelerating time */
352 void dsys_warp(struct dsys_demo *demo, demotime_t tm)
353 {
354 fprintf(stderr, "dsys_warp not implemented yet\n");
355 }
357 void dsys_warp_norm(struct dsys_demo *demo, float t)
358 {
359 dsys_warp(demo, t * demo->duration);
360 }
363 /* events */
364 struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name)
365 {
366 struct dsys_event *iter = demo->evlist;
368 while(iter) {
369 if(strcmp(iter->name, name) == 0) {
370 return iter;
371 }
372 iter = iter->next;
373 }
374 return 0;
375 }
377 void dsys_set_event_eval(struct dsys_event *ev, float (*eval)(struct dsys_event*, demotime_t))
378 {
379 ev->eval = eval;
380 }
382 enum dsys_evtype dsys_event_type(struct dsys_event *ev)
383 {
384 return ev->type;
385 }
387 float dsys_event_value(struct dsys_event *ev)
388 {
389 return ev->val;
390 }
392 int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls)
393 {
394 struct callback *cb;
396 if(!(cb = malloc(sizeof *cb))) {
397 perror("failed to allocate memory");
398 return -1;
399 }
400 cb->func = func;
401 cb->cls = cls;
402 cb->next = ev->cblist;
403 ev->cblist = cb;
404 return 0;
405 }
407 int dsys_event_link(struct dsys_event *ev, float *link)
408 {
409 return dsys_event_callback(ev, link_callback, link);
410 }
412 static void link_callback(struct dsys_event *ev, void *cls)
413 {
414 *(float*)cls = ev->val;
415 }
418 /* time conversion */
419 demotime_t dsys_sec_to_dtime(float sec)
420 {
421 return sec;
422 }
424 demotime_t dsys_msec_to_dtime(unsigned long msec)
425 {
426 return (demotime_t)msec / 1000.0;
427 }
429 float dsys_dtime_to_sec(demotime_t tm)
430 {
431 return tm;
432 }
434 unsigned long dsys_dtime_to_msec(demotime_t tm)
435 {
436 return (unsigned long)(tm * 1000.0);
437 }
440 float dsys_eval_step(struct dsys_event *ev, demotime_t t)
441 {
442 return t >= ev->t1 ? 1.0 : 0.0;
443 }
445 #define CLAMP(x, low, high) ((x) < (low) ? (low) : ((x) > (high) ? (high) : (x)))
447 float dsys_eval_lerp(struct dsys_event *ev, demotime_t t)
448 {
449 float res = (t - ev->t0) / (ev->t1 - ev->t0);
450 return CLAMP(res, 0.0, 1.0);
451 }
453 float dsys_eval_sigmoid(struct dsys_event *ev, demotime_t t)
454 {
455 t = dsys_eval_lerp(ev, t);
456 return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
457 }
459 static void free_event(struct dsys_event *ev)
460 {
461 while(ev->cblist) {
462 struct callback *cb = ev->cblist;
463 ev->cblist = ev->cblist->next;
464 free(cb);
465 }
466 }
468 static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev)
469 {
470 int i, num_left, num_right;
471 struct dsys_event *left, *right, *node = list;
473 if(num_ev < 2) {
474 return list;
475 }
477 num_left = num_ev / 2;
478 num_right = num_ev - num_left;
480 for(i=0; i<num_ev/2; i++) {
481 node = node->next;
482 }
484 if(node->prev) {
485 node->prev->next = 0;
486 node->prev = 0;
487 }
489 left = sort_evlist(list, num_left);
490 right = sort_evlist(node, num_right);
492 return merge_evlists(left, right);
493 }
495 static struct dsys_event *merge_evlists(struct dsys_event *list1, struct dsys_event *list2)
496 {
497 struct dsys_event *head, *tail, *node;
499 if(!list1) {
500 return list2;
501 }
502 if(!list2) {
503 return list1;
504 }
506 head = tail = 0;
508 while(list1 && list2) {
509 if(list1->t0 < list2->t0) {
510 node = list1;
511 list1 = list1->next;
512 } else {
513 node = list2;
514 list2 = list2->next;
515 }
517 node->next = 0;
518 node->prev = tail;
520 if(!head) {
521 head = node;
522 } else {
523 tail->next = node;
524 }
525 tail = node;
526 }
528 if(list1) {
529 tail->next = list1;
530 list1->prev = tail;
531 } else if(list2) {
532 tail->next = list2;
533 list2->prev = tail;
534 }
536 return head;
537 }