Last active
July 28, 2020 06:15
-
-
Save planetis-m/1e9faf735b737bc67d8827646736d495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on: https://dewitters.com/dewitters-gameloop/ | |
# http://lspiroengine.com/?p=378 | |
proc run(game: var Game) = | |
const | |
ticksPerSec = 25 | |
skippedTicks = 1_000_000_000 div ticksPerSec # to nanosecs per tick | |
maxFramesSkipped = 5 # 20% of ticksPerSec | |
var lastTime = getMonoTime().ticks | |
while true: | |
handleInput(game) | |
if not game.isRunning: break | |
let now = getMonoTime().ticks | |
var framesSkipped = 0 | |
while now - lastTime >= skippedTicks and framesSkipped < maxFramesSkipped: | |
game.update() | |
lastTime += skippedTicks | |
framesSkipped.inc | |
game.render(float32(now - lastTime) / skippedTicks.float32)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment