]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Core.cc
game loop tweaks; shapes hierarchy defined
[chaz/yoink] / src / Moof / Core.cc
index b9ad92a63a8e29e8c7f59e1e6cc310a03bca5b36..b659f9890784c2c66cdc867b91671914d0f5260c 100644 (file)
@@ -70,6 +70,7 @@ public:
        void run()
        {
                init();
+               ASSERT(video && "cannot run core without a current video context");
 
                Scalar totalTime = 0.0;
                Scalar ticks = Timer::getTicks();
@@ -79,49 +80,50 @@ public:
                Scalar nextSecond = ticks + SCALAR(1.0);
 
                mFps = 0;
-               int frames = 0;
+               int frameCount = 0;
 
-               const int MAX_FRAMESKIP = 15;
-               const Scalar inverseTimestep = SCALAR(1.0) / mTimestep;
+               const Scalar timestep = mTimestep;
+               const Scalar framerate = mFramerate;
 
-               ASSERT(video && "cannot run core without a current video context");
+               const int MAX_FRAMESKIP = 15;
+               const Scalar inverseTimestep = SCALAR(1.0) / timestep;
 
                do
                {
-                       Timer::fireIfExpired();
-                       dispatchEvents();
+                       Timer::fireIfExpired();                         // 1. fire timers
+                       dispatchEvents();                                       // 2. dispatch events
 
                        int i = 0;
                        while (nextUpdate < Timer::getTicks() && i < MAX_FRAMESKIP)
                        {
-                               totalTime += mTimestep;
-                               update(totalTime, mTimestep);
+                               totalTime += timestep;
+                               update(totalTime, timestep);    // 3. update state
 
-                               nextUpdate += mTimestep;
+                               nextUpdate += timestep;
                                ++i;
                        }
 
                        if (nextDraw < (ticks = Timer::getTicks()))
                        {
-                               ++frames;
-                               draw((ticks + mTimestep - nextUpdate) * inverseTimestep);
-                               video->swap();
+                               draw((ticks + timestep - nextUpdate) * inverseTimestep);
+                               video->swap();                                  // 4. draw state
 
-                               nextDraw += mFramerate;
+                               nextDraw += framerate;
+                               ++frameCount;
 
-                               if (mShowFps && nextSecond < ticks)
+                               if (nextSecond < Timer::getTicks())
                                {
-                                       mFps = frames;
-                                       frames = 0;
+                                       mFps = frameCount;
+                                       frameCount = 0;
 
-                                       logInfo << mFps << " fps" << std::endl;
+                                       if (mShowFps) logInfo << mFps << " fps" << std::endl;
 
                                        nextSecond += SCALAR(1.0);
                                }
                        }
 
-                       // be a good citizen and give back what you don't need
-                       Timer::sleep(0.0);
+                       ticks = Timer::getTicks();                      // 5. yield timeslice
+                       if (ticks < nextUpdate && ticks < nextDraw) Timer::sleep(0.0);
                }
                while (!mStack.empty());
 
This page took 0.020108 seconds and 4 git commands to generate.