dsys2

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