aboutsummaryrefslogtreecommitdiff
path: root/src/lua/embed/boot.lua.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-07-28 22:46:38 +0800
committerchai <chaifix@163.com>2018-07-28 22:46:38 +0800
commit178d7eea3903f41d6260777f8058a898d9107a31 (patch)
tree8e7145cb460903ccc3d06a4502739b5fb52566e3 /src/lua/embed/boot.lua.h
parent52693d68f7181d707e1a192d67a617145b358394 (diff)
*update
Diffstat (limited to 'src/lua/embed/boot.lua.h')
-rw-r--r--src/lua/embed/boot.lua.h77
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