]> git.jsancho.org Git - lugaru.git/blobdiff - Source/OpenGL_Windows.cpp
beginnings of windowed mode support
[lugaru.git] / Source / OpenGL_Windows.cpp
index 36998186c323d4351157336184096481115cedd5..c74939ec77a30f3472b2e3c6e9aece7263eb7561 100644 (file)
@@ -54,25 +54,15 @@ 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;
 extern int slomo;
 extern bool cellophane;
-extern float terraindetail;
 extern float texdetail;
 
 extern bool osx;
-extern int numplayers;
 extern bool freeze;
-extern Person player[maxplayers];
 extern bool stillloading;
 extern int mainmenu;
 /*extern*/ bool gameFocused;
@@ -198,24 +188,95 @@ bool cmdline(const char *cmd)
 
 // OpenGL Drawing
 
-static void sdlEventProc(const SDL_Event &e, Game &game)
-{
-    switch(e.type)
-       {
+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;
+       }
+
+    //TODO: load textures here
+}
+
+static void toggleFullscreen(){
+    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);
+    initGL();
+}
+
+static void sdlEventProc(const SDL_Event &e, Game &game){
+    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());
+                toggleFullscreen();
+            }
+            break;
+        case SDL_ACTIVEEVENT:
+            if(e.active.state&SDL_APPINPUTFOCUS){
+                if(e.active.gain){
+                    SDL_WM_GrabInput(SDL_GRAB_ON);
+                    gameFocused=true;
+                }else{
+                    SDL_WM_GrabInput(SDL_GRAB_OFF);
+                    gameFocused=false;
+                }
             }
-            return;
+            break;
     }
 }
 
@@ -230,12 +291,9 @@ Boolean SetUp (Game & game)
 
        LOGFUNC;
 
-       randSeed = UpTime().lo;
-
        osx = 0;
        cellophane=0;
        texdetail=4;
-       terraindetail=2;
        slomospeed=0.25;
        slomofreq=8012;
        numplayers=1;
@@ -309,6 +367,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)
     {
@@ -347,46 +408,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;
@@ -399,13 +421,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;
@@ -566,29 +581,6 @@ static bool IsFocused()
 }
 
 
-static void launch_web_browser(const char *url)
-{
-#ifdef WIN32
-    ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
-
-#elif (defined(__APPLE__) && defined(__MACH__))
-    const char *fmt = "open '%s'";
-    const size_t len = strlen(fmt) + strlen(url) + 16;
-    char *buf = new char[len];
-    snprintf(buf, len, fmt, url);
-    system(buf);
-    delete[] buf;
-
-#elif PLATFORM_LINUX
-    const char *fmt = "PATH=$PATH:. xdg-open '%s'";
-    const size_t len = strlen(fmt) + strlen(url) + 16;
-    char *buf = new char[len];
-    snprintf(buf, len, fmt, url);
-    system(buf);
-    delete[] buf;
-#endif
-}
-
 
 #ifndef WIN32
 // (code lifted from physfs: http://icculus.org/physfs/ ... zlib license.)
@@ -708,7 +700,6 @@ int main(int argc, char **argv)
 
        try
        {
-               bool regnow = false;
                {
                        Game game;
                        pgame = &game;
@@ -723,9 +714,8 @@ int main(int argc, char **argv)
 
                        while (!gDone&&!game.quit&&(!game.tryquit))
                        {
-                               if (IsFocused())
-                               {
-                                       gameFocused = true;
+                               //if (IsFocused()) {
+                                       //gameFocused = true;
 
                                        // check windows messages
                        
@@ -734,22 +724,24 @@ int main(int argc, char **argv)
                                        SDL_Event e;
                                        if(!game.isWaiting()) {
                                                // message pump
-                                               while( SDL_PollEvent( &e ) )
-                                               {
-                                                       if( e.type == SDL_QUIT )
-                                                       {
-                                                               gDone=true;
-                                                               break;
+                                               while( SDL_PollEvent( &e ) ){
+                            switch(e.type){
+                            case SDL_QUIT:
+                                gDone=true;
+                                break;
+                            default:
+                                sdlEventProc(e, game);
+                                break;
                                                        }
-                                                       sdlEventProc(e, game);
                                                }
                                        }
 
                                        // game
                                        DoUpdate(game);
-                               }
-                               else
-                               {
+                               /*
+                }
+                else
+                {
                                        if (gameFocused)
                                        {
                                                // allow game chance to pause
@@ -765,14 +757,14 @@ int main(int argc, char **argv)
                                        else if (evt.type == SDL_QUIT)
                                                gDone = true;
                                }
+                */
                        }
 
-                       regnow = game.registernow;
                }
                pgame = 0;
 
                CleanUp ();
-               
+
                return 0;
        }
        catch (const std::exception& error)
@@ -796,62 +788,6 @@ int main(int argc, char **argv)
 
 // --------------------------------------------------------------------------
 
-extern int channels[100];
-extern OPENAL_SAMPLE * samp[100];
-extern OPENAL_STREAM * strm[20];
-
-extern "C" void PlaySoundEx(int chan, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
-{
-       const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
-       if (currSample && currSample == samp[chan])
-       {
-               if (OPENAL_GetPaused(channels[chan]))
-               {
-                       OPENAL_StopSound(channels[chan]);
-                       channels[chan] = OPENAL_FREE;
-               }
-               else if (OPENAL_IsPlaying(channels[chan]))
-               {
-                       int loop_mode = OPENAL_GetLoopMode(channels[chan]);
-                       if (loop_mode & OPENAL_LOOP_OFF)
-                       {
-                               channels[chan] = OPENAL_FREE;
-                       }
-               }
-       }
-       else
-       {
-               channels[chan] = OPENAL_FREE;
-       }
-
-       channels[chan] = OPENAL_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
-       if (channels[chan] < 0)
-       {
-               channels[chan] = OPENAL_PlaySoundEx(OPENAL_FREE, sptr, dsp, startpaused);
-       }
-}
-
-extern "C" void PlayStreamEx(int chan, OPENAL_STREAM *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused)
-{
-       const OPENAL_SAMPLE * currSample = OPENAL_GetCurrentSample(channels[chan]);
-       if (currSample && currSample == OPENAL_Stream_GetSample(sptr))
-       {
-                       OPENAL_StopSound(channels[chan]);
-                       OPENAL_Stream_Stop(sptr);
-       }
-       else
-       {
-               OPENAL_Stream_Stop(sptr);
-               channels[chan] = OPENAL_FREE;
-       }
-
-       channels[chan] = OPENAL_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
-       if (channels[chan] < 0)
-       {
-               channels[chan] = OPENAL_Stream_PlayEx(OPENAL_FREE, sptr, dsp, startpaused);
-       }
-}
-
 
 bool LoadImage(const char * fname, TGAImageRec & tex)
 {