tick.lua 1.6 KB

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