Skip to content

Instantly share code, notes, and snippets.

@matyklug18
Created December 8, 2019 16:55
Show Gist options
  • Save matyklug18/dcf2fd513468d626dc15f2ffb81f6825 to your computer and use it in GitHub Desktop.
Save matyklug18/dcf2fd513468d626dc15f2ffb81f6825 to your computer and use it in GitHub Desktop.
while (!close)
{
double now = System.nanoTime();
int updateCount = 0;
if (!paused)
{
while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER )
{
update();
lastUpdateTime += TIME_BETWEEN_UPDATES;
updateCount++;
}
if ( now - lastUpdateTime > TIME_BETWEEN_UPDATES)
{
lastUpdateTime = now - TIME_BETWEEN_UPDATES;
}
render();
lastRenderTime = now;
int thisSecond = (int) (lastUpdateTime / 1000000000);
if (thisSecond > lastSecondTime)
{
lastSecondTime = thisSecond;
}
while ( now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES)
{
Thread.yield();
try {Thread.sleep(1);} catch(Exception e) {}
now = System.nanoTime();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment