diff options
Diffstat (limited to 'src/lua/modules/time/je_lua_timer.cpp')
-rw-r--r-- | src/lua/modules/time/je_lua_timer.cpp | 57 |
1 files changed, 24 insertions, 33 deletions
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; } |