diff options
author | chai <chaifix@163.com> | 2018-10-27 01:11:28 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-27 01:11:28 +0800 |
commit | e21485a60da2b304a0d529d72e9a47061a3f9502 (patch) | |
tree | c5319e96fce7385009faeb14ff4e4e95472cdc2a /src/lua/embed/boot.lua.h | |
parent | 6219608238afd56634bdd76e096ee894e90b3a2b (diff) |
*修改boot
Diffstat (limited to 'src/lua/embed/boot.lua.h')
-rw-r--r-- | src/lua/embed/boot.lua.h | 133 |
1 files changed, 61 insertions, 72 deletions
diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h index 390cb47..13368d6 100644 --- a/src/lua/embed/boot.lua.h +++ b/src/lua/embed/boot.lua.h @@ -1,28 +1,11 @@ /* boot.lua */ static const char* boot_lua = R"( +-- Set game root directory jin.args[2] = jin.args[2] or '.' jin.filesystem.init() jin.filesystem.mount(jin.args[2]) -------------------------------------------------------------------------- --- Config game -------------------------------------------------------------------------- - jin.config = {} -if jin.filesystem.exist("config.lua") then - jin.config = require "config" -end -jin.config.width = jin.config.width or 580 -jin.config.height = jin.config.height or 450 -jin.config.vsync = jin.config.vsync or true -jin.config.title = jin.config.title or ("jin v" .. jin.version) -jin.config.resizable = jin.config.resizable or false -jin.config.fullscreen = jin.config.fullscreen or false -jin.config.fps = jin.config.fps or 60 - -------------------------------------------------------------------------- --- Default game loop -------------------------------------------------------------------------- local function call(func, ...) if func then @@ -30,6 +13,17 @@ local function call(func, ...) end end +local function open_sub_systems() + jin.audio.init() + jin.graphics.init(jin.config) +end + +local function close_sub_systems() + jin.audio.destroy() + jin.graphics.destroy() +end + +-- Default game loop function jin.core.run() call(jin.core.onLoad) jin.graphics.reset() @@ -52,78 +46,73 @@ function jin.core.run() jin.graphics.clear() call(jin.core.onDraw) jin.graphics.present() - -- Sleep 1 ms jin.time.sleep(0.001) end end -------------------------------------------------------------------------- --- Boot game -------------------------------------------------------------------------- - -local function plainLoop() - while jin.core.running() do - for _, e in pairs(jin.event.poll()) do - if e.type == "Quit" then - jin.core.stop() - end +local function noGame() + jin.core.onLoad = nil + jin.core.onEvent = function(e) + if e.type == "Quit" then + jin.core.stop() end - jin.time.sleep(0.001) end + jin.core.onUpdate = nil + jin.core.onDraw = function() + jin.graphics.print("No game", 5, 5) + end +end + +local function main() + -- Load config file + if jin.filesystem.exist("config.lua") then + jin.config = require "config" + end + jin.config.width = jin.config.width or 580 + jin.config.height = jin.config.height or 450 + jin.config.vsync = jin.config.vsync or true + jin.config.title = jin.config.title or ("jin v" .. jin.version) + jin.config.resizable = jin.config.resizable or false + jin.config.fullscreen = jin.config.fullscreen or false + jin.config.fps = jin.config.fps or 60 + + -- Load game source. + if jin.filesystem.exist("main.lua") then + call(function() require"main" end) + else + call(noGame) + end + + open_sub_systems() + + call(jin.core.run) + + close_sub_systems() + + -- Quit + jin.core.quit() end -- Display error message. local function onError(msg) - jin.graphics.init(jin.config) + open_sub_systems() local err = "Error:\n" .. msg .. "\n" .. debug.traceback() jin.graphics.reset() jin.graphics.setClearColor(100, 100, 100, 255) jin.graphics.clear() jin.graphics.print(err, 5, 5) jin.graphics.present() - plainLoop() -end - --- No game screen. -local function noGame() - jin.graphics.reset() - jin.graphics.clear() - jin.graphics.print("No Game", 5, 5) - jin.graphics.present() - plainLoop() -end - -------------------------------------------------------------------------- --- Initialize sub systems -------------------------------------------------------------------------- - --- TODO: Disable some internal lua modules. - -local function boot() - if jin.filesystem.exist("main.lua") then - call(function() require"main" end) - jin.graphics.init(jin.config) - jin.audio.init() - call(jin.core.run) - else - jin.graphics.init(jin.config) - noGame() + while jin.core.running() do + for _, e in pairs(jin.event.poll()) do + if e.type == "Quit" then + jin.core.stop() + end + end + jin.time.sleep(0.001) end + close_sub_systems() end -xpcall(boot, onError) - -------------------------------------------------------------------------- --- Destroy sub-systems -------------------------------------------------------------------------- - -jin.graphics.destroy() -jin.audio.destroy() - -------------------------------------------------------------------------- --- Quit game -------------------------------------------------------------------------- - -jin.core.quit() +xpcall(main, onError) )";
\ No newline at end of file |