ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/test.C
Revision: 1.2
Committed: Sat Oct 2 15:54:43 2004 UTC (22 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +5 -124 lines
Log Message:
*** empty log message ***

File Contents

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