-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathloop.lua
executable file
·283 lines (243 loc) · 6.3 KB
/
loop.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
local Observable = require("Observable")
local thread = require("thread")
local delay = require("delay")
local asynckey = require("asynckey")
local just = require("just")
local flux = require("flux")
local reqprof = require("reqprof")
local sleep = require("sleep")
local jit = require("jit")
local MidiInputFactory = require("native.midi.MidiInputFactory")
if jit.os == "Windows" then
sleep = love.timer.sleep
end
-- Static class
---@class sphere.Loop: util.Observable
---@operator call: sphere.Loop
local Loop = Observable + {}
Loop.fpslimit = 240
Loop.time = 0
Loop.dt = 0
Loop.eventTime = 0
Loop.startTime = 0
Loop.stats = {}
Loop.asynckey = false
Loop.dwmflush = false
Loop.timings = {
event = 0,
update = 0,
draw = 0,
}
local dwmapi
if love.system.getOS() == "Windows" then
local ffi = require("ffi")
dwmapi = ffi.load("dwmapi")
ffi.cdef("void DwmFlush();")
end
Loop.quitting = false
---@return number?
function Loop:quittingLoop()
love.event.pump()
for name, a, b, c, d, e, f in love.event.poll() do
if name == "quit" then
Loop:send({name = "quit"})
return 0
end
end
thread.update()
delay.update()
if thread.current == 0 then
Loop:send({name = "quit"})
return 0
end
if love.graphics and love.graphics.isActive() then
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf("waiting for " .. thread.current .. " coroutines", 0, 0, 1000, "left")
love.graphics.present()
end
love.timer.sleep(0.1)
end
local framestarted = {name = "framestarted"}
---@return function
function Loop:run()
love.math.setRandomSeed(os.time())
math.randomseed(os.time())
love.timer.step()
local fpsLimitTime = love.timer.getTime()
Loop.time = fpsLimitTime
Loop.startTime = fpsLimitTime
Loop.dt = 0
local midiInputFactory = MidiInputFactory()
local midiInput = midiInputFactory:getMidiInput()
setmetatable(_G, {
__newindex = function(a, b, c)
print("__newindex", a, b, c, debug.traceback())
rawset(a, b, c)
end
})
return function()
if Loop.quitting then
return Loop:quittingLoop()
end
reqprof.start()
if Loop.asynckey and asynckey.start then
asynckey.start()
end
Loop.dt = love.timer.step()
Loop.time = love.timer.getTime()
local timingsEvent = Loop.time
love.event.pump()
framestarted.time = Loop.time
framestarted.dt = Loop.dt
Loop:send(framestarted)
local asynckeyWorking = Loop.asynckey and asynckey.events
if asynckeyWorking then
if love.window.hasFocus() then
for event in asynckey.events do
Loop.eventTime = event.time
if event.state then
love.keypressed(event.key, event.key)
else
love.keyreleased(event.key, event.key)
end
end
else
asynckey.clear()
end
end
Loop.eventTime = Loop.time - Loop.dt / 2
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
Loop:quit()
return a or 0
end
end
if not asynckeyWorking or name ~= "keypressed" and name ~= "keyreleased" then
love.handlers[name](a, b, c, d, e, f)
end
end
for port, note, status in midiInput:events() do
if status then
love.midipressed(note)
else
love.midireleased(note)
end
end
local timingsUpdate = love.timer.getTime()
Loop.timings.event = timingsUpdate - timingsEvent
thread.update()
delay.update()
flux.update(math.min(Loop.dt, 1 / 60))
love.update(Loop.dt)
local timingsDraw = love.timer.getTime()
Loop.timings.update = timingsDraw - timingsUpdate
local frameEndTime
if love.graphics and love.graphics.isActive() then
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
love.draw()
just._end()
love.graphics.origin()
love.graphics.getStats(Loop.stats)
love.graphics.present() -- all new events are read when present is called
if dwmapi and Loop.dwmflush then
dwmapi.DwmFlush()
end
frameEndTime = love.timer.getTime()
end
local timingsSleep = love.timer.getTime()
Loop.timings.draw = timingsSleep - timingsDraw
if Loop.fpslimit > 0 then
fpsLimitTime = math.max(fpsLimitTime + 1 / Loop.fpslimit, frameEndTime)
sleep(fpsLimitTime - frameEndTime)
end
end
end
Loop.callbacks = {
"update",
"draw",
"textinput",
"keypressed",
"keyreleased",
"mousepressed",
"gamepadaxis",
"gamepadpressed",
"gamepadreleased",
"joystickaxis",
"joystickpressed",
"joystickreleased",
"midipressed",
"midireleased",
"mousemoved",
"mousereleased",
"wheelmoved",
"resize",
-- "quit",
"filedropped",
"directorydropped",
"focus",
"mousefocus",
}
-- all events are from [time - dt, time]
---@param time number
---@return number
local function clampEventTime(time)
return math.min(math.max(time, Loop.time - Loop.dt), Loop.time)
end
local function transformInputEvent(name, ...)
if name == "keypressed" then
return "keyboard", 1, select(2, ...), true
elseif name == "keyreleased" then
return "keyboard", 1, select(2, ...), false
elseif name == "gamepadpressed" then
return "gamepad", select(1, ...):getID(), select(2, ...), true
elseif name == "gamepadreleased" then
return "gamepad", select(1, ...):getID(), select(2, ...), false
elseif name == "joystickpressed" then
return "joystick", select(1, ...):getID(), select(2, ...), true
elseif name == "joystickreleased" then
return "joystick", select(1, ...):getID(), select(2, ...), false
elseif name == "midipressed" then
return "midi", 1, select(1, ...), true
elseif name == "midireleased" then
return "midi", 1, select(1, ...), false
end
end
local re = {}
local function resend_transformed(...)
if not ... then
return
end
local name = "inputchanged"
local icb = just.callbacks[name]
if icb and icb(...) then return end
re[1], re[2], re[3], re[4], re[5], re[6] = ...
re.name = name
re.time = clampEventTime(Loop.eventTime)
return Loop:send(re)
end
function Loop:init()
local e = {}
for _, name in pairs(Loop.callbacks) do
love[name] = function(...)
resend_transformed(transformInputEvent(name, ...))
local icb = just.callbacks[name]
if icb and icb(...) then return end
e[1], e[2], e[3], e[4], e[5], e[6] = ...
e.name = name
e.time = clampEventTime(Loop.eventTime)
return Loop:send(e)
end
end
love.quit = function(...)
print("Quitting")
Loop.quitting = true
return true
end
end
function Loop:quit()
LuaMidi.gc()
end
return Loop