-
Notifications
You must be signed in to change notification settings - Fork 0
/
loft.timer.lua
51 lines (51 loc) · 1.1 KB
/
loft.timer.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
local love = require("loft")
-- local clk = function() return _clk()*2.5 end
love.timer = {}
local prov = love._provider
local clk = os.clock -- works on "CPU time", will be off by a significant amount
if prov.thread and prov.thread.time then
clk = prov.thread.time
end
local _start = clk()
function love.timer.getTime()
return clk() - _start
end
function love.timer.sleep(s)
if s == nil then
s = 1 / 30
end
if prov.thread and prov.thread.sleep then
prov.thread.sleep(s)
return
end
-- I mean I guess...
-- bit destructive
local start = clk()
repeat
until clk() - start >= s
end
local last_frame = clk()
local delta = 0
local avg, avgc = 0, 0
local average_delta = 0
function love.timer.step()
delta = clk() - last_frame
avg = avg + delta
avgc = avgc + 1
if avg >= 1 then
average_delta = avg / avgc
avg, avgc = 0, 0
end
last_frame = clk()
return delta
end
function love.timer.getDelta()
return delta
end
function love.timer.getFPS()
return 1 / delta
end
function love.timer.getAverageDelta()
return average_delta
end
return love.timer