aboutsummaryrefslogtreecommitdiff
path: root/src/lua
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
parent22bb9b537caff927ef8c83bde82d58253ffbb1e4 (diff)
*update
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/audio/luaopen_audio.cpp18
-rw-r--r--src/lua/debug/luaopen_debug.cpp8
-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
-rw-r--r--src/lua/graphics/luaopen_graphics.cpp76
-rw-r--r--src/lua/luax.h5
-rw-r--r--src/lua/net/luaopen_net.cpp8
-rw-r--r--src/lua/thread/luaopen_Thread.cpp7
-rw-r--r--src/lua/thread/luaopen_thread.cpp7
14 files changed, 143 insertions, 251 deletions
diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp
index 03b33e6..c87df3a 100644
--- a/src/lua/audio/luaopen_audio.cpp
+++ b/src/lua/audio/luaopen_audio.cpp
@@ -84,15 +84,15 @@ namespace lua
}
static const luaL_Reg f[] = {
- { "init", l_init },
- { "play", l_play },
- { "stop", l_stop },
- { "pause", l_pause },
- { "resume", l_resume },
- { "setVolume",l_setVolume },
- { "Source", l_newSource },
- { "destroy", l_destroy },
- { 0, 0 }
+ { "init", l_init },
+ { "play", l_play },
+ { "stop", l_stop },
+ { "pause", l_pause },
+ { "resume", l_resume },
+ { "setVolume", l_setVolume },
+ { "newSource", l_newSource },
+ { "destroy", l_destroy },
+ { 0, 0 }
};
extern int luaopen_Source(lua_State* L);
diff --git a/src/lua/debug/luaopen_debug.cpp b/src/lua/debug/luaopen_debug.cpp
deleted file mode 100644
index b33c058..0000000
--- a/src/lua/debug/luaopen_debug.cpp
+++ /dev/null
@@ -1,8 +0,0 @@
-namespace jin
-{
-namespace debug
-{
-
-
-}
-} \ No newline at end of file
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
diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp
index 42e8f3c..c189675 100644
--- a/src/lua/graphics/luaopen_graphics.cpp
+++ b/src/lua/graphics/luaopen_graphics.cpp
@@ -46,14 +46,28 @@ namespace lua
return 0;
}
- static int l_getSize(lua_State* L)
+ static int l_getSize(lua_State* L)
{
Window* wnd = Window::get();
luax_pushnumber(L, wnd->getW());
- luax_pushnumber(L, wnd->getH());
- return 2;
+ luax_pushnumber(L, wnd->getH());
+ return 2;
}
-
+
+ static int l_getWidth(lua_State* L)
+ {
+ Window* wnd = Window::get();
+ luax_pushnumber(L, wnd->getW());
+ return 1;
+ }
+
+ static int l_getHeight(lua_State* L)
+ {
+ Window* wnd = Window::get();
+ luax_pushnumber(L, wnd->getH());
+ return 1;
+ }
+
static int l_newImage(lua_State* L)
{
Filesystem* fs = Filesystem::get();
@@ -426,32 +440,34 @@ namespace lua
}
static const luaL_Reg f[] = {
- { "init", l_init },
- { "size", l_getSize },
- { "Image", l_newImage },
- { "Shader", l_newShader },
- { "Canvas", l_newCanvas },
- { "Font", l_newFont },
- { "box", l_box },
- { "write", l_write },
- { "clear", l_clear },
- { "draw", l_draw },
- { "color", l_setColor },
- { "palette", l_getColor },
- { "present", l_present },
- { "study", l_study },
- { "bind", l_bindCanvas },
- { "unbind", l_unbindCanvas },
- { "use", l_useShader },
- { "unuse", l_unuseShader },
- { "point", l_drawpoint },
- { "line", l_drawLine },
- { "rect", l_drawRect },
- { "circle", l_drawCircle },
- { "triangle", l_drawTriangle },
- { "polygon", l_drawPolygon },
- { "destroy", l_destroy },
- { 0, 0 }
+ { "init", l_init },
+ { "size", l_getSize },
+ { "getWidth", l_getWidth },
+ { "getHeight", l_getHeight },
+ { "newImage", l_newImage },
+ { "newShader", l_newShader },
+ { "newCanvas", l_newCanvas },
+ { "newFont", l_newFont },
+ { "box", l_box },
+ { "write", l_write },
+ { "clear", l_clear },
+ { "draw", l_draw },
+ { "color", l_setColor },
+ { "palette", l_getColor },
+ { "present", l_present },
+ { "study", l_study },
+ { "bind", l_bindCanvas },
+ { "unbind", l_unbindCanvas },
+ { "use", l_useShader },
+ { "unuse", l_unuseShader },
+ { "point", l_drawpoint },
+ { "line", l_drawLine },
+ { "rect", l_drawRect },
+ { "circle", l_drawCircle },
+ { "triangle", l_drawTriangle },
+ { "polygon", l_drawPolygon },
+ { "destroy", l_destroy },
+ { 0, 0 }
};
extern int luaopen_Image(lua_State* L);
diff --git a/src/lua/luax.h b/src/lua/luax.h
index 3b587d3..78c15a6 100644
--- a/src/lua/luax.h
+++ b/src/lua/luax.h
@@ -1,10 +1,7 @@
#ifndef __JIN_LUA_LUAX_H
#define __JIN_LUA_LUAX_H
-#include "3rdparty/lua51/lua.h"
-#include "3rdparty/lua51/lauxlib.h"
-#include "3rdparty/lua51/lualib.h"
-
+#include "LuaJIT/lua.hpp"
#include "3rdparty/luax/luax.h"
#endif \ No newline at end of file
diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp
index 600f479..12e87eb 100644
--- a/src/lua/net/luaopen_net.cpp
+++ b/src/lua/net/luaopen_net.cpp
@@ -63,10 +63,10 @@ namespace lua
}
static const luaL_Reg f[] = {
- { "init", l_initNetwork },
- { "Socket", l_Socket },
- { "Buffer", l_Buffer },
- { 0, 0 }
+ { "init", l_initNetwork },
+ { "newSocket", l_Socket },
+ { "newBuffer", l_Buffer },
+ { 0, 0 }
};
extern int luaopen_Socket(lua_State* L);
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp
index a427e30..141b873 100644
--- a/src/lua/thread/luaopen_Thread.cpp
+++ b/src/lua/thread/luaopen_Thread.cpp
@@ -212,7 +212,6 @@ namespace lua
return 0;
}
- // jin.thread.Thread(name)
static int l_newThread(lua_State* L)
{
const char* name = luax_checkstring(L, 1);
@@ -231,7 +230,7 @@ namespace lua
}
static const luaL_Reg f[] = {
- { "Thread", l_newThread },
+ { "newThread", l_newThread },
{ "getThread", l_getThread },
{ 0, 0 }
};
@@ -245,5 +244,5 @@ namespace lua
return 1;
}
-}
-} \ No newline at end of file
+} // lua
+} // jin \ No newline at end of file
diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp
index a427e30..141b873 100644
--- a/src/lua/thread/luaopen_thread.cpp
+++ b/src/lua/thread/luaopen_thread.cpp
@@ -212,7 +212,6 @@ namespace lua
return 0;
}
- // jin.thread.Thread(name)
static int l_newThread(lua_State* L)
{
const char* name = luax_checkstring(L, 1);
@@ -231,7 +230,7 @@ namespace lua
}
static const luaL_Reg f[] = {
- { "Thread", l_newThread },
+ { "newThread", l_newThread },
{ "getThread", l_getThread },
{ 0, 0 }
};
@@ -245,5 +244,5 @@ namespace lua
return 1;
}
-}
-} \ No newline at end of file
+} // lua
+} // jin \ No newline at end of file