dbf-udg

changeset 9:7056437a361b

added demosys
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 19 Feb 2013 18:17:17 +0200
parents f0a47f46ee45
children 1120c069eb17
files Makefile demoscript libs/dsys2/dsys.c libs/dsys2/dsys.h libs/dsys2/dsys_impl.h src/mballs.cc src/mballs.h src/udg.cc
diffstat 7 files changed, 649 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Mon Feb 18 06:53:44 2013 +0200
     1.2 +++ b/Makefile	Tue Feb 19 18:17:17 2013 +0200
     1.3 @@ -1,11 +1,12 @@
     1.4  csrc = $(wildcard src/*.c) \
     1.5 -	   $(wildcard libs/metasurf/*.c)
     1.6 +	   $(wildcard libs/metasurf/*.c) \
     1.7 +	   $(wildcard libs/dsys2/*.c)
     1.8  ccsrc = $(wildcard src/*.cc)
     1.9  obj = $(csrc:.c=.o) $(ccsrc:.cc=.o)
    1.10  dep = $(obj:.o=.d)
    1.11  bin = udg
    1.12  
    1.13 -CFLAGS = -pedantic -Wall -g -O3 -Ilibs/metasurf
    1.14 +CFLAGS = -pedantic -Wall -g -O3 -Ilibs/metasurf -Ilibs/dsys2
    1.15  CXXFLAGS = -std=c++11 $(CFLAGS)
    1.16  LDFLAGS = $(libgl) -limago -lvmath
    1.17  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/libs/dsys2/dsys.c	Tue Feb 19 18:17:17 2013 +0200
     2.3 @@ -0,0 +1,515 @@
     2.4 +#include <stdio.h>
     2.5 +#include <math.h>
     2.6 +#include <stdlib.h>
     2.7 +#include <string.h>
     2.8 +#include <ctype.h>
     2.9 +#include <errno.h>
    2.10 +#include "dsys.h"
    2.11 +#include "dsys_impl.h"
    2.12 +
    2.13 +static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname);
    2.14 +static char *strip_ws(char *buf);
    2.15 +static void dbg_print_events(struct dsys_event *ev);
    2.16 +
    2.17 +static void proc_event(struct dsys_event *ev, demotime_t tm);
    2.18 +static void link_callback(struct dsys_event *ev, void *cls);
    2.19 +static void free_event(struct dsys_event *ev);
    2.20 +
    2.21 +static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev);
    2.22 +static struct dsys_event *merge_evlists(struct dsys_event *list1, struct dsys_event *list2);
    2.23 +
    2.24 +
    2.25 +struct dsys_demo *dsys_open(const char *fname)
    2.26 +{
    2.27 +	FILE *fp;
    2.28 +	struct dsys_demo *demo;
    2.29 +
    2.30 +	if(!(fp = fopen(fname, "r"))) {
    2.31 +		fprintf(stderr, "failed to open demoscript: %s: %s\n", fname, strerror(errno));
    2.32 +		return 0;
    2.33 +	}
    2.34 +
    2.35 +	if(!(demo = malloc(sizeof *demo))) {
    2.36 +		perror("failed to allocate memory");
    2.37 +		fclose(fp);
    2.38 +		return 0;
    2.39 +	}
    2.40 +	memset(demo, 0, sizeof *demo);
    2.41 +
    2.42 +	demo->src_tm = demo->start_tm = -1;
    2.43 +
    2.44 +	if(read_script(demo, fp, fname) == -1) {
    2.45 +		free(demo);
    2.46 +		fclose(fp);
    2.47 +		return 0;
    2.48 +	}
    2.49 +
    2.50 +	fclose(fp);
    2.51 +	return demo;
    2.52 +}
    2.53 +
    2.54 +struct dsys_demo *dsys_open_stream(FILE *fp)
    2.55 +{
    2.56 +	struct dsys_demo *demo;
    2.57 +
    2.58 +	if(!(demo = malloc(sizeof *demo))) {
    2.59 +		perror("failed to allocate memory");
    2.60 +		return 0;
    2.61 +	}
    2.62 +	memset(demo, 0, sizeof *demo);
    2.63 +
    2.64 +	demo->src_tm = demo->start_tm = -1;
    2.65 +
    2.66 +	if(read_script(demo, fp, 0) == -1) {
    2.67 +		free(demo);
    2.68 +		return 0;
    2.69 +	}
    2.70 +
    2.71 +	return demo;
    2.72 +}
    2.73 +
    2.74 +void dsys_close(struct dsys_demo *demo)
    2.75 +{
    2.76 +	while(demo->evlist) {
    2.77 +		struct dsys_event *ev = demo->evlist;
    2.78 +		demo->evlist = demo->evlist->next;
    2.79 +		free_event(ev);
    2.80 +	}
    2.81 +
    2.82 +	free(demo);
    2.83 +}
    2.84 +
    2.85 +
    2.86 +#define SEP	" \t\n\r"
    2.87 +
    2.88 +static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname)
    2.89 +{
    2.90 +	int nline = 0;
    2.91 +	char buf[512], *line, *tok, *endp;
    2.92 +	unsigned int t0, t1;
    2.93 +	struct dsys_event *ev;
    2.94 +
    2.95 +	if(!fname) {
    2.96 +		fname = "<unknown>";
    2.97 +	}
    2.98 +
    2.99 +	demo->duration = dsys_msec_to_dtime(0);
   2.100 +
   2.101 +	while(fgets(buf, sizeof buf, fp)) {
   2.102 +		nline++;
   2.103 +
   2.104 +		line = strip_ws(buf);
   2.105 +
   2.106 +		if(!line || !*line) {
   2.107 +			continue;
   2.108 +		}
   2.109 +
   2.110 +		if(!(tok = strtok(line, SEP)) || (t0 = strtol(tok, &endp, 10), endp == tok)) {
   2.111 +			fprintf(stderr, "%s line: %d, error: expected timestamp t0\n", fname, nline);
   2.112 +			return -1;
   2.113 +		}
   2.114 +
   2.115 +		if(!(tok = strtok(0, SEP))) {
   2.116 +			fprintf(stderr, "%s line: %d, error: expected second timestamp or event name\n", fname, nline);
   2.117 +			return -1;
   2.118 +		}
   2.119 +
   2.120 +		t1 = strtol(tok, &endp, 10);
   2.121 +		if(endp == tok) {
   2.122 +			t1 = t0;
   2.123 +		} else {
   2.124 +			if(!(tok = strtok(0, SEP))) {
   2.125 +				fprintf(stderr, "%s line: %d, error: expected event name\n", fname, nline);
   2.126 +				return -1;
   2.127 +			}
   2.128 +		}
   2.129 +
   2.130 +		if(!(ev = malloc(sizeof *ev))) {
   2.131 +			perror("read_script: failed to allocate memory for an event\n");
   2.132 +			return -1;
   2.133 +		}
   2.134 +		memset(ev, 0, sizeof *ev);
   2.135 +		ev->t0 = dsys_msec_to_dtime(t0);
   2.136 +		ev->t1 = dsys_msec_to_dtime(t1);
   2.137 +
   2.138 +		if(!(ev->name = malloc(strlen(tok) + 1))) {
   2.139 +			free(ev);
   2.140 +			fprintf(stderr, "read_script: failed to allocate memory for the event name: %s\n", tok);
   2.141 +			return -1;
   2.142 +		}
   2.143 +		strcpy(ev->name, tok);
   2.144 +
   2.145 +		ev->eval = t0 == t1 ? dsys_eval_step : dsys_eval_lerp;
   2.146 +
   2.147 +		ev->next = demo->evlist;
   2.148 +		ev->prev = 0;
   2.149 +		if(demo->evlist) {
   2.150 +			demo->evlist->prev = ev;
   2.151 +		}
   2.152 +		demo->evlist = ev;
   2.153 +		demo->num_ev++;
   2.154 +
   2.155 +		if(ev->t1 > demo->duration) {
   2.156 +			demo->duration = ev->t1;
   2.157 +		}
   2.158 +	}
   2.159 +
   2.160 +	demo->evlist = sort_evlist(demo->evlist, demo->num_ev);
   2.161 +
   2.162 +	/*dbg_print_events(demo->evlist);*/
   2.163 +
   2.164 +	return 0;
   2.165 +}
   2.166 +
   2.167 +static char *strip_ws(char *buf)
   2.168 +{
   2.169 +	char *ptr;
   2.170 +
   2.171 +	while(isspace(*buf)) {
   2.172 +		buf++;
   2.173 +	}
   2.174 +
   2.175 +	ptr = buf;
   2.176 +	while(*ptr) {
   2.177 +		if(*ptr == '\n' || *ptr == '\r' || *ptr == '#') {
   2.178 +			*ptr = 0;
   2.179 +			break;
   2.180 +		}
   2.181 +		ptr++;
   2.182 +	}
   2.183 +
   2.184 +	return buf;
   2.185 +}
   2.186 +
   2.187 +static void dbg_print_events(struct dsys_event *ev)
   2.188 +{
   2.189 +	int i;
   2.190 +
   2.191 +	for(i=0; ev; i++) {
   2.192 +		printf("%02d - %s (%f -> %f) [%s]\n", i, ev->eval == dsys_eval_step ? "step" : "lerp",
   2.193 +				ev->t0, ev->t1, ev->name);
   2.194 +		ev = ev->next;
   2.195 +	}
   2.196 +}
   2.197 +
   2.198 +void dsys_update(struct dsys_demo *demo, demotime_t tm)
   2.199 +{
   2.200 +	struct dsys_event *ev;
   2.201 +
   2.202 +	demo->src_tm = tm;
   2.203 +
   2.204 +	if(demo->start_tm == -1) {
   2.205 +		dsys_start(demo);
   2.206 +	}
   2.207 +
   2.208 +	if(!demo->running) {
   2.209 +		return;	/* nothing changes */
   2.210 +	}
   2.211 +
   2.212 +	demo->tm = tm - demo->start_tm - demo->stoppage_tm;
   2.213 +
   2.214 +	if(demo->tm < 0) {
   2.215 +		demo->tm = 0;
   2.216 +	}
   2.217 +	if(demo->tm > demo->duration) {
   2.218 +		demo->tm = demo->duration;
   2.219 +	}
   2.220 +
   2.221 +	while(demo->active && demo->active->t1 <= demo->tm) {
   2.222 +		proc_event(demo->active, demo->tm);
   2.223 +		demo->active = demo->active->next;
   2.224 +	}
   2.225 +
   2.226 +	ev = demo->active;
   2.227 +	while(ev && ev->t0 <= demo->tm) {
   2.228 +		proc_event(ev, demo->tm);
   2.229 +		ev = ev->next;
   2.230 +	}
   2.231 +	demo->nextev = ev;
   2.232 +
   2.233 +
   2.234 +	if(demo->tm >= demo->duration) {
   2.235 +		dsys_stop(demo);
   2.236 +	}
   2.237 +}
   2.238 +
   2.239 +static void proc_event(struct dsys_event *ev, demotime_t tm)
   2.240 +{
   2.241 +	float val = ev->eval(ev, tm);
   2.242 +
   2.243 +	if(ev->val != val) {
   2.244 +		struct callback *cb = ev->cblist;
   2.245 +
   2.246 +		ev->val = val;
   2.247 +
   2.248 +		while(cb) {
   2.249 +			cb->func(ev, cb->cls);
   2.250 +			cb = cb->next;
   2.251 +		}
   2.252 +	}
   2.253 +}
   2.254 +
   2.255 +void dsys_start(struct dsys_demo *demo)
   2.256 +{
   2.257 +	if(demo->running) {
   2.258 +		return;
   2.259 +	}
   2.260 +
   2.261 +	if(demo->start_tm == -1) {
   2.262 +		demo->start_tm = demo->src_tm;
   2.263 +		demo->nextev = demo->active = demo->evlist;
   2.264 +	} else {
   2.265 +		demo->stoppage_tm += demo->src_tm - demo->stop_tm;
   2.266 +	}
   2.267 +
   2.268 +	demo->running = 1;
   2.269 +}
   2.270 +
   2.271 +void dsys_stop(struct dsys_demo *demo)
   2.272 +{
   2.273 +	if(!demo->running) {
   2.274 +		return;
   2.275 +	}
   2.276 +
   2.277 +	demo->stop_tm = demo->src_tm;
   2.278 +	demo->running = 0;
   2.279 +}
   2.280 +
   2.281 +int dsys_is_running(struct dsys_demo *demo)
   2.282 +{
   2.283 +	return demo->running;
   2.284 +}
   2.285 +
   2.286 +
   2.287 +demotime_t dsys_duration(struct dsys_demo *demo)
   2.288 +{
   2.289 +	return demo->duration;
   2.290 +}
   2.291 +
   2.292 +demotime_t dsys_time(struct dsys_demo *demo)
   2.293 +{
   2.294 +	return demo->tm;
   2.295 +}
   2.296 +
   2.297 +float dsys_progress(struct dsys_demo *demo)
   2.298 +{
   2.299 +	return demo->tm / demo->duration;
   2.300 +}
   2.301 +
   2.302 +/* seek without continuity */
   2.303 +void dsys_seek(struct dsys_demo *demo, demotime_t tm)
   2.304 +{
   2.305 +	struct dsys_event *ev;
   2.306 +
   2.307 +	if(tm < 0) {
   2.308 +		tm = 0;
   2.309 +	}
   2.310 +	if(tm > demo->duration) {
   2.311 +		tm = demo->duration;
   2.312 +	}
   2.313 +
   2.314 +	if(tm < demo->tm) {
   2.315 +		/* on backwards seek, invalidate the sliding window */
   2.316 +		demo->nextev = demo->active = demo->evlist;
   2.317 +	}
   2.318 +
   2.319 +	demo->start_tm = demo->src_tm - tm;
   2.320 +	demo->stoppage_tm = 0;
   2.321 +	demo->stop_tm = demo->src_tm;
   2.322 +	demo->tm = tm;
   2.323 +
   2.324 +	/* recalculate events */
   2.325 +	ev = demo->evlist;
   2.326 +	while(ev) {
   2.327 +		proc_event(ev, tm);
   2.328 +		ev = ev->next;
   2.329 +	}
   2.330 +}
   2.331 +
   2.332 +void dsys_seek_norm(struct dsys_demo *demo, float t)
   2.333 +{
   2.334 +	dsys_seek(demo, t * demo->duration);
   2.335 +}
   2.336 +
   2.337 +/* seek by accelerating time */
   2.338 +void dsys_warp(struct dsys_demo *demo, demotime_t tm)
   2.339 +{
   2.340 +	fprintf(stderr, "dsys_warp not implemented yet\n");
   2.341 +}
   2.342 +
   2.343 +void dsys_warp_norm(struct dsys_demo *demo, float t)
   2.344 +{
   2.345 +	dsys_warp(demo, t * demo->duration);
   2.346 +}
   2.347 +
   2.348 +
   2.349 +/* events */
   2.350 +struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name)
   2.351 +{
   2.352 +	struct dsys_event *iter = demo->evlist;
   2.353 +
   2.354 +	while(iter) {
   2.355 +		if(strcmp(iter->name, name) == 0) {
   2.356 +			return iter;
   2.357 +		}
   2.358 +		iter = iter->next;
   2.359 +	}
   2.360 +	return 0;
   2.361 +}
   2.362 +
   2.363 +enum dsys_evtype dsys_event_type(struct dsys_event *ev)
   2.364 +{
   2.365 +	return ev->type;
   2.366 +}
   2.367 +
   2.368 +float dsys_event_value(struct dsys_event *ev)
   2.369 +{
   2.370 +	return ev->val;
   2.371 +}
   2.372 +
   2.373 +int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls)
   2.374 +{
   2.375 +	struct callback *cb;
   2.376 +
   2.377 +	if(!(cb = malloc(sizeof *cb))) {
   2.378 +		perror("failed to allocate memory");
   2.379 +		return -1;
   2.380 +	}
   2.381 +	cb->func = func;
   2.382 +	cb->cls = cls;
   2.383 +	cb->next = ev->cblist;
   2.384 +	ev->cblist = cb;
   2.385 +	return 0;
   2.386 +}
   2.387 +
   2.388 +int dsys_event_link(struct dsys_event *ev, float *link)
   2.389 +{
   2.390 +	return dsys_event_callback(ev, link_callback, link);
   2.391 +}
   2.392 +
   2.393 +static void link_callback(struct dsys_event *ev, void *cls)
   2.394 +{
   2.395 +	*(float*)cls = ev->val;
   2.396 +}
   2.397 +
   2.398 +
   2.399 +/* time conversion */
   2.400 +demotime_t dsys_sec_to_dtime(float sec)
   2.401 +{
   2.402 +	return sec;
   2.403 +}
   2.404 +
   2.405 +demotime_t dsys_msec_to_dtime(unsigned long msec)
   2.406 +{
   2.407 +	return (demotime_t)msec / 1000.0;
   2.408 +}
   2.409 +
   2.410 +float dsys_dtime_to_sec(demotime_t tm)
   2.411 +{
   2.412 +	return tm;
   2.413 +}
   2.414 +
   2.415 +unsigned long dsys_dtime_to_msec(demotime_t tm)
   2.416 +{
   2.417 +	return (unsigned long)(tm * 1000.0);
   2.418 +}
   2.419 +
   2.420 +
   2.421 +float dsys_eval_step(struct dsys_event *ev, demotime_t t)
   2.422 +{
   2.423 +	return t >= ev->t1 ? 1.0 : 0.0;
   2.424 +}
   2.425 +
   2.426 +#define CLAMP(x, low, high)	((x) < (low) ? (low) : ((x) > (high) ? (high) : (x)))
   2.427 +
   2.428 +float dsys_eval_lerp(struct dsys_event *ev, demotime_t t)
   2.429 +{
   2.430 +	float res = (t - ev->t0) / (ev->t1 - ev->t0);
   2.431 +	return CLAMP(res, 0.0, 1.0);
   2.432 +}
   2.433 +
   2.434 +float dsys_eval_sigmoid(struct dsys_event *ev, demotime_t t)
   2.435 +{
   2.436 +	t = dsys_eval_lerp(ev, t);
   2.437 +	return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
   2.438 +}
   2.439 +
   2.440 +static void free_event(struct dsys_event *ev)
   2.441 +{
   2.442 +	while(ev->cblist) {
   2.443 +		struct callback *cb = ev->cblist;
   2.444 +		ev->cblist = ev->cblist->next;
   2.445 +		free(cb);
   2.446 +	}
   2.447 +}
   2.448 +
   2.449 +static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev)
   2.450 +{
   2.451 +	int i, num_left, num_right;
   2.452 +	struct dsys_event *left, *right, *node = list;
   2.453 +
   2.454 +	if(num_ev < 2) {
   2.455 +		return list;
   2.456 +	}
   2.457 +
   2.458 +	num_left = num_ev / 2;
   2.459 +	num_right = num_ev - num_left;
   2.460 +
   2.461 +	for(i=0; i<num_ev/2; i++) {
   2.462 +		node = node->next;
   2.463 +	}
   2.464 +
   2.465 +	if(node->prev) {
   2.466 +		node->prev->next = 0;
   2.467 +		node->prev = 0;
   2.468 +	}
   2.469 +
   2.470 +	left = sort_evlist(list, num_left);
   2.471 +	right = sort_evlist(node, num_right);
   2.472 +
   2.473 +	return merge_evlists(left, right);
   2.474 +}
   2.475 +
   2.476 +static struct dsys_event *merge_evlists(struct dsys_event *list1, struct dsys_event *list2)
   2.477 +{
   2.478 +	struct dsys_event *head, *tail, *node;
   2.479 +
   2.480 +	if(!list1) {
   2.481 +		return list2;
   2.482 +	}
   2.483 +	if(!list2) {
   2.484 +		return list1;
   2.485 +	}
   2.486 +
   2.487 +	head = tail = 0;
   2.488 +
   2.489 +	while(list1 && list2) {
   2.490 +		if(list1->t0 < list2->t0) {
   2.491 +			node = list1;
   2.492 +			list1 = list1->next;
   2.493 +		} else {
   2.494 +			node = list2;
   2.495 +			list2 = list2->next;
   2.496 +		}
   2.497 +
   2.498 +		node->next = 0;
   2.499 +		node->prev = tail;
   2.500 +
   2.501 +		if(!head) {
   2.502 +			head = node;
   2.503 +		} else {
   2.504 +			tail->next = node;
   2.505 +		}
   2.506 +		tail = node;
   2.507 +	}
   2.508 +
   2.509 +	if(list1) {
   2.510 +		tail->next = list1;
   2.511 +		list1->prev = tail;
   2.512 +	} else if(list2) {
   2.513 +		tail->next = list2;
   2.514 +		list2->prev = tail;
   2.515 +	}
   2.516 +
   2.517 +	return head;
   2.518 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/libs/dsys2/dsys.h	Tue Feb 19 18:17:17 2013 +0200
     3.3 @@ -0,0 +1,71 @@
     3.4 +#ifndef DSYS2_H_
     3.5 +#define DSYS2_H_
     3.6 +
     3.7 +#include <stdio.h>
     3.8 +
     3.9 +struct dsys_demo;
    3.10 +struct dsys_event;
    3.11 +
    3.12 +typedef float demotime_t;
    3.13 +
    3.14 +enum dsys_evtype {
    3.15 +	DSYS_SINGLE,
    3.16 +	DSYS_PERIODIC
    3.17 +};
    3.18 +
    3.19 +#ifdef __cplusplus
    3.20 +extern "C" {
    3.21 +#endif
    3.22 +
    3.23 +struct dsys_demo *dsys_open(const char *fname);
    3.24 +struct dsys_demo *dsys_open_stream(FILE *fp);
    3.25 +void dsys_close(struct dsys_demo *demo);
    3.26 +
    3.27 +void dsys_update(struct dsys_demo *demo, demotime_t tm);
    3.28 +
    3.29 +
    3.30 +void dsys_start(struct dsys_demo *demo);
    3.31 +void dsys_stop(struct dsys_demo *demo);
    3.32 +int dsys_is_running(struct dsys_demo *demo);
    3.33 +
    3.34 +
    3.35 +demotime_t dsys_duration(struct dsys_demo *demo);
    3.36 +demotime_t dsys_time(struct dsys_demo *demo);
    3.37 +float dsys_progress(struct dsys_demo *demo);
    3.38 +
    3.39 +/* seek without continuity */
    3.40 +void dsys_seek(struct dsys_demo *demo, demotime_t tm);
    3.41 +void dsys_seek_norm(struct dsys_demo *demo, float t);
    3.42 +
    3.43 +/* seek by accelerating time */
    3.44 +void dsys_warp(struct dsys_demo *demo, demotime_t tm);
    3.45 +void dsys_warp_norm(struct dsys_demo *demo, float t);
    3.46 +
    3.47 +
    3.48 +/* events */
    3.49 +struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name);
    3.50 +
    3.51 +enum dsys_evtype dsys_event_type(struct dsys_event *ev);
    3.52 +float dsys_event_value(struct dsys_event *ev);
    3.53 +
    3.54 +int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls);
    3.55 +int dsys_event_link(struct dsys_event *ev, float *link);
    3.56 +
    3.57 +/* event evaluators */
    3.58 +float dsys_eval_step(struct dsys_event *ev, demotime_t t);
    3.59 +float dsys_eval_lerp(struct dsys_event *ev, demotime_t t);
    3.60 +float dsys_eval_sigmoid(struct dsys_event *ev, demotime_t t);
    3.61 +
    3.62 +/* time conversion */
    3.63 +demotime_t dsys_sec_to_dtime(float sec);
    3.64 +demotime_t dsys_msec_to_dtime(unsigned long msec);
    3.65 +
    3.66 +float dsys_dtime_to_sec(demotime_t tm);
    3.67 +unsigned long dsys_dtime_to_msec(demotime_t tm);
    3.68 +
    3.69 +#ifdef __cplusplus
    3.70 +}
    3.71 +#endif
    3.72 +
    3.73 +
    3.74 +#endif	/* DSYS2_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/libs/dsys2/dsys_impl.h	Tue Feb 19 18:17:17 2013 +0200
     4.3 @@ -0,0 +1,40 @@
     4.4 +#ifndef DSYS_IMPL_H_
     4.5 +#define DSYS_IMPL_H_
     4.6 +
     4.7 +#include "dsys.h"
     4.8 +
     4.9 +struct dsys_demo {
    4.10 +	demotime_t tm, src_tm, start_tm, stop_tm, duration;
    4.11 +	demotime_t stoppage_tm;
    4.12 +
    4.13 +	struct dsys_event *evlist;
    4.14 +	int num_ev;
    4.15 +
    4.16 +	struct dsys_event *nextev, *active;
    4.17 +
    4.18 +	int running;
    4.19 +};
    4.20 +
    4.21 +struct callback {
    4.22 +	void (*func)(struct dsys_event*, void*);
    4.23 +	void *cls;
    4.24 +
    4.25 +	struct callback *next;
    4.26 +};
    4.27 +
    4.28 +
    4.29 +struct dsys_event {
    4.30 +	enum dsys_evtype type;
    4.31 +
    4.32 +	char *name;
    4.33 +	demotime_t t0, t1;
    4.34 +	float val;
    4.35 +
    4.36 +	float (*eval)(struct dsys_event*, demotime_t);
    4.37 +
    4.38 +	struct callback *cblist;
    4.39 +
    4.40 +	struct dsys_event *next, *prev;
    4.41 +};
    4.42 +
    4.43 +#endif	/* DSYS_IMPL_H_ */
     5.1 --- a/src/mballs.cc	Mon Feb 18 06:53:44 2013 +0200
     5.2 +++ b/src/mballs.cc	Tue Feb 19 18:17:17 2013 +0200
     5.3 @@ -3,6 +3,7 @@
     5.4  #include "mballs.h"
     5.5  #include "metasurf.h"
     5.6  #include "vmath/vmath.h"
     5.7 +#include "dsys.h"
     5.8  
     5.9  struct MetaBall {
    5.10  	Vector3 pos;
    5.11 @@ -14,7 +15,7 @@
    5.12  #define VOL_SZ			2
    5.13  #define MBALL_GRID_SZ	50
    5.14  
    5.15 -static void update();
    5.16 +static void update(float sec);
    5.17  static float calc_field(float x, float y, float z);
    5.18  static float eval(float x, float y, float z);
    5.19  static void vertex(float x, float y, float z);
    5.20 @@ -43,7 +44,7 @@
    5.21  	for(int i=0; i<10; i++) {
    5.22  		MetaBall mb;
    5.23  		mb.orbit = 0.25 * rand() / (float)RAND_MAX + 0.35;
    5.24 -		mb.energy = 0.2 * rand() / (float)RAND_MAX + 0.15;
    5.25 +		mb.energy = 0.1 * rand() / (float)RAND_MAX + 0.15;
    5.26  		mb.phase_offs = rand() / (float)RAND_MAX * M_PI * 2.0;
    5.27  		balls.push_back(mb);
    5.28  	}
    5.29 @@ -51,9 +52,9 @@
    5.30  	return true;
    5.31  }
    5.32  
    5.33 -void mball_render()
    5.34 +void mball_render(float sec)
    5.35  {
    5.36 -	update();
    5.37 +	update(sec);
    5.38  
    5.39  	const float blue[] = {0.4, 0.45, 1.0, 1};
    5.40  	const float dark_red[] = {0.6, 0.2, 0.1, 1};
    5.41 @@ -114,11 +115,8 @@
    5.42  }
    5.43  
    5.44  
    5.45 -static void update()
    5.46 +static void update(float sec)
    5.47  {
    5.48 -	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
    5.49 -	float sec = msec / 1000.0;
    5.50 -
    5.51  	for(size_t i=0; i<balls.size(); i++) {
    5.52  		float t = sec + balls[i].phase_offs;
    5.53  		balls[i].pos.x = cos(t * 1.8) * balls[i].orbit;
     6.1 --- a/src/mballs.h	Mon Feb 18 06:53:44 2013 +0200
     6.2 +++ b/src/mballs.h	Tue Feb 19 18:17:17 2013 +0200
     6.3 @@ -2,6 +2,6 @@
     6.4  #define MBALLS_H_
     6.5  
     6.6  bool mball_init();
     6.7 -void mball_render();
     6.8 +void mball_render(float sec);
     6.9  
    6.10  #endif	/* MBALLS_H_ */
     7.1 --- a/src/udg.cc	Mon Feb 18 06:53:44 2013 +0200
     7.2 +++ b/src/udg.cc	Tue Feb 19 18:17:17 2013 +0200
     7.3 @@ -7,6 +7,7 @@
     7.4  #include "dither_matrix.h"
     7.5  #include "scroller.h"
     7.6  #include "mballs.h"
     7.7 +#include "dsys.h"
     7.8  
     7.9  #define DITHER_SZ		8
    7.10  #define DITHER_LEVELS	16
    7.11 @@ -44,6 +45,8 @@
    7.12  int opt_highres, opt_regular_render;
    7.13  bool opt_autorot = true;
    7.14  
    7.15 +struct dsys_demo *demo;
    7.16 +
    7.17  
    7.18  int main(int argc, char **argv)
    7.19  {
    7.20 @@ -127,6 +130,10 @@
    7.21  		return false;
    7.22  	}
    7.23  
    7.24 +	if(!(demo = dsys_open("demoscript"))) {
    7.25 +		return false;
    7.26 +	}
    7.27 +
    7.28  	glEnable(GL_CULL_FACE);
    7.29  	glEnable(GL_DEPTH_TEST);
    7.30  	glEnable(GL_LIGHTING);
    7.31 @@ -175,9 +182,12 @@
    7.32  	float lcol[] = {1, 1, 1, 1};
    7.33  	float lcol2[] = {0.35, 0.3, 0.15, 1};
    7.34  
    7.35 +	dsys_update(demo, dsys_msec_to_dtime(glutGet(GLUT_ELAPSED_TIME)));
    7.36 +
    7.37 +	float sec = dsys_dtime_to_sec(dsys_time(demo));
    7.38 +	float auto_angle = sec * 10.0;
    7.39 +
    7.40  	int xres, yres;
    7.41 -	float auto_angle = glutGet(GLUT_ELAPSED_TIME) / 100.0;
    7.42 -
    7.43  	if(opt_highres) {
    7.44  		xres = xsz;
    7.45  		yres = ysz;
    7.46 @@ -223,7 +233,7 @@
    7.47  	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
    7.48  
    7.49  	bind_program(phong_prog);
    7.50 -	mball_render();
    7.51 +	mball_render(sec);
    7.52  	bind_program(0);
    7.53  
    7.54