diff options
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/embed/boot.lua.h | 130 | ||||
-rw-r--r-- | src/lua/embed/graphics.lua.h | 13 | ||||
-rw-r--r-- | src/lua/modules/audio/je_lua_audio.cpp | 14 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_graphics.cpp | 31 |
4 files changed, 114 insertions, 74 deletions
diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h index 13368d6..212a1e5 100644 --- a/src/lua/embed/boot.lua.h +++ b/src/lua/embed/boot.lua.h @@ -1,11 +1,28 @@ /* 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 + xpcall(function()jin.config = require "config" end, function()end) +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 @@ -13,20 +30,9 @@ 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() + call(jin.core.onLoad) local dt = 0 local previous = jin.time.second() local current = previous @@ -50,52 +56,12 @@ function jin.core.run() end end -local function noGame() - jin.core.onLoad = nil - jin.core.onEvent = function(e) - if e.type == "Quit" then - jin.core.stop() - end - 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 +------------------------------------------------------------------------- +-- Boot game +------------------------------------------------------------------------- -- Display error message. local function onError(msg) - open_sub_systems() local err = "Error:\n" .. msg .. "\n" .. debug.traceback() jin.graphics.reset() jin.graphics.setClearColor(100, 100, 100, 255) @@ -110,9 +76,57 @@ local function onError(msg) end jin.time.sleep(0.001) end - close_sub_systems() end -xpcall(main, onError) +-- No game screen. +local function noGame() + jin.graphics.reset() + jin.graphics.reset() + jin.graphics.setClearColor(100, 100, 100, 255) + jin.graphics.clear() + jin.graphics.print("No Game", 5, 5) + jin.graphics.present() + 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 +end + +local function boot() + if jin.filesystem.exist("main.lua") then + call(function() + require"main" + jin.core.run() + end) + else + noGame() + end +end + +------------------------------------------------------------------------- +-- Initialize sub systems +------------------------------------------------------------------------- + +jin.audio.init() +jin.graphics.init(jin.config) + +xpcall(boot, onError) + +------------------------------------------------------------------------- +-- Destroy sub-systems +------------------------------------------------------------------------- + +jin.graphics.destroy() +jin.audio.destroy() + +------------------------------------------------------------------------- +-- Quit game +------------------------------------------------------------------------- + +jin.core.quit() )";
\ No newline at end of file diff --git a/src/lua/embed/graphics.lua.h b/src/lua/embed/graphics.lua.h index 5fa5dad..b2a19b5 100644 --- a/src/lua/embed/graphics.lua.h +++ b/src/lua/embed/graphics.lua.h @@ -24,11 +24,18 @@ Color frag(Color col, Texture tex, Vertex v) ]] local _init = jin.graphics.init +local initialized = false jin.graphics.init = function(setting) - _init(setting); - default_shader = jin.graphics.newShader(default_shader_source) - jin.graphics.useShader(default_shader) + if initialized then + return initialized + end + initialized = _init(setting) + if initialized then + default_shader = jin.graphics.newShader(default_shader_source) + jin.graphics.useShader(default_shader) + end + return initialized end jin.graphics.unuseShader = function() diff --git a/src/lua/modules/audio/je_lua_audio.cpp b/src/lua/modules/audio/je_lua_audio.cpp index c021ef1..8ffa794 100644 --- a/src/lua/modules/audio/je_lua_audio.cpp +++ b/src/lua/modules/audio/je_lua_audio.cpp @@ -14,12 +14,24 @@ namespace JinEngine typedef SDLAudio Audio; typedef SDLSource Source; + struct + { + bool initialized = false; + } context; + LUA_IMPLEMENT int l_init(lua_State* L) { + if (context.initialized) + { + // Already initialized. + luax_pushboolean(L, true); + return 1; + } Audio::Setting setting; setting.samplerate = 44100; setting.samples = 44100; - if (!Audio::get()->init(&setting)) + context.initialized = Audio::get()->init(&setting); + if (!context.initialized) { luax_error(L, "could not init audio"); luax_pushboolean(L, false); diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp index 83ef40b..d59a67d 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -24,10 +24,17 @@ namespace JinEngine Color curClearColor; Font* curFont = nullptr; Font* defaultFont = nullptr; + bool initialized = false; } context; LUA_IMPLEMENT int l_init(lua_State* L) { + if (context.initialized) + { + luax_pushboolean(L, true); + return 1; + } + Window* wnd = Window::get(); Window::Setting setting; setting.width = luax_getfieldinteger(L, 1, "width"); @@ -36,21 +43,21 @@ namespace JinEngine setting.vsync = luax_getfieldbool(L, 1, "vsync"); setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen"); setting.resizable = luax_getfieldbool(L, 1, "resizable"); - if (!wnd->init(&setting)) + context.initialized = wnd->init(&setting); + if (!context.initialized) { - luax_pushboolean(L, false); + luax_pushboolean(L, context.initialized); return 1; } - { - /* load default font */ - Bitmap* bitmap = Bitmap::createBitmap(default_font_bitmap, sizeof(default_font_bitmap)); - TextureFont* tf = TextureFont::createTextureFont(bitmap, Text(Encode::UTF8, default_charset), default_font_split, bitmap->getHeight()); - context.defaultFont = tf; - delete bitmap; - } - context.curFont = context.defaultFont; - - luax_pushboolean(L, true); + + /* load default font */ + Bitmap* bitmap = Bitmap::createBitmap(default_font_bitmap, sizeof(default_font_bitmap)); + TextureFont* tf = TextureFont::createTextureFont(bitmap, Text(Encode::UTF8, default_charset), default_font_split, bitmap->getHeight()); + delete bitmap; + context.defaultFont = tf; + context.curFont = tf; + + luax_pushboolean(L, context.initialized); return 1; } |