]> git.jsancho.org Git - lugaru.git/blobdiff - Source/OpenGL_Windows.cpp
Refactor of the texture system
[lugaru.git] / Source / OpenGL_Windows.cpp
index 8a9b6923c2662945978da3161d66777ea4d3c4ce..c2091fb66c30036ee27edba036787b2d5a8fce71 100644 (file)
@@ -48,34 +48,21 @@ extern "C" {
 static bool load_image(const char * fname, TGAImageRec & tex);
 static bool load_png(const char * fname, TGAImageRec & tex);
 static bool load_jpg(const char * fname, TGAImageRec & tex);
-static bool save_image(const char * fname);
+bool save_image(const char * fname);
 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;
-// MODIFIED GWC
-//extern int terraindetail;
-//extern int texdetail;
-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;
@@ -101,8 +88,6 @@ extern float slomofreq;
 
 using namespace std;
 
-
-
 SDL_Rect **resolutions = NULL;
 static SDL_Rect rect_1024_768 = { 0, 0, 1024, 768 };
 static SDL_Rect rect_800_600  = { 0, 0, 800,  600 };
@@ -114,34 +99,14 @@ static SDL_Rect *hardcoded_resolutions[] = {
     NULL
 };
 
-
-
-unsigned int resolutionDepths[8][2] = {0};
-
-int closestResolution(int width, int height);
-int resolutionID(int width, int height);
-
-void ReportError (char * strError);
-
 void DrawGL(Game & game);
 
-void CreateGLWindow (void);
 Boolean SetUp (Game & game);
 void DoUpdate (Game & game);
 
-void DoEvent (void);
 void CleanUp (void);
 
-
 // statics/globals (internal only) ------------------------------------------
-#ifndef WIN32
-typedef struct tagPOINT { 
-  int x;
-  int y;
-} POINT, *PPOINT; 
-#endif
-
-
 
 #ifdef _MSC_VER
 #pragma warning(push)
@@ -186,50 +151,17 @@ static void GLAPIENTRY glDeleteTextures_doNothing(GLsizei n, const GLuint *textu
     // no-op.
 }
 
-
-
-void sdlGetCursorPos(POINT *pt)
-{
-    SDL_GetMouseState(&(pt->x), &(pt->y));
-}
-#define GetCursorPos(x) sdlGetCursorPos(x)
-#define SetCursorPos(x, y) SDL_WarpMouse(x, y)
-#define ScreenToClient(x, pt)
-#define ClientToScreen(x, pt)
 #ifdef MessageBox
 #undef MessageBox
 #endif
 #define MessageBox(hwnd,text,title,flags) STUBBED("msgbox")
 
-
-Point delta;
-
-static bool g_button, fullscreen = true;
-
-
 // Menu defs
-enum 
-{
-       kFileQuit = 1
-};
-
-enum 
-{
-       kForegroundSleep = 10,
-       kBackgroundSleep = 10000
-};
-
 
 int kContextWidth;
 int kContextHeight;
 
-const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 };
-
-GLuint gFontList;
-char gcstrMode [256] = "";
-
-UInt32 gSleepTime = kForegroundSleep;
-Boolean gDone = false, gfFrontProcess = true;
+Boolean gDone = false;
 
 Game * pgame = 0;
 
@@ -252,80 +184,100 @@ bool cmdline(const char *cmd)
     return false;
 }
 
+//-----------------------------------------------------------------------------------------------------------------------
 
-// --------------------------------------------------------------------------
-
-void ReportError (char * strError)
-{
-#ifdef _MSC_VER  // !!! FIXME.  --ryan.
-       throw std::exception( strError);
-#endif
-
-       /*      char errMsgCStr [256];
-       Str255 strErr;
-
-       sprintf (errMsgCStr, "%s", strError); 
+// OpenGL Drawing
 
-       // out as debug string
-       CToPStr (strErr, errMsgCStr);
-       DebugStr (strErr);
-       */
-}
+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);
 
-// OpenGL Drawing
+       // 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);
 
-void DrawGL (Game & game)
-{
-       if ( stereomode == stereoNone ) {
-               game.DrawGLScene(stereoCenter);
+       if ( CanInitStereo(stereomode) ) {
+               InitStereo(stereomode);
        } else {
-               game.DrawGLScene(stereoLeft);
-               game.DrawGLScene(stereoRight);
+               fprintf(stderr, "Failed to initialize stereo, disabling.\n");
+               stereomode = stereoNone;
        }
 }
 
-static inline int clamp_sdl_mouse_button(Uint8 button)
-{
-    if (button == 2)   // right mouse button is button 3 in SDL.
-        button = 3;
-    else if (button == 3)
-        button = 2;
-
-    if ((button >= 1) && (button <= 3))
-        return button - 1;
-    return -1;
+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)
 {
-    int val;
-    SDLMod mod;
-
-    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;
-
-        case SDL_KEYUP:
-            return;
+            break;
     }
 }
 
 
+
 // --------------------------------------------------------------------------
 
 static Point gMidPoint;
@@ -336,12 +288,9 @@ Boolean SetUp (Game & game)
 
        LOGFUNC;
 
-       randSeed = UpTime().lo;
-
        osx = 0;
        cellophane=0;
        texdetail=4;
-       terraindetail=2;
        slomospeed=0.25;
        slomofreq=8012;
        numplayers=1;
@@ -406,6 +355,7 @@ Boolean SetUp (Game & game)
        }
 
     Uint32 sdlflags = SDL_OPENGL;
+    
     if (!cmdline("windowed"))
         sdlflags |= SDL_FULLSCREEN;
 
@@ -415,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)
     {
@@ -453,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;
@@ -505,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;
@@ -521,7 +428,7 @@ Boolean SetUp (Game & game)
 static void DoMouse(Game & game)
 {
 
-       if(mainmenu||(abs(game.deltah)<10*realmultiplier*1000&&abs(game.deltav)<10*realmultiplier*1000))
+       if(mainmenu|| ( (abs(game.deltah)<10*realmultiplier*1000) && (abs(game.deltav)<10*realmultiplier*1000) ))
        {
                game.deltah *= usermousesensitivity;
                game.deltav *= usermousesensitivity;
@@ -588,7 +495,6 @@ void DoUpdate (Game & game)
 
        count = multiplier*sps;
        if(count<2)count=2;
-       //if(count>10)count=10;
 
        realmultiplier=multiplier;
        multiplier*=gamespeed;
@@ -596,9 +502,7 @@ void DoUpdate (Game & game)
        if(difficulty==0)multiplier*=.8;
 
        if(game.loading==4)multiplier*=.00001;
-       //multiplier*.9;
        if(slomo&&!mainmenu)multiplier*=slomospeed;
-       //if(freeze)multiplier*=0.00001;
        oldmult=multiplier;
        multiplier/=(float)count;
 
@@ -647,7 +551,7 @@ void DoUpdate (Game & game)
                num_channels = 0;
        }
 */
-       DrawGL (game);
+       game.DrawGL();
 }
 
 // --------------------------------------------------------------------------
@@ -657,11 +561,6 @@ void CleanUp (void)
 {
        LOGFUNC;
 
-//     game.Dispose();
-
-
-
-
     SDL_Quit();
     #define GL_FUNC(ret,fn,params,call,rt) p##fn = NULL;
     #include "glstubs.h"
@@ -680,29 +579,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.)
@@ -820,13 +696,8 @@ int main(int argc, char **argv)
 
        LOGFUNC;
 
-       //memset( &g_theKeys, 0, sizeof( KeyMap));
-
-    //initSDLKeyTable();
-
        try
        {
-               bool regnow = false;
                {
                        Game game;
                        pgame = &game;
@@ -841,51 +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;
+                                       }
                        }
 
-                       regnow = game.registernow;
+
                }
                pgame = 0;
 
@@ -912,77 +783,21 @@ 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)
-       {
-               if ( tex.data == NULL )
-                       return false;
-               else
-                       return load_image(fname, tex);
-       }
+bool LoadImage(const char * fname, TGAImageRec & tex)
+{
+       if ( tex.data == NULL )
+               return false;
+       else
+               return load_image(fname, tex);
+}
 
-       void ScreenShot(const char * fname)
-       {
-        save_image(fname);
-       }
+void ScreenShot(const char * fname)
+{
+       
+}
 
 
 
@@ -1150,7 +965,7 @@ png_done:
 }
 
 
-static bool save_image(const char *file_name)
+bool save_image(const char *file_name)
 {
     const char *ptr = strrchr((char *)file_name, '.');
     if (ptr)