ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.1
Committed: Sat Oct 2 12:10:40 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5
6 #include "SDL.h"
7
8 #include "SDL_opengl.h"
9
10 /* Undefine this if you want a flat cube instead of a rainbow cube */
11 #define SHADED_CUBE
12
13 /* Define this to be the name of the logo image to use with -logo */
14 #define LOGO_FILE "icon.bmp"
15
16 /* The SDL_OPENGLBLIT interface is deprecated.
17 The code is still available for benchmark purposes though.
18 */
19
20 static SDL_bool USE_DEPRECATED_OPENGLBLIT = SDL_FALSE;
21
22 static SDL_Surface *global_image = NULL;
23 static GLuint global_texture = 0;
24
25 /**********************************************************************/
26
27 void
28 HotKey_ToggleFullScreen (void)
29 {
30 SDL_Surface *screen;
31
32 screen = SDL_GetVideoSurface ();
33 if (SDL_WM_ToggleFullScreen (screen))
34 {
35 printf ("Toggled fullscreen mode - now %s\n",
36 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
37 }
38 else
39 {
40 printf ("Unable to toggle fullscreen mode\n");
41 }
42 }
43
44 void
45 HotKey_ToggleGrab (void)
46 {
47 SDL_GrabMode mode;
48
49 printf ("Ctrl-G: toggling input grab!\n");
50 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
51 if (mode == SDL_GRAB_ON)
52 {
53 printf ("Grab was on\n");
54 }
55 else
56 {
57 printf ("Grab was off\n");
58 }
59
60 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
61 if (mode == SDL_GRAB_ON)
62 {
63 printf ("Grab is now on\n");
64 }
65 else
66 {
67 printf ("Grab is now off\n");
68 }
69 }
70
71 void
72 HotKey_Iconify (void)
73 {
74 printf ("Ctrl-Z: iconifying window!\n");
75 SDL_WM_IconifyWindow ();
76 }
77
78 int
79 HandleEvent (SDL_Event * event)
80 {
81 int done;
82
83 done = 0;
84 switch (event->type)
85 {
86 case SDL_ACTIVEEVENT:
87 /* See what happened */
88 printf ("app %s ", event->active.gain ? "gained" : "lost");
89 if (event->active.state & SDL_APPACTIVE)
90 {
91 printf ("active ");
92 }
93 else if (event->active.state & SDL_APPMOUSEFOCUS)
94 {
95 printf ("mouse ");
96 }
97 else if (event->active.state & SDL_APPINPUTFOCUS)
98 {
99 printf ("input ");
100 }
101
102 printf ("focus\n");
103 break;
104
105 case SDL_KEYDOWN:
106 if (event->key.keysym.sym == SDLK_ESCAPE)
107 {
108 done = 1;
109 }
110
111 if ((event->key.keysym.sym == SDLK_g) &&
112 (event->key.keysym.mod & KMOD_CTRL))
113 {
114 HotKey_ToggleGrab ();
115 }
116
117 if ((event->key.keysym.sym == SDLK_z) &&
118 (event->key.keysym.mod & KMOD_CTRL))
119 {
120 HotKey_Iconify ();
121 }
122
123 if ((event->key.keysym.sym == SDLK_RETURN) &&
124 (event->key.keysym.mod & KMOD_ALT))
125 {
126 HotKey_ToggleFullScreen ();
127 }
128
129 printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym));
130 break;
131
132 case SDL_QUIT:
133 done = 1;
134 break;
135 }
136
137 return (done);
138 }
139
140 void
141 SDL_GL_Enter2DMode ()
142 {
143 SDL_Surface *screen = SDL_GetVideoSurface ();
144
145 /* Note, there may be other things you need to change,
146 depending on how you have your OpenGL state set up.
147 */
148 glPushAttrib (GL_ENABLE_BIT);
149 glDisable (GL_DEPTH_TEST);
150 glDisable (GL_CULL_FACE);
151 glEnable (GL_TEXTURE_2D);
152
153 /* This allows alpha blending of 2D textures with the scene */
154 glEnable (GL_BLEND);
155 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
156
157 glViewport (0, 0, screen->w, screen->h);
158
159 glMatrixMode (GL_PROJECTION);
160 glPushMatrix ();
161 glLoadIdentity ();
162
163 glOrtho (0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
164
165 glMatrixMode (GL_MODELVIEW);
166 glPushMatrix ();
167 glLoadIdentity ();
168
169 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
170 }
171
172 void
173 SDL_GL_Leave2DMode ()
174 {
175 glMatrixMode (GL_MODELVIEW);
176 glPopMatrix ();
177
178 glMatrixMode (GL_PROJECTION);
179 glPopMatrix ();
180
181 glPopAttrib ();
182 }
183
184 /* Quick utility function for texture creation */
185 static int
186 power_of_two (int input)
187 {
188 int value = 1;
189
190 while (value < input)
191 {
192 value <<= 1;
193 }
194 return value;
195 }
196
197 GLuint
198 SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
199 {
200 GLuint texture;
201 int w, h;
202 SDL_Surface *image;
203 SDL_Rect area;
204 Uint32 saved_flags;
205 Uint8 saved_alpha;
206
207 /* Use the surface width and height expanded to powers of 2 */
208 w = power_of_two (surface->w);
209 h = power_of_two (surface->h);
210 texcoord[0] = 0.0f; /* Min X */
211 texcoord[1] = 0.0f; /* Min Y */
212 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
213 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
214
215 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
216 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
217 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
218 #else
219 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
220 #endif
221 );
222 if (image == NULL)
223 {
224 return 0;
225 }
226
227 /* Save the alpha blending attributes */
228 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
229 saved_alpha = surface->format->alpha;
230 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
231 {
232 SDL_SetAlpha (surface, 0, 0);
233 }
234
235 /* Copy the surface into the GL texture image */
236 area.x = 0;
237 area.y = 0;
238 area.w = surface->w;
239 area.h = surface->h;
240 SDL_BlitSurface (surface, &area, image, &area);
241
242 /* Restore the alpha blending attributes */
243 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
244 {
245 SDL_SetAlpha (surface, saved_flags, saved_alpha);
246 }
247
248 /* Create an OpenGL texture for the image */
249 glGenTextures (1, &texture);
250 glBindTexture (GL_TEXTURE_2D, texture);
251 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
252 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
253 glTexImage2D (GL_TEXTURE_2D,
254 0,
255 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
256 SDL_FreeSurface (image); /* No longer needed */
257
258 return texture;
259 }
260
261
262 void
263 DrawLogoTexture (void)
264 {
265 static GLfloat texMinX, texMinY;
266 static GLfloat texMaxX, texMaxY;
267 static int x = 0;
268 static int y = 0;
269 static int w, h;
270 static int delta_x = 1;
271 static int delta_y = 1;
272 static Uint32 last_moved = 0;
273
274 SDL_Surface *screen = SDL_GetVideoSurface ();
275
276 if (!global_texture)
277 {
278 SDL_Surface *image;
279 GLfloat texcoord[4];
280
281 /* Load the image (could use SDL_image library here) */
282 image = SDL_LoadBMP (LOGO_FILE);
283 if (image == NULL)
284 {
285 return;
286 }
287 w = image->w;
288 h = image->h;
289
290 /* Convert the image into an OpenGL texture */
291 global_texture = SDL_GL_LoadTexture (image, texcoord);
292
293 /* Make texture coordinates easy to understand */
294 texMinX = texcoord[0];
295 texMinY = texcoord[1];
296 texMaxX = texcoord[2];
297 texMaxY = texcoord[3];
298
299 /* We don't need the original image anymore */
300 SDL_FreeSurface (image);
301
302 /* Make sure that the texture conversion is okay */
303 if (!global_texture)
304 {
305 return;
306 }
307 }
308
309 /* Move the image around */
310 x += delta_x;
311 if (x < 0)
312 {
313 x = 0;
314 delta_x = -delta_x;
315 }
316 else if ((x + w) > screen->w)
317 {
318 x = screen->w - w;
319 delta_x = -delta_x;
320 }
321 y += delta_y;
322 if (y < 0)
323 {
324 y = 0;
325 delta_y = -delta_y;
326 }
327 else if ((y + h) > screen->h)
328 {
329 y = screen->h - h;
330 delta_y = -delta_y;
331 }
332
333 /* Show the image on the screen */
334 SDL_GL_Enter2DMode ();
335 glBindTexture (GL_TEXTURE_2D, global_texture);
336 glBegin (GL_TRIANGLE_STRIP);
337 glTexCoord2f (texMinX, texMinY);
338 glVertex2i (x, y);
339 glTexCoord2f (texMaxX, texMinY);
340 glVertex2i (x + w, y);
341 glTexCoord2f (texMinX, texMaxY);
342 glVertex2i (x, y + h);
343 glTexCoord2f (texMaxX, texMaxY);
344 glVertex2i (x + w, y + h);
345 glEnd ();
346 SDL_GL_Leave2DMode ();
347 }
348
349 int
350 RunGLTest (int argc, char *argv[],
351 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
352 {
353 int i;
354 int rgb_size[3];
355 int w = 640;
356 int h = 480;
357 int done = 0;
358 int frames;
359 Uint32 start_time, this_time;
360 int value;
361
362 if (SDL_Init (SDL_INIT_VIDEO) < 0)
363 {
364 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
365 exit (1);
366 }
367
368 /* See if we should detect the display depth */
369 if (bpp == 0)
370 {
371 if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8)
372 {
373 bpp = 8;
374 }
375 else
376 {
377 bpp = 16; /* More doesn't seem to work */
378 }
379 }
380
381 /* Set the flags we want to use for setting the video mode */
382 if (logo && USE_DEPRECATED_OPENGLBLIT)
383 {
384 video_flags = SDL_OPENGLBLIT;
385 }
386 else
387 {
388 video_flags = SDL_OPENGL;
389 }
390 for (i = 1; argv[i]; ++i)
391 {
392 if (strcmp (argv[1], "-fullscreen") == 0)
393 {
394 video_flags |= SDL_FULLSCREEN;
395 }
396 }
397
398 if (noframe)
399 {
400 video_flags |= SDL_NOFRAME;
401 }
402
403 /* Initialize the display */
404 switch (bpp)
405 {
406 case 8:
407 rgb_size[0] = 3;
408 rgb_size[1] = 3;
409 rgb_size[2] = 2;
410 break;
411 case 15:
412 case 16:
413 rgb_size[0] = 5;
414 rgb_size[1] = 5;
415 rgb_size[2] = 5;
416 break;
417 default:
418 rgb_size[0] = 8;
419 rgb_size[1] = 8;
420 rgb_size[2] = 8;
421 break;
422 }
423 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
424 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
425 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
426 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
427 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
428 if (fsaa)
429 {
430 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
431 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa);
432 }
433 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL)
434 {
435 fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ());
436 SDL_Quit ();
437 exit (1);
438 }
439
440 printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel);
441 printf ("\n");
442 printf ("Vendor : %s\n", glGetString (GL_VENDOR));
443 printf ("Renderer : %s\n", glGetString (GL_RENDERER));
444 printf ("Version : %s\n", glGetString (GL_VERSION));
445 printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS));
446 printf ("\n");
447
448 SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value);
449 printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
450 SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value);
451 printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
452 SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value);
453 printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
454 SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value);
455 printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
456 SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value);
457 printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
458 if (fsaa)
459 {
460 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value);
461 printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
462 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value);
463 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
464 value);
465 }
466
467 /* Set the window manager title bar */
468 SDL_WM_SetCaption ("SDL GL test", "testgl");
469
470 /* Set the gamma for the window */
471 if (gamma != 0.0)
472 {
473 SDL_SetGamma (gamma, gamma, gamma);
474 }
475
476 glViewport (0, 0, w, h);
477 glMatrixMode (GL_PROJECTION);
478 glLoadIdentity ();
479
480 glOrtho (-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
481
482 glMatrixMode (GL_MODELVIEW);
483 glLoadIdentity ();
484
485 glEnable (GL_DEPTH_TEST);
486
487 glDepthFunc (GL_LESS);
488
489 glShadeModel (GL_SMOOTH);
490
491 /* Loop until done. */
492 start_time = SDL_GetTicks ();
493 frames = 0;
494 while (!done)
495 {
496 GLenum gl_error;
497 char *sdl_error;
498 SDL_Event event;
499
500 /* Do our drawing, too. */
501 glClearColor (0.0, 0.0, 0.0, 1.0);
502 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
503
504 glBegin (GL_QUADS);
505
506 #ifdef SHADED_CUBE
507 glColor3fv (color[0]);
508 glVertex3fv (cube[0]);
509 glColor3fv (color[1]);
510 glVertex3fv (cube[1]);
511 glColor3fv (color[2]);
512 glVertex3fv (cube[2]);
513 glColor3fv (color[3]);
514 glVertex3fv (cube[3]);
515
516 glColor3fv (color[3]);
517 glVertex3fv (cube[3]);
518 glColor3fv (color[4]);
519 glVertex3fv (cube[4]);
520 glColor3fv (color[7]);
521 glVertex3fv (cube[7]);
522 glColor3fv (color[2]);
523 glVertex3fv (cube[2]);
524
525 glColor3fv (color[0]);
526 glVertex3fv (cube[0]);
527 glColor3fv (color[5]);
528 glVertex3fv (cube[5]);
529 glColor3fv (color[6]);
530 glVertex3fv (cube[6]);
531 glColor3fv (color[1]);
532 glVertex3fv (cube[1]);
533
534 glColor3fv (color[5]);
535 glVertex3fv (cube[5]);
536 glColor3fv (color[4]);
537 glVertex3fv (cube[4]);
538 glColor3fv (color[7]);
539 glVertex3fv (cube[7]);
540 glColor3fv (color[6]);
541 glVertex3fv (cube[6]);
542
543 glColor3fv (color[5]);
544 glVertex3fv (cube[5]);
545 glColor3fv (color[0]);
546 glVertex3fv (cube[0]);
547 glColor3fv (color[3]);
548 glVertex3fv (cube[3]);
549 glColor3fv (color[4]);
550 glVertex3fv (cube[4]);
551
552 glColor3fv (color[6]);
553 glVertex3fv (cube[6]);
554 glColor3fv (color[1]);
555 glVertex3fv (cube[1]);
556 glColor3fv (color[2]);
557 glVertex3fv (cube[2]);
558 glColor3fv (color[7]);
559 glVertex3fv (cube[7]);
560 #else // flat cube
561 glColor3f (1.0, 0.0, 0.0);
562 glVertex3fv (cube[0]);
563 glVertex3fv (cube[1]);
564 glVertex3fv (cube[2]);
565 glVertex3fv (cube[3]);
566
567 glColor3f (0.0, 1.0, 0.0);
568 glVertex3fv (cube[3]);
569 glVertex3fv (cube[4]);
570 glVertex3fv (cube[7]);
571 glVertex3fv (cube[2]);
572
573 glColor3f (0.0, 0.0, 1.0);
574 glVertex3fv (cube[0]);
575 glVertex3fv (cube[5]);
576 glVertex3fv (cube[6]);
577 glVertex3fv (cube[1]);
578
579 glColor3f (0.0, 1.0, 1.0);
580 glVertex3fv (cube[5]);
581 glVertex3fv (cube[4]);
582 glVertex3fv (cube[7]);
583 glVertex3fv (cube[6]);
584
585 glColor3f (1.0, 1.0, 0.0);
586 glVertex3fv (cube[5]);
587 glVertex3fv (cube[0]);
588 glVertex3fv (cube[3]);
589 glVertex3fv (cube[4]);
590
591 glColor3f (1.0, 0.0, 1.0);
592 glVertex3fv (cube[6]);
593 glVertex3fv (cube[1]);
594 glVertex3fv (cube[2]);
595 glVertex3fv (cube[7]);
596 #endif /* SHADED_CUBE */
597
598 glEnd ();
599
600 glMatrixMode (GL_MODELVIEW);
601 glRotatef (5.0, 1.0, 1.0, 1.0);
602
603 SDL_GL_SwapBuffers ();
604
605 /* Check for error conditions. */
606 gl_error = glGetError ();
607
608 if (gl_error != GL_NO_ERROR)
609 {
610 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
611 }
612
613 sdl_error = SDL_GetError ();
614
615 if (sdl_error[0] != '\0')
616 {
617 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
618 SDL_ClearError ();
619 }
620
621 /* Allow the user to see what's happening */
622 if (slowly)
623 {
624 SDL_Delay (20);
625 }
626 SDL_Delay (20);
627
628 /* Check if there's a pending event. */
629 while (SDL_PollEvent (&event))
630 {
631 done = HandleEvent (&event);
632 }
633 ++frames;
634 }
635
636 /* Print out the frames per second */
637 this_time = SDL_GetTicks ();
638 if (this_time != start_time)
639 {
640 printf ("%2.2f FPS\n",
641 ((float) frames / (this_time - start_time)) * 1000.0);
642 }
643
644 if (global_image)
645 {
646 SDL_FreeSurface (global_image);
647 global_image = NULL;
648 }
649 if (global_texture)
650 {
651 glDeleteTextures (1, &global_texture);
652 global_texture = 0;
653 }
654
655 /* Destroy our GL context, etc. */
656 SDL_Quit ();
657 return (0);
658 }
659
660 int
661 main (int argc, char *argv[])
662 {
663 int i, logo;
664 int numtests;
665 int bpp = 0;
666 int slowly;
667 float gamma = 0.0;
668 int noframe = 0;
669 int fsaa = 0;
670
671 logo = 0;
672 slowly = 0;
673 numtests = 1;
674 for (i = 1; argv[i]; ++i)
675 {
676 if (strcmp (argv[i], "-twice") == 0)
677 {
678 ++numtests;
679 }
680 if (strcmp (argv[i], "-logo") == 0)
681 {
682 logo = 1;
683 USE_DEPRECATED_OPENGLBLIT = SDL_FALSE;
684 }
685 if (strcmp (argv[i], "-logoblit") == 0)
686 {
687 logo = 1;
688 USE_DEPRECATED_OPENGLBLIT = SDL_TRUE;
689 }
690 if (strcmp (argv[i], "-slow") == 0)
691 {
692 slowly = 1;
693 }
694 if (strcmp (argv[i], "-bpp") == 0)
695 {
696 bpp = atoi (argv[++i]);
697 }
698 if (strcmp (argv[i], "-gamma") == 0)
699 {
700 gamma = (float) atof (argv[++i]);
701 }
702 if (strcmp (argv[i], "-noframe") == 0)
703 {
704 noframe = 1;
705 }
706 if (strcmp (argv[i], "-fsaa") == 0)
707 {
708 ++fsaa;
709 }
710 if (strncmp (argv[i], "-h", 2) == 0)
711 {
712 printf
713 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
714 argv[0]);
715 exit (0);
716 }
717 }
718 for (i = 0; i < numtests; ++i)
719 {
720 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
721 }
722 return 0;
723 }