]> git.jsancho.org Git - lugaru.git/blobdiff - Source/OpenGL_Windows.cpp
convert Game class to namespace
[lugaru.git] / Source / OpenGL_Windows.cpp
index cc8103ce8e80e7cfbbf61f4093006d0c421c3aea..a9edfb56952e54b6a068342e2d366fe18e99fea0 100644 (file)
@@ -45,6 +45,8 @@ extern "C" {
        #endif
 }
 
+using namespace Game;
+
 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);
@@ -99,10 +101,8 @@ static SDL_Rect *hardcoded_resolutions[] = {
     NULL
 };
 
-void DrawGL(Game & game);
-
-Boolean SetUp (Game & game);
-void DoUpdate (Game & game);
+Boolean SetUp ();
+void DoUpdate ();
 
 void CleanUp (void);
 
@@ -163,8 +163,6 @@ int kContextHeight;
 
 Boolean gDone = false;
 
-Game * pgame = 0;
-
 #ifndef __MINGW32__
 static int _argc = 0;
 static char **_argv = NULL;
@@ -239,57 +237,50 @@ void initGL(){
 }
 
 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);
-    //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();
+       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){
+static void sdlEventProc(const SDL_Event &e)
+{
+    switch(e.type) {
         case SDL_MOUSEMOTION:
-            game.deltah += e.motion.xrel;
-            game.deltav += e.motion.yrel;
+            deltah += e.motion.xrel;
+            deltav += e.motion.yrel;
             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) ) {
-                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;
-                }
+                                (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();
             }
             break;
     }
 }
 
 
+
 // --------------------------------------------------------------------------
 
 static Point gMidPoint;
 
-Boolean SetUp (Game & game)
+Boolean SetUp ()
 {
        char string[10];
 
@@ -302,7 +293,7 @@ Boolean SetUp (Game & game)
        slomofreq=8012;
        numplayers=1;
        
-       DefaultSettings(game);
+       DefaultSettings();
 
     if (!SDL_WasInit(SDL_INIT_VIDEO))
         if (SDL_Init(SDL_INIT_VIDEO) == -1)
@@ -310,9 +301,9 @@ Boolean SetUp (Game & game)
             fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
             return false;
         }
-       if(!LoadSettings(game)) {
+       if(!LoadSettings()) {
                fprintf(stderr, "Failed to load config, creating default\n");
-               SaveSettings(game);
+               SaveSettings();
        }
        if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
                kBitsPerPixel=16;
@@ -362,9 +353,9 @@ Boolean SetUp (Game & game)
        }
 
     Uint32 sdlflags = SDL_OPENGL;
-    //TODO: commented out temporarily
-    //if (!cmdline("windowed"))
-        //sdlflags |= SDL_FULLSCREEN;
+    
+    if (!cmdline("windowed"))
+        sdlflags |= SDL_FULLSCREEN;
 
     SDL_WM_SetCaption("Lugaru", "Lugaru");
 
@@ -422,33 +413,33 @@ Boolean SetUp (Game & game)
        screenwidth=width;
        screenheight=height;
 
-       game.newdetail=detail;
-       game.newscreenwidth=screenwidth;
-       game.newscreenheight=screenheight;
+       newdetail=detail;
+       newscreenwidth=screenwidth;
+       newscreenheight=screenheight;
 
-       game.InitGame();
+       InitGame();
 
        return true;
 }
 
 
-static void DoMouse(Game & game)
+static void DoMouse()
 {
 
-       if(mainmenu|| ( (abs(game.deltah)<10*realmultiplier*1000) && (abs(game.deltav)<10*realmultiplier*1000) ))
+       if(mainmenu|| ( (abs(deltah)<10*realmultiplier*1000) && (abs(deltav)<10*realmultiplier*1000) ))
        {
-               game.deltah *= usermousesensitivity;
-               game.deltav *= usermousesensitivity;
-               game.mousecoordh += game.deltah;
-               game.mousecoordv += game.deltav;
-        if (game.mousecoordh < 0)
-            game.mousecoordh = 0;
-        else if (game.mousecoordh >= kContextWidth)
-            game.mousecoordh = kContextWidth - 1;
-        if (game.mousecoordv < 0)
-            game.mousecoordv = 0;
-        else if (game.mousecoordv >= kContextHeight)
-            game.mousecoordv = kContextHeight - 1;
+               deltah *= usermousesensitivity;
+               deltav *= usermousesensitivity;
+               mousecoordh += deltah;
+               mousecoordv += deltav;
+        if (mousecoordh < 0)
+            mousecoordh = 0;
+        else if (mousecoordh >= kContextWidth)
+            mousecoordh = kContextWidth - 1;
+        if (mousecoordv < 0)
+            mousecoordv = 0;
+        else if (mousecoordv >= kContextHeight)
+            mousecoordv = kContextHeight - 1;
        }
 
 }
@@ -489,7 +480,7 @@ void DoFrameRate (int update)
 }
 
 
-void DoUpdate (Game & game)
+void DoUpdate ()
 {
        static float sps=200;
        static int count;
@@ -498,7 +489,7 @@ void DoUpdate (Game & game)
        DoFrameRate(1);
        if(multiplier>.6)multiplier=.6;
 
-       game.fps=1/multiplier;
+       fps=1/multiplier;
 
        count = multiplier*sps;
        if(count<2)count=2;
@@ -508,22 +499,22 @@ void DoUpdate (Game & game)
        if(difficulty==1)multiplier*=.9;
        if(difficulty==0)multiplier*=.8;
 
-       if(game.loading==4)multiplier*=.00001;
+       if(loading==4)multiplier*=.00001;
        if(slomo&&!mainmenu)multiplier*=slomospeed;
        oldmult=multiplier;
        multiplier/=(float)count;
 
-       DoMouse(game);
+       DoMouse();
 
-       game.TickOnce();
+       TickOnce();
 
        for(int i=0;i<count;i++)
        {
-               game.Tick();
+               Tick();
        }
        multiplier=oldmult;
 
-       game.TickOnceAfter();
+       TickOnceAfter();
 /* - Debug code to test how many channels were active on average per frame
        static long frames = 0;
 
@@ -558,7 +549,7 @@ void DoUpdate (Game & game)
                num_channels = 0;
        }
 */
-       game.DrawGL();
+       DrawGL();
 }
 
 // --------------------------------------------------------------------------
@@ -706,67 +697,64 @@ int main(int argc, char **argv)
        try
        {
                {
-                       Game game;
-                       pgame = &game;
+                       newGame();
 
                        //ofstream os("error.txt");
                        //os.close();
                        //ofstream os("log.txt");
                        //os.close();
 
-                       if (!SetUp (game))
+                       if (!SetUp ())
                 return 42;
 
-                       while (!gDone&&!game.quit&&(!game.tryquit))
+                       while (!gDone&&!quit&&(!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 ) ){
-                            switch(e.type){
-                            case SDL_QUIT:
-                                gDone=true;
-                                break;
-                            default:
-                                sdlEventProc(e, game);
-                                break;
+                                                       deltah = 0;
+                                                       deltav = 0;
+                                                       SDL_Event e;
+                                                       if(!waiting) {
+                                                                       // message pump
+                                                                       while( SDL_PollEvent( &e ) )
+                                                                       {
+                                                                                       if( e.type == SDL_QUIT )
+                                                                                       {
+                                                                                                       gDone=true;
+                                                                                                       break;
+                                                                                       }
+                                                                                       sdlEventProc(e);
+                                                                       }
                                                        }
-                                               }
-                                       }
 
-                                       // game
-                                       DoUpdate(game);
-                               /*
-                }
-                else
-                {
-                                       if (gameFocused)
-                                       {
-                                               // allow game chance to pause
-                                               gameFocused = false;
-                                               DoUpdate(game);
+                                                       // game
+                                                       DoUpdate();
                                        }
+                                       else
+                                       {
+                                                       if (gameFocused)
+                                                       {
+                                                                       // allow game chance to pause
+                                                                       gameFocused = false;
+                                                                       DoUpdate();
+                                                       }
 
-                                       // 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;
+                                       }
                        }
 
+            deleteGame();
                }
-               pgame = 0;
 
                CleanUp ();