diff options
Diffstat (limited to 'gameloop/init.lua')
-rw-r--r-- | gameloop/init.lua | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/gameloop/init.lua b/gameloop/init.lua new file mode 100644 index 0000000..030a151 --- /dev/null +++ b/gameloop/init.lua @@ -0,0 +1,42 @@ +local function call(func, ...) + if func then + return func(...) + end +end + +function jin.core.run() + call(jin.core.onLoad) + local dt = 0 + local previous = jin.time.second() + local current = previous + -- TODO: 重写,事件处理事件应该高频率调用 + while jin.core.running() do + for _, e in pairs(jin.event.poll()) do + if e.type == "keydown" then + jin.keyboard.set(e.key, true) + elseif e.type == "keyup" then + jin.keyboard.set(e.key, false) + end + call(jin.core.onEvent, e) + end + + previous = current + current = jin.time.second() + dt = current - previous + + call(jin.core.onUpdate, dt) + + if jin.graphics then + jin.graphics.unbindCanvas() + jin.graphics.clear() + jin.graphics.setColor() + jin.graphics.setFont() + call(jin.core.onDraw) + jin.graphics.present() + end + + if jin.time then + jin.time.sleep(0.001) + end + end +end |