Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gameLoop: ->
@running = true
@lastTickCount = 0
@totalTickCount = 0
lastTick = lastFrame = psTicks = 0
ticksSince = 0
frames = ticks = 0
doTick = doFrame = no
updateLoop = (currentTicks) =>
doTick = (currentTicks - lastTick) > (1000 / @targetTickrate)
if currentTicks - psTicks > 1000
psTicks = currentTicks
console.log "FPS: #{frames}; TPS: #{ticks}"
frames = ticks = 0
if @vsync is on
doFrame = doTick
else
doFrame = yes
if doTick
ticksSince = currentTicks - lastTick
@update ticksSince / 1000 / @targetTickrate
lastTick += ticksSince
ticks++
if doFrame
ticksSince = currentTicks - lastFrame
@render ticksSince / 1000 / @targetTickrate
lastFrame += ticksSince
frames++
requestAnimationFrame updateLoop
requestAnimationFrame updateLoop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment