Skip to content
Snippets Groups Projects
Commit 09f52dfd authored by Philip Trettner's avatar Philip Trettner
Browse files

Made update routine in mainLoop more robust

parent edffb3ae
No related branches found
No related tags found
No related merge requests found
......@@ -324,28 +324,31 @@ void GlfwApp::mainLoop()
double lastTime = glfwGetTime();
double lastStatsTime = lastTime;
double timeAccum = 0.000001;
double renderTimestep = 1.0 / mUpdateRate;
mCurrentTime = 0.0;
size_t primitives = 0;
size_t fragments = 0;
double gpuTime = 0;
double cpuTime = 0;
int updatesPerFrame = 1;
double renderTimestep = 1.0 / mUpdateRate; // tracks time between renders, init is for first frame only
while (!shouldClose())
{
updateInput();
// Update
auto maxTicks = mMaxFrameSkip;
while (timeAccum > 0)
{
if (maxTicks-- < 0)
{
glow::warning() << "Too many updates queued, frame skip of " << timeAccum << " secs";
timeAccum = 0.0;
break;
}
double dt = 1.0 / mUpdateRate;
// # of updates
auto updates = updatesPerFrame;
if (timeAccum > updatesPerFrame * dt) // lags one behind: do one more
++updates;
if (timeAccum < -dt) // is more than one ahead: skip one
--updates;
auto dt = 1.0 / mUpdateRate;
// do updates
for (auto i = 0; i < updates; ++i)
{
auto cpuStart = glfwGetTime();
update(dt);
cpuTime += glfwGetTime() - cpuStart;
......@@ -353,6 +356,29 @@ void GlfwApp::mainLoop()
mCurrentTime += dt;
}
// update adjustment (AFTER updates! timeAccum should be within -dt..dt now)
if (timeAccum > 2.5 * dt)
{
++updatesPerFrame;
// glow::info() << "increasing frames per sec";
}
else if (timeAccum < -2.5 * dt)
{
if (updatesPerFrame > 0)
--updatesPerFrame;
// glow::info() << "decreasing frames per sec";
}
// frameskip
if (timeAccum > mMaxFrameSkip * dt)
{
glow::warning() << "Too many updates queued, frame skip of " << timeAccum << " secs";
timeAccum = mMaxFrameSkip * dt * 0.5;
}
// glow::info() << updates << ", " << timeAccum / dt;
}
beginRender();
// Render here
......
......@@ -79,7 +79,7 @@ class GlfwApp
private:
std::string mTitle = "GLFW/GLOW Application"; ///< window title
int mUpdateRate = 60; ///< rate at which update(...) is called
double mUpdateRate = 60; ///< rate at which update(...) is called
int mMaxFrameSkip = 4; ///< maximum number of update(...) steps that are performed without rendering
GLFWwindow* mWindow = nullptr; ///< current GLFW window
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment