dsys2

annotate test.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 1705e550bd91
children 4ad7a01c4ff5
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@2 2 #include <stdlib.h>
nuclear@2 3 #include <time.h>
nuclear@2 4 #include <sys/time.h>
nuclear@2 5
nuclear@2 6 #ifndef __APPLE__
nuclear@2 7 #include <GL/glut.h>
nuclear@2 8 #else
nuclear@2 9 #include <GLUT/glut.h>
nuclear@2 10 #endif
nuclear@2 11
nuclear@2 12 #include "dsys2.h"
nuclear@2 13
nuclear@2 14 void disp(void);
nuclear@7 15 void draw_progress(float p);
nuclear@2 16 void draw_teapot(float sec);
nuclear@2 17 void reshape(int x, int y);
nuclear@2 18 void keyb(unsigned char key, int x, int y);
nuclear@7 19 void skeyb(int key, int x, int y);
nuclear@2 20 unsigned int get_ticks(void);
nuclear@2 21
nuclear@2 22 struct dsys_demo *demo;
nuclear@2 23
nuclear@2 24 int main(int argc, char **argv)
nuclear@2 25 {
nuclear@2 26 float lpos[] = {-100, 100, 100, 1};
nuclear@2 27
nuclear@2 28 glutInit(&argc, argv);
nuclear@2 29 glutInitWindowSize(800, 600);
nuclear@2 30 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@7 31 glutCreateWindow("dsys test");
nuclear@2 32
nuclear@2 33 glutDisplayFunc(disp);
nuclear@2 34 glutReshapeFunc(reshape);
nuclear@2 35 glutKeyboardFunc(keyb);
nuclear@7 36 glutSpecialFunc(skeyb);
nuclear@2 37 glutIdleFunc(glutPostRedisplay);
nuclear@2 38
nuclear@2 39 glEnable(GL_LIGHTING);
nuclear@2 40 glEnable(GL_LIGHT0);
nuclear@2 41 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@2 42
nuclear@2 43 glEnable(GL_CULL_FACE);
nuclear@2 44 glEnable(GL_DEPTH_TEST);
nuclear@2 45
nuclear@7 46 if(!(demo = dsys_open("testscript.dsys"))) {
nuclear@2 47 return 1;
nuclear@2 48 }
nuclear@2 49
nuclear@2 50 glutMainLoop();
nuclear@2 51 return 0;
nuclear@2 52 }
nuclear@2 53
nuclear@2 54
nuclear@2 55 void disp(void)
nuclear@2 56 {
nuclear@2 57 float sec;
nuclear@2 58
nuclear@2 59 dsys_update(demo, dsys_msec_to_dtime(get_ticks()));
nuclear@2 60 sec = dsys_dtime_to_sec(dsys_time(demo));
nuclear@2 61
nuclear@2 62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 63
nuclear@2 64 glMatrixMode(GL_MODELVIEW);
nuclear@2 65 glLoadIdentity();
nuclear@2 66 glTranslatef(0, 0, -8);
nuclear@2 67 glRotatef(30.0, 1, 0, 0);
nuclear@2 68
nuclear@2 69 draw_teapot(sec);
nuclear@2 70
nuclear@7 71 draw_progress(dsys_progress(demo));
nuclear@7 72
nuclear@2 73 glutSwapBuffers();
nuclear@2 74 }
nuclear@2 75
nuclear@7 76 void draw_progress(float p)
nuclear@7 77 {
nuclear@7 78 glMatrixMode(GL_MODELVIEW);
nuclear@7 79 glPushMatrix();
nuclear@7 80 glLoadIdentity();
nuclear@7 81 glMatrixMode(GL_PROJECTION);
nuclear@7 82 glPushMatrix();
nuclear@7 83 glLoadIdentity();
nuclear@7 84
nuclear@7 85 glPushAttrib(GL_ENABLE_BIT);
nuclear@7 86 glDisable(GL_LIGHTING);
nuclear@7 87 glDisable(GL_DEPTH_TEST);
nuclear@7 88
nuclear@7 89 glBegin(GL_QUADS);
nuclear@7 90 glColor3f(1, 1, 1);
nuclear@7 91 glVertex2f(-0.5, 0.9);
nuclear@7 92 glVertex2f(0.5, 0.9);
nuclear@7 93 glVertex2f(0.5, 0.98);
nuclear@7 94 glVertex2f(-0.5, 0.98);
nuclear@7 95
nuclear@7 96 glColor3f(0, 0, 0);
nuclear@7 97 glVertex2f(-0.49, 0.91);
nuclear@7 98 glVertex2f(0.49, 0.91);
nuclear@7 99 glVertex2f(0.49, 0.97);
nuclear@7 100 glVertex2f(-0.49, 0.97);
nuclear@7 101
nuclear@7 102 glColor3f(1, 0, 0);
nuclear@7 103 glVertex2f(-0.48, 0.92);
nuclear@7 104 glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.92);
nuclear@7 105 glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.96);
nuclear@7 106 glVertex2f(-0.48, 0.96);
nuclear@7 107 glEnd();
nuclear@7 108
nuclear@7 109 glPopAttrib();
nuclear@7 110
nuclear@7 111 glPopMatrix();
nuclear@7 112 glMatrixMode(GL_MODELVIEW);
nuclear@7 113 glPopMatrix();
nuclear@7 114 }
nuclear@7 115
nuclear@2 116 void draw_teapot(float sec)
nuclear@2 117 {
nuclear@2 118 float dcol[] = {0.2, 0.4, 0.8, 1.0};
nuclear@2 119 float scol[] = {0.8, 0.8, 0.8, 1.0};
nuclear@2 120
nuclear@2 121 glPushMatrix();
nuclear@2 122 glRotatef(sec * 100.0, 0, 1, 0);
nuclear@2 123
nuclear@2 124 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
nuclear@2 125 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
nuclear@2 126 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
nuclear@2 127
nuclear@2 128 glFrontFace(GL_CW);
nuclear@2 129 glutSolidTeapot(1.0);
nuclear@2 130 glFrontFace(GL_CCW);
nuclear@2 131
nuclear@2 132 glPopMatrix();
nuclear@2 133 }
nuclear@2 134
nuclear@2 135 void reshape(int x, int y)
nuclear@2 136 {
nuclear@2 137 glViewport(0, 0, x, y);
nuclear@2 138
nuclear@2 139 glMatrixMode(GL_PROJECTION);
nuclear@2 140 glLoadIdentity();
nuclear@2 141 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
nuclear@2 142 }
nuclear@2 143
nuclear@2 144 void keyb(unsigned char key, int x, int y)
nuclear@2 145 {
nuclear@2 146 switch(key) {
nuclear@2 147 case 27:
nuclear@2 148 exit(0);
nuclear@2 149
nuclear@2 150 case ' ':
nuclear@2 151 if(dsys_is_running(demo)) {
nuclear@2 152 dsys_stop(demo);
nuclear@2 153 } else {
nuclear@2 154 dsys_start(demo);
nuclear@2 155 }
nuclear@2 156 break;
nuclear@2 157
nuclear@2 158 default:
nuclear@7 159 if(key >= '1' && key <= '9') {
nuclear@7 160 float n = (float)(key - '0') / 10.0;
nuclear@7 161 printf("seek to: %f percent\n", n * 100.0);
nuclear@7 162 dsys_seek_norm(demo, n);
nuclear@7 163 }
nuclear@7 164 break;
nuclear@7 165 }
nuclear@7 166 }
nuclear@7 167
nuclear@7 168 void skeyb(int key, int x, int y)
nuclear@7 169 {
nuclear@7 170 switch(key) {
nuclear@7 171 case GLUT_KEY_LEFT:
nuclear@7 172 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5));
nuclear@7 173 break;
nuclear@7 174
nuclear@7 175 case GLUT_KEY_RIGHT:
nuclear@7 176 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5));
nuclear@7 177 break;
nuclear@7 178
nuclear@7 179 case GLUT_KEY_UP:
nuclear@7 180 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(30));
nuclear@7 181 break;
nuclear@7 182
nuclear@7 183 case GLUT_KEY_DOWN:
nuclear@7 184 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(30));
nuclear@7 185 break;
nuclear@7 186
nuclear@7 187 case GLUT_KEY_PAGE_UP:
nuclear@7 188 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5 * 60));
nuclear@7 189 break;
nuclear@7 190
nuclear@7 191 case GLUT_KEY_PAGE_DOWN:
nuclear@7 192 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5 * 60));
nuclear@7 193 break;
nuclear@7 194
nuclear@7 195 default:
nuclear@2 196 break;
nuclear@2 197 }
nuclear@2 198 }
nuclear@2 199
nuclear@2 200 unsigned int get_ticks(void)
nuclear@2 201 {
nuclear@2 202 static struct timeval tv0;
nuclear@2 203 struct timeval tv;
nuclear@2 204
nuclear@2 205 gettimeofday(&tv, 0);
nuclear@2 206
nuclear@2 207 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
nuclear@2 208 tv0 = tv;
nuclear@2 209 }
nuclear@2 210 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
nuclear@2 211 }