nuclear@2: #include nuclear@2: #include nuclear@8: #include nuclear@2: #include nuclear@2: #include nuclear@2: nuclear@2: #ifndef __APPLE__ nuclear@2: #include nuclear@2: #else nuclear@2: #include nuclear@2: #endif nuclear@2: nuclear@2: #include "dsys2.h" nuclear@2: nuclear@2: void disp(void); nuclear@7: void draw_progress(float p); nuclear@2: void draw_teapot(float sec); nuclear@8: void flash_callback(struct dsys_event *ev, void *cls); nuclear@2: void reshape(int x, int y); nuclear@2: void keyb(unsigned char key, int x, int y); nuclear@7: void skeyb(int key, int x, int y); nuclear@2: unsigned int get_ticks(void); nuclear@2: nuclear@2: struct dsys_demo *demo; nuclear@2: nuclear@8: struct dsys_event *evfoo, *evfoolong, *evfooshort; nuclear@8: nuclear@8: float teapot_color[][4] = { nuclear@8: {0.9, 0.5, 0.3, 1.0}, nuclear@8: {0.3, 0.4, 0.8, 1.0} nuclear@8: }; nuclear@8: nuclear@2: int main(int argc, char **argv) nuclear@2: { nuclear@2: float lpos[] = {-100, 100, 100, 1}; nuclear@2: nuclear@2: glutInit(&argc, argv); nuclear@2: glutInitWindowSize(800, 600); nuclear@2: glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); nuclear@7: glutCreateWindow("dsys test"); nuclear@2: nuclear@2: glutDisplayFunc(disp); nuclear@2: glutReshapeFunc(reshape); nuclear@2: glutKeyboardFunc(keyb); nuclear@7: glutSpecialFunc(skeyb); nuclear@2: glutIdleFunc(glutPostRedisplay); nuclear@2: nuclear@2: glEnable(GL_LIGHTING); nuclear@2: glEnable(GL_LIGHT0); nuclear@2: glLightfv(GL_LIGHT0, GL_POSITION, lpos); nuclear@2: nuclear@2: glEnable(GL_CULL_FACE); nuclear@2: glEnable(GL_DEPTH_TEST); nuclear@2: nuclear@7: if(!(demo = dsys_open("testscript.dsys"))) { nuclear@2: return 1; nuclear@2: } nuclear@2: nuclear@8: evfoo = dsys_event(demo, "foo"); nuclear@8: evfoolong = dsys_event(demo, "foolong"); nuclear@8: evfooshort = dsys_event(demo, "fooshort"); nuclear@8: nuclear@8: dsys_event_callback(evfooshort, flash_callback, 0); nuclear@8: nuclear@2: glutMainLoop(); nuclear@2: return 0; nuclear@2: } nuclear@2: nuclear@2: nuclear@2: void disp(void) nuclear@2: { nuclear@8: int i; nuclear@2: float sec; nuclear@8: static const float trans[][2] = {{-3, 0}, {3, 0}, {0, -3}, {0, 3}}; nuclear@2: nuclear@2: dsys_update(demo, dsys_msec_to_dtime(get_ticks())); nuclear@2: sec = dsys_dtime_to_sec(dsys_time(demo)); nuclear@2: nuclear@2: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); nuclear@2: nuclear@2: glMatrixMode(GL_MODELVIEW); nuclear@2: glLoadIdentity(); nuclear@2: glTranslatef(0, 0, -8); nuclear@2: glRotatef(30.0, 1, 0, 0); nuclear@2: nuclear@2: draw_teapot(sec); nuclear@2: nuclear@8: glEnable(GL_BLEND); nuclear@8: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); nuclear@8: nuclear@8: for(i=0; i<4; i++) { nuclear@8: glPushMatrix(); nuclear@8: glRotatef(-sec * 100.0, 0, 1, 0); nuclear@8: glTranslatef(trans[i][0], 0, trans[i][1]); nuclear@8: glScalef(0.7, 0.7, 0.7); nuclear@8: draw_teapot(sec); nuclear@8: glPopMatrix(); nuclear@8: } nuclear@8: glDisable(GL_BLEND); nuclear@8: nuclear@7: draw_progress(dsys_progress(demo)); nuclear@7: nuclear@2: glutSwapBuffers(); nuclear@2: } nuclear@2: nuclear@7: void draw_progress(float p) nuclear@7: { nuclear@7: glMatrixMode(GL_MODELVIEW); nuclear@7: glPushMatrix(); nuclear@7: glLoadIdentity(); nuclear@7: glMatrixMode(GL_PROJECTION); nuclear@7: glPushMatrix(); nuclear@7: glLoadIdentity(); nuclear@7: nuclear@7: glPushAttrib(GL_ENABLE_BIT); nuclear@7: glDisable(GL_LIGHTING); nuclear@7: glDisable(GL_DEPTH_TEST); nuclear@7: nuclear@7: glBegin(GL_QUADS); nuclear@7: glColor3f(1, 1, 1); nuclear@7: glVertex2f(-0.5, 0.9); nuclear@7: glVertex2f(0.5, 0.9); nuclear@7: glVertex2f(0.5, 0.98); nuclear@7: glVertex2f(-0.5, 0.98); nuclear@7: nuclear@7: glColor3f(0, 0, 0); nuclear@7: glVertex2f(-0.49, 0.91); nuclear@7: glVertex2f(0.49, 0.91); nuclear@7: glVertex2f(0.49, 0.97); nuclear@7: glVertex2f(-0.49, 0.97); nuclear@7: nuclear@7: glColor3f(1, 0, 0); nuclear@7: glVertex2f(-0.48, 0.92); nuclear@7: glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.92); nuclear@7: glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.96); nuclear@7: glVertex2f(-0.48, 0.96); nuclear@7: glEnd(); nuclear@7: nuclear@7: glPopAttrib(); nuclear@7: nuclear@7: glPopMatrix(); nuclear@7: glMatrixMode(GL_MODELVIEW); nuclear@7: glPopMatrix(); nuclear@7: } nuclear@7: nuclear@8: #define LERP(a, b, t) ((a) + ((b) - (a)) * (t)) nuclear@8: nuclear@2: void draw_teapot(float sec) nuclear@2: { nuclear@8: float dcol[4]; nuclear@2: float scol[] = {0.8, 0.8, 0.8, 1.0}; nuclear@8: float t = dsys_event_value(evfoo); nuclear@8: nuclear@8: dcol[0] = LERP(teapot_color[0][0], teapot_color[1][0], t); nuclear@8: dcol[1] = LERP(teapot_color[0][1], teapot_color[1][1], t); nuclear@8: dcol[2] = LERP(teapot_color[0][2], teapot_color[1][2], t); nuclear@8: dcol[3] = dsys_event_value(evfoolong); nuclear@2: nuclear@2: glPushMatrix(); nuclear@2: glRotatef(sec * 100.0, 0, 1, 0); nuclear@2: nuclear@2: glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol); nuclear@2: glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol); nuclear@2: glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0); nuclear@2: nuclear@2: glFrontFace(GL_CW); nuclear@2: glutSolidTeapot(1.0); nuclear@2: glFrontFace(GL_CCW); nuclear@2: nuclear@2: glPopMatrix(); nuclear@2: } nuclear@2: nuclear@8: void flash_callback(struct dsys_event *ev, void *cls) nuclear@8: { nuclear@8: float t = sin(dsys_event_value(ev) * M_PI); nuclear@8: nuclear@8: glClearColor(t, t, t, 1.0); nuclear@8: } nuclear@8: nuclear@2: void reshape(int x, int y) nuclear@2: { nuclear@2: glViewport(0, 0, x, y); nuclear@2: nuclear@2: glMatrixMode(GL_PROJECTION); nuclear@2: glLoadIdentity(); nuclear@2: gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); nuclear@2: } nuclear@2: nuclear@2: void keyb(unsigned char key, int x, int y) nuclear@2: { nuclear@2: switch(key) { nuclear@2: case 27: nuclear@2: exit(0); nuclear@2: nuclear@2: case ' ': nuclear@2: if(dsys_is_running(demo)) { nuclear@2: dsys_stop(demo); nuclear@2: } else { nuclear@2: dsys_start(demo); nuclear@2: } nuclear@2: break; nuclear@2: nuclear@2: default: nuclear@7: if(key >= '1' && key <= '9') { nuclear@7: float n = (float)(key - '0') / 10.0; nuclear@7: printf("seek to: %f percent\n", n * 100.0); nuclear@7: dsys_seek_norm(demo, n); nuclear@7: } nuclear@7: break; nuclear@7: } nuclear@7: } nuclear@7: nuclear@7: void skeyb(int key, int x, int y) nuclear@7: { nuclear@7: switch(key) { nuclear@7: case GLUT_KEY_LEFT: nuclear@7: dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5)); nuclear@7: break; nuclear@7: nuclear@7: case GLUT_KEY_RIGHT: nuclear@7: dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5)); nuclear@7: break; nuclear@7: nuclear@7: case GLUT_KEY_UP: nuclear@7: dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(30)); nuclear@7: break; nuclear@7: nuclear@7: case GLUT_KEY_DOWN: nuclear@7: dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(30)); nuclear@7: break; nuclear@7: nuclear@7: case GLUT_KEY_PAGE_UP: nuclear@7: dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5 * 60)); nuclear@7: break; nuclear@7: nuclear@7: case GLUT_KEY_PAGE_DOWN: nuclear@7: dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5 * 60)); nuclear@7: break; nuclear@7: nuclear@7: default: nuclear@2: break; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: unsigned int get_ticks(void) nuclear@2: { nuclear@2: static struct timeval tv0; nuclear@2: struct timeval tv; nuclear@2: nuclear@2: gettimeofday(&tv, 0); nuclear@2: nuclear@2: if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { nuclear@2: tv0 = tv; nuclear@2: } nuclear@2: return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; nuclear@2: }