--- libgender/test.C 2004/10/02 15:54:43 1.2 +++ libgender/test.C 2004/10/03 04:00:51 1.9 @@ -18,8 +18,12 @@ #include "entity.h" +#include "txtprt_import.h" + /**********************************************************************/ +view camera; + void HotKey_ToggleFullScreen (void) { @@ -27,14 +31,10 @@ screen = SDL_GetVideoSurface (); if (SDL_WM_ToggleFullScreen (screen)) - { - printf ("Toggled fullscreen mode - now %s\n", - (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); - } + printf ("Toggled fullscreen mode - now %s\n", + (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); else - { - printf ("Unable to toggle fullscreen mode\n"); - } + printf ("Unable to toggle fullscreen mode\n"); } void @@ -45,23 +45,15 @@ printf ("Ctrl-G: toggling input grab!\n"); mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); if (mode == SDL_GRAB_ON) - { - printf ("Grab was on\n"); - } + printf ("Grab was on\n"); else - { - printf ("Grab was off\n"); - } + printf ("Grab was off\n"); mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); if (mode == SDL_GRAB_ON) - { - printf ("Grab is now on\n"); - } + printf ("Grab is now on\n"); else - { - printf ("Grab is now off\n"); - } + printf ("Grab is now off\n"); } void @@ -99,30 +91,31 @@ break; case SDL_KEYDOWN: + + if (event->key.keysym.sym == SDLK_UP) + camera.p.z -= 1; + if (event->key.keysym.sym == SDLK_DOWN) + camera.p.z += 1; + if (event->key.keysym.sym == SDLK_LEFT) + camera.p.x -= 1; + if (event->key.keysym.sym == SDLK_RIGHT) + camera.p.x += 1; + if (event->key.keysym.sym == SDLK_ESCAPE) - { - done = 1; - } + done = 1; if ((event->key.keysym.sym == SDLK_g) && (event->key.keysym.mod & KMOD_CTRL)) - { - HotKey_ToggleGrab (); - } + HotKey_ToggleGrab (); if ((event->key.keysym.sym == SDLK_z) && (event->key.keysym.mod & KMOD_CTRL)) - { - HotKey_Iconify (); - } + HotKey_Iconify (); if ((event->key.keysym.sym == SDLK_RETURN) && (event->key.keysym.mod & KMOD_ALT)) - { - HotKey_ToggleFullScreen (); - } + HotKey_ToggleFullScreen (); - printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym)); break; case SDL_QUIT: @@ -133,50 +126,6 @@ return (done); } -void -SDL_GL_Enter2DMode () -{ - SDL_Surface *screen = SDL_GetVideoSurface (); - - /* Note, there may be other things you need to change, - depending on how you have your OpenGL state set up. - */ - glPushAttrib (GL_ENABLE_BIT); - glDisable (GL_DEPTH_TEST); - glDisable (GL_CULL_FACE); - glEnable (GL_TEXTURE_2D); - - /* This allows alpha blending of 2D textures with the scene */ - glEnable (GL_BLEND); - glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - glViewport (0, 0, screen->w, screen->h); - - glMatrixMode (GL_PROJECTION); - glPushMatrix (); - glLoadIdentity (); - - glOrtho (0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0); - - glMatrixMode (GL_MODELVIEW); - glPushMatrix (); - glLoadIdentity (); - - glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); -} - -void -SDL_GL_Leave2DMode () -{ - glMatrixMode (GL_MODELVIEW); - glPopMatrix (); - - glMatrixMode (GL_PROJECTION); - glPopMatrix (); - - glPopAttrib (); -} - /* Quick utility function for texture creation */ static int power_of_two (int input) @@ -254,94 +203,6 @@ return texture; } - -void -DrawLogoTexture (void) -{ - static GLfloat texMinX, texMinY; - static GLfloat texMaxX, texMaxY; - static int x = 0; - static int y = 0; - static int w, h; - static int delta_x = 1; - static int delta_y = 1; - static Uint32 last_moved = 0; - - SDL_Surface *screen = SDL_GetVideoSurface (); - - if (!global_texture) - { - SDL_Surface *image; - GLfloat texcoord[4]; - - /* Load the image (could use SDL_image library here) */ - image = SDL_LoadBMP (LOGO_FILE); - if (image == NULL) - { - return; - } - w = image->w; - h = image->h; - - /* Convert the image into an OpenGL texture */ - global_texture = SDL_GL_LoadTexture (image, texcoord); - - /* Make texture coordinates easy to understand */ - texMinX = texcoord[0]; - texMinY = texcoord[1]; - texMaxX = texcoord[2]; - texMaxY = texcoord[3]; - - /* We don't need the original image anymore */ - SDL_FreeSurface (image); - - /* Make sure that the texture conversion is okay */ - if (!global_texture) - { - return; - } - } - - /* Move the image around */ - x += delta_x; - if (x < 0) - { - x = 0; - delta_x = -delta_x; - } - else if ((x + w) > screen->w) - { - x = screen->w - w; - delta_x = -delta_x; - } - y += delta_y; - if (y < 0) - { - y = 0; - delta_y = -delta_y; - } - else if ((y + h) > screen->h) - { - y = screen->h - h; - delta_y = -delta_y; - } - - /* Show the image on the screen */ - SDL_GL_Enter2DMode (); - glBindTexture (GL_TEXTURE_2D, global_texture); - glBegin (GL_TRIANGLE_STRIP); - glTexCoord2f (texMinX, texMinY); - glVertex2i (x, y); - glTexCoord2f (texMaxX, texMinY); - glVertex2i (x + w, y); - glTexCoord2f (texMinX, texMaxY); - glVertex2i (x, y + h); - glTexCoord2f (texMaxX, texMaxY); - glVertex2i (x + w, y + h); - glEnd (); - SDL_GL_Leave2DMode (); -} - int RunGLTest (int argc, char *argv[], int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) @@ -366,29 +227,19 @@ if (bpp == 0) { if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8) - { - bpp = 8; - } + bpp = 8; else - { - bpp = 16; /* More doesn't seem to work */ - } + bpp = 16; /* More doesn't seem to work */ } video_flags = SDL_OPENGL; for (i = 1; argv[i]; ++i) - { - if (strcmp (argv[1], "-fullscreen") == 0) - { - video_flags |= SDL_FULLSCREEN; - } - } + if (strcmp (argv[1], "-fullscreen") == 0) + video_flags |= SDL_FULLSCREEN; if (noframe) - { - video_flags |= SDL_NOFRAME; - } + video_flags |= SDL_NOFRAME; /* Initialize the display */ switch (bpp) @@ -398,12 +249,14 @@ rgb_size[1] = 3; rgb_size[2] = 2; break; + case 15: case 16: rgb_size[0] = 5; rgb_size[1] = 5; rgb_size[2] = 5; break; + default: rgb_size[0] = 8; rgb_size[1] = 8; @@ -415,11 +268,13 @@ SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); + if (fsaa) { SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa); } + if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL) { fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); @@ -445,6 +300,7 @@ printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value); SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value); printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value); + if (fsaa) { SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value); @@ -459,15 +315,13 @@ /* Set the gamma for the window */ if (gamma != 0.0) - { - SDL_SetGamma (gamma, gamma, gamma); - } + SDL_SetGamma (gamma, gamma, gamma); - glViewport (0, 0, w, h); - glMatrixMode (GL_PROJECTION); - glLoadIdentity (); - - glOrtho (-2.0, 2.0, -2.0, 2.0, -20.0, 20.0); + camera.p = point (0, 0, 10); + camera.d = vec3 (0, 0, -1); + camera.u = vec3 (0, 1, 0); + camera.w = w; camera.h = h; + camera.fov = 90; glMatrixMode (GL_MODELVIEW); glLoadIdentity (); @@ -478,6 +332,14 @@ glShadeModel (GL_SMOOTH); + glEnable (GL_LIGHTING); + + //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; + //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); + //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); + glEnable (GL_LIGHT0); + glEnable (GL_COLOR_MATERIAL); + /* Loop until done. */ start_time = SDL_GetTicks (); frames = 0; @@ -491,9 +353,23 @@ glClearColor (0.0, 0.0, 0.0, 1.0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBegin (GL_QUADS); + GLfloat lightp[4]; + lightp[0] = camera.p.x; + lightp[1] = camera.p.y; + lightp[2] = camera.p.z; + lightp[3] = 1; + glLightfv (GL_LIGHT0, GL_POSITION, lightp); + +#if 0 + static GLfloat ry; + ry += 0.03; + v.d.x = cos (ry); + v.d.z = sin (ry); +#endif - glEnd (); + draw_context c; + c.mode = draw_context::LIGHTED; + camera.draw (c); SDL_GL_SwapBuffers (); @@ -501,9 +377,7 @@ gl_error = glGetError (); if (gl_error != GL_NO_ERROR) - { - fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); - } + fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); sdl_error = SDL_GetError (); @@ -514,7 +388,7 @@ } /* Allow the user to see what's happening */ - SDL_Delay (20); + //SDL_Delay (20); /* Check if there's a pending event. */ while (SDL_PollEvent (&event)) @@ -559,6 +433,16 @@ int noframe = 0; int fsaa = 0; + // load a entity + txtprt_parser p; + entity *e; + try { + e = p.read ("test.blasc"); + } catch (txtprt_i_exception & e) { + cout << "ERR: " << e.msg << endl; + } + e->show (); + logo = 0; slowly = 0; numtests = 1;