diff options
Diffstat (limited to 'src/lua/embed/boot.lua.h')
-rw-r--r-- | src/lua/embed/boot.lua.h | 77 |
1 files changed, 28 insertions, 49 deletions
diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h index c1d22a3..da1feac 100644 --- a/src/lua/embed/boot.lua.h +++ b/src/lua/embed/boot.lua.h @@ -26,6 +26,12 @@ if jin._argv[3] == '-d' then jin.debug.init() end +local function safecall(func, ...) + if func then + func(...) + end +end + function jin.core.run() local load = jin.core.load local running = jin.core.running @@ -41,74 +47,47 @@ function jin.core.run() local onEvent = jin.core.onEvent local present = jin.graphics.present local setkey = jin.keyboard.set - local dstatus = jin.debug.status local drender = jin.debug.render local fps = conf.fps - local _onEvent = function (e) - if e.type == "keydown" then - setkey(e.key, true) - elseif e.type == "keyup" then - setkey(e.key, false) - end - if onEvent then - onEvent(e) - end - end - - if load then - load() - end - - local now = second() - local last = now - local fsec = 1/fps - local dt = 0 - + safecall(load) + local previous = second() + local SEC_PER_UPDATE = 1 / fps + local dt = SEC_PER_UPDATE while(running()) do - -- frame controle - last = now - now = second() - dt = now - last - if dt < fsec then - sleep(fsec - dt) - dt = fsec - end - - -- handle events for _, e in pairs(poll()) do - if _onEvent then - _onEvent(e) + if e.type == "keydown" then + setkey(e.key, true) + elseif e.type == "keyup" then + setkey(e.key, false) end + safecall(onEvent, e) end - - -- update - if onUpdate then - -- while do - onUpdate(dt) - -- end - end - + safecall(onUpdate, dt) -- bind to default render buffer unbind() clear() color() study() - - -- custom drawing - if onDraw then - onDraw() - end - + safecall(onDraw) -- render debug window if dstatus() then drender() end - - -- swap window buffer present() + -- frame control + local current = second() + dt = current - previous + local wait = SEC_PER_UPDATE - dt + previous = previous + SEC_PER_UPDATE + if wait > 0 then + sleep(wait) + dt = SEC_PER_UPDATE + else + previous = current + end end end |