diff options
author | chai <chaifix@163.com> | 2018-07-28 22:46:38 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-07-28 22:46:38 +0800 |
commit | 178d7eea3903f41d6260777f8058a898d9107a31 (patch) | |
tree | 8e7145cb460903ccc3d06a4502739b5fb52566e3 | |
parent | 52693d68f7181d707e1a192d67a617145b358394 (diff) |
*update
-rw-r--r-- | bin/config.lua | 4 | ||||
-rw-r--r-- | bin/jin.exe | bin | 746496 -> 746496 bytes | |||
-rw-r--r-- | src/libjin/Core/Game.cpp | 8 | ||||
-rw-r--r-- | src/libjin/Core/Game.h | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/Drawable.h | 2 | ||||
-rw-r--r-- | src/libjin/core/game.cpp | 8 | ||||
-rw-r--r-- | src/libjin/core/game.h | 2 | ||||
-rw-r--r-- | src/lua/embed/boot.lua.h | 77 | ||||
-rw-r--r-- | src/lua/event/luaopen_event.cpp | 5 |
9 files changed, 51 insertions, 57 deletions
diff --git a/bin/config.lua b/bin/config.lua index a6b4ae2..fe8e519 100644 --- a/bin/config.lua +++ b/bin/config.lua @@ -5,6 +5,6 @@ return fps = 60, vsync = false, title = "动态光照demo", - fullscreen = false, - resize = false + fullscreen = true, + resizable = false }
\ No newline at end of file diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex 7fec565..bc46b85 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/src/libjin/Core/Game.cpp b/src/libjin/Core/Game.cpp index 4509478..ffd8015 100644 --- a/src/libjin/Core/Game.cpp +++ b/src/libjin/Core/Game.cpp @@ -25,6 +25,7 @@ namespace core _running = true; Event e; int previous = getMilliSecond(); + float dt = MS_PER_UPDATE / 1000.0f; while (_running) { while (jin::input::pollEvent(&e)) @@ -32,13 +33,18 @@ namespace core SAFECALL(_onEvent, &e); if (!_running) goto stoploop; } - SAFECALL(_onUpdate); + SAFECALL(_onUpdate, dt); SAFECALL(_onDraw); + wnd->swapBuffers(); const int current = getMilliSecond(); + dt = (current - previous) / 1000.0f; const int wait = MS_PER_UPDATE - (current - previous); previous += MS_PER_UPDATE; if (wait > 0) + { sleep(wait); + dt = MS_PER_UPDATE / 1000.0f; + } else previous = current; } diff --git a/src/libjin/Core/Game.h b/src/libjin/Core/Game.h index 31825ba..090e7c6 100644 --- a/src/libjin/Core/Game.h +++ b/src/libjin/Core/Game.h @@ -17,7 +17,7 @@ namespace core public: typedef void(*onEvent)(jin::input::Event* e); - typedef void(*onUpdate)(); + typedef void(*onUpdate)(float dt); typedef void(*onDraw)(); struct Setting : SettingBase diff --git a/src/libjin/Graphics/Drawable.h b/src/libjin/Graphics/Drawable.h index c05d8a4..0b96379 100644 --- a/src/libjin/Graphics/Drawable.h +++ b/src/libjin/Graphics/Drawable.h @@ -8,6 +8,7 @@ namespace jin { namespace graphics { + class Drawable { public: @@ -51,6 +52,7 @@ namespace graphics float* vertCoord; }; + } // render } // jin diff --git a/src/libjin/core/game.cpp b/src/libjin/core/game.cpp index 4509478..ffd8015 100644 --- a/src/libjin/core/game.cpp +++ b/src/libjin/core/game.cpp @@ -25,6 +25,7 @@ namespace core _running = true; Event e; int previous = getMilliSecond(); + float dt = MS_PER_UPDATE / 1000.0f; while (_running) { while (jin::input::pollEvent(&e)) @@ -32,13 +33,18 @@ namespace core SAFECALL(_onEvent, &e); if (!_running) goto stoploop; } - SAFECALL(_onUpdate); + SAFECALL(_onUpdate, dt); SAFECALL(_onDraw); + wnd->swapBuffers(); const int current = getMilliSecond(); + dt = (current - previous) / 1000.0f; const int wait = MS_PER_UPDATE - (current - previous); previous += MS_PER_UPDATE; if (wait > 0) + { sleep(wait); + dt = MS_PER_UPDATE / 1000.0f; + } else previous = current; } diff --git a/src/libjin/core/game.h b/src/libjin/core/game.h index 31825ba..090e7c6 100644 --- a/src/libjin/core/game.h +++ b/src/libjin/core/game.h @@ -17,7 +17,7 @@ namespace core public: typedef void(*onEvent)(jin::input::Event* e); - typedef void(*onUpdate)(); + typedef void(*onUpdate)(float dt); typedef void(*onDraw)(); struct Setting : SettingBase 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 diff --git a/src/lua/event/luaopen_event.cpp b/src/lua/event/luaopen_event.cpp index 8802175..522a650 100644 --- a/src/lua/event/luaopen_event.cpp +++ b/src/lua/event/luaopen_event.cpp @@ -101,5 +101,6 @@ namespace lua luax_newlib(L, f); return 1; } -} -}
\ No newline at end of file + +} // lua +} // jin
\ No newline at end of file |