tick.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. local tick = {
  2. framerate = nil,
  3. rate = .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 not timer then
  15. error('love.timer is required for tick')
  16. end
  17. if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
  18. timer.step()
  19. local lastframe = 0
  20. love.update(0)
  21. while true do
  22. tick.dt = timer.step() * tick.timescale
  23. tick.accum = tick.accum + tick.dt
  24. while tick.accum >= tick.rate do
  25. tick.accum = tick.accum - tick.rate
  26. if love.event then
  27. love.event.pump()
  28. for name, a, b, c, d, e, f in love.event.poll() do
  29. if name == 'quit' then
  30. if not love.quit or not love.quit() then
  31. return a or 0
  32. end
  33. end
  34. love.handlers[name](a, b, c, d, e, f)
  35. end
  36. end
  37. tick.tick = tick.tick + 1
  38. if love.update then love.update(tick.rate) end
  39. end
  40. while tick.framerate and timer.getTime() - lastframe < 1 / tick.framerate do
  41. timer.sleep(.0005)
  42. end
  43. lastframe = timer.getTime()
  44. if graphics and graphics.isActive() then
  45. graphics.origin()
  46. graphics.clear(graphics.getBackgroundColor())
  47. tick.frame = tick.frame + 1
  48. if love.draw then love.draw() end
  49. graphics.present()
  50. end
  51. timer.sleep(tick.sleep)
  52. end
  53. end
  54. return tick