]> git.jsancho.org Git - lugaru.git/blobdiff - Source/OpenGL_Windows.cpp
Refactor of the texture system
[lugaru.git] / Source / OpenGL_Windows.cpp
index 5b3d92671336f5bd2ce16b9d7639f802e4e07740..c2091fb66c30036ee27edba036787b2d5a8fce71 100644 (file)
@@ -54,13 +54,6 @@ static bool save_png(const char * fname);
 
 #include "openal_wrapper.h"
 
-// ADDED GWC
-#ifdef _MSC_VER
-#pragma comment(lib, "opengl32.lib")
-#pragma comment(lib, "glu32.lib")
-#pragma comment(lib, "glaux.lib")
-#endif
-
 extern float multiplier;
 extern float sps;
 extern float realmultiplier;
@@ -195,28 +188,96 @@ bool cmdline(const char *cmd)
 
 // OpenGL Drawing
 
+void initGL(){
+       glClear( GL_COLOR_BUFFER_BIT );
+       swap_gl_buffers();
+
+       // clear all states
+       glDisable( GL_ALPHA_TEST);
+       glDisable( GL_BLEND);
+       glDisable( GL_DEPTH_TEST);
+       //      glDisable( GL_DITHER);
+       glDisable( GL_FOG);
+       glDisable( GL_LIGHTING);
+       glDisable( GL_LOGIC_OP);
+       glDisable( GL_TEXTURE_1D);
+       glDisable( GL_TEXTURE_2D);
+       glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
+       glPixelTransferi( GL_RED_SCALE, 1);
+       glPixelTransferi( GL_RED_BIAS, 0);
+       glPixelTransferi( GL_GREEN_SCALE, 1);
+       glPixelTransferi( GL_GREEN_BIAS, 0);
+       glPixelTransferi( GL_BLUE_SCALE, 1);
+       glPixelTransferi( GL_BLUE_BIAS, 0);
+       glPixelTransferi( GL_ALPHA_SCALE, 1);
+       glPixelTransferi( GL_ALPHA_BIAS, 0);
+
+       // set initial rendering states
+       glShadeModel( GL_SMOOTH);
+       glClearDepth( 1.0f);
+       glDepthFunc( GL_LEQUAL);
+       glDepthMask( GL_TRUE);
+       //      glDepthRange( FRONT_CLIP, BACK_CLIP);
+       glEnable( GL_DEPTH_TEST);
+       glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
+       glCullFace( GL_FRONT);
+       glEnable( GL_CULL_FACE);
+       glEnable( GL_LIGHTING);
+//     glEnable( GL_LIGHT_MODEL_AMBIENT);
+       glEnable( GL_DITHER);
+       glEnable( GL_COLOR_MATERIAL);
+       glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+       glAlphaFunc( GL_GREATER, 0.5f);
+
+       if ( CanInitStereo(stereomode) ) {
+               InitStereo(stereomode);
+       } else {
+               fprintf(stderr, "Failed to initialize stereo, disabling.\n");
+               stereomode = stereoNone;
+       }
+}
+
+static void toggleFullscreen(){
+       SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
+    //~ SDL_Surface* screen=SDL_GetVideoSurface();
+    //~ Uint32 flags=screen->flags;
+    //~ screen=SDL_SetVideoMode(0,0,0,flags^SDL_FULLSCREEN);
+    //~ if(!screen)
+        //~ screen=SDL_SetVideoMode(0,0,0,flags);
+    //~ if(!screen)
+        //~ exit(1);
+    //~ //reload opengl state
+    //~ initGL();
+    //~ for(std::vector<TextureInfo>::iterator it=Game::textures.begin(); it!=Game::textures.end(); it++) {
+        //~ it->load();
+    //~ }
+    //~ pgame->text.BuildFont();
+    //~ pgame->LoadScreenTexture();
+}
+
 static void sdlEventProc(const SDL_Event &e, Game &game)
 {
-    switch(e.type)
-       {
+    switch(e.type) {
         case SDL_MOUSEMOTION:
             game.deltah += e.motion.xrel;
             game.deltav += e.motion.yrel;
-            return;
+            break;
 
         case SDL_KEYDOWN:
             if ((e.key.keysym.sym == SDLK_g) &&
-                               (e.key.keysym.mod & KMOD_CTRL) &&
-                               !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN) ) {
-                               SDL_WM_GrabInput( ((SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON) ? SDL_GRAB_OFF:SDL_GRAB_ON) );
-                       } else if ( (e.key.keysym.sym == SDLK_RETURN) && (e.key.keysym.mod & KMOD_ALT) ) {
-                               SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
+                                (e.key.keysym.mod & KMOD_CTRL) &&
+                                !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN) ) {
+                                SDL_WM_GrabInput( ((SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON) ? SDL_GRAB_OFF:SDL_GRAB_ON) );
+            } else if ( (e.key.keysym.sym == SDLK_RETURN) && (e.key.keysym.mod & KMOD_ALT) ) {
+                                toggleFullscreen();
             }
-            return;
+            break;
     }
 }
 
 
+
 // --------------------------------------------------------------------------
 
 static Point gMidPoint;
@@ -294,6 +355,7 @@ Boolean SetUp (Game & game)
        }
 
     Uint32 sdlflags = SDL_OPENGL;
+    
     if (!cmdline("windowed"))
         sdlflags |= SDL_FULLSCREEN;
 
@@ -303,6 +365,9 @@ Boolean SetUp (Game & game)
 
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
+#if SDL_VERSION_ATLEAST(1, 2, 10)
+    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vblsync);
+#endif
     
     if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
     {
@@ -341,46 +406,7 @@ Boolean SetUp (Game & game)
         SDL_WM_GrabInput(SDL_GRAB_ON);
 
 
-       glClear( GL_COLOR_BUFFER_BIT );
-       swap_gl_buffers();
-
-       // clear all states
-       glDisable( GL_ALPHA_TEST);
-       glDisable( GL_BLEND);
-       glDisable( GL_DEPTH_TEST);
-       //      glDisable( GL_DITHER);
-       glDisable( GL_FOG);
-       glDisable( GL_LIGHTING);
-       glDisable( GL_LOGIC_OP);
-       glDisable( GL_TEXTURE_1D);
-       glDisable( GL_TEXTURE_2D);
-       glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
-       glPixelTransferi( GL_RED_SCALE, 1);
-       glPixelTransferi( GL_RED_BIAS, 0);
-       glPixelTransferi( GL_GREEN_SCALE, 1);
-       glPixelTransferi( GL_GREEN_BIAS, 0);
-       glPixelTransferi( GL_BLUE_SCALE, 1);
-       glPixelTransferi( GL_BLUE_BIAS, 0);
-       glPixelTransferi( GL_ALPHA_SCALE, 1);
-       glPixelTransferi( GL_ALPHA_BIAS, 0);
-
-       // set initial rendering states
-       glShadeModel( GL_SMOOTH);
-       glClearDepth( 1.0f);
-       glDepthFunc( GL_LEQUAL);
-       glDepthMask( GL_TRUE);
-       //      glDepthRange( FRONT_CLIP, BACK_CLIP);
-       glEnable( GL_DEPTH_TEST);
-       glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
-       glCullFace( GL_FRONT);
-       glEnable( GL_CULL_FACE);
-       glEnable( GL_LIGHTING);
-//     glEnable( GL_LIGHT_MODEL_AMBIENT);
-       glEnable( GL_DITHER);
-       glEnable( GL_COLOR_MATERIAL);
-       glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-       glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-       glAlphaFunc( GL_GREATER, 0.5f);
+    initGL();
 
        GLint width = kContextWidth;
        GLint height = kContextHeight;
@@ -393,13 +419,6 @@ Boolean SetUp (Game & game)
        game.newscreenwidth=screenwidth;
        game.newscreenheight=screenheight;
 
-       if ( CanInitStereo(stereomode) ) {
-               InitStereo(stereomode);
-       } else {
-               fprintf(stderr, "Failed to initialize stereo, disabling.\n");
-               stereomode = stereoNone;
-       }
-
        game.InitGame();
 
        return true;
@@ -693,50 +712,51 @@ int main(int argc, char **argv)
 
                        while (!gDone&&!game.quit&&(!game.tryquit))
                        {
-                               if (IsFocused())
-                               {
-                                       gameFocused = true;
+                                       if (IsFocused())
+                                       {
+                                                       gameFocused = true;
 
-                                       // check windows messages
+                                                       // check windows messages
                        
-                                       game.deltah = 0;
-                                       game.deltav = 0;
-                                       SDL_Event e;
-                                       if(!game.isWaiting()) {
-                                               // message pump
-                                               while( SDL_PollEvent( &e ) )
-                                               {
-                                                       if( e.type == SDL_QUIT )
-                                                       {
-                                                               gDone=true;
-                                                               break;
+                                                       game.deltah = 0;
+                                                       game.deltav = 0;
+                                                       SDL_Event e;
+                                                       if(!game.isWaiting()) {
+                                                                       // message pump
+                                                                       while( SDL_PollEvent( &e ) )
+                                                                       {
+                                                                                       if( e.type == SDL_QUIT )
+                                                                                       {
+                                                                                                       gDone=true;
+                                                                                                       break;
+                                                                                       }
+                                                                                       sdlEventProc(e, game);
+                                                                       }
                                                        }
-                                                       sdlEventProc(e, game);
-                                               }
-                                       }
 
-                                       // game
-                                       DoUpdate(game);
-                               }
-                               else
-                               {
-                                       if (gameFocused)
-                                       {
-                                               // allow game chance to pause
-                                               gameFocused = false;
-                                               DoUpdate(game);
+                                                       // game
+                                                       DoUpdate(game);
                                        }
+                                       else
+                                       {
+                                                       if (gameFocused)
+                                                       {
+                                                                       // allow game chance to pause
+                                                                       gameFocused = false;
+                                                                       DoUpdate(game);
+                                                       }
 
-                                       // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
-                                       SDL_ActiveEvent evt;
-                                       SDL_WaitEvent((SDL_Event*)&evt);
-                                       if (evt.type == SDL_ACTIVEEVENT && evt.gain == 1)
-                                               gameFocused = true;
-                                       else if (evt.type == SDL_QUIT)
-                                               gDone = true;
-                               }
+                                                       // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
+                                                       SDL_ActiveEvent evt;
+                                                       SDL_WaitEvent((SDL_Event*)&evt);
+                                                       if (evt.type == SDL_ACTIVEEVENT && evt.gain == 1)
+                                                                       gameFocused = true;
+                                                       else if (evt.type == SDL_QUIT)
+                                                                       gDone = true;
+                                       }
                        }
 
+
                }
                pgame = 0;