ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
(Generate patch)

Comparing libgender/test.C (file contents):
Revision 1.2 by root, Sat Oct 2 15:54:43 2004 UTC vs.
Revision 1.9 by root, Sun Oct 3 04:00:51 2004 UTC

16static SDL_Surface *global_image = NULL; 16static SDL_Surface *global_image = NULL;
17static GLuint global_texture = 0; 17static GLuint global_texture = 0;
18 18
19#include "entity.h" 19#include "entity.h"
20 20
21#include "txtprt_import.h"
22
21/**********************************************************************/ 23/**********************************************************************/
24
25view camera;
22 26
23void 27void
24HotKey_ToggleFullScreen (void) 28HotKey_ToggleFullScreen (void)
25{ 29{
26 SDL_Surface *screen; 30 SDL_Surface *screen;
27 31
28 screen = SDL_GetVideoSurface (); 32 screen = SDL_GetVideoSurface ();
29 if (SDL_WM_ToggleFullScreen (screen)) 33 if (SDL_WM_ToggleFullScreen (screen))
30 {
31 printf ("Toggled fullscreen mode - now %s\n", 34 printf ("Toggled fullscreen mode - now %s\n",
32 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); 35 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
33 }
34 else 36 else
35 {
36 printf ("Unable to toggle fullscreen mode\n"); 37 printf ("Unable to toggle fullscreen mode\n");
37 }
38} 38}
39 39
40void 40void
41HotKey_ToggleGrab (void) 41HotKey_ToggleGrab (void)
42{ 42{
43 SDL_GrabMode mode; 43 SDL_GrabMode mode;
44 44
45 printf ("Ctrl-G: toggling input grab!\n"); 45 printf ("Ctrl-G: toggling input grab!\n");
46 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); 46 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
47 if (mode == SDL_GRAB_ON) 47 if (mode == SDL_GRAB_ON)
48 {
49 printf ("Grab was on\n"); 48 printf ("Grab was on\n");
50 }
51 else 49 else
52 {
53 printf ("Grab was off\n"); 50 printf ("Grab was off\n");
54 }
55 51
56 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); 52 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
57 if (mode == SDL_GRAB_ON) 53 if (mode == SDL_GRAB_ON)
58 {
59 printf ("Grab is now on\n"); 54 printf ("Grab is now on\n");
60 }
61 else 55 else
62 {
63 printf ("Grab is now off\n"); 56 printf ("Grab is now off\n");
64 }
65} 57}
66 58
67void 59void
68HotKey_Iconify (void) 60HotKey_Iconify (void)
69{ 61{
97 89
98 printf ("focus\n"); 90 printf ("focus\n");
99 break; 91 break;
100 92
101 case SDL_KEYDOWN: 93 case SDL_KEYDOWN:
94
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
102 if (event->key.keysym.sym == SDLK_ESCAPE) 104 if (event->key.keysym.sym == SDLK_ESCAPE)
103 {
104 done = 1; 105 done = 1;
105 }
106 106
107 if ((event->key.keysym.sym == SDLK_g) && 107 if ((event->key.keysym.sym == SDLK_g) &&
108 (event->key.keysym.mod & KMOD_CTRL)) 108 (event->key.keysym.mod & KMOD_CTRL))
109 {
110 HotKey_ToggleGrab (); 109 HotKey_ToggleGrab ();
111 }
112 110
113 if ((event->key.keysym.sym == SDLK_z) && 111 if ((event->key.keysym.sym == SDLK_z) &&
114 (event->key.keysym.mod & KMOD_CTRL)) 112 (event->key.keysym.mod & KMOD_CTRL))
115 {
116 HotKey_Iconify (); 113 HotKey_Iconify ();
117 }
118 114
119 if ((event->key.keysym.sym == SDLK_RETURN) && 115 if ((event->key.keysym.sym == SDLK_RETURN) &&
120 (event->key.keysym.mod & KMOD_ALT)) 116 (event->key.keysym.mod & KMOD_ALT))
121 {
122 HotKey_ToggleFullScreen (); 117 HotKey_ToggleFullScreen ();
123 }
124 118
125 printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym));
126 break; 119 break;
127 120
128 case SDL_QUIT: 121 case SDL_QUIT:
129 done = 1; 122 done = 1;
130 break; 123 break;
131 } 124 }
132 125
133 return (done); 126 return (done);
134}
135
136void
137SDL_GL_Enter2DMode ()
138{
139 SDL_Surface *screen = SDL_GetVideoSurface ();
140
141 /* Note, there may be other things you need to change,
142 depending on how you have your OpenGL state set up.
143 */
144 glPushAttrib (GL_ENABLE_BIT);
145 glDisable (GL_DEPTH_TEST);
146 glDisable (GL_CULL_FACE);
147 glEnable (GL_TEXTURE_2D);
148
149 /* This allows alpha blending of 2D textures with the scene */
150 glEnable (GL_BLEND);
151 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
152
153 glViewport (0, 0, screen->w, screen->h);
154
155 glMatrixMode (GL_PROJECTION);
156 glPushMatrix ();
157 glLoadIdentity ();
158
159 glOrtho (0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
160
161 glMatrixMode (GL_MODELVIEW);
162 glPushMatrix ();
163 glLoadIdentity ();
164
165 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
166}
167
168void
169SDL_GL_Leave2DMode ()
170{
171 glMatrixMode (GL_MODELVIEW);
172 glPopMatrix ();
173
174 glMatrixMode (GL_PROJECTION);
175 glPopMatrix ();
176
177 glPopAttrib ();
178} 127}
179 128
180/* Quick utility function for texture creation */ 129/* Quick utility function for texture creation */
181static int 130static int
182power_of_two (int input) 131power_of_two (int input)
252 SDL_FreeSurface (image); /* No longer needed */ 201 SDL_FreeSurface (image); /* No longer needed */
253 202
254 return texture; 203 return texture;
255} 204}
256 205
257
258void
259DrawLogoTexture (void)
260{
261 static GLfloat texMinX, texMinY;
262 static GLfloat texMaxX, texMaxY;
263 static int x = 0;
264 static int y = 0;
265 static int w, h;
266 static int delta_x = 1;
267 static int delta_y = 1;
268 static Uint32 last_moved = 0;
269
270 SDL_Surface *screen = SDL_GetVideoSurface ();
271
272 if (!global_texture)
273 {
274 SDL_Surface *image;
275 GLfloat texcoord[4];
276
277 /* Load the image (could use SDL_image library here) */
278 image = SDL_LoadBMP (LOGO_FILE);
279 if (image == NULL)
280 {
281 return;
282 }
283 w = image->w;
284 h = image->h;
285
286 /* Convert the image into an OpenGL texture */
287 global_texture = SDL_GL_LoadTexture (image, texcoord);
288
289 /* Make texture coordinates easy to understand */
290 texMinX = texcoord[0];
291 texMinY = texcoord[1];
292 texMaxX = texcoord[2];
293 texMaxY = texcoord[3];
294
295 /* We don't need the original image anymore */
296 SDL_FreeSurface (image);
297
298 /* Make sure that the texture conversion is okay */
299 if (!global_texture)
300 {
301 return;
302 }
303 }
304
305 /* Move the image around */
306 x += delta_x;
307 if (x < 0)
308 {
309 x = 0;
310 delta_x = -delta_x;
311 }
312 else if ((x + w) > screen->w)
313 {
314 x = screen->w - w;
315 delta_x = -delta_x;
316 }
317 y += delta_y;
318 if (y < 0)
319 {
320 y = 0;
321 delta_y = -delta_y;
322 }
323 else if ((y + h) > screen->h)
324 {
325 y = screen->h - h;
326 delta_y = -delta_y;
327 }
328
329 /* Show the image on the screen */
330 SDL_GL_Enter2DMode ();
331 glBindTexture (GL_TEXTURE_2D, global_texture);
332 glBegin (GL_TRIANGLE_STRIP);
333 glTexCoord2f (texMinX, texMinY);
334 glVertex2i (x, y);
335 glTexCoord2f (texMaxX, texMinY);
336 glVertex2i (x + w, y);
337 glTexCoord2f (texMinX, texMaxY);
338 glVertex2i (x, y + h);
339 glTexCoord2f (texMaxX, texMaxY);
340 glVertex2i (x + w, y + h);
341 glEnd ();
342 SDL_GL_Leave2DMode ();
343}
344
345int 206int
346RunGLTest (int argc, char *argv[], 207RunGLTest (int argc, char *argv[],
347 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) 208 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
348{ 209{
349 int i; 210 int i;
364 225
365 /* See if we should detect the display depth */ 226 /* See if we should detect the display depth */
366 if (bpp == 0) 227 if (bpp == 0)
367 { 228 {
368 if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8) 229 if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8)
369 {
370 bpp = 8; 230 bpp = 8;
371 }
372 else 231 else
373 {
374 bpp = 16; /* More doesn't seem to work */ 232 bpp = 16; /* More doesn't seem to work */
375 }
376 } 233 }
377 234
378 video_flags = SDL_OPENGL; 235 video_flags = SDL_OPENGL;
379 236
380 for (i = 1; argv[i]; ++i) 237 for (i = 1; argv[i]; ++i)
381 {
382 if (strcmp (argv[1], "-fullscreen") == 0) 238 if (strcmp (argv[1], "-fullscreen") == 0)
383 {
384 video_flags |= SDL_FULLSCREEN; 239 video_flags |= SDL_FULLSCREEN;
385 }
386 }
387 240
388 if (noframe) 241 if (noframe)
389 {
390 video_flags |= SDL_NOFRAME; 242 video_flags |= SDL_NOFRAME;
391 }
392 243
393 /* Initialize the display */ 244 /* Initialize the display */
394 switch (bpp) 245 switch (bpp)
395 { 246 {
396 case 8: 247 case 8:
397 rgb_size[0] = 3; 248 rgb_size[0] = 3;
398 rgb_size[1] = 3; 249 rgb_size[1] = 3;
399 rgb_size[2] = 2; 250 rgb_size[2] = 2;
400 break; 251 break;
252
401 case 15: 253 case 15:
402 case 16: 254 case 16:
403 rgb_size[0] = 5; 255 rgb_size[0] = 5;
404 rgb_size[1] = 5; 256 rgb_size[1] = 5;
405 rgb_size[2] = 5; 257 rgb_size[2] = 5;
406 break; 258 break;
259
407 default: 260 default:
408 rgb_size[0] = 8; 261 rgb_size[0] = 8;
409 rgb_size[1] = 8; 262 rgb_size[1] = 8;
410 rgb_size[2] = 8; 263 rgb_size[2] = 8;
411 break; 264 break;
413 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); 266 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
414 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); 267 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
415 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); 268 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
416 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); 269 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
417 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); 270 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
271
418 if (fsaa) 272 if (fsaa)
419 { 273 {
420 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); 274 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
421 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa); 275 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa);
422 } 276 }
277
423 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL) 278 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL)
424 { 279 {
425 fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); 280 fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ());
426 SDL_Quit (); 281 SDL_Quit ();
427 exit (1); 282 exit (1);
443 printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value); 298 printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
444 SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value); 299 SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value);
445 printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value); 300 printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
446 SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value); 301 SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value);
447 printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value); 302 printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
303
448 if (fsaa) 304 if (fsaa)
449 { 305 {
450 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value); 306 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value);
451 printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); 307 printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
452 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value); 308 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value);
457 /* Set the window manager title bar */ 313 /* Set the window manager title bar */
458 SDL_WM_SetCaption ("SDL GL test", "testgl"); 314 SDL_WM_SetCaption ("SDL GL test", "testgl");
459 315
460 /* Set the gamma for the window */ 316 /* Set the gamma for the window */
461 if (gamma != 0.0) 317 if (gamma != 0.0)
462 {
463 SDL_SetGamma (gamma, gamma, gamma); 318 SDL_SetGamma (gamma, gamma, gamma);
464 }
465 319
466 glViewport (0, 0, w, h); 320 camera.p = point (0, 0, 10);
467 glMatrixMode (GL_PROJECTION); 321 camera.d = vec3 (0, 0, -1);
468 glLoadIdentity (); 322 camera.u = vec3 (0, 1, 0);
469 323 camera.w = w; camera.h = h;
470 glOrtho (-2.0, 2.0, -2.0, 2.0, -20.0, 20.0); 324 camera.fov = 90;
471 325
472 glMatrixMode (GL_MODELVIEW); 326 glMatrixMode (GL_MODELVIEW);
473 glLoadIdentity (); 327 glLoadIdentity ();
474 328
475 glEnable (GL_DEPTH_TEST); 329 glEnable (GL_DEPTH_TEST);
476 330
477 glDepthFunc (GL_LESS); 331 glDepthFunc (GL_LESS);
478 332
479 glShadeModel (GL_SMOOTH); 333 glShadeModel (GL_SMOOTH);
334
335 glEnable (GL_LIGHTING);
336
337 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
338 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
339 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
340 glEnable (GL_LIGHT0);
341 glEnable (GL_COLOR_MATERIAL);
480 342
481 /* Loop until done. */ 343 /* Loop until done. */
482 start_time = SDL_GetTicks (); 344 start_time = SDL_GetTicks ();
483 frames = 0; 345 frames = 0;
484 while (!done) 346 while (!done)
489 351
490 /* Do our drawing, too. */ 352 /* Do our drawing, too. */
491 glClearColor (0.0, 0.0, 0.0, 1.0); 353 glClearColor (0.0, 0.0, 0.0, 1.0);
492 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 354 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
493 355
494 glBegin (GL_QUADS); 356 GLfloat lightp[4];
357 lightp[0] = camera.p.x;
358 lightp[1] = camera.p.y;
359 lightp[2] = camera.p.z;
360 lightp[3] = 1;
361 glLightfv (GL_LIGHT0, GL_POSITION, lightp);
495 362
496 glEnd (); 363#if 0
364 static GLfloat ry;
365 ry += 0.03;
366 v.d.x = cos (ry);
367 v.d.z = sin (ry);
368#endif
369
370 draw_context c;
371 c.mode = draw_context::LIGHTED;
372 camera.draw (c);
497 373
498 SDL_GL_SwapBuffers (); 374 SDL_GL_SwapBuffers ();
499 375
500 /* Check for error conditions. */ 376 /* Check for error conditions. */
501 gl_error = glGetError (); 377 gl_error = glGetError ();
502 378
503 if (gl_error != GL_NO_ERROR) 379 if (gl_error != GL_NO_ERROR)
504 {
505 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); 380 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
506 }
507 381
508 sdl_error = SDL_GetError (); 382 sdl_error = SDL_GetError ();
509 383
510 if (sdl_error[0] != '\0') 384 if (sdl_error[0] != '\0')
511 { 385 {
512 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 386 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
513 SDL_ClearError (); 387 SDL_ClearError ();
514 } 388 }
515 389
516 /* Allow the user to see what's happening */ 390 /* Allow the user to see what's happening */
517 SDL_Delay (20); 391 //SDL_Delay (20);
518 392
519 /* Check if there's a pending event. */ 393 /* Check if there's a pending event. */
520 while (SDL_PollEvent (&event)) 394 while (SDL_PollEvent (&event))
521 { 395 {
522 done = HandleEvent (&event); 396 done = HandleEvent (&event);
557 int slowly; 431 int slowly;
558 float gamma = 0.0; 432 float gamma = 0.0;
559 int noframe = 0; 433 int noframe = 0;
560 int fsaa = 0; 434 int fsaa = 0;
561 435
436 // load a entity
437 txtprt_parser p;
438 entity *e;
439 try {
440 e = p.read ("test.blasc");
441 } catch (txtprt_i_exception & e) {
442 cout << "ERR: " << e.msg << endl;
443 }
444 e->show ();
445
562 logo = 0; 446 logo = 0;
563 slowly = 0; 447 slowly = 0;
564 numtests = 1; 448 numtests = 1;
565 for (i = 1; argv[i]; ++i) 449 for (i = 1; argv[i]; ++i)
566 { 450 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines