dsys2

view test.c @ 14:66d4fe7a8613

test.c included dsys2.h, but I've dropped the 2 at some point
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Oct 2011 09:46:49 +0300
parents 4ad7a01c4ff5
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <time.h>
5 #include <sys/time.h>
7 #ifndef __APPLE__
8 #include <GL/glut.h>
9 #else
10 #include <GLUT/glut.h>
11 #endif
13 #include "dsys.h"
15 void disp(void);
16 void draw_progress(float p);
17 void draw_teapot(float sec);
18 void flash_callback(struct dsys_event *ev, void *cls);
19 void reshape(int x, int y);
20 void keyb(unsigned char key, int x, int y);
21 void skeyb(int key, int x, int y);
22 unsigned int get_ticks(void);
24 struct dsys_demo *demo;
26 struct dsys_event *evfoo, *evfoolong, *evfooshort;
28 float teapot_color[][4] = {
29 {0.9, 0.5, 0.3, 1.0},
30 {0.3, 0.4, 0.8, 1.0}
31 };
33 int main(int argc, char **argv)
34 {
35 float lpos[] = {-100, 100, 100, 1};
37 glutInit(&argc, argv);
38 glutInitWindowSize(800, 600);
39 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
40 glutCreateWindow("dsys test");
42 glutDisplayFunc(disp);
43 glutReshapeFunc(reshape);
44 glutKeyboardFunc(keyb);
45 glutSpecialFunc(skeyb);
46 glutIdleFunc(glutPostRedisplay);
48 glEnable(GL_LIGHTING);
49 glEnable(GL_LIGHT0);
50 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
52 glEnable(GL_CULL_FACE);
53 glEnable(GL_DEPTH_TEST);
55 if(!(demo = dsys_open("testscript.dsys"))) {
56 return 1;
57 }
59 evfoo = dsys_event(demo, "foo");
60 evfoolong = dsys_event(demo, "foolong");
61 evfooshort = dsys_event(demo, "fooshort");
63 dsys_event_callback(evfooshort, flash_callback, 0);
65 glutMainLoop();
66 return 0;
67 }
70 void disp(void)
71 {
72 int i;
73 float sec;
74 static const float trans[][2] = {{-3, 0}, {3, 0}, {0, -3}, {0, 3}};
76 dsys_update(demo, dsys_msec_to_dtime(get_ticks()));
77 sec = dsys_dtime_to_sec(dsys_time(demo));
79 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
81 glMatrixMode(GL_MODELVIEW);
82 glLoadIdentity();
83 glTranslatef(0, 0, -8);
84 glRotatef(30.0, 1, 0, 0);
86 draw_teapot(sec);
88 glEnable(GL_BLEND);
89 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
91 for(i=0; i<4; i++) {
92 glPushMatrix();
93 glRotatef(-sec * 100.0, 0, 1, 0);
94 glTranslatef(trans[i][0], 0, trans[i][1]);
95 glScalef(0.7, 0.7, 0.7);
96 draw_teapot(sec);
97 glPopMatrix();
98 }
99 glDisable(GL_BLEND);
101 draw_progress(dsys_progress(demo));
103 glutSwapBuffers();
104 }
106 void draw_progress(float p)
107 {
108 glMatrixMode(GL_MODELVIEW);
109 glPushMatrix();
110 glLoadIdentity();
111 glMatrixMode(GL_PROJECTION);
112 glPushMatrix();
113 glLoadIdentity();
115 glPushAttrib(GL_ENABLE_BIT);
116 glDisable(GL_LIGHTING);
117 glDisable(GL_DEPTH_TEST);
119 glBegin(GL_QUADS);
120 glColor3f(1, 1, 1);
121 glVertex2f(-0.5, 0.9);
122 glVertex2f(0.5, 0.9);
123 glVertex2f(0.5, 0.98);
124 glVertex2f(-0.5, 0.98);
126 glColor3f(0, 0, 0);
127 glVertex2f(-0.49, 0.91);
128 glVertex2f(0.49, 0.91);
129 glVertex2f(0.49, 0.97);
130 glVertex2f(-0.49, 0.97);
132 glColor3f(1, 0, 0);
133 glVertex2f(-0.48, 0.92);
134 glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.92);
135 glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.96);
136 glVertex2f(-0.48, 0.96);
137 glEnd();
139 glPopAttrib();
141 glPopMatrix();
142 glMatrixMode(GL_MODELVIEW);
143 glPopMatrix();
144 }
146 #define LERP(a, b, t) ((a) + ((b) - (a)) * (t))
148 void draw_teapot(float sec)
149 {
150 float dcol[4];
151 float scol[] = {0.8, 0.8, 0.8, 1.0};
152 float t = dsys_event_value(evfoo);
154 dcol[0] = LERP(teapot_color[0][0], teapot_color[1][0], t);
155 dcol[1] = LERP(teapot_color[0][1], teapot_color[1][1], t);
156 dcol[2] = LERP(teapot_color[0][2], teapot_color[1][2], t);
157 dcol[3] = dsys_event_value(evfoolong);
159 glPushMatrix();
160 glRotatef(sec * 100.0, 0, 1, 0);
162 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
163 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
164 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
166 glFrontFace(GL_CW);
167 glutSolidTeapot(1.0);
168 glFrontFace(GL_CCW);
170 glPopMatrix();
171 }
173 void flash_callback(struct dsys_event *ev, void *cls)
174 {
175 float t = sin(dsys_event_value(ev) * M_PI);
177 glClearColor(t, t, t, 1.0);
178 }
180 void reshape(int x, int y)
181 {
182 glViewport(0, 0, x, y);
184 glMatrixMode(GL_PROJECTION);
185 glLoadIdentity();
186 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
187 }
189 void keyb(unsigned char key, int x, int y)
190 {
191 switch(key) {
192 case 27:
193 exit(0);
195 case ' ':
196 if(dsys_is_running(demo)) {
197 dsys_stop(demo);
198 } else {
199 dsys_start(demo);
200 }
201 break;
203 default:
204 if(key >= '1' && key <= '9') {
205 float n = (float)(key - '0') / 10.0;
206 printf("seek to: %f percent\n", n * 100.0);
207 dsys_seek_norm(demo, n);
208 }
209 break;
210 }
211 }
213 void skeyb(int key, int x, int y)
214 {
215 switch(key) {
216 case GLUT_KEY_LEFT:
217 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5));
218 break;
220 case GLUT_KEY_RIGHT:
221 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5));
222 break;
224 case GLUT_KEY_UP:
225 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(30));
226 break;
228 case GLUT_KEY_DOWN:
229 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(30));
230 break;
232 case GLUT_KEY_PAGE_UP:
233 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5 * 60));
234 break;
236 case GLUT_KEY_PAGE_DOWN:
237 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5 * 60));
238 break;
240 default:
241 break;
242 }
243 }
245 unsigned int get_ticks(void)
246 {
247 static struct timeval tv0;
248 struct timeval tv;
250 gettimeofday(&tv, 0);
252 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
253 tv0 = tv;
254 }
255 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
256 }