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