aboutsummaryrefslogtreecommitdiff
path: root/src/lua/embed
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-20 08:15:34 +0800
committerchai <chaifix@163.com>2018-08-20 08:15:34 +0800
commit98ab086f430d418f33c4410cf81f4eac52d5b835 (patch)
tree446cce66de209a10c9342bf98e21524de6e4ba05 /src/lua/embed
parent22bb9b537caff927ef8c83bde82d58253ffbb1e4 (diff)
*update
Diffstat (limited to 'src/lua/embed')
-rw-r--r--src/lua/embed/boot.lua.h108
-rw-r--r--src/lua/embed/debug.lua.h132
-rw-r--r--src/lua/embed/embed.h8
-rw-r--r--src/lua/embed/graphics.lua.h5
-rw-r--r--src/lua/embed/keyboard.lua.h4
-rw-r--r--src/lua/embed/mouse.lua.h4
-rw-r--r--src/lua/embed/path.lua.h4
7 files changed, 77 insertions, 188 deletions
diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h
index c2efd0e..36615b0 100644
--- a/src/lua/embed/boot.lua.h
+++ b/src/lua/embed/boot.lua.h
@@ -1,6 +1,5 @@
/* boot.lua */
static const char* boot_lua = R"(
-
jin._argv[2] = jin._argv[2] or '.'
jin.filesystem.init()
jin.filesystem.mount(jin._argv[2])
@@ -9,31 +8,26 @@ 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())
-conf.resizable = conf.resizable or false
+conf.width = conf.width or 576
+conf.height = conf.height or 448
+conf.fps = conf.fps or 60
+conf.vsync = conf.vsync or false
+conf.title = conf.title or ("jin v" .. jin.version())
+conf.resizable = conf.resizable or false
conf.fullscreen = conf.fullscreen or false
-- initialize subsystems
jin.graphics.init(conf)
jin.audio.init()
--- open debug mode, must after jin.graphics.init
-if jin._argv[3] == '-d' then
- jin.debug.init()
-end
-
-local function safecall(func, ...)
+local function call(func, ...)
if func then
- func(...)
+ return func(...)
end
end
function jin.core.run()
- safecall(jin.core.onLoad)
+ call(jin.core.onLoad)
local previous = jin.time.second()
local SEC_PER_UPDATE = 1 / conf.fps
local dt = SEC_PER_UPDATE
@@ -45,22 +39,19 @@ function jin.core.run()
elseif e.type == "keyup" then
jin.keyboard.set(e.key, false)
end
- safecall(jin.core.onEvent, e)
+ call(jin.core.onEvent, e)
running = jin.core.running()
if not running then break end
end
if not running then break end
- safecall(jin.core.onUpdate, dt)
+ call(jin.core.onUpdate, dt)
jin.graphics.unbind()
jin.graphics.clear()
jin.graphics.color()
jin.graphics.study()
- safecall(jin.core.onDraw)
- if jin.debug.status() then
- jin.debug.render()
- end
+ call(jin.core.onDraw)
jin.graphics.present()
local current = jin.time.second()
@@ -91,24 +82,73 @@ local function onError(msg)
end
end
+-------------------------------------------------------------------------------
+-- No game handler
+-------------------------------------------------------------------------------
+
+jin.nogame = {
+ cs = 64,
+ sw = jin.graphics.getWidth(),
+ sh = jin.graphics.getHeight(),
+ cw = 0,
+ ch = 0,
+ ww = 6,
+ ww2 = 6*2,
+ speed = 4,
+ t = 0,
+ load = function()
+ local nogame = jin.nogame
+ nogame.cw = nogame.sw / nogame.cs
+ nogame.ch = nogame.sh / nogame.cs
+ nogame.t = nogame.ww - 1
+ end,
+ event = function(e)
+ if e.type == 'quit' then
+ jin.core.stop()
+ end
+ end,
+ update = function(dt)
+ local nogame = jin.nogame
+ nogame.t = nogame.t + dt * nogame.speed
+ if nogame.t > nogame.ww2 then
+ nogame.t = nogame.t - nogame.ww2
+ end
+ end,
+ circle = function(x, y, r)
+ local nogame = jin.nogame
+ if r % nogame.ww2 > nogame.ww then
+ return
+ end
+ r = math.sin((r/nogame.ww)*math.pi)*nogame.cs/2
+ local fact = (x + y) / nogame.ch * nogame.cw
+ jin.graphics.color(155 + 100 * math.sin(fact), 155 + 100 * math.cos(fact), 155 + 100 * math.sin(fact * fact), 255)
+ jin.graphics.circle("fill", x*nogame.cs + nogame.cs/2, y*nogame.cs + nogame.cs/2, r)
+ end,
+ draw = function()
+ local nogame = jin.nogame
+ for y = 0, nogame.ch - 1 do
+ for x = 0, nogame.cw - 1 do
+ nogame.circle(x, y, nogame.t+x+y)
+ end
+ end
+ end
+}
+
+-------------------------------------------------------------------------------
+-- Boot jin
+-------------------------------------------------------------------------------
+
local function boot()
if jin.filesystem.exist("main.lua") then
-- require main game script
xpcall(function() require"main" end, onError)
jin.core.run()
- else
+ else
-- no game
- function jin.core.onEvent(e)
- if e.type == 'quit' then
- jin.core.stop()
- 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.onLoad = jin.nogame.load
+ jin.core.onEvent = jin.nogame.event
+ jin.core.onUpdate = jin.nogame.update
+ jin.core.onDraw = jin.nogame.draw
jin.core.run()
end
-- quit subsystems
@@ -118,6 +158,6 @@ local function boot()
jin.core.quit()
end
-boot()
+xpcall(boot, onError)
)";
diff --git a/src/lua/embed/debug.lua.h b/src/lua/embed/debug.lua.h
deleted file mode 100644
index 79c95ba..0000000
--- a/src/lua/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/lua/embed/embed.h b/src/lua/embed/embed.h
index 33620b0..18ed1d8 100644
--- a/src/lua/embed/embed.h
+++ b/src/lua/embed/embed.h
@@ -19,7 +19,7 @@ namespace embed
*/
struct jin_Embed
{
- const char* fname, *source;
+ const char* file, *source;
};
static void boot(lua_State* L)
@@ -28,7 +28,6 @@ namespace embed
#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
@@ -36,14 +35,13 @@ namespace embed
{ "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);
+ for (int i = 0; scripts[i].file; ++i)
+ embed(L, scripts[i].source, scripts[i].file);
}
}
}
diff --git a/src/lua/embed/graphics.lua.h b/src/lua/embed/graphics.lua.h
index c27baaf..1414efc 100644
--- a/src/lua/embed/graphics.lua.h
+++ b/src/lua/embed/graphics.lua.h
@@ -1,9 +1,4 @@
/* graphics.lua */
static const char* graphics_lua = R"(
------------------
--- jin.graphics
------------------
-
jin.graphics = jin.graphics or {}
-
)";
diff --git a/src/lua/embed/keyboard.lua.h b/src/lua/embed/keyboard.lua.h
index e892b66..77bf3a9 100644
--- a/src/lua/embed/keyboard.lua.h
+++ b/src/lua/embed/keyboard.lua.h
@@ -1,9 +1,5 @@
static const char* keyboard_lua = R"(
---[[
- jin.keyboard extension
-]]
-
jin.keyboard = jin.keyboard or {}
local keys = {}
diff --git a/src/lua/embed/mouse.lua.h b/src/lua/embed/mouse.lua.h
index f57d08c..3c222f3 100644
--- a/src/lua/embed/mouse.lua.h
+++ b/src/lua/embed/mouse.lua.h
@@ -1,8 +1,4 @@
static const char* mouse_lua = R"(
---[[
- jin.mouse extension
-]]
-
jin.mouse = jin.mouse or {}
local button = {}
diff --git a/src/lua/embed/path.lua.h b/src/lua/embed/path.lua.h
index b398c99..648adf8 100644
--- a/src/lua/embed/path.lua.h
+++ b/src/lua/embed/path.lua.h
@@ -1,9 +1,5 @@
/* path.lua */
static const char* path_lua = R"(
---[[
- jin.path extension
-]]
-
jin.path = jin.path or {}
-- game root directory