ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.4
Committed: Sun Oct 3 02:19:07 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +11 -0 lines
Log Message:
Added txtprt exporter and libgender importer.

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