ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.3
Committed: Sun Oct 3 01:14:40 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.2: +28 -116 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     int
258     RunGLTest (int argc, char *argv[],
259     int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
260     {
261     int i;
262     int rgb_size[3];
263     int w = 640;
264     int h = 480;
265     int done = 0;
266     int frames;
267     Uint32 start_time, this_time;
268 root 1.2 Uint32 video_flags;
269 root 1.1 int value;
270    
271     if (SDL_Init (SDL_INIT_VIDEO) < 0)
272     {
273     fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
274     exit (1);
275     }
276    
277     /* See if we should detect the display depth */
278     if (bpp == 0)
279     {
280     if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8)
281 root 1.3 bpp = 8;
282 root 1.1 else
283 root 1.3 bpp = 16; /* More doesn't seem to work */
284 root 1.1 }
285    
286 root 1.2 video_flags = SDL_OPENGL;
287    
288 root 1.1 for (i = 1; argv[i]; ++i)
289 root 1.3 if (strcmp (argv[1], "-fullscreen") == 0)
290     video_flags |= SDL_FULLSCREEN;
291 root 1.1
292     if (noframe)
293 root 1.3 video_flags |= SDL_NOFRAME;
294 root 1.1
295     /* Initialize the display */
296     switch (bpp)
297     {
298     case 8:
299     rgb_size[0] = 3;
300     rgb_size[1] = 3;
301     rgb_size[2] = 2;
302     break;
303 root 1.3
304 root 1.1 case 15:
305     case 16:
306     rgb_size[0] = 5;
307     rgb_size[1] = 5;
308     rgb_size[2] = 5;
309     break;
310 root 1.3
311 root 1.1 default:
312     rgb_size[0] = 8;
313     rgb_size[1] = 8;
314     rgb_size[2] = 8;
315     break;
316     }
317     SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
318     SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
319     SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
320     SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
321     SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
322 root 1.3
323 root 1.1 if (fsaa)
324     {
325     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
326     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa);
327     }
328 root 1.3
329 root 1.1 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL)
330     {
331     fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ());
332     SDL_Quit ();
333     exit (1);
334     }
335    
336     printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel);
337     printf ("\n");
338     printf ("Vendor : %s\n", glGetString (GL_VENDOR));
339     printf ("Renderer : %s\n", glGetString (GL_RENDERER));
340     printf ("Version : %s\n", glGetString (GL_VERSION));
341     printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS));
342     printf ("\n");
343    
344     SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value);
345     printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
346     SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value);
347     printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
348     SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value);
349     printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
350     SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value);
351     printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
352     SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value);
353     printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
354 root 1.3
355 root 1.1 if (fsaa)
356     {
357     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value);
358     printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
359     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value);
360     printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
361     value);
362     }
363    
364     /* Set the window manager title bar */
365     SDL_WM_SetCaption ("SDL GL test", "testgl");
366    
367     /* Set the gamma for the window */
368     if (gamma != 0.0)
369 root 1.3 SDL_SetGamma (gamma, gamma, gamma);
370 root 1.1
371 root 1.3 view v;
372     v.p = point (0, 0, 0);
373     v.d = vec3 (0, 0, -1);
374     v.u = vec3 (0, 1, 0);
375     v.w = w; v.h = h;
376     v.fov = 90;
377 root 1.1
378     glMatrixMode (GL_MODELVIEW);
379     glLoadIdentity ();
380    
381     glEnable (GL_DEPTH_TEST);
382    
383     glDepthFunc (GL_LESS);
384    
385     glShadeModel (GL_SMOOTH);
386 root 1.3 GLfloat ambient[4] = { 1, 1, 1, 1 };
387     glEnable (GL_LIGHTING);
388     glEnable (GL_COLOR_MATERIAL);
389     glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambient);
390 root 1.1
391     /* Loop until done. */
392     start_time = SDL_GetTicks ();
393     frames = 0;
394     while (!done)
395     {
396     GLenum gl_error;
397     char *sdl_error;
398     SDL_Event event;
399    
400     /* Do our drawing, too. */
401     glClearColor (0.0, 0.0, 0.0, 1.0);
402     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
403    
404 root 1.3 static GLfloat ry;
405     ry += 0.1;
406     v.d.x = cos (ry);
407     v.d.z = sin (ry);
408 root 1.1
409 root 1.3 draw_context c;
410     v.draw (c);
411 root 1.1
412     SDL_GL_SwapBuffers ();
413    
414     /* Check for error conditions. */
415     gl_error = glGetError ();
416    
417     if (gl_error != GL_NO_ERROR)
418 root 1.3 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
419 root 1.1
420     sdl_error = SDL_GetError ();
421    
422     if (sdl_error[0] != '\0')
423     {
424     fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
425     SDL_ClearError ();
426     }
427    
428     /* Allow the user to see what's happening */
429     SDL_Delay (20);
430    
431     /* Check if there's a pending event. */
432     while (SDL_PollEvent (&event))
433     {
434     done = HandleEvent (&event);
435     }
436     ++frames;
437     }
438    
439     /* Print out the frames per second */
440     this_time = SDL_GetTicks ();
441     if (this_time != start_time)
442     {
443     printf ("%2.2f FPS\n",
444     ((float) frames / (this_time - start_time)) * 1000.0);
445     }
446    
447     if (global_image)
448     {
449     SDL_FreeSurface (global_image);
450     global_image = NULL;
451     }
452     if (global_texture)
453     {
454     glDeleteTextures (1, &global_texture);
455     global_texture = 0;
456     }
457    
458     /* Destroy our GL context, etc. */
459     SDL_Quit ();
460     return (0);
461     }
462    
463     int
464     main (int argc, char *argv[])
465     {
466     int i, logo;
467     int numtests;
468     int bpp = 0;
469     int slowly;
470     float gamma = 0.0;
471     int noframe = 0;
472     int fsaa = 0;
473    
474     logo = 0;
475     slowly = 0;
476     numtests = 1;
477     for (i = 1; argv[i]; ++i)
478     {
479     if (strcmp (argv[i], "-twice") == 0)
480     {
481     ++numtests;
482     }
483     if (strcmp (argv[i], "-slow") == 0)
484     {
485     slowly = 1;
486     }
487     if (strcmp (argv[i], "-bpp") == 0)
488     {
489     bpp = atoi (argv[++i]);
490     }
491     if (strcmp (argv[i], "-gamma") == 0)
492     {
493     gamma = (float) atof (argv[++i]);
494     }
495     if (strcmp (argv[i], "-noframe") == 0)
496     {
497     noframe = 1;
498     }
499     if (strcmp (argv[i], "-fsaa") == 0)
500     {
501     ++fsaa;
502     }
503     if (strncmp (argv[i], "-h", 2) == 0)
504     {
505     printf
506     ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
507     argv[0]);
508     exit (0);
509     }
510     }
511     for (i = 0; i < numtests; ++i)
512     {
513     RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
514     }
515     return 0;
516     }