dsys2
changeset 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 |
files | src/dsys.c test.c testscript.dsys |
diffstat | 3 files changed, 159 insertions(+), 7 deletions(-) [+] |
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)
2.1 --- a/test.c Fri Sep 02 04:49:18 2011 +0300 2.2 +++ b/test.c Fri Sep 02 08:14:40 2011 +0300 2.3 @@ -12,9 +12,11 @@ 2.4 #include "dsys2.h" 2.5 2.6 void disp(void); 2.7 +void draw_progress(float p); 2.8 void draw_teapot(float sec); 2.9 void reshape(int x, int y); 2.10 void keyb(unsigned char key, int x, int y); 2.11 +void skeyb(int key, int x, int y); 2.12 unsigned int get_ticks(void); 2.13 2.14 struct dsys_demo *demo; 2.15 @@ -26,11 +28,12 @@ 2.16 glutInit(&argc, argv); 2.17 glutInitWindowSize(800, 600); 2.18 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 2.19 - glutCreateWindow("foo"); 2.20 + glutCreateWindow("dsys test"); 2.21 2.22 glutDisplayFunc(disp); 2.23 glutReshapeFunc(reshape); 2.24 glutKeyboardFunc(keyb); 2.25 + glutSpecialFunc(skeyb); 2.26 glutIdleFunc(glutPostRedisplay); 2.27 2.28 glEnable(GL_LIGHTING); 2.29 @@ -40,7 +43,7 @@ 2.30 glEnable(GL_CULL_FACE); 2.31 glEnable(GL_DEPTH_TEST); 2.32 2.33 - if(!(demo = dsys_open("/dev/null"))) { 2.34 + if(!(demo = dsys_open("testscript.dsys"))) { 2.35 return 1; 2.36 } 2.37 2.38 @@ -65,9 +68,51 @@ 2.39 2.40 draw_teapot(sec); 2.41 2.42 + draw_progress(dsys_progress(demo)); 2.43 + 2.44 glutSwapBuffers(); 2.45 } 2.46 2.47 +void draw_progress(float p) 2.48 +{ 2.49 + glMatrixMode(GL_MODELVIEW); 2.50 + glPushMatrix(); 2.51 + glLoadIdentity(); 2.52 + glMatrixMode(GL_PROJECTION); 2.53 + glPushMatrix(); 2.54 + glLoadIdentity(); 2.55 + 2.56 + glPushAttrib(GL_ENABLE_BIT); 2.57 + glDisable(GL_LIGHTING); 2.58 + glDisable(GL_DEPTH_TEST); 2.59 + 2.60 + glBegin(GL_QUADS); 2.61 + glColor3f(1, 1, 1); 2.62 + glVertex2f(-0.5, 0.9); 2.63 + glVertex2f(0.5, 0.9); 2.64 + glVertex2f(0.5, 0.98); 2.65 + glVertex2f(-0.5, 0.98); 2.66 + 2.67 + glColor3f(0, 0, 0); 2.68 + glVertex2f(-0.49, 0.91); 2.69 + glVertex2f(0.49, 0.91); 2.70 + glVertex2f(0.49, 0.97); 2.71 + glVertex2f(-0.49, 0.97); 2.72 + 2.73 + glColor3f(1, 0, 0); 2.74 + glVertex2f(-0.48, 0.92); 2.75 + glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.92); 2.76 + glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.96); 2.77 + glVertex2f(-0.48, 0.96); 2.78 + glEnd(); 2.79 + 2.80 + glPopAttrib(); 2.81 + 2.82 + glPopMatrix(); 2.83 + glMatrixMode(GL_MODELVIEW); 2.84 + glPopMatrix(); 2.85 +} 2.86 + 2.87 void draw_teapot(float sec) 2.88 { 2.89 float dcol[] = {0.2, 0.4, 0.8, 1.0}; 2.90 @@ -111,6 +156,43 @@ 2.91 break; 2.92 2.93 default: 2.94 + if(key >= '1' && key <= '9') { 2.95 + float n = (float)(key - '0') / 10.0; 2.96 + printf("seek to: %f percent\n", n * 100.0); 2.97 + dsys_seek_norm(demo, n); 2.98 + } 2.99 + break; 2.100 + } 2.101 +} 2.102 + 2.103 +void skeyb(int key, int x, int y) 2.104 +{ 2.105 + switch(key) { 2.106 + case GLUT_KEY_LEFT: 2.107 + dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5)); 2.108 + break; 2.109 + 2.110 + case GLUT_KEY_RIGHT: 2.111 + dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5)); 2.112 + break; 2.113 + 2.114 + case GLUT_KEY_UP: 2.115 + dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(30)); 2.116 + break; 2.117 + 2.118 + case GLUT_KEY_DOWN: 2.119 + dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(30)); 2.120 + break; 2.121 + 2.122 + case GLUT_KEY_PAGE_UP: 2.123 + dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5 * 60)); 2.124 + break; 2.125 + 2.126 + case GLUT_KEY_PAGE_DOWN: 2.127 + dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5 * 60)); 2.128 + break; 2.129 + 2.130 + default: 2.131 break; 2.132 } 2.133 }