Remove allegro driver
[mesa.git] / progs / directfb / df_gears.c
1 /*
2 (c) Copyright 2001 convergence integrated media GmbH.
3 All rights reserved.
4
5 Written by Denis Oliver Kropp <dok@convergence.de> and
6 Andreas Hundt <andi@convergence.de>.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <math.h>
28
29 #include <directfb.h>
30 #include <directfbgl.h>
31
32 #include <GL/gl.h>
33
34
35 /* the super interface */
36 IDirectFB *dfb;
37
38 /* the primary surface (surface of primary layer) */
39 IDirectFBSurface *primary;
40
41 /* the GL context */
42 IDirectFBGL *primary_gl;
43
44 /* our font */
45 IDirectFBFont *font;
46
47 /* event buffer */
48 IDirectFBEventBuffer *events;
49
50 /* macro for a safe call to DirectFB functions */
51 #define DFBCHECK(x...) \
52 { \
53 err = x; \
54 if (err != DFB_OK) { \
55 fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
56 DirectFBErrorFatal( #x, err ); \
57 } \
58 }
59
60 static int screen_width, screen_height;
61
62 static unsigned long T0 = 0;
63 static GLint Frames = 0;
64 static GLfloat fps = 0;
65
66 static inline unsigned long get_millis()
67 {
68 struct timeval tv;
69
70 gettimeofday (&tv, NULL);
71 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
72 }
73
74
75 #ifndef M_PI
76 #define M_PI 3.14159265
77 #endif
78
79 /**
80
81 Draw a gear wheel. You'll probably want to call this function when
82 building a display list since we do a lot of trig here.
83
84 Input: inner_radius - radius of hole at center
85 outer_radius - radius at center of teeth
86 width - width of gear
87 teeth - number of teeth
88 tooth_depth - depth of tooth
89
90 **/
91
92 static void
93 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
94 GLint teeth, GLfloat tooth_depth)
95 {
96 GLint i;
97 GLfloat r0, r1, r2;
98 GLfloat angle, da;
99 GLfloat u, v, len;
100
101 r0 = inner_radius;
102 r1 = outer_radius - tooth_depth / 2.0;
103 r2 = outer_radius + tooth_depth / 2.0;
104
105 da = 2.0 * M_PI / teeth / 4.0;
106
107 glShadeModel(GL_FLAT);
108
109 glNormal3f(0.0, 0.0, 1.0);
110
111 /* draw front face */
112 glBegin(GL_QUAD_STRIP);
113 for (i = 0; i <= teeth; i++) {
114 angle = i * 2.0 * M_PI / teeth;
115 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
116 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
117 if (i < teeth) {
118 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
119 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
120 }
121 }
122 glEnd();
123
124 /* draw front sides of teeth */
125 glBegin(GL_QUADS);
126 da = 2.0 * M_PI / teeth / 4.0;
127 for (i = 0; i < teeth; i++) {
128 angle = i * 2.0 * M_PI / teeth;
129
130 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
131 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
132 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
133 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
134 }
135 glEnd();
136
137 glNormal3f(0.0, 0.0, -1.0);
138
139 /* draw back face */
140 glBegin(GL_QUAD_STRIP);
141 for (i = 0; i <= teeth; i++) {
142 angle = i * 2.0 * M_PI / teeth;
143 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
144 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
145 if (i < teeth) {
146 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
147 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
148 }
149 }
150 glEnd();
151
152 /* draw back sides of teeth */
153 glBegin(GL_QUADS);
154 da = 2.0 * M_PI / teeth / 4.0;
155 for (i = 0; i < teeth; i++) {
156 angle = i * 2.0 * M_PI / teeth;
157
158 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
159 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
160 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
161 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
162 }
163 glEnd();
164
165 /* draw outward faces of teeth */
166 glBegin(GL_QUAD_STRIP);
167 for (i = 0; i < teeth; i++) {
168 angle = i * 2.0 * M_PI / teeth;
169
170 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
171 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
172 u = r2 * cos(angle + da) - r1 * cos(angle);
173 v = r2 * sin(angle + da) - r1 * sin(angle);
174 len = sqrt(u * u + v * v);
175 u /= len;
176 v /= len;
177 glNormal3f(v, -u, 0.0);
178 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
179 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
180 glNormal3f(cos(angle), sin(angle), 0.0);
181 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
182 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
183 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
184 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
185 glNormal3f(v, -u, 0.0);
186 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
187 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
188 glNormal3f(cos(angle), sin(angle), 0.0);
189 }
190
191 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
192 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
193
194 glEnd();
195
196 glShadeModel(GL_SMOOTH);
197
198 /* draw inside radius cylinder */
199 glBegin(GL_QUAD_STRIP);
200 for (i = 0; i <= teeth; i++) {
201 angle = i * 2.0 * M_PI / teeth;
202 glNormal3f(-cos(angle), -sin(angle), 0.0);
203 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
204 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
205 }
206 glEnd();
207
208 }
209
210 static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
211 static GLfloat inc_rotx = 0, inc_roty = 0, inc_rotz = 0;
212 static GLint gear1, gear2, gear3;
213 static GLfloat angle = 0.0;
214
215 static void
216 draw(void)
217 {
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
219
220 glPushMatrix();
221 glRotatef(view_rotx, 1.0, 0.0, 0.0);
222 glRotatef(view_roty, 0.0, 1.0, 0.0);
223 glRotatef(view_rotz, 0.0, 0.0, 1.0);
224
225 glPushMatrix();
226 glTranslatef(-3.0, -2.0, 0.0);
227 glRotatef(angle, 0.0, 0.0, 1.0);
228 glCallList(gear1);
229 glPopMatrix();
230
231 glPushMatrix();
232 glTranslatef(3.1, -2.0, 0.0);
233 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
234 glCallList(gear2);
235 glPopMatrix();
236
237 glPushMatrix();
238 glTranslatef(-3.1, 4.2, 0.0);
239 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
240 glCallList(gear3);
241 glPopMatrix();
242
243 glPopMatrix();
244 }
245
246 /* new window size or exposure */
247 static void
248 reshape(int width, int height)
249 {
250 GLfloat h = (GLfloat) height / (GLfloat) width;
251
252 glViewport(0, 0, (GLint) width, (GLint) height);
253 glMatrixMode(GL_PROJECTION);
254 glLoadIdentity();
255 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
256 glMatrixMode(GL_MODELVIEW);
257 glLoadIdentity();
258 glTranslatef(0.0, 0.0, -40.0);
259 }
260
261 static void
262 init(int argc, char *argv[])
263 {
264 static GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0};
265 static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
266 static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
267 static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
268 GLint i;
269
270 glLightfv(GL_LIGHT0, GL_POSITION, pos);
271 glEnable(GL_CULL_FACE);
272 glEnable(GL_LIGHTING);
273 glEnable(GL_LIGHT0);
274 glEnable(GL_DEPTH_TEST);
275
276 /* make the gears */
277 gear1 = glGenLists(1);
278 glNewList(gear1, GL_COMPILE);
279 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
280 gear(1.0, 4.0, 1.0, 20, 0.7);
281 glEndList();
282
283 gear2 = glGenLists(1);
284 glNewList(gear2, GL_COMPILE);
285 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
286 gear(0.5, 2.0, 2.0, 10, 0.7);
287 glEndList();
288
289 gear3 = glGenLists(1);
290 glNewList(gear3, GL_COMPILE);
291 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
292 gear(1.3, 2.0, 0.5, 10, 0.7);
293 glEndList();
294
295 glEnable(GL_NORMALIZE);
296
297 for ( i=1; i<argc; i++ ) {
298 if (strcmp(argv[i], "-info")==0) {
299 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
300 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
301 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
302 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
303 }
304 }
305 }
306
307 int main( int argc, char *argv[] )
308 {
309 int quit = 0;
310 DFBResult err;
311 DFBSurfaceDescription dsc;
312
313 DFBCHECK(DirectFBInit( &argc, &argv ));
314
315 /* create the super interface */
316 DFBCHECK(DirectFBCreate( &dfb ));
317
318 /* create an event buffer for all devices with these caps */
319 DFBCHECK(dfb->CreateInputEventBuffer( dfb, DICAPS_KEYS | DICAPS_AXES,
320 DFB_FALSE, &events ));
321
322 /* set our cooperative level to DFSCL_FULLSCREEN
323 for exclusive access to the primary layer */
324 dfb->SetCooperativeLevel( dfb, DFSCL_FULLSCREEN );
325
326 /* get the primary surface, i.e. the surface of the
327 primary layer we have exclusive access to */
328 dsc.flags = DSDESC_CAPS;
329 dsc.caps = DSCAPS_PRIMARY | DSCAPS_DOUBLE;
330
331 DFBCHECK(dfb->CreateSurface( dfb, &dsc, &primary ));
332
333 /* get the size of the surface and fill it */
334 DFBCHECK(primary->GetSize( primary, &screen_width, &screen_height ));
335 DFBCHECK(primary->FillRectangle( primary, 0, 0,
336 screen_width, screen_height ));
337 primary->Flip( primary, NULL, 0 );
338
339 /* create the default font and set it */
340 DFBCHECK(dfb->CreateFont( dfb, NULL, NULL, &font ));
341 DFBCHECK(primary->SetFont( primary, font ));
342
343 /* get the GL context */
344 DFBCHECK(primary->GetGL( primary, &primary_gl ));
345
346 DFBCHECK(primary_gl->Lock( primary_gl ));
347
348 init(argc, argv);
349 reshape(screen_width, screen_height);
350
351 DFBCHECK(primary_gl->Unlock( primary_gl ));
352
353 T0 = get_millis();
354
355 while (!quit) {
356 DFBInputEvent evt;
357 unsigned long t;
358
359 DFBCHECK(primary_gl->Lock( primary_gl ));
360
361 draw();
362
363 DFBCHECK(primary_gl->Unlock( primary_gl ));
364
365 if (fps) {
366 char buf[64];
367
368 snprintf(buf, 64, "%4.1f FPS\n", fps);
369
370 primary->SetColor( primary, 0xff, 0, 0, 0xff );
371 primary->DrawString( primary, buf, -1, screen_width - 5, 5, DSTF_TOPRIGHT );
372 }
373
374 primary->Flip( primary, NULL, 0 );
375 Frames++;
376
377
378 t = get_millis();
379 if (t - T0 >= 2000) {
380 GLfloat seconds = (t - T0) / 1000.0;
381
382 fps = Frames / seconds;
383
384 T0 = t;
385 Frames = 0;
386 }
387
388
389 while (events->GetEvent( events, DFB_EVENT(&evt) ) == DFB_OK) {
390 switch (evt.type) {
391 case DIET_KEYPRESS:
392 switch (evt.key_symbol) {
393 case DIKS_ESCAPE:
394 quit = 1;
395 break;
396 case DIKS_CURSOR_UP:
397 inc_rotx = 5.0;
398 break;
399 case DIKS_CURSOR_DOWN:
400 inc_rotx = -5.0;
401 break;
402 case DIKS_CURSOR_LEFT:
403 inc_roty = 5.0;
404 break;
405 case DIKS_CURSOR_RIGHT:
406 inc_roty = -5.0;
407 break;
408 case DIKS_PAGE_UP:
409 inc_rotz = 5.0;
410 break;
411 case DIKS_PAGE_DOWN:
412 inc_rotz = -5.0;
413 break;
414 default:
415 ;
416 }
417 break;
418 case DIET_KEYRELEASE:
419 switch (evt.key_symbol) {
420 case DIKS_CURSOR_UP:
421 inc_rotx = 0;
422 break;
423 case DIKS_CURSOR_DOWN:
424 inc_rotx = 0;
425 break;
426 case DIKS_CURSOR_LEFT:
427 inc_roty = 0;
428 break;
429 case DIKS_CURSOR_RIGHT:
430 inc_roty = 0;
431 break;
432 case DIKS_PAGE_UP:
433 inc_rotz = 0;
434 break;
435 case DIKS_PAGE_DOWN:
436 inc_rotz = 0;
437 break;
438 default:
439 ;
440 }
441 break;
442 case DIET_AXISMOTION:
443 if (evt.flags & DIEF_AXISREL) {
444 switch (evt.axis) {
445 case DIAI_X:
446 view_roty += evt.axisrel / 2.0;
447 break;
448 case DIAI_Y:
449 view_rotx += evt.axisrel / 2.0;
450 break;
451 case DIAI_Z:
452 view_rotz += evt.axisrel / 2.0;
453 break;
454 default:
455 ;
456 }
457 }
458 break;
459 default:
460 ;
461 }
462 }
463
464 angle += 2.0;
465
466 view_rotx += inc_rotx;
467 view_roty += inc_roty;
468 view_rotz += inc_rotz;
469 }
470
471 /* release our interfaces to shutdown DirectFB */
472 primary_gl->Release( primary_gl );
473 primary->Release( primary );
474 font->Release( font );
475 events->Release( events );
476 dfb->Release( dfb );
477
478 return 0;
479 }
480