tick.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. local tick = {
  2. framerate = -1,
  3. tickrate = .03,
  4. timescale = 1,
  5. sleep = .001,
  6. dt = 0,
  7. accum = 0,
  8. tick = 1,
  9. frame = 1
  10. }
  11. local timer = love.timer
  12. local graphics = love.graphics
  13. love.run = function()
  14. if love.math then
  15. love.math.setRandomSeed(os.time())
  16. for i = 1, 3 do love.math.random() end
  17. end
  18. if love.event then love.event.pump() end
  19. if love.load then love.load(arg) end
  20. timer.step()
  21. local lastframe = 0
  22. while true do
  23. timer.step()
  24. tick.dt = timer.getDelta() * tick.timescale
  25. tick.accum = tick.accum + tick.dt
  26. while tick.accum >= tick.tickrate do
  27. tick.accum = tick.accum - tick.tickrate
  28. if love.event then
  29. love.event.pump()
  30. for e, a, b, c, d in love.event.poll() do
  31. if e == 'quit' then
  32. if not love.quit or not love.quit() then
  33. if love.audio then love.audio.stop() end
  34. return
  35. end
  36. end
  37. love.handlers[e](a, b, c, d)
  38. end
  39. end
  40. tick.tick = tick.tick + 1
  41. if love.update then love.update(tick.tickrate) end
  42. end
  43. while timer.getTime() - lastframe < 1 / tick.framerate do
  44. timer.sleep(.0005)
  45. end
  46. lastframe = timer.getTime()
  47. if graphics and love.window and love.window.isCreated() then
  48. graphics.clear()
  49. graphics.origin()
  50. tick.frame = tick.frame + 1
  51. if love.draw then love.draw() end
  52. graphics.present()
  53. end
  54. timer.sleep(tick.sleep)
  55. end
  56. end
  57. return tick