-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtick.lua
70 lines (57 loc) · 1.49 KB
/
tick.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- tick
-- https://github.com/bjornbytes/tick
-- MIT License
local tick = {
framerate = nil,
rate = .03,
timescale = 1,
sleep = .001,
dt = 0,
accum = 0,
tick = 1,
frame = 1
}
local timer = love.timer
local graphics = love.graphics
love.run = function()
if not timer then
error('love.timer is required for tick')
end
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
timer.step()
local lastframe = 0
if love.update then love.update(0) end
return function()
tick.dt = timer.step() * tick.timescale
tick.accum = tick.accum + tick.dt
while tick.accum >= tick.rate do
tick.accum = tick.accum - tick.rate
if love.event then
love.event.pump()
for name, a, b, c, d, e, f in love.event.poll() do
if name == 'quit' then
if not love.quit or not love.quit() then
return a or 0
end
end
love.handlers[name](a, b, c, d, e, f)
end
end
tick.tick = tick.tick + 1
if love.update then love.update(tick.rate) end
end
while tick.framerate and timer.getTime() - lastframe < 1 / tick.framerate do
timer.sleep(.0005)
end
lastframe = timer.getTime()
if graphics and graphics.isActive() then
graphics.origin()
graphics.clear(graphics.getBackgroundColor())
tick.frame = tick.frame + 1
if love.draw then love.draw() end
graphics.present()
end
timer.sleep(tick.sleep)
end
end
return tick