You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

  1. -- tick
  2. -- https://github.com/bjornbytes/tick
  3. -- MIT License
  4. local tick = {
  5. framerate = nil,
  6. rate = .03,
  7. timescale = 1,
  8. sleep = .001,
  9. dt = 0,
  10. accum = 0,
  11. tick = 1,
  12. frame = 1
  13. }
  14. local timer = love.timer
  15. local graphics = love.graphics
  16. love.run = function()
  17. if not timer then
  18. error('love.timer is required for tick')
  19. end
  20. if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
  21. timer.step()
  22. local lastframe = 0
  23. love.update(0)
  24. return function()
  25. tick.dt = timer.step() * tick.timescale
  26. tick.accum = tick.accum + tick.dt
  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 name, a, b, c, d, e, f in love.event.poll() do
  32. if name == 'quit' then
  33. if not love.quit or not love.quit() then
  34. return a or 0
  35. end
  36. end
  37. love.handlers[name](a, b, c, d, e, f)
  38. end
  39. end
  40. tick.tick = tick.tick + 1
  41. if love.update then love.update(tick.rate) end
  42. end
  43. while tick.framerate and timer.getTime() - lastframe < 1 / tick.framerate do
  44. timer.sleep(.0005)
  45. end
  46. lastframe = timer.getTime()
  47. if graphics and graphics.isActive() then
  48. graphics.origin()
  49. graphics.clear(graphics.getBackgroundColor())
  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