ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.5
Committed: Sun Oct 3 02:38:33 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +13 -22 lines
Log Message:
*** empty log message ***

File Contents

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