dsys2

diff src/dsys.c @ 7:3258d163cfbc

hurray it seems like working for the most part
author John Tsiombikas <nuclear@siggraph.org>
date Fri, 02 Sep 2011 08:14:40 +0300
parents 80f86f0f67ec
children 4ad7a01c4ff5
line diff
     1.1 --- a/src/dsys.c	Fri Sep 02 04:49:18 2011 +0300
     1.2 +++ b/src/dsys.c	Fri Sep 02 08:14:40 2011 +0300
     1.3 @@ -2,11 +2,14 @@
     1.4  #include <math.h>
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7 +#include <ctype.h>
     1.8  #include <errno.h>
     1.9  #include "dsys2.h"
    1.10  #include "dsys_impl.h"
    1.11  
    1.12  static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname);
    1.13 +static char *strip_ws(char *buf);
    1.14 +static void dbg_print_events(struct dsys_event *ev);
    1.15  
    1.16  static void proc_event(struct dsys_event *ev, demotime_t tm);
    1.17  static void link_callback(struct dsys_event *ev, void *cls);
    1.18 @@ -90,10 +93,12 @@
    1.19  		fname = "<unknown>";
    1.20  	}
    1.21  
    1.22 +	demo->duration = dsys_msec_to_dtime(0);
    1.23 +
    1.24  	while(fgets(buf, sizeof buf, fp)) {
    1.25  		nline++;
    1.26  
    1.27 -		line = buf;/*strip_ws(buf);*/
    1.28 +		line = strip_ws(buf);
    1.29  
    1.30  		if(!line || !*line) {
    1.31  			continue;
    1.32 @@ -123,8 +128,8 @@
    1.33  			perror("read_script: failed to allocate memory for an event\n");
    1.34  			return -1;
    1.35  		}
    1.36 -		ev->t0 = t0;
    1.37 -		ev->t1 = t1;
    1.38 +		ev->t0 = dsys_msec_to_dtime(t0);
    1.39 +		ev->t1 = dsys_msec_to_dtime(t1);
    1.40  
    1.41  		if(!(ev->name = malloc(strlen(tok) + 1))) {
    1.42  			free(ev);
    1.43 @@ -142,13 +147,49 @@
    1.44  		}
    1.45  		demo->evlist = ev;
    1.46  		demo->num_ev++;
    1.47 +
    1.48 +		if(ev->t1 > demo->duration) {
    1.49 +			demo->duration = ev->t1;
    1.50 +		}
    1.51  	}
    1.52  
    1.53  	demo->evlist = sort_evlist(demo->evlist, demo->num_ev);
    1.54  
    1.55 +	dbg_print_events(demo->evlist);
    1.56 +
    1.57  	return 0;
    1.58  }
    1.59  
    1.60 +static char *strip_ws(char *buf)
    1.61 +{
    1.62 +	char *ptr;
    1.63 +
    1.64 +	while(isspace(*buf)) {
    1.65 +		buf++;
    1.66 +	}
    1.67 +
    1.68 +	ptr = buf;
    1.69 +	while(*ptr) {
    1.70 +		if(*ptr == '\n' || *ptr == '\r' || *ptr == '#') {
    1.71 +			*ptr = 0;
    1.72 +			break;
    1.73 +		}
    1.74 +		ptr++;
    1.75 +	}
    1.76 +
    1.77 +	return buf;
    1.78 +}
    1.79 +
    1.80 +static void dbg_print_events(struct dsys_event *ev)
    1.81 +{
    1.82 +	int i;
    1.83 +
    1.84 +	for(i=0; ev; i++) {
    1.85 +		printf("%02d - %s (%f -> %f) [%s]\n", i, ev->eval == dsys_eval_step ? "step" : "lerp",
    1.86 +				ev->t0, ev->t1, ev->name);
    1.87 +		ev = ev->next;
    1.88 +	}
    1.89 +}
    1.90  
    1.91  void dsys_update(struct dsys_demo *demo, demotime_t tm)
    1.92  {
    1.93 @@ -166,17 +207,29 @@
    1.94  
    1.95  	demo->tm = tm - demo->start_tm - demo->stoppage_tm;
    1.96  
    1.97 +	if(demo->tm < 0) {
    1.98 +		demo->tm = 0;
    1.99 +	}
   1.100 +	if(demo->tm > demo->duration) {
   1.101 +		demo->tm = demo->duration;
   1.102 +	}
   1.103 +
   1.104  	while(demo->active->t1 < demo->tm) {
   1.105  		proc_event(demo->active, demo->tm);
   1.106  		demo->active = demo->active->next;
   1.107  	}
   1.108  
   1.109  	ev = demo->active;
   1.110 -	while(ev->t0 <= demo->tm) {
   1.111 +	while(ev && ev->t0 <= demo->tm) {
   1.112  		proc_event(ev, demo->tm);
   1.113  		ev = ev->next;
   1.114  	}
   1.115  	demo->nextev = ev;
   1.116 +
   1.117 +
   1.118 +	if(demo->tm >= demo->duration) {
   1.119 +		dsys_stop(demo);
   1.120 +	}
   1.121  }
   1.122  
   1.123  static void proc_event(struct dsys_event *ev, demotime_t tm)
   1.124 @@ -244,6 +297,13 @@
   1.125  /* seek without continuity */
   1.126  void dsys_seek(struct dsys_demo *demo, demotime_t tm)
   1.127  {
   1.128 +	if(tm < 0) {
   1.129 +		tm = 0;
   1.130 +	}
   1.131 +	if(tm > demo->duration) {
   1.132 +		tm = demo->duration;
   1.133 +	}
   1.134 +
   1.135  	demo->start_tm = demo->src_tm - tm;
   1.136  	demo->stoppage_tm = 0;
   1.137  }
   1.138 @@ -342,9 +402,12 @@
   1.139  	return t >= ev->t1 ? 1.0 : 0.0;
   1.140  }
   1.141  
   1.142 +#define CLAMP(x, low, high)	((x) < (low) ? (low) : ((x) > (high) ? (high) : (x)))
   1.143 +
   1.144  float dsys_eval_lerp(struct dsys_event *ev, demotime_t t)
   1.145  {
   1.146 -	return (t - ev->t0) / (ev->t1 - ev->t0);
   1.147 +	float res = (t - ev->t0) / (ev->t1 - ev->t0);
   1.148 +	return CLAMP(res, 0.0, 1.0);
   1.149  }
   1.150  
   1.151  float dsys_eval_sigmoid(struct dsys_event *ev, demotime_t t)