diff options
Diffstat (limited to 'src/script/embed')
-rw-r--r-- | src/script/embed/boot.lua | 132 | ||||
-rw-r--r-- | src/script/embed/boot.lua.h | 156 | ||||
-rw-r--r-- | src/script/embed/debug.lua | 128 | ||||
-rw-r--r-- | src/script/embed/debug.lua.h | 132 | ||||
-rw-r--r-- | src/script/embed/embed.h | 51 | ||||
-rw-r--r-- | src/script/embed/graphics.lua | 6 | ||||
-rw-r--r-- | src/script/embed/graphics.lua.h | 8 | ||||
-rw-r--r-- | src/script/embed/keyboard.lua | 16 | ||||
-rw-r--r-- | src/script/embed/keyboard.lua.h | 20 | ||||
-rw-r--r-- | src/script/embed/mouse.lua | 15 | ||||
-rw-r--r-- | src/script/embed/mouse.lua.h | 18 | ||||
-rw-r--r-- | src/script/embed/path.lua | 15 | ||||
-rw-r--r-- | src/script/embed/path.lua.h | 18 |
13 files changed, 0 insertions, 715 deletions
diff --git a/src/script/embed/boot.lua b/src/script/embed/boot.lua deleted file mode 100644 index 2f6fa93..0000000 --- a/src/script/embed/boot.lua +++ /dev/null @@ -1,132 +0,0 @@ ---[[ - program entry -]] - -local function _onEvent(e) - -- update keyboard status - if e.type == "keydown" then - jin.keyboard.set(e.key, true) - elseif e.type == "keyup" then - jin.keyboard.set(e.key, false) - end - - -- call user onEvent function - if jin.core.onEvent then - jin.core.onEvent(e) - end -end - -------------------------------------------------- --- init file system -------------------------------------------------- -jin._argv[2] = jin._argv[2] or '.' -jin.filesystem.init() -jin.filesystem.mount(jin._argv[2]) - --- config -local conf = {} -if jin.filesystem.exist("config.lua") then - conf = require "config" -end -conf.width = conf.width or 600 -conf.height = conf.height or 500 -conf.fps = conf.fps or 60 -conf.title = conf.title or ("jin v" .. jin.version()) - --- init video subsystem -jin.graphics.init(conf.width,conf.height,conf.title) - --- open debug mode, must after jin.graphics.init -if jin._argv[3] == '-d' then - jin.debug.init() -end - -function jin.core.run() - local now = jin.time.second() - local last = now - local fsec = 1/conf.fps - -- for loading resources - if jin.core.load then - jin.core.load() - end - local dt = 0 - while(jin.core.running()) do - -- frame controle - last = now - now = jin.time.second() - if (now - last) < fsec then - jin.time.sleep(fsec - now + last) - end - - -- handle events - for _, e in pairs(jin.event.poll()) do - if _onEvent then - _onEvent(e) - end - end - - -- update - dt = now - last - if dt < fsec then - dt = fsec - end - if jin.core.onUpdate then - jin.core.onUpdate(dt) - end - - -- bind to default render buffer - jin.graphics.bind() - jin.graphics.clear() - jin.graphics.color() - jin.graphics.study() - - -- custom drawing - if jin.core.onDraw then - jin.core.onDraw() - end - - -- render debug window - if jin.debug.status() then - jin.debug.render() - end - - -- swap window buffer - jin.graphics.present() - - end -end - -local function onError(msg) - local tab = ' ' - print("Error:\n" .. msg) - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - local ww, wh = jin.graphics.size() - function jin.core.onDraw() - jin.graphics.write("Error: ", 10, 10, 30, 3, 30) - jin.graphics.write(msg, 10, 50) - end -end - -if jin.filesystem.exist("main.lua") then - -- require main game script - xpcall(function() require"main" end, onError) - jin.core.run() -else - -- no game - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - function jin.core.onDraw() - jin.graphics.clear(111, 134, 125, 255) - local ww, wh = jin.graphics.size() - local fw, fh = jin.graphics.box("no game", 20, 1, 20) - jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) - end - jin.core.run() -end diff --git a/src/script/embed/boot.lua.h b/src/script/embed/boot.lua.h deleted file mode 100644 index 21c1899..0000000 --- a/src/script/embed/boot.lua.h +++ /dev/null @@ -1,156 +0,0 @@ -/* boot.lua */ -static const char* boot_lua = R"( - --- init filesystem -jin._argv[2] = jin._argv[2] or '.' -jin.filesystem.init() -jin.filesystem.mount(jin._argv[2]) - --- config -local conf = {} -if jin.filesystem.exist("config.lua") then - conf = require "config" -end -conf.width = conf.width or 600 -conf.height = conf.height or 500 -conf.fps = conf.fps or 60 -conf.vsync = conf.vsync or false -conf.title = conf.title or ("jin v" .. jin.version()) - --- initialize subsystems -jin.graphics.init(conf) ---jin.audio.init(conf) - --- open debug mode, must after jin.graphics.init -if jin._argv[3] == '-d' then - jin.debug.init() -end - -function jin.core.run() - local load = jin.core.load - local running = jin.core.running - local second = jin.time.second - local sleep = jin.time.sleep - local poll = jin.event.poll - local unbind = jin.graphics.unbind - local clear = jin.graphics.clear - local color = jin.graphics.color - local study = jin.graphics.study - local onDraw = jin.core.onDraw - local onUpdate = jin.core.onUpdate - 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 - - 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) - end - end - - -- update - if onUpdate then - onUpdate(dt) - end - - -- bind to default render buffer - unbind() - clear() - color() - study() - - -- custom drawing - if onDraw then - onDraw() - end - - -- render debug window - if dstatus() then - drender() - end - - -- swap window buffer - present() - end -end - -local function onError(msg) - local tab = ' ' - print("Error:\n" .. msg) - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - local ww, wh = jin.graphics.size() - function jin.core.onDraw() - jin.graphics.write("Error: ", 10, 10, 30, 3, 30) - jin.graphics.write(msg, 10, 50) - end -end - -local function main() - if jin.filesystem.exist("main.lua") then - -- require main game script - xpcall(function() require"main" end, onError) - jin.core.run() - else - -- no game - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - function jin.core.onDraw() - jin.graphics.clear(111, 134, 125, 255) - local ww, wh = jin.graphics.size() - local fw, fh = jin.graphics.box("no game", 20, 1, 20) - jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) - end - jin.core.run() - end - -- quit subsystems - jin.graphics.destroy() - -- exit whole game - jin.core.exit() -end - -main() - -)"; diff --git a/src/script/embed/debug.lua b/src/script/embed/debug.lua deleted file mode 100644 index 76f59ed..0000000 --- a/src/script/embed/debug.lua +++ /dev/null @@ -1,128 +0,0 @@ ---[[ - for debug purpose - +-------------------+ - |debug msg old | - |... | - |... | - |... | - |debug msg new | - +-------------------+ -]] - -jin.debug = jin.debug or {} - --- render panel -local panel = nil - -local debug = false - --- debug msg buffer -local buffer = {} - --- configure -local bsize = 10 -local fsize = 15 -local lheight = 18 -local alpha = 220 -local margin = 10 - --- refresh buffer or not -local refresh = true - -function jin.debug.init() - debug = true - panel = jin.graphics.Canvas(jin.graphics.size()) -end - --- set buffer size -function jin.debug.size(c) - bsize = c -end - -function jin.debug.print(msg) - if not debug then return end - - msg = tostring(msg) - local tp = type(msg) - if tp ~= "string" and tp ~= "number" then - msg = string.format("print failed, expect string or number but get a %s", tp) - end - - -- remove the first one (old msg) - if #buffer >= bsize then - table.remove(buffer, 1) - end - - buffer[#buffer + 1] = msg - refresh = true -end - --- clear debug buffer -function jin.debug.clear() - buffer = {} -end - -local function getStrHeight(str, lheight) - local h = lheight - if #str == 0 then - h = 0 - end - for i = 1, #str do - local c = string.sub(str, i, i) - if c == '\n' then - h = h + lheight - end - end - return h -end - -local function getBgQuad() - local width, height = 0, 0 - for i = 1, #buffer do - local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) - height = height + h - if width < w then - width = w - end - end - return width, height -end - --- render to screen -function jin.debug.render() - if not debug then return end - - if refresh then - - jin.graphics.bind(panel) - - jin.graphics.clear(0, 0, 0, 0) - - jin.graphics.study() - - local ww, wh = jin.graphics.size() - local bgw, bgh = getBgQuad() - jin.graphics.color(0, 0, 0, alpha) - jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) - - jin.graphics.color() - local y = wh - for i = #buffer, 1, -1 do - local msg = buffer[i] - local h = getStrHeight(msg, lheight) - y = y - h - jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) - end - - jin.graphics.bind() - - refresh = false - end - - jin.graphics.color() - jin.graphics.draw(panel, 0, 0) -end - -function jin.debug.status() - return debug -end diff --git a/src/script/embed/debug.lua.h b/src/script/embed/debug.lua.h deleted file mode 100644 index 79c95ba..0000000 --- a/src/script/embed/debug.lua.h +++ /dev/null @@ -1,132 +0,0 @@ -/* debug.lua */ -static const char* debug_lua = R"( ---[[ - for debug purpose - +-------------------+ - |debug msg old | - |... | - |... | - |... | - |debug msg new | - +-------------------+ -]] - -jin.debug = jin.debug or {} - --- render panel -local panel = nil - -local debug = false - --- debug msg buffer -local buffer = {} - --- configure -local bsize = 10 -local fsize = 15 -local lheight = 18 -local alpha = 220 -local margin = 10 - --- refresh buffer or not -local refresh = true - -function jin.debug.init() - debug = true - panel = jin.graphics.Canvas(jin.graphics.size()) -end - --- set buffer size -function jin.debug.size(c) - bsize = c -end - -function jin.debug.print(msg) - if not debug then return end - - msg = tostring(msg) - local tp = type(msg) - if tp ~= "string" and tp ~= "number" then - msg = string.format("print failed, expect string or number but get a %s", tp) - end - - -- remove the first one (old msg) - if #buffer >= bsize then - table.remove(buffer, 1) - end - - buffer[#buffer + 1] = msg - refresh = true -end - --- clear debug buffer -function jin.debug.clear() - buffer = {} -end - -local function getStrHeight(str, lheight) - local h = lheight - if #str == 0 then - h = 0 - end - for i = 1, #str do - local c = string.sub(str, i, i) - if c == '\n' then - h = h + lheight - end - end - return h -end - -local function getBgQuad() - local width, height = 0, 0 - for i = 1, #buffer do - local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) - height = height + h - if width < w then - width = w - end - end - return width, height -end - --- render to screen -function jin.debug.render() - if not debug then return end - - if refresh then - - jin.graphics.bind(panel) - - jin.graphics.clear(0, 0, 0, 0) - - jin.graphics.study() - - local ww, wh = jin.graphics.size() - local bgw, bgh = getBgQuad() - jin.graphics.color(0, 0, 0, alpha) - jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) - - jin.graphics.color() - local y = wh - for i = #buffer, 1, -1 do - local msg = buffer[i] - local h = getStrHeight(msg, lheight) - y = y - h - jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) - end - - jin.graphics.bind() - - refresh = false - end - - jin.graphics.color() - jin.graphics.draw(panel, 0, 0) -end - -function jin.debug.status() - return debug -end - -)";
\ No newline at end of file diff --git a/src/script/embed/embed.h b/src/script/embed/embed.h deleted file mode 100644 index 2ef8b75..0000000 --- a/src/script/embed/embed.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef __JIN_LUA_EMBED_H -#define __JIN_LUA_EMBED_H -#include <cstring> - -namespace jin -{ -namespace embed -{ - - /** - * embed lua script to context. - */ -#define embed(L, script, name) \ - if(luaL_loadbuffer(L, script, strlen(script), name) == 0)\ - lua_call(L, 0, 0); - - /** - * embed structure. - */ - struct jin_Embed - { - const char* fname, *source; - }; - - static void boot(lua_State* L) - { - // embed scripts - #include "graphics.lua.h" // graphics - #include "keyboard.lua.h" // keyboard - #include "mouse.lua.h" // mouse - #include "debug.lua.h" // debug - #include "boot.lua.h" // boot - - // embed scripts - const jin_Embed scripts[] = { - {"graphics.lua", graphics_lua}, - {"keyboard.lua", keyboard_lua}, - {"mouse.lua", mouse_lua}, - {"debug.lua", debug_lua}, - {"boot.lua", boot_lua}, - {0, 0} - }; - - // load all emebd lua scripts - for (int i = 0; scripts[i].fname; ++i) - embed(L, scripts[i].source, scripts[i].fname); - } -} -} - -#endif
\ No newline at end of file diff --git a/src/script/embed/graphics.lua b/src/script/embed/graphics.lua deleted file mode 100644 index 03a891d..0000000 --- a/src/script/embed/graphics.lua +++ /dev/null @@ -1,6 +0,0 @@ ------------------ --- jin.graphics ------------------ - -jin.graphics = jin.graphics or {} - diff --git a/src/script/embed/graphics.lua.h b/src/script/embed/graphics.lua.h deleted file mode 100644 index 85cf979..0000000 --- a/src/script/embed/graphics.lua.h +++ /dev/null @@ -1,8 +0,0 @@ -/* graphics.lua */ -static const char* graphics_lua = R"( ------------------ --- jin.graphics ------------------ - -jin.graphics = jin.graphics or {} -)"; diff --git a/src/script/embed/keyboard.lua b/src/script/embed/keyboard.lua deleted file mode 100644 index 08214f8..0000000 --- a/src/script/embed/keyboard.lua +++ /dev/null @@ -1,16 +0,0 @@ ---[[ - jin.keyboard extension -]] - -jin.keyboard = jin.keyboard or {} - -local keys = {} - -function jin.keyboard.isDown(k) - return keys[k] -end - -function jin.keyboard.set(k, status) - keys[k] = status -end - diff --git a/src/script/embed/keyboard.lua.h b/src/script/embed/keyboard.lua.h deleted file mode 100644 index 66e3c2a..0000000 --- a/src/script/embed/keyboard.lua.h +++ /dev/null @@ -1,20 +0,0 @@ - -static const char* keyboard_lua = R"( ---[[ - jin.keyboard extension -]] - -jin.keyboard = jin.keyboard or {} - -local keys = {} - -function jin.keyboard.isDown(k) - return keys[k] -end - -function jin.keyboard.set(k, status) - keys[k] = status -end - - -)"; diff --git a/src/script/embed/mouse.lua b/src/script/embed/mouse.lua deleted file mode 100644 index 9dcd472..0000000 --- a/src/script/embed/mouse.lua +++ /dev/null @@ -1,15 +0,0 @@ ---[[ - jin.mouse extension -]] - -jin.mouse = jin.mouse or {} - -local button = {} - -function jin.mouse.isDown(btn) - return button[btn] -end - -function jin.mouse.set(btn, status) - button[btn] = status -end diff --git a/src/script/embed/mouse.lua.h b/src/script/embed/mouse.lua.h deleted file mode 100644 index f57d08c..0000000 --- a/src/script/embed/mouse.lua.h +++ /dev/null @@ -1,18 +0,0 @@ -static const char* mouse_lua = R"( ---[[ - jin.mouse extension -]] - -jin.mouse = jin.mouse or {} - -local button = {} - -function jin.mouse.isDown(btn) - return button[btn] -end - -function jin.mouse.set(btn, status) - button[btn] = status -end - -)";
\ No newline at end of file diff --git a/src/script/embed/path.lua b/src/script/embed/path.lua deleted file mode 100644 index 5b99dd2..0000000 --- a/src/script/embed/path.lua +++ /dev/null @@ -1,15 +0,0 @@ ---[[ - jin.path extension -]] - -jin.path = jin.path or {} - --- game root directory -jin._root = nil - --- return full path of a given path -function jin.path.full(path) - local root = jin._dir .. '/' .. jin._argv[2] - return root .. '/' .. path -end - diff --git a/src/script/embed/path.lua.h b/src/script/embed/path.lua.h deleted file mode 100644 index b398c99..0000000 --- a/src/script/embed/path.lua.h +++ /dev/null @@ -1,18 +0,0 @@ -/* path.lua */ -static const char* path_lua = R"( ---[[ - jin.path extension -]] - -jin.path = jin.path or {} - --- game root directory -jin._root = nil - --- return full path of a given path -function jin.path.full(path) - local root = jin._dir .. '/' .. jin._argv[2] - return root .. '/' .. path -end - -)";
\ No newline at end of file |