moved event pumping onto the draw thread, should fix windows event problems
[physics.git] / src / main.cpp
index 1e9d4ad..e932742 100644 (file)
 
 /// ***** Private Method Headers *****
 
-void init();
+void mainInit();
+void mainClean();
+
+void updatesInit();
+void updatesClean();
+
+void drawInit();
+void drawClean();
 
 void sighandler( int sig );
 
 void run();
 void clean();
 
-void blockUpdate();
 void updateFPSCounters();
 
 void handleInput();
 void update(float);
 void draw();
 
+int startUpdateThread(void*);
+int startDrawThread(void*);
+
 /// ***** Private Variables *****
 
 // variable used to determine if it is time to shutdown
@@ -52,9 +61,7 @@ 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
@@ -62,150 +69,103 @@ bool is_Running;
  * last_Second := stores the time of the last second, used for ups and fps
  */
 int target_UPS = 100;
-long int last_Block_Update;
-float update_Sum = 0;
+float update_Wait = 0;
 
-int ups=100, fps=100;
+int ups, fps;
 int update_Count, draw_Count;
-long int last_Second;
-
+MICRO last_Second;
 
-float target_time_steps_per_second = 1000;
-float times;
-float time_steps_Count;
+const int min_wait_milli = 20;
+const int min_wait_micro = min_wait_milli * 1000;
+const float game_step    = 10;
 
-
-// experiment with rolling averages
-float rUpdate = 100;
-float rDraw = 100;
-float rInput = 100;
-float rRun = 100;
-
-float num = 10;
-
-float total = 0;
+bool updateInitialized  = false;
+bool drawInitialized    = false;
 
 /// ***** MAIN Method *****
 int main(int argc, char** args)
 {
-    init();
+    mainInit();
     run();
-    clean();
+    mainClean();
     return 0;
 }
 
 /// ***** Initializers/Cleaners *****
 
-void init()
+void mainInit()
 {
     installSignal();
 
-    graphics::init();
+    debug::init();
+}
 
-#ifdef DEBUGGING
-    cout << "Graphics initialized" << endl;
-    cout << "Graphics initialized2" << endl;
-#endif
+void mainClean()
+{
+    debug::clean();
+}
 
-    game::init();
+void updatesInit()
+{
+    while(!drawInitialized)
+        SDL_Delay(100);
 
-#ifdef DEBUGGING
-    cout << "Game initialized" << endl;
-#endif
+    game::init();
+    DPF(0, "Game initialized");
 
     input::init();
-
-#ifdef DEBUGGING
-    cout << "Input initialized" << endl;
-#endif
+    DPF(0, "Input initialized");
 
     cfg::init();
+    DPF(0, "Configs initialized");
 
-#ifdef DEBUGGING
-    cout << "Configs initialized" << endl;
-#endif
+    DPF(0, "Initialization Complete");
 
-#ifdef DEBUGGING
-    cout << "Initialization Complete" << endl;
-#endif
+    updateInitialized = true;
 }
 
-void clean()
+void updatesClean()
 {
-#ifdef DEBUGGING
-    cout << "Cleaning up" << endl;
-#endif
+    DPF(0, "Update Thread Cleaning");
 
     cfg::clean();
 
     input::clean();
 
     game::clean();
-
-    graphics::clean();
 }
 
-/// ***** Private Methods *****
-
-void run()
+void drawInit()
 {
-    is_Running = true;
-    last_Block_Update = tickCountMicro();
-    last_Second = tickCountMicro();
-
-    while(is_Running)
-    {
-        long int time;
-
-        time = tickCountMicro();
-            blockUpdate();
-            updateFPSCounters();
-            draw();
-        time = tickCountMicro() - time;
+    graphics::init();
+    DPF(0, "Graphics initialized");
 
-        rRun = (rRun * (num-1) + time) / num;
+    drawInitialized = true;
 
-        //cout << "total:\t" << total << endl;
-        //cout << "rR:\t" << rRun << endl;
-        //total = 0;
-    }
+    while(!updateInitialized)
+        SDL_Delay(100);
 }
 
-void blockUpdate()
+void drawClean()
 {
-    long int start = tickCountMicro();
-    long int diff = start - last_Block_Update;
-
-    //cout << "Block" << endl;
+    DPF(0, "Draw Thread Cleaning");
 
-    // Calculate the updates that should be run for the next draw
-    update_Sum += (float)(diff * target_UPS) / 1000000;
-
-    // insures the float to int cast is done once.
-    int iupdate_sum = (int)update_Sum;
+    graphics::clean();
+}
 
-    // 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
+/// ***** Private Methods *****
+SDL_Thread* updatesThread   = NULL;
+SDL_Thread* drawThread      = NULL;
 
-        //float time_step = ((float)diff) / iupdate_sum / 1000;
-        //float time_step = 1000 / (100000 / rUpdate) / iupdate_sum;
-        //float time_step = 1000 / ups / iupdate_sum;
-        float time_step = 10;
+void run()
+{
+    is_Running = true;
 
-        // run the updates
-        for (int i = 0; i < iupdate_sum; i++)
-        {
-            handleInput();
-            update(time_step);
-        }
+    updatesThread   = SDL_CreateThread(startUpdateThread,   NULL);
+    drawThread      = SDL_CreateThread(startDrawThread,     NULL);
 
-        // remove the updates that were run from the sum
-        update_Sum -= iupdate_sum;
-        last_Block_Update = tickCountMicro();
-    }
+    SDL_WaitThread(updatesThread,   NULL);
+    SDL_WaitThread(drawThread,      NULL);
 }
 
 void updateFPSCounters()
@@ -215,77 +175,89 @@ void updateFPSCounters()
     {
         ups = update_Count;
         fps = draw_Count;
-        times = time_steps_Count;
-        update_Count = 0;
-        draw_Count = 0;
-        time_steps_Count = 0;
+
+        // NOT thread safe, but they're estimates anyways
+        update_Count -= ups;
+        draw_Count -= fps;
 
         last_Second = tickCountMicro();
 
 #ifdef FPSUPS
         cout << "ups:\t" << ups << endl;
         cout << "fps:\t" << fps << endl;
-        cout << "times:\t" << times << endl;
 #endif
     }
 }
 
 void handleInput()
 {
-    long int time;
+    input::update();
 
-    time = tickCountMicro();
-        input::update();
+    game::handleInput();
 
-        game::handleInput();
-
-        if(cfg::endGame())
-            is_Running = false;
-    time = tickCountMicro() - time;
-
-    rInput = (rInput*(num-1) + time) /num;
-
-    total += rInput;
+    if(cfg::endGame())
+        is_Running = false;
 }
 
 void update(float time_step)
 {
-    long int time;
-
+    game::update(time_step);
     update_Count++;
-    time_steps_Count += time_step;
+}
 
-    time = tickCountMicro();
-        game::update(time_step);
-    time = tickCountMicro() - time;
+void draw()
+{
+    game::draw();
+
+    SDL_PumpEvents(); // has to be on the Draw thread for the Windows API
 
-    rUpdate = (rUpdate * (num-1) + time) / num;
+    SDL_GL_SwapBuffers();
 
-    //cout << "ts:\t" << time_step << endl;
-    //cout << "ru:\t" << rUpdate << endl;
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-    total += rUpdate;
+    draw_Count++;
 }
 
-void draw()
+int startUpdateThread(void*)
 {
-    long int time;
+    updatesInit();
 
-    draw_Count++;
+    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;
 
-    time = tickCountMicro();
-        game::draw();
+        while(min_wait_micro < update_Wait)
+        {
+            update_Wait -= min_wait_micro;
+            SDL_Delay(min_wait_milli);
+        }
+    }
 
-        SDL_GL_SwapBuffers();
+    updatesClean();
 
-        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+    return 0;
+}
 
-        //SDL_Delay(10);
-    time = tickCountMicro() - time;
+int startDrawThread(void*)
+{
+    drawInit();
 
-    rDraw = (rDraw*(num-1) + time) /num;
+    while(is_Running)
+    {
+        draw();
+    }
 
-    //cout << "rd:\t" << rDraw << endl;
+    drawClean();
 
-    total += rDraw;
+    return 0;
 }