time step fixed
[physics.git] / src / main.cpp
index 7313306..01afeaa 100644 (file)
@@ -1,3 +1,20 @@
+/*
+ *  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 "graphics/graphics.h"
 #include "input/inputManager.h"
-
-
+#include "config/config.h"
 
 /// ***** Private Method Headers *****
+
 void init();
 
 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;
-}
-
-
 /// ***** Private Variables *****
 
 // variable used to determine if it is time to shutdown
@@ -55,40 +61,76 @@ bool is_Running;
  * draw_Count  := counts this seconds draws
  * last_Second := stores the time of the last second, used for ups and fps
  */
-int target_UPS = 250;
+int target_UPS = 100;
 long int last_Block_Update;
 float update_Sum = 0;
 
-int ups, fps;
+int ups =100, fps=100;
 int update_Count, draw_Count;
 long int last_Second;
 
 
-/// ***** Private Methods *****
+float target_time_steps_per_second = 1000;
+float times;
+float time_steps_Count;
+
+
+// experiment with rolling averages
+float rUpdate = 100;
+float rDraw = 100;
+float rInput = 100;
+float rRun = 100;
+
+float num = 10;
+
+
+/// ***** MAIN Method *****
+int main()
+{
+    init();
+    run();
+    clean();
+    return 0;
+}
+
+/// ***** Initializers/Cleaners *****
+
 void init()
 {
     installSignal();
 
-    graphicsInit();
+    graphics::init();
+
+    game::init();
+
+    input::init();
 
-    gameInit();
+    cfg::init();
 
 #ifdef DEBUGGING
     cout << "Initialization Complete" << endl;
 #endif
 }
 
-void cleanUp()
+void clean()
 {
 #ifdef DEBUGGING
     cout << "Cleaning up" << endl;
 #endif
 
-    gameClean();
+    cfg::clean();
 
-    graphicsCleanUp();
+    input::clean();
+
+    game::clean();
+
+    graphics::clean();
 }
 
+/// ***** Private Methods *****
+
+float total = 0;
+
 void run()
 {
     is_Running = true;
@@ -97,18 +139,31 @@ void run()
 
     while(is_Running)
     {
-        blockUpdate();
-        updateFPSCounters();
-        draw();
+        long int time;
+
+        time = tickCountMicro();
+            blockUpdate();
+            updateFPSCounters();
+            draw();
+        time = tickCountMicro() - time;
+
+        rRun = (rRun * (num-1) + time) / num;
+
+        //cout << "total:\t" << total << endl;
+        //cout << "rR:\t" << rRun << endl;
+        //total = 0;
     }
 }
 
 void blockUpdate()
 {
     long int start = tickCountMicro();
+    long int diff = start - last_Block_Update;
+
+    //cout << "Block" << endl;
 
     // Calculate the updates that should be run for the next draw
-    update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS);
+    update_Sum += diff / (1000000 / (float)target_UPS);
 
     // insures the float to int cast is done once.
     int iupdate_sum = (int)update_Sum;
@@ -118,13 +173,16 @@ void blockUpdate()
     {
         // 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;
+
+        //float time_step = ((float)diff) / iupdate_sum / 1000;
+        //float time_step = 1000 / (100000 / rUpdate) / iupdate_sum;
+        float time_step = 10;
 
         // run the updates
-        for (int i = 1; i <= iupdate_sum; i++)
+        for (int i = 0; i < iupdate_sum; i++)
         {
-            input();
-            update(time_step*i / 1000);
+            handleInput();
+            update(time_step);
         }
         // remove the updates that where run from the sum
         update_Sum -= iupdate_sum;
@@ -139,40 +197,77 @@ void updateFPSCounters()
     {
         ups = update_Count;
         fps = draw_Count;
+        times = time_steps_Count;
         update_Count = 0;
         draw_Count = 0;
+        time_steps_Count = 0;
 
         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;
+        cout << "times:\t" << times << endl;
+#endif
     }
 }
 
-void input()
+void handleInput()
 {
-    inputUpdate();
+    long int time;
+
+    time = tickCountMicro();
+        input::update();
+
+        game::handleInput();
+
+        if(cfg::endGame())
+            is_Running = false;
+    time = tickCountMicro() - time;
 
-    gameInput();
+    rInput = (rInput*(num-1) + time) /num;
 
-    if(wasReleased(SDLK_ESCAPE))
-        is_Running = false;
+    total += rInput;
 }
 
 void update(float time_step)
 {
+    long int time;
+
     update_Count++;
+    time_steps_Count += time_step;
+
+    time = tickCountMicro();
+        game::update(time_step);
+    time = tickCountMicro() - time;
+
+    rUpdate = (rUpdate * (num-1) + time) / num;
 
-    gameUpdate(time_step);
+    //cout << "ts:\t" << time_step << endl;
+    //cout << "ru:\t" << rUpdate << endl;
+
+    total += rUpdate;
 }
 
 void draw()
 {
+    long int time;
+
     draw_Count++;
 
-    gameDraw();
+    time = tickCountMicro();
+        game::draw();
+
+        SDL_GL_SwapBuffers();
+
+        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+//        SDL_Delay(10);
+    time = tickCountMicro() - time;
+
+    rDraw = (rDraw*(num-1) + time) /num;
 
-    SDL_GL_SwapBuffers();
+    //cout << "rd:\t" << rDraw << endl;
 
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+    total += rDraw;
 }