# HG changeset patch # User John Tsiombikas # Date 1314928158 -10800 # Node ID 80f86f0f67ecc60bd6c2d31b5996b719389178d0 # Parent 94ce16dd20c0d684dbf6bc89b841927e3ad6e352 I think I've done most of it... diff -r 94ce16dd20c0 -r 80f86f0f67ec src/dsys.c --- a/src/dsys.c Fri Sep 02 01:57:00 2011 +0300 +++ b/src/dsys.c Fri Sep 02 04:49:18 2011 +0300 @@ -8,6 +8,8 @@ static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname); +static void proc_event(struct dsys_event *ev, demotime_t tm); +static void link_callback(struct dsys_event *ev, void *cls); static void free_event(struct dsys_event *ev); static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev); @@ -131,7 +133,7 @@ } strcpy(ev->name, tok); - ev->eval_func = t0 == t1 ? dsys_eval_step : dsys_eval_lerp; + ev->eval = t0 == t1 ? dsys_eval_step : dsys_eval_lerp; ev->next = demo->evlist; ev->prev = 0; @@ -150,17 +152,46 @@ void dsys_update(struct dsys_demo *demo, demotime_t tm) { + struct dsys_event *ev; + demo->src_tm = tm; if(demo->start_tm == -1) { dsys_start(demo); } - if(demo->running) { - demo->tm = tm - demo->start_tm - demo->stoppage_tm; + if(!demo->running) { + return; /* nothing changes */ } - /* TODO check the events list etc etc */ + demo->tm = tm - demo->start_tm - demo->stoppage_tm; + + while(demo->active->t1 < demo->tm) { + proc_event(demo->active, demo->tm); + demo->active = demo->active->next; + } + + ev = demo->active; + while(ev->t0 <= demo->tm) { + proc_event(ev, demo->tm); + ev = ev->next; + } + demo->nextev = ev; +} + +static void proc_event(struct dsys_event *ev, demotime_t tm) +{ + float val = ev->eval(ev, tm); + + if(ev->val != val) { + struct callback *cb = ev->cblist; + + while(cb) { + cb->func(ev, cb->cls); + cb = cb->next; + } + ev->val = val; + } } void dsys_start(struct dsys_demo *demo) @@ -171,6 +202,7 @@ if(demo->start_tm == -1) { demo->start_tm = demo->src_tm; + demo->nextev = demo->active = demo->evlist; } else { demo->stoppage_tm += demo->src_tm - demo->stop_tm; } @@ -212,7 +244,8 @@ /* seek without continuity */ void dsys_seek(struct dsys_demo *demo, demotime_t tm) { - /* TODO */ + demo->start_tm = demo->src_tm - tm; + demo->stoppage_tm = 0; } void dsys_seek_norm(struct dsys_demo *demo, float t) @@ -221,8 +254,15 @@ } /* seek by accelerating time */ -void dsys_warp(struct dsys_demo *demo, demotime_t tm); -void dsys_warp_norm(struct dsys_demo *demo, float t); +void dsys_warp(struct dsys_demo *demo, demotime_t tm) +{ + fprintf(stderr, "dsys_warp not implemented yet\n"); +} + +void dsys_warp_norm(struct dsys_demo *demo, float t) +{ + dsys_warp(demo, t * demo->duration); +} /* events */ @@ -246,14 +286,33 @@ float dsys_event_value(struct dsys_event *ev) { - return ev->value; + return ev->val; } -void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls) +int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls) { + struct callback *cb; + + if(!(cb = malloc(sizeof *cb))) { + perror("failed to allocate memory"); + return -1; + } + cb->func = func; + cb->cls = cls; + cb->next = ev->cblist; + ev->cblist = cb; + return 0; } -void dsys_event_link(struct dsys_event *ev, float *link); +int dsys_event_link(struct dsys_event *ev, float *link) +{ + return dsys_event_callback(ev, link_callback, link); +} + +static void link_callback(struct dsys_event *ev, void *cls) +{ + *(float*)cls = ev->val; +} /* time conversion */ diff -r 94ce16dd20c0 -r 80f86f0f67ec src/dsys2.h --- a/src/dsys2.h Fri Sep 02 01:57:00 2011 +0300 +++ b/src/dsys2.h Fri Sep 02 04:49:18 2011 +0300 @@ -45,8 +45,8 @@ enum dsys_evtype dsys_event_type(struct dsys_event *ev); float dsys_event_value(struct dsys_event *ev); -void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls); -void dsys_event_link(struct dsys_event *ev, float *link); +int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls); +int dsys_event_link(struct dsys_event *ev, float *link); /* event evaluators */ float dsys_eval_step(struct dsys_event *ev, demotime_t t); @@ -56,11 +56,9 @@ /* time conversion */ demotime_t dsys_sec_to_dtime(float sec); demotime_t dsys_msec_to_dtime(unsigned long msec); -demotime_t dsys_norm_to_dtime(float t); float dsys_dtime_to_sec(demotime_t tm); unsigned long dsys_dtime_to_msec(demotime_t tm); -float dsys_dtime_to_norm(demotime_t tm); #endif /* DSYS2_H_ */ diff -r 94ce16dd20c0 -r 80f86f0f67ec src/dsys_impl.h --- a/src/dsys_impl.h Fri Sep 02 01:57:00 2011 +0300 +++ b/src/dsys_impl.h Fri Sep 02 04:49:18 2011 +0300 @@ -10,6 +10,8 @@ struct dsys_event *evlist; int num_ev; + struct dsys_event *nextev, *active; + int running; }; @@ -28,7 +30,7 @@ demotime_t t0, t1; float val; - float (*eval_func)(struct dsys_event*, demotime_t); + float (*eval)(struct dsys_event*, demotime_t); struct callback *cblist;