diff options
Diffstat (limited to 'src/lua/modules/time')
-rw-r--r-- | src/lua/modules/time/je_lua_time.cpp | 44 | ||||
-rw-r--r-- | src/lua/modules/time/je_lua_timer.cpp | 140 | ||||
-rw-r--r-- | src/lua/modules/time/je_lua_timer.h | 5 |
3 files changed, 168 insertions, 21 deletions
diff --git a/src/lua/modules/time/je_lua_time.cpp b/src/lua/modules/time/je_lua_time.cpp index c9f3563..e565eab 100644 --- a/src/lua/modules/time/je_lua_time.cpp +++ b/src/lua/modules/time/je_lua_time.cpp @@ -2,6 +2,7 @@ #include "lua/common/je_lua_common.h" #include "lua/modules/luax.h" #include "libjin/jin.h" +#include "../types.h" using namespace JinEngine::Time; @@ -10,6 +11,12 @@ namespace JinEngine namespace Lua { + static struct + { + float previous; + float current; + } context; + LUA_IMPLEMENT int l_sec(lua_State* L) { luax_pushnumber(L, getSecond()); @@ -18,19 +25,46 @@ namespace JinEngine LUA_IMPLEMENT int l_sleep(lua_State* L) { - double sec = luax_checknumber(L, 1); + double sec = luax_checknumber(L, 1); sleep(sec * 1000.0f); - return 0; + return 0; + } + + LUA_IMPLEMENT int l_newTimer(lua_State* L) + { + Proxy* proxy = luax_newinstance(L, JIN_TIME_TIMER); + proxy->bind(new Shared<Timer>(new Timer(), JIN_TIME_TIMER)); + return 1; + } + + LUA_IMPLEMENT int l_getDelta(lua_State* L) + { + luax_pushnumber(L, context.current - context.previous); + return 1; + } + + LUA_IMPLEMENT int l_step(lua_State* L) + { + context.previous = context.current; + context.current = getSecond(); + return 0; } LUA_IMPLEMENT const luaL_Reg f[] = { - { "second", l_sec }, - { "sleep", l_sleep }, - { 0, 0 }, + { "second", l_sec }, + { "sleep", l_sleep }, + { "newTimer", l_newTimer }, + { "step", l_step }, + { "getDelta", l_getDelta }, + { 0, 0 }, }; + LUA_PORT int luaopen_Timer(lua_State* L); + LUA_EXPORT int luaopen_time(lua_State* L) { + luaopen_Timer(L); + luax_newlib(L, f); return 1; } diff --git a/src/lua/modules/time/je_lua_timer.cpp b/src/lua/modules/time/je_lua_timer.cpp index e57c666..c79c16b 100644 --- a/src/lua/modules/time/je_lua_timer.cpp +++ b/src/lua/modules/time/je_lua_timer.cpp @@ -2,28 +2,146 @@ #include "lua/common/je_lua_common.h" #include "je_lua_timer.h" +using namespace JinEngine::Time; + namespace JinEngine { namespace Lua { -/* - typedef Shared<Timer>& TimerRef; - LUA_IMPLEMENT inline TimerRef checkTimer(lua_State* L) + typedef Shared<Timer>& SharedTimer; + + class Callback + { + public: + Callback() + { + } + ~Callback() + { + delete func; + delete param; + } + + Reference* func; + Reference* param; + }; + +#define TIMER_CALLBACK \ +[=](void* data)->void { \ + Callback* cbk = static_cast<Callback*>(data); \ + cbk->func->push(); \ + cbk->param->push(); \ + luax_call(L, 1, 0); \ +} + +#define FINISH_CALLBACK \ +[=](void* data) { \ + Callback* cbk = static_cast<Callback*>(data); \ + delete cbk; \ +} + + LUA_IMPLEMENT inline SharedTimer checkTimer(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); return proxy->getShared<Timer>(); } -*/ + + // timer:every(time, callback, parameter) LUA_IMPLEMENT int l_every(lua_State* L) { - //TimerRef shared = checkTimer(L); - //Timer* timer = shared.getObject(); - //int n = luax_checkinteger(L, 1); - //int shared = luax_ref(L, 2); - //timer->every(n, [](void* data)->void { - // - //}, ); + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + Callback* callback = new Callback(); + callback->func = new Reference(L, 3); + callback->param = new Reference(L, 4); + Timer::Handler* handler = timer->every(s, TIMER_CALLBACK, callback, FINISH_CALLBACK); + Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); + proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER)); + return 1; + } + + // timer:after(time, callback, parameter) + LUA_IMPLEMENT int l_after(lua_State* L) + { + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + Callback* callback = new Callback(); + callback->func = new Reference(L, 3); + callback->param = new Reference(L, 4); + Timer::Handler* handler = timer->after(s, TIMER_CALLBACK, callback, FINISH_CALLBACK); + Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); + proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER)); + return 1; + } + + // timer:repeat(time, callback, parameter) + LUA_IMPLEMENT int l_repeat(lua_State* L) + { + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + int count = luax_checkinteger(L, 3); + Callback* callback = new Callback(); + callback->func = new Reference(L, 4); + callback->param = new Reference(L, 5); + Timer::Handler* handler = timer->repeat(s, count, TIMER_CALLBACK, callback, FINISH_CALLBACK); + Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); + proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER)); + return 1; + } + + LUA_IMPLEMENT int l_update(lua_State* L) + { + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + timer->update(s); + return 0; + } + + LUA_IMPLEMENT int l_cancel(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); + Timer* timer = p->getObject<Timer>(); + Proxy* ph = (Proxy*)luax_checktype(L, 2, JIN_TIME_HANDLER); + Timer::Handler* handler = ph->getObject<Timer::Handler>(); + timer->cancel(handler); + return 0; + } + + LUA_IMPLEMENT int l_cancelAll(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); + Timer* timer = p->getObject<Timer>(); + timer->cancelAll(); + return 0; + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); + p->release(); + return 0; + } + + LUA_IMPLEMENT const luaL_Reg f[] = { + { "__gc", l_gc }, + { "every", l_every }, + { "after", l_after }, + { "duplicate", l_repeat }, + { "update", l_update }, + { "cancel", l_cancel }, + { "cancelAll", l_cancelAll }, + { 0, 0 } + }; + + LUA_EXPORT int luaopen_Timer(lua_State* L) + { + luax_newtype(L, JIN_TIME_TIMER, f); + return 0; } } diff --git a/src/lua/modules/time/je_lua_timer.h b/src/lua/modules/time/je_lua_timer.h index 9beadf5..6382c38 100644 --- a/src/lua/modules/time/je_lua_timer.h +++ b/src/lua/modules/time/je_lua_timer.h @@ -8,11 +8,6 @@ namespace JinEngine namespace Lua { - class Timer : public JinEngine::Time::Timer - { - - }; - } } |