aboutsummaryrefslogtreecommitdiff
path: root/src/lua/embed
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/embed')
-rw-r--r--src/lua/embed/boot.lua132
-rw-r--r--src/lua/embed/boot.lua.h156
-rw-r--r--src/lua/embed/debug.lua128
-rw-r--r--src/lua/embed/debug.lua.h132
-rw-r--r--src/lua/embed/embed.h51
-rw-r--r--src/lua/embed/graphics.lua6
-rw-r--r--src/lua/embed/graphics.lua.h8
-rw-r--r--src/lua/embed/keyboard.lua16
-rw-r--r--src/lua/embed/keyboard.lua.h20
-rw-r--r--src/lua/embed/mouse.lua15
-rw-r--r--src/lua/embed/mouse.lua.h18
-rw-r--r--src/lua/embed/path.lua15
-rw-r--r--src/lua/embed/path.lua.h18
13 files changed, 715 insertions, 0 deletions
diff --git a/src/lua/embed/boot.lua b/src/lua/embed/boot.lua
new file mode 100644
index 0000000..2f6fa93
--- /dev/null
+++ b/src/lua/embed/boot.lua
@@ -0,0 +1,132 @@
+--[[
+ 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/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h
new file mode 100644
index 0000000..21c1899
--- /dev/null
+++ b/src/lua/embed/boot.lua.h
@@ -0,0 +1,156 @@
+/* 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/lua/embed/debug.lua b/src/lua/embed/debug.lua
new file mode 100644
index 0000000..76f59ed
--- /dev/null
+++ b/src/lua/embed/debug.lua
@@ -0,0 +1,128 @@
+--[[
+ 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/lua/embed/debug.lua.h b/src/lua/embed/debug.lua.h
new file mode 100644
index 0000000..79c95ba
--- /dev/null
+++ b/src/lua/embed/debug.lua.h
@@ -0,0 +1,132 @@
+/* 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/lua/embed/embed.h b/src/lua/embed/embed.h
new file mode 100644
index 0000000..2ef8b75
--- /dev/null
+++ b/src/lua/embed/embed.h
@@ -0,0 +1,51 @@
+#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/lua/embed/graphics.lua b/src/lua/embed/graphics.lua
new file mode 100644
index 0000000..03a891d
--- /dev/null
+++ b/src/lua/embed/graphics.lua
@@ -0,0 +1,6 @@
+-----------------
+-- jin.graphics
+-----------------
+
+jin.graphics = jin.graphics or {}
+
diff --git a/src/lua/embed/graphics.lua.h b/src/lua/embed/graphics.lua.h
new file mode 100644
index 0000000..85cf979
--- /dev/null
+++ b/src/lua/embed/graphics.lua.h
@@ -0,0 +1,8 @@
+/* graphics.lua */
+static const char* graphics_lua = R"(
+-----------------
+-- jin.graphics
+-----------------
+
+jin.graphics = jin.graphics or {}
+)";
diff --git a/src/lua/embed/keyboard.lua b/src/lua/embed/keyboard.lua
new file mode 100644
index 0000000..08214f8
--- /dev/null
+++ b/src/lua/embed/keyboard.lua
@@ -0,0 +1,16 @@
+--[[
+ 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/lua/embed/keyboard.lua.h b/src/lua/embed/keyboard.lua.h
new file mode 100644
index 0000000..66e3c2a
--- /dev/null
+++ b/src/lua/embed/keyboard.lua.h
@@ -0,0 +1,20 @@
+
+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/lua/embed/mouse.lua b/src/lua/embed/mouse.lua
new file mode 100644
index 0000000..9dcd472
--- /dev/null
+++ b/src/lua/embed/mouse.lua
@@ -0,0 +1,15 @@
+--[[
+ 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/lua/embed/mouse.lua.h b/src/lua/embed/mouse.lua.h
new file mode 100644
index 0000000..f57d08c
--- /dev/null
+++ b/src/lua/embed/mouse.lua.h
@@ -0,0 +1,18 @@
+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/lua/embed/path.lua b/src/lua/embed/path.lua
new file mode 100644
index 0000000..5b99dd2
--- /dev/null
+++ b/src/lua/embed/path.lua
@@ -0,0 +1,15 @@
+--[[
+ 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/lua/embed/path.lua.h b/src/lua/embed/path.lua.h
new file mode 100644
index 0000000..b398c99
--- /dev/null
+++ b/src/lua/embed/path.lua.h
@@ -0,0 +1,18 @@
+/* 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