moved event pumping onto the draw thread, should fix windows event problems
[physics.git] / src / main.cpp
index 5053640..e932742 100644 (file)
@@ -1,42 +1,58 @@
+/*
+ *  Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <GL/gl.h>
 #include <GL/glu.h>
 #include <SDL/SDL.h>
 
-#include <vector>
-using std::vector;
-
 #include "debug.h"
+#include "handleSignal.h"
 
 #include "game.h"
 #include "ticks.h"
 
 #include "graphics/graphics.h"
 #include "input/inputManager.h"
-
+#include "config/config.h"
 
 /// ***** Private Method Headers *****
-void init();
+
+void mainInit();
+void mainClean();
+
+void updatesInit();
+void updatesClean();
+
+void drawInit();
+void drawClean();
+
+void sighandler( int sig );
 
 void run();
-void cleanUp();
+void clean();
 
-void blockUpdate();
 void updateFPSCounters();
 
-void input();
+void handleInput();
 void update(float);
 void draw();
 
-
-/// ***** MAIN Method *****
-int main()
-{
-    init();
-    run();
-    cleanUp();
-    return 0;
-}
-
+int startUpdateThread(void*);
+int startDrawThread(void*);
 
 /// ***** Private Variables *****
 
@@ -45,91 +61,111 @@ bool is_Running;
 
 /* Values for the main game loop
  * target_UPS := the amount of updates that is wanted in one second
- * last_Update := stores the time of the last update
- * update_Sum := used to determine the updates needs for this run
-
+ *
  * ups := updates per second for the last second
  * fps := frames per second for the last second
  * update_Count := counts this seconds updates
  * draw_Count  := counts this seconds draws
  * last_Second := stores the time of the last second, used for ups and fps
  */
-int target_UPS = 250;
-long int last_Block_Update;
-float update_Sum = 0;
+int target_UPS = 100;
+float update_Wait = 0;
 
 int ups, fps;
 int update_Count, draw_Count;
-long int last_Second;
+MICRO last_Second;
 
+const int min_wait_milli = 20;
+const int min_wait_micro = min_wait_milli * 1000;
+const float game_step    = 10;
 
-/// ***** Private Methods *****
-void init()
+bool updateInitialized  = false;
+bool drawInitialized    = false;
+
+/// ***** MAIN Method *****
+int main(int argc, char** args)
 {
-    graphicsInit();
+    mainInit();
+    run();
+    mainClean();
+    return 0;
+}
 
-    gameInit();
+/// ***** Initializers/Cleaners *****
 
-    // TODO
-    // add a game state
+void mainInit()
+{
+    installSignal();
 
-#ifdef DEBUGGING
-    cout << "Initialization Complete" << endl;
-#endif
+    debug::init();
 }
 
-void cleanUp()
+void mainClean()
 {
-#ifdef DEBUGGING
-    cout << "Cleaning up" << endl;
-#endif
+    debug::clean();
+}
+
+void updatesInit()
+{
+    while(!drawInitialized)
+        SDL_Delay(100);
+
+    game::init();
+    DPF(0, "Game initialized");
 
-    gameClean();
+    input::init();
+    DPF(0, "Input initialized");
 
-    graphicsCleanUp();
+    cfg::init();
+    DPF(0, "Configs initialized");
+
+    DPF(0, "Initialization Complete");
+
+    updateInitialized = true;
 }
 
-void run()
+void updatesClean()
 {
-    is_Running = true;
-    last_Block_Update = tickCountMicro();
-    last_Second = tickCountMicro();
+    DPF(0, "Update Thread Cleaning");
 
-    while(is_Running)
-    {
-        blockUpdate();
-        updateFPSCounters();
-        draw();
-    }
+    cfg::clean();
+
+    input::clean();
+
+    game::clean();
 }
 
-void blockUpdate()
+void drawInit()
 {
-    long int start = tickCountMicro();
+    graphics::init();
+    DPF(0, "Graphics initialized");
 
-    // Calculate the updates that should be run for the next draw
-    update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS);
+    drawInitialized = true;
 
-    // insures the float to int cast is done once.
-    int iupdate_sum = (int)update_Sum;
+    while(!updateInitialized)
+        SDL_Delay(100);
+}
 
-    // TODO the main run loop needs to be tested and pruned
-    if (iupdate_sum > 0)
-    {
-        // Calculate a time step that spreads the updates out as much as possible
-        // used because really quick updates are nearly wasted
-        float time_step = ((float)(start - last_Block_Update)) / iupdate_sum;
+void drawClean()
+{
+    DPF(0, "Draw Thread Cleaning");
 
-        // run the updates
-        for (int i = 1; i <= iupdate_sum; i++)
-        {
-            input();
-            update(time_step*i / 1000);
-        }
-        // remove the updates that where run from the sum
-        update_Sum -= iupdate_sum;
-        last_Block_Update = tickCountMicro();
-    }
+    graphics::clean();
+}
+
+/// ***** Private Methods *****
+SDL_Thread* updatesThread   = NULL;
+SDL_Thread* drawThread      = NULL;
+
+void run()
+{
+    is_Running = true;
+
+    updatesThread   = SDL_CreateThread(startUpdateThread,   NULL);
+    drawThread      = SDL_CreateThread(startDrawThread,     NULL);
+
+    SDL_WaitThread(updatesThread,   NULL);
+    SDL_WaitThread(drawThread,      NULL);
 }
 
 void updateFPSCounters()
@@ -139,40 +175,89 @@ void updateFPSCounters()
     {
         ups = update_Count;
         fps = draw_Count;
-        update_Count = 0;
-        draw_Count = 0;
+
+        // NOT thread safe, but they're estimates anyways
+        update_Count -= ups;
+        draw_Count -= fps;
 
         last_Second = tickCountMicro();
 
-        //cout << "ups:\t" << ups << endl;
-        //cout << "fps:\t" << fps << endl;
+#ifdef FPSUPS
+        cout << "ups:\t" << ups << endl;
+        cout << "fps:\t" << fps << endl;
+#endif
     }
 }
 
-void input()
+void handleInput()
 {
-    inputUpdate();
+    input::update();
 
-    gameInput();
+    game::handleInput();
 
-    if(wasReleased(SDLK_ESCAPE))
+    if(cfg::endGame())
         is_Running = false;
 }
 
 void update(float time_step)
 {
+    game::update(time_step);
     update_Count++;
-
-    gameUpdate(time_step);
 }
 
 void draw()
 {
-    draw_Count++;
+    game::draw();
 
-    gameDraw();
+    SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
 
     SDL_GL_SwapBuffers();
 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+    draw_Count++;
+}
+
+int startUpdateThread(void*)
+{
+    updatesInit();
+
+    last_Second = tickCountMicro();
+
+    while(is_Running)
+    {
+        MICRO time = tickCountMicro();
+            handleInput();
+            update(game_step);
+
+            updateFPSCounters();
+        time = tickCountMicro() - time;
+
+        float wait = (1000000.0 / target_UPS - time);
+        update_Wait += 0 < wait ? wait : 0;
+
+        while(min_wait_micro < update_Wait)
+        {
+            update_Wait -= min_wait_micro;
+            SDL_Delay(min_wait_milli);
+        }
+    }
+
+    updatesClean();
+
+    return 0;
+}
+
+int startDrawThread(void*)
+{
+    drawInit();
+
+    while(is_Running)
+    {
+        draw();
+    }
+
+    drawClean();
+
+    return 0;
 }