void run()
{
init();
+ ASSERT(video && "cannot run core without a current video context");
Scalar totalTime = 0.0;
Scalar ticks = Timer::getTicks();
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());