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