From 5cafb38a6fde6b786ba9b45b8faba02bc8f887cf Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 22 Aug 2018 14:08:20 +0800 Subject: *update --- gameloop/init.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 gameloop/init.lua (limited to 'gameloop/init.lua') 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 -- cgit v1.1-26-g67d0