aboutsummaryrefslogtreecommitdiff
path: root/src/lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/common/je_lua_callback.cpp (renamed from src/lua/common/je_lua_function.cpp)16
-rw-r--r--src/lua/common/je_lua_callback.h (renamed from src/lua/common/je_lua_function.h)35
-rw-r--r--src/lua/common/je_lua_reference.h17
-rw-r--r--src/lua/common/je_lua_shared.hpp8
-rw-r--r--src/lua/jin.cpp47
-rw-r--r--src/lua/luax.h4
-rw-r--r--src/lua/modules/audio/je_lua_audio.cpp4
-rw-r--r--src/lua/modules/graphics/je_lua_graphics.cpp36
-rw-r--r--src/lua/modules/net/je_lua_net.cpp7
-rw-r--r--src/lua/modules/time/je_lua_time.cpp5
-rw-r--r--src/lua/modules/time/je_lua_timer.cpp57
11 files changed, 122 insertions, 114 deletions
diff --git a/src/lua/common/je_lua_function.cpp b/src/lua/common/je_lua_callback.cpp
index e202d99..392f919 100644
--- a/src/lua/common/je_lua_function.cpp
+++ b/src/lua/common/je_lua_callback.cpp
@@ -1,44 +1,42 @@
-#include "je_lua_function.h"
+#include "je_lua_callback.h"
namespace JinEngine
{
namespace Lua
{
- LuaFunc::LuaFunc(lua_State* L)
+ LuaCallback::LuaCallback(lua_State* L)
: mLuaFunc(nullptr)
, mParams(0)
, mL(L)
{
}
- LuaFunc::~LuaFunc()
+ LuaCallback::~LuaCallback()
{
delete mLuaFunc;
for (auto p : mParams)
delete p;
}
- void LuaFunc::setFunc(int i, uint nresults )
+ void LuaCallback::setFunc(int i)
{
if (mLuaFunc != nullptr)
delete mLuaFunc;
mLuaFunc = new LuaRef(mL, i);
- mNResults = nresults;
}
- void LuaFunc::pushParam(int i)
+ void LuaCallback::pushParam(int i)
{
mParams.push_back(new LuaRef(mL, i));
}
- uint LuaFunc::call()
+ void LuaCallback::call()
{
mLuaFunc->push();
for (auto p : mParams)
p->push();
- luax_call(mL, mParams.size(), mNResults);
- return mNResults;
+ luax_call(mL, mParams.size(), 0);
}
}
diff --git a/src/lua/common/je_lua_function.h b/src/lua/common/je_lua_callback.h
index a2d5ccc..f3301fc 100644
--- a/src/lua/common/je_lua_function.h
+++ b/src/lua/common/je_lua_callback.h
@@ -12,16 +12,26 @@ namespace JinEngine
namespace Lua
{
- class LuaFunc
+ ///
+ ///
+ ///
+ class LuaCallback
{
public:
- LuaFunc(lua_State* L);
- ~LuaFunc();
+ ///
+ ///
+ ///
+ LuaCallback(lua_State* L);
+
+ ///
+ ///
+ ///
+ ~LuaCallback();
///
///
///
- void setFunc(int i, uint nresults);
+ void setFunc(int i);
///
///
@@ -31,13 +41,24 @@ namespace JinEngine
///
///
///
- uint call();
+ void call();
private:
+ ///
+ ///
+ ///
LuaRef* mLuaFunc;
+
+ ///
+ ///
+ ///
std::vector<LuaRef*> mParams;
- lua_State* mL;
- uint mNResults;
+
+ ///
+ ///
+ ///
+ lua_State* const mL;
+
};
} // namespace Lua
diff --git a/src/lua/common/je_lua_reference.h b/src/lua/common/je_lua_reference.h
index d2d077f..f338762 100644
--- a/src/lua/common/je_lua_reference.h
+++ b/src/lua/common/je_lua_reference.h
@@ -15,9 +15,19 @@ namespace JinEngine
class LuaRef
{
public:
+ ///
+ ///
+ ///
LuaRef(lua_State* L, int i);
+
+ ///
+ ///
+ ///
~LuaRef();
+ ///
+ ///
+ ///
void unref();
///
@@ -26,7 +36,14 @@ namespace JinEngine
void push();
private:
+ ///
+ ///
+ ///
lua_State* const mL;
+
+ ///
+ ///
+ ///
int mIndex;
};
diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp
index 7ac8923..876fe28 100644
--- a/src/lua/common/je_lua_shared.hpp
+++ b/src/lua/common/je_lua_shared.hpp
@@ -6,7 +6,7 @@ namespace JinEngine
namespace Lua
{
- /*abstract*/class SharedBase
+ class SharedBase
{
public:
void retain()
@@ -20,7 +20,7 @@ namespace JinEngine
delete this;
}
- // object type string
+ // Object type string.
const char* const type;
void setUserdata(void* data)
@@ -63,12 +63,12 @@ namespace JinEngine
T* operator->()
{
- return (T*)object;
+ return static_cast<T*>(object);
}
T* getObject()
{
- return (T*)object;
+ return static_cast<T*>(object);
}
private:
diff --git a/src/lua/jin.cpp b/src/lua/jin.cpp
index 186b9b2..d9f685b 100644
--- a/src/lua/jin.cpp
+++ b/src/lua/jin.cpp
@@ -64,39 +64,38 @@ namespace JinEngine
{ 0, 0 }
};
- /* sub modules */
- LUA_IMPLEMENT const luax_Reg mods[] = {
- { "core", luaopen_core },
- { "event", luaopen_event },
- { "graphics", luaopen_graphics },
- { "time", luaopen_time },
- { "mouse", luaopen_mouse },
- { "keyboard", luaopen_keyboard },
- { "filesystem", luaopen_filesystem },
- { "net", luaopen_net },
- { "audio", luaopen_audio },
- { "joypad", luaopen_joypad },
- { "math", luaopen_math },
- { "thread", luaopen_thread },
- { "bit", luaopen_bit },
- //{"ai", luaopen_ai },
- { 0, 0 }
- };
-
/* register jin module, keep it on the top of stack */
LUA_EXPORT int luaopen_jin(lua_State* L)
{
luax_globaltable(L, MODULE_NAME);
- /* register values */
+ // Register values.
luax_setfieldstrings(L, s);
luax_setfieldnumbers(L, n);
- /* register submodules */
- for (int i = 0; mods[i].name; ++i)
+ luax_Reg modules[] = {
+ { "core", luaopen_core },
+ { "event", luaopen_event },
+ { "graphics", luaopen_graphics },
+ { "time", luaopen_time },
+ { "mouse", luaopen_mouse },
+ { "keyboard", luaopen_keyboard },
+ { "filesystem", luaopen_filesystem },
+ { "net", luaopen_net },
+ { "audio", luaopen_audio },
+ { "joypad", luaopen_joypad },
+ { "math", luaopen_math },
+ { "thread", luaopen_thread },
+ { "bit", luaopen_bit },
+ //{"ai", luaopen_ai },
+ { 0, 0 }
+ };
+
+ // Register sub modules.
+ for (int i = 0; modules[i].name; ++i)
{
- mods[i].func(L);
- luax_setfield(L, -2, mods[i].name);
+ modules[i].func(L);
+ luax_setfield(L, -2, modules[i].name);
}
return 1;
diff --git a/src/lua/luax.h b/src/lua/luax.h
index 89e456e..1c2081c 100644
--- a/src/lua/luax.h
+++ b/src/lua/luax.h
@@ -4,4 +4,8 @@
#include "LuaJIT/lua.hpp"
#include "lua/libraries/luax/luax.h"
+#define luax_newclass(l, f) \
+extern int f(lua_State*);\
+f(l)
+
#endif \ No newline at end of file
diff --git a/src/lua/modules/audio/je_lua_audio.cpp b/src/lua/modules/audio/je_lua_audio.cpp
index 92e2e04..29705b2 100644
--- a/src/lua/modules/audio/je_lua_audio.cpp
+++ b/src/lua/modules/audio/je_lua_audio.cpp
@@ -109,11 +109,9 @@ namespace JinEngine
return 0;
}
- LUA_PORT int luaopen_Source(lua_State* L);
-
LUA_EXPORT int luaopen_audio(lua_State* L)
{
- luaopen_Source(L);
+ luax_newclass(L, luaopen_Source);
luaL_Reg f[] = {
{ "init", l_init },
diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp
index 3155aa0..b1437e2 100644
--- a/src/lua/modules/graphics/je_lua_graphics.cpp
+++ b/src/lua/modules/graphics/je_lua_graphics.cpp
@@ -426,7 +426,7 @@ namespace JinEngine
{
if (luax_gettop(L) == 0)
{
- glColor4f(1, 1, 1, 1);
+ gl.setColor(Color(255, 255, 255, 255));
return 0;
}
@@ -437,10 +437,7 @@ namespace JinEngine
context.curRenderColor.a = luax_checknumber(L, 4);
else
context.curRenderColor.a = 255;
- glColor4f(context.curRenderColor.r / 255.f,
- context.curRenderColor.g / 255.f,
- context.curRenderColor.b / 255.f,
- context.curRenderColor.a / 255.f);
+ gl.setColor(context.curRenderColor);
return 0;
}
@@ -738,28 +735,17 @@ namespace JinEngine
return 0;
}
- LUA_PORT int luaopen_Texture(lua_State* L);
- LUA_PORT int luaopen_Text(lua_State* L);
- LUA_PORT int luaopen_TTF(lua_State* L);
- LUA_PORT int luaopen_TextureFont(lua_State* L);
- LUA_PORT int luaopen_TTFData(lua_State* L);
- LUA_PORT int luaopen_Page(lua_State* L);
- LUA_PORT int luaopen_Canvas(lua_State* L);
- LUA_PORT int luaopen_JSL(lua_State* L);
- LUA_PORT int luaopen_Bitmap(lua_State* L);
-
LUA_EXPORT int luaopen_graphics(lua_State* L)
{
- // register types
- luaopen_Bitmap(L);
- luaopen_Texture(L);
- luaopen_Canvas(L);
- luaopen_TTFData(L);
- luaopen_TTF(L);
- luaopen_Text(L);
- luaopen_TextureFont(L);
- luaopen_Page(L);
- luaopen_JSL(L);
+ luax_newclass(L, luaopen_Bitmap);
+ luax_newclass(L, luaopen_Texture);
+ luax_newclass(L, luaopen_Canvas);
+ luax_newclass(L, luaopen_TTFData);
+ luax_newclass(L, luaopen_TTF);
+ luax_newclass(L, luaopen_Text);
+ luax_newclass(L, luaopen_TextureFont);
+ luax_newclass(L, luaopen_Page);
+ luax_newclass(L, luaopen_JSL);
luaL_Reg f[] = {
/* window */
diff --git a/src/lua/modules/net/je_lua_net.cpp b/src/lua/modules/net/je_lua_net.cpp
index 4240903..1d0204a 100644
--- a/src/lua/modules/net/je_lua_net.cpp
+++ b/src/lua/modules/net/je_lua_net.cpp
@@ -62,13 +62,10 @@ namespace Lua
return 1;
}
- LUA_PORT int luaopen_Socket(lua_State* L);
- LUA_PORT int luaopen_Buffer(lua_State* L);
-
LUA_EXPORT int luaopen_net(lua_State* L)
{
- luaopen_Socket(L);
- luaopen_Buffer(L);
+ luax_newclass(L, luaopen_Socket);
+ luax_newclass(L, luaopen_Buffer);
luaL_Reg f[] = {
{ "init", l_initNetwork },
diff --git a/src/lua/modules/time/je_lua_time.cpp b/src/lua/modules/time/je_lua_time.cpp
index c42fc0f..4546337 100644
--- a/src/lua/modules/time/je_lua_time.cpp
+++ b/src/lua/modules/time/je_lua_time.cpp
@@ -50,12 +50,9 @@ namespace JinEngine
return 0;
}
- LUA_PORT int luaopen_Timer(lua_State* L);
-
LUA_EXPORT int luaopen_time(lua_State* L)
{
- luaopen_Timer(L);
-
+ luax_newclass(L, luaopen_Timer);
luaL_Reg f[] = {
{ "second", l_sec },
{ "sleep", l_sleep },
diff --git a/src/lua/modules/time/je_lua_timer.cpp b/src/lua/modules/time/je_lua_timer.cpp
index 42d4215..6e5c390 100644
--- a/src/lua/modules/time/je_lua_timer.cpp
+++ b/src/lua/modules/time/je_lua_timer.cpp
@@ -1,5 +1,5 @@
#include "../types.h"
-#include "lua/common/je_lua_function.h"
+#include "lua/common/je_lua_callback.h"
#include "lua/common/je_lua_common.h"
#include "je_lua_timer.h"
@@ -12,31 +12,15 @@ namespace JinEngine
typedef Shared<Timer>& SharedTimer;
- class Callback
- {
- public:
- Callback()
- {
- }
- ~Callback()
- {
- delete func;
- delete param;
- }
-
- LuaRef* func;
- LuaRef* param;
- };
-
- static Timer::TimerCallback Timer_Callback = [](void* data)->void
+ static Timer::TimerCallback timerCallback = [](void* data)->void
{
- LuaFunc* func = static_cast<LuaFunc*>(data);
+ LuaCallback* func = static_cast<LuaCallback*>(data);
func->call();
};
- static Timer::FinishCallback Finish_Callback = [](void* data)->void
+ static Timer::FinishCallback finishCallback = [](void* data)->void
{
- LuaFunc* func = static_cast<LuaFunc*>(data);
+ LuaCallback* func = static_cast<LuaCallback*>(data);
delete func;
};
@@ -49,13 +33,15 @@ namespace JinEngine
// timer:every(time, callback, parameter)
LUA_IMPLEMENT int l_every(lua_State* L)
{
+ int n = luax_gettop(L);
SharedTimer shared = checkTimer(L);
Timer* timer = shared.getObject();
float s = luax_checknumber(L, 2);
- LuaFunc* func = new LuaFunc(L);
- func->setFunc(3, 0);
- func->pushParam(4);
- Timer::Handler* handler = timer->every(s, Timer_Callback, func, Finish_Callback);
+ LuaCallback* func = new LuaCallback(L);
+ func->setFunc(3);
+ for(int i = 4; i <= n; ++i)
+ func->pushParam(i);
+ Timer::Handler* handler = timer->every(s, timerCallback, func, finishCallback);
Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER);
proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER));
return 1;
@@ -64,13 +50,15 @@ namespace JinEngine
// timer:after(time, callback, parameter)
LUA_IMPLEMENT int l_after(lua_State* L)
{
+ int n = luax_gettop(L);
SharedTimer shared = checkTimer(L);
Timer* timer = shared.getObject();
float s = luax_checknumber(L, 2);
- LuaFunc* func = new LuaFunc(L);
- func->setFunc(3, 0);
- func->pushParam(4);
- Timer::Handler* handler = timer->after(s, Timer_Callback, func, Finish_Callback);
+ LuaCallback* func = new LuaCallback(L);
+ func->setFunc(3);
+ for (int i = 4; i <= n; ++i)
+ func->pushParam(i);
+ Timer::Handler* handler = timer->after(s, timerCallback, func, finishCallback);
Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER);
proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER));
return 1;
@@ -79,14 +67,16 @@ namespace JinEngine
// timer:repeat(time, callback, parameter)
LUA_IMPLEMENT int l_repeat(lua_State* L)
{
+ int n = luax_gettop(L);
SharedTimer shared = checkTimer(L);
Timer* timer = shared.getObject();
float s = luax_checknumber(L, 2);
int count = luax_checkinteger(L, 3);
- LuaFunc* func = new LuaFunc(L);
- func->setFunc(4, 0);
- func->pushParam(5);
- Timer::Handler* handler = timer->repeat(s, count, Timer_Callback, func, Finish_Callback);
+ LuaCallback* func = new LuaCallback(L);
+ func->setFunc(4);
+ for (int i = 5; i <= n; ++i)
+ func->pushParam(i);
+ Timer::Handler* handler = timer->repeat(s, count, timerCallback, func, finishCallback);
Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER);
proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER));
return 1;
@@ -140,6 +130,7 @@ namespace JinEngine
};
luax_newtype(L, JIN_TIME_TIMER, f);
+
return 0;
}