ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.20
Committed: Mon Oct 4 16:03:49 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.19: +2 -1 lines
Log Message:
added some simple scheduler and events

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