]> Dogcows Code - chaz/yoink/blobdiff - src/engine.cc
main loop code fixed to decouple updates and draws
[chaz/yoink] / src / engine.cc
index ec6faa15e9d0505c027c659aa2867ae62eb263ba..7284799343a1f144d136ac65705f94c0ff54ad67 100644 (file)
@@ -75,11 +75,11 @@ public:
                timestep = scalar(ts);
 
                long maxfps = 40;
-               config.get("engine.maxfps", maxfps);
+               config.getNumber("video.maxfps", maxfps);
                drawrate = 1.0 / scalar(maxfps);
 
                printfps = false;
-               config.get("engine.printfps", printfps);
+               config.get("video.printfps", printfps);
        }
 
        ~engine_impl()
@@ -92,6 +92,13 @@ public:
        }
 
 
+       /**
+        * The main loop.  This just calls dispatchEvents(), update(), and draw()
+        * over and over again.  The timing of the update and draw are decoupled.
+        * The actual frame rate is also calculated here.  This function will return
+        * with a value of 0 if the member variable running becomes true.
+        */
+
        int run()
        {
                scalar ticksNow = ticks();
@@ -101,6 +108,7 @@ public:
                scalar nextFPSUpdate = ticksNow + 1.0;
 
                scalar totalTime = 0.0;
+               scalar deltaTime = 0.0;
                scalar accumulator = timestep;
 
                fps = 0;
@@ -109,26 +117,22 @@ public:
                running = true;
                do
                {
-                       dispatchEvents();
-
                        scalar newTicks = ticks();
-                       accumulator += newTicks - ticksNow;
+                       deltaTime = newTicks - ticksNow;
                        ticksNow = newTicks;
 
-                       if (accumulator >= timestep)
+                       if (deltaTime >= 0.25) deltaTime = 0.25;
+                       accumulator += deltaTime;
+
+                       while (accumulator >= timestep)
                        {
+                               dispatchEvents();
                                interface->update(totalTime, timestep);
 
                                totalTime += timestep;
                                accumulator -= timestep;
 
                                nextStep += timestep;
-                               if (ticksNow >= nextStep)
-                               {
-                                       // we missed some scheduled steps, so reset the schedule
-                                       nextStep = ticksNow + timestep;
-                                       accumulator = 0.0;
-                               }
                        }
 
                        if (ticksNow >= nextDraw)
This page took 0.023722 seconds and 4 git commands to generate.