Skip to content

Instantly share code, notes, and snippets.

@EddieRingle
Created May 4, 2012 16:11
Show Gist options
  • Save EddieRingle/2595818 to your computer and use it in GitHub Desktop.
Save EddieRingle/2595818 to your computer and use it in GitHub Desktop.
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