# HG changeset patch # User John Tsiombikas # Date 1314940480 -10800 # Node ID 3258d163cfbcad92ca237b24b30d17b73b6b0b28 # Parent 80f86f0f67ecc60bd6c2d31b5996b719389178d0 hurray it seems like working for the most part diff -r 80f86f0f67ec -r 3258d163cfbc src/dsys.c --- a/src/dsys.c Fri Sep 02 04:49:18 2011 +0300 +++ b/src/dsys.c Fri Sep 02 08:14:40 2011 +0300 @@ -2,11 +2,14 @@ #include #include #include +#include #include #include "dsys2.h" #include "dsys_impl.h" static int read_script(struct dsys_demo *demo, FILE *fp, const char *fname); +static char *strip_ws(char *buf); +static void dbg_print_events(struct dsys_event *ev); static void proc_event(struct dsys_event *ev, demotime_t tm); static void link_callback(struct dsys_event *ev, void *cls); @@ -90,10 +93,12 @@ fname = ""; } + demo->duration = dsys_msec_to_dtime(0); + while(fgets(buf, sizeof buf, fp)) { nline++; - line = buf;/*strip_ws(buf);*/ + line = strip_ws(buf); if(!line || !*line) { continue; @@ -123,8 +128,8 @@ perror("read_script: failed to allocate memory for an event\n"); return -1; } - ev->t0 = t0; - ev->t1 = t1; + ev->t0 = dsys_msec_to_dtime(t0); + ev->t1 = dsys_msec_to_dtime(t1); if(!(ev->name = malloc(strlen(tok) + 1))) { free(ev); @@ -142,13 +147,49 @@ } demo->evlist = ev; demo->num_ev++; + + if(ev->t1 > demo->duration) { + demo->duration = ev->t1; + } } demo->evlist = sort_evlist(demo->evlist, demo->num_ev); + dbg_print_events(demo->evlist); + return 0; } +static char *strip_ws(char *buf) +{ + char *ptr; + + while(isspace(*buf)) { + buf++; + } + + ptr = buf; + while(*ptr) { + if(*ptr == '\n' || *ptr == '\r' || *ptr == '#') { + *ptr = 0; + break; + } + ptr++; + } + + return buf; +} + +static void dbg_print_events(struct dsys_event *ev) +{ + int i; + + for(i=0; ev; i++) { + printf("%02d - %s (%f -> %f) [%s]\n", i, ev->eval == dsys_eval_step ? "step" : "lerp", + ev->t0, ev->t1, ev->name); + ev = ev->next; + } +} void dsys_update(struct dsys_demo *demo, demotime_t tm) { @@ -166,17 +207,29 @@ demo->tm = tm - demo->start_tm - demo->stoppage_tm; + if(demo->tm < 0) { + demo->tm = 0; + } + if(demo->tm > demo->duration) { + demo->tm = demo->duration; + } + 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) { + while(ev && ev->t0 <= demo->tm) { proc_event(ev, demo->tm); ev = ev->next; } demo->nextev = ev; + + + if(demo->tm >= demo->duration) { + dsys_stop(demo); + } } static void proc_event(struct dsys_event *ev, demotime_t tm) @@ -244,6 +297,13 @@ /* seek without continuity */ void dsys_seek(struct dsys_demo *demo, demotime_t tm) { + if(tm < 0) { + tm = 0; + } + if(tm > demo->duration) { + tm = demo->duration; + } + demo->start_tm = demo->src_tm - tm; demo->stoppage_tm = 0; } @@ -342,9 +402,12 @@ return t >= ev->t1 ? 1.0 : 0.0; } +#define CLAMP(x, low, high) ((x) < (low) ? (low) : ((x) > (high) ? (high) : (x))) + float dsys_eval_lerp(struct dsys_event *ev, demotime_t t) { - return (t - ev->t0) / (ev->t1 - ev->t0); + float res = (t - ev->t0) / (ev->t1 - ev->t0); + return CLAMP(res, 0.0, 1.0); } float dsys_eval_sigmoid(struct dsys_event *ev, demotime_t t) diff -r 80f86f0f67ec -r 3258d163cfbc test.c --- a/test.c Fri Sep 02 04:49:18 2011 +0300 +++ b/test.c Fri Sep 02 08:14:40 2011 +0300 @@ -12,9 +12,11 @@ #include "dsys2.h" void disp(void); +void draw_progress(float p); void draw_teapot(float sec); void reshape(int x, int y); void keyb(unsigned char key, int x, int y); +void skeyb(int key, int x, int y); unsigned int get_ticks(void); struct dsys_demo *demo; @@ -26,11 +28,12 @@ glutInit(&argc, argv); glutInitWindowSize(800, 600); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); - glutCreateWindow("foo"); + glutCreateWindow("dsys test"); glutDisplayFunc(disp); glutReshapeFunc(reshape); glutKeyboardFunc(keyb); + glutSpecialFunc(skeyb); glutIdleFunc(glutPostRedisplay); glEnable(GL_LIGHTING); @@ -40,7 +43,7 @@ glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); - if(!(demo = dsys_open("/dev/null"))) { + if(!(demo = dsys_open("testscript.dsys"))) { return 1; } @@ -65,9 +68,51 @@ draw_teapot(sec); + draw_progress(dsys_progress(demo)); + glutSwapBuffers(); } +void draw_progress(float p) +{ + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_DEPTH_TEST); + + glBegin(GL_QUADS); + glColor3f(1, 1, 1); + glVertex2f(-0.5, 0.9); + glVertex2f(0.5, 0.9); + glVertex2f(0.5, 0.98); + glVertex2f(-0.5, 0.98); + + glColor3f(0, 0, 0); + glVertex2f(-0.49, 0.91); + glVertex2f(0.49, 0.91); + glVertex2f(0.49, 0.97); + glVertex2f(-0.49, 0.97); + + glColor3f(1, 0, 0); + glVertex2f(-0.48, 0.92); + glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.92); + glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.96); + glVertex2f(-0.48, 0.96); + glEnd(); + + glPopAttrib(); + + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); +} + void draw_teapot(float sec) { float dcol[] = {0.2, 0.4, 0.8, 1.0}; @@ -111,6 +156,43 @@ break; default: + if(key >= '1' && key <= '9') { + float n = (float)(key - '0') / 10.0; + printf("seek to: %f percent\n", n * 100.0); + dsys_seek_norm(demo, n); + } + break; + } +} + +void skeyb(int key, int x, int y) +{ + switch(key) { + case GLUT_KEY_LEFT: + dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5)); + break; + + case GLUT_KEY_RIGHT: + dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5)); + break; + + case GLUT_KEY_UP: + dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(30)); + break; + + case GLUT_KEY_DOWN: + dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(30)); + break; + + case GLUT_KEY_PAGE_UP: + dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5 * 60)); + break; + + case GLUT_KEY_PAGE_DOWN: + dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5 * 60)); + break; + + default: break; } } diff -r 80f86f0f67ec -r 3258d163cfbc testscript.dsys --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testscript.dsys Fri Sep 02 08:14:40 2011 +0300 @@ -0,0 +1,7 @@ +# test + +1000 2000 foo +4000 8000 foolong +500 700 fooshort + +10000 end