dsys2

changeset 6:80f86f0f67ec

I think I've done most of it...
author John Tsiombikas <nuclear@siggraph.org>
date Fri, 02 Sep 2011 04:49:18 +0300
parents 94ce16dd20c0
children 3258d163cfbc
files src/dsys.c src/dsys2.h src/dsys_impl.h
diffstat 3 files changed, 74 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/src/dsys.c	Fri Sep 02 01:57:00 2011 +0300
     1.2 +++ b/src/dsys.c	Fri Sep 02 04:49:18 2011 +0300
     1.3 @@ -8,6 +8,8 @@
     1.4  
     1.5  static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname);
     1.6  
     1.7 +static void proc_event(struct dsys_event *ev, demotime_t tm);
     1.8 +static void link_callback(struct dsys_event *ev, void *cls);
     1.9  static void free_event(struct dsys_event *ev);
    1.10  
    1.11  static struct dsys_event *sort_evlist(struct dsys_event *list, int num_ev);
    1.12 @@ -131,7 +133,7 @@
    1.13  		}
    1.14  		strcpy(ev->name, tok);
    1.15  
    1.16 -		ev->eval_func = t0 == t1 ? dsys_eval_step : dsys_eval_lerp;
    1.17 +		ev->eval = t0 == t1 ? dsys_eval_step : dsys_eval_lerp;
    1.18  
    1.19  		ev->next = demo->evlist;
    1.20  		ev->prev = 0;
    1.21 @@ -150,17 +152,46 @@
    1.22  
    1.23  void dsys_update(struct dsys_demo *demo, demotime_t tm)
    1.24  {
    1.25 +	struct dsys_event *ev;
    1.26 +
    1.27  	demo->src_tm = tm;
    1.28  
    1.29  	if(demo->start_tm == -1) {
    1.30  		dsys_start(demo);
    1.31  	}
    1.32  
    1.33 -	if(demo->running) {
    1.34 -		demo->tm = tm - demo->start_tm - demo->stoppage_tm;
    1.35 +	if(!demo->running) {
    1.36 +		return;	/* nothing changes */
    1.37  	}
    1.38  
    1.39 -	/* TODO check the events list etc etc */
    1.40 +	demo->tm = tm - demo->start_tm - demo->stoppage_tm;
    1.41 +
    1.42 +	while(demo->active->t1 < demo->tm) {
    1.43 +		proc_event(demo->active, demo->tm);
    1.44 +		demo->active = demo->active->next;
    1.45 +	}
    1.46 +
    1.47 +	ev = demo->active;
    1.48 +	while(ev->t0 <= demo->tm) {
    1.49 +		proc_event(ev, demo->tm);
    1.50 +		ev = ev->next;
    1.51 +	}
    1.52 +	demo->nextev = ev;
    1.53 +}
    1.54 +
    1.55 +static void proc_event(struct dsys_event *ev, demotime_t tm)
    1.56 +{
    1.57 +	float val = ev->eval(ev, tm);
    1.58 +
    1.59 +	if(ev->val != val) {
    1.60 +		struct callback *cb = ev->cblist;
    1.61 +
    1.62 +		while(cb) {
    1.63 +			cb->func(ev, cb->cls);
    1.64 +			cb = cb->next;
    1.65 +		}
    1.66 +		ev->val = val;
    1.67 +	}
    1.68  }
    1.69  
    1.70  void dsys_start(struct dsys_demo *demo)
    1.71 @@ -171,6 +202,7 @@
    1.72  
    1.73  	if(demo->start_tm == -1) {
    1.74  		demo->start_tm = demo->src_tm;
    1.75 +		demo->nextev = demo->active = demo->evlist;
    1.76  	} else {
    1.77  		demo->stoppage_tm += demo->src_tm - demo->stop_tm;
    1.78  	}
    1.79 @@ -212,7 +244,8 @@
    1.80  /* seek without continuity */
    1.81  void dsys_seek(struct dsys_demo *demo, demotime_t tm)
    1.82  {
    1.83 -	/* TODO */
    1.84 +	demo->start_tm = demo->src_tm - tm;
    1.85 +	demo->stoppage_tm = 0;
    1.86  }
    1.87  
    1.88  void dsys_seek_norm(struct dsys_demo *demo, float t)
    1.89 @@ -221,8 +254,15 @@
    1.90  }
    1.91  
    1.92  /* seek by accelerating time */
    1.93 -void dsys_warp(struct dsys_demo *demo, demotime_t tm);
    1.94 -void dsys_warp_norm(struct dsys_demo *demo, float t);
    1.95 +void dsys_warp(struct dsys_demo *demo, demotime_t tm)
    1.96 +{
    1.97 +	fprintf(stderr, "dsys_warp not implemented yet\n");
    1.98 +}
    1.99 +
   1.100 +void dsys_warp_norm(struct dsys_demo *demo, float t)
   1.101 +{
   1.102 +	dsys_warp(demo, t * demo->duration);
   1.103 +}
   1.104  
   1.105  
   1.106  /* events */
   1.107 @@ -246,14 +286,33 @@
   1.108  
   1.109  float dsys_event_value(struct dsys_event *ev)
   1.110  {
   1.111 -	return ev->value;
   1.112 +	return ev->val;
   1.113  }
   1.114  
   1.115 -void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls)
   1.116 +int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls)
   1.117  {
   1.118 +	struct callback *cb;
   1.119 +
   1.120 +	if(!(cb = malloc(sizeof *cb))) {
   1.121 +		perror("failed to allocate memory");
   1.122 +		return -1;
   1.123 +	}
   1.124 +	cb->func = func;
   1.125 +	cb->cls = cls;
   1.126 +	cb->next = ev->cblist;
   1.127 +	ev->cblist = cb;
   1.128 +	return 0;
   1.129  }
   1.130  
   1.131 -void dsys_event_link(struct dsys_event *ev, float *link);
   1.132 +int dsys_event_link(struct dsys_event *ev, float *link)
   1.133 +{
   1.134 +	return dsys_event_callback(ev, link_callback, link);
   1.135 +}
   1.136 +
   1.137 +static void link_callback(struct dsys_event *ev, void *cls)
   1.138 +{
   1.139 +	*(float*)cls = ev->val;
   1.140 +}
   1.141  
   1.142  
   1.143  /* time conversion */
     2.1 --- a/src/dsys2.h	Fri Sep 02 01:57:00 2011 +0300
     2.2 +++ b/src/dsys2.h	Fri Sep 02 04:49:18 2011 +0300
     2.3 @@ -45,8 +45,8 @@
     2.4  enum dsys_evtype dsys_event_type(struct dsys_event *ev);
     2.5  float dsys_event_value(struct dsys_event *ev);
     2.6  
     2.7 -void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls);
     2.8 -void dsys_event_link(struct dsys_event *ev, float *link);
     2.9 +int dsys_event_callback(struct dsys_event *ev, void (*func)(struct dsys_event*, void*), void *cls);
    2.10 +int dsys_event_link(struct dsys_event *ev, float *link);
    2.11  
    2.12  /* event evaluators */
    2.13  float dsys_eval_step(struct dsys_event *ev, demotime_t t);
    2.14 @@ -56,11 +56,9 @@
    2.15  /* time conversion */
    2.16  demotime_t dsys_sec_to_dtime(float sec);
    2.17  demotime_t dsys_msec_to_dtime(unsigned long msec);
    2.18 -demotime_t dsys_norm_to_dtime(float t);
    2.19  
    2.20  float dsys_dtime_to_sec(demotime_t tm);
    2.21  unsigned long dsys_dtime_to_msec(demotime_t tm);
    2.22 -float dsys_dtime_to_norm(demotime_t tm);
    2.23  
    2.24  
    2.25  #endif	/* DSYS2_H_ */
     3.1 --- a/src/dsys_impl.h	Fri Sep 02 01:57:00 2011 +0300
     3.2 +++ b/src/dsys_impl.h	Fri Sep 02 04:49:18 2011 +0300
     3.3 @@ -10,6 +10,8 @@
     3.4  	struct dsys_event *evlist;
     3.5  	int num_ev;
     3.6  
     3.7 +	struct dsys_event *nextev, *active;
     3.8 +
     3.9  	int running;
    3.10  };
    3.11  
    3.12 @@ -28,7 +30,7 @@
    3.13  	demotime_t t0, t1;
    3.14  	float val;
    3.15  
    3.16 -	float (*eval_func)(struct dsys_event*, demotime_t);
    3.17 +	float (*eval)(struct dsys_event*, demotime_t);
    3.18  
    3.19  	struct callback *cblist;
    3.20