diff options
author | chai <chaifix@163.com> | 2018-11-12 21:58:05 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-11-12 21:58:05 +0800 |
commit | af62f267a24091a16cbafe844d59cdbd4e78f5b1 (patch) | |
tree | 3862cee497b30a9c54761c7cd60c5de5af4a78d3 /src/lua | |
parent | 72e45f0062d727cedd576d1e1251f6722454a119 (diff) |
*使用int做状态机key
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/common/je_lua_function.cpp | 45 | ||||
-rw-r--r-- | src/lua/common/je_lua_function.h | 46 | ||||
-rw-r--r-- | src/lua/common/je_lua_reference.cpp | 31 | ||||
-rw-r--r-- | src/lua/common/je_lua_reference.h | 27 | ||||
-rw-r--r-- | src/lua/modules/ai/je_lua_state_machine.cpp | 10 | ||||
-rw-r--r-- | src/lua/modules/time/je_lua_timer.cpp | 51 |
6 files changed, 158 insertions, 52 deletions
diff --git a/src/lua/common/je_lua_function.cpp b/src/lua/common/je_lua_function.cpp new file mode 100644 index 0000000..e202d99 --- /dev/null +++ b/src/lua/common/je_lua_function.cpp @@ -0,0 +1,45 @@ +#include "je_lua_function.h" + +namespace JinEngine +{ + namespace Lua + { + + LuaFunc::LuaFunc(lua_State* L) + : mLuaFunc(nullptr) + , mParams(0) + , mL(L) + { + } + + LuaFunc::~LuaFunc() + { + delete mLuaFunc; + for (auto p : mParams) + delete p; + } + + void LuaFunc::setFunc(int i, uint nresults ) + { + if (mLuaFunc != nullptr) + delete mLuaFunc; + mLuaFunc = new LuaRef(mL, i); + mNResults = nresults; + } + + void LuaFunc::pushParam(int i) + { + mParams.push_back(new LuaRef(mL, i)); + } + + uint LuaFunc::call() + { + mLuaFunc->push(); + for (auto p : mParams) + p->push(); + luax_call(mL, mParams.size(), mNResults); + return mNResults; + } + + } +}
\ No newline at end of file diff --git a/src/lua/common/je_lua_function.h b/src/lua/common/je_lua_function.h new file mode 100644 index 0000000..a2d5ccc --- /dev/null +++ b/src/lua/common/je_lua_function.h @@ -0,0 +1,46 @@ +#ifndef __JIN_COMMON_FUNCTION_H +#define __JIN_COMMON_FUNCTION_H + +#include <vector> + +#include "libjin/jin.h" +#include "../luax.h" +#include "je_lua_reference.h" + +namespace JinEngine +{ + namespace Lua + { + + class LuaFunc + { + public: + LuaFunc(lua_State* L); + ~LuaFunc(); + + /// + /// + /// + void setFunc(int i, uint nresults); + + /// + /// + /// + void pushParam(int i); + + /// + /// + /// + uint call(); + + private: + LuaRef* mLuaFunc; + std::vector<LuaRef*> mParams; + lua_State* mL; + uint mNResults; + }; + + } // namespace Lua +} // namespace JinEngine + +#endif // __JIN_COMMON_REFERENCE_H
\ No newline at end of file diff --git a/src/lua/common/je_lua_reference.cpp b/src/lua/common/je_lua_reference.cpp new file mode 100644 index 0000000..90223de --- /dev/null +++ b/src/lua/common/je_lua_reference.cpp @@ -0,0 +1,31 @@ +#include "je_lua_reference.h" + +namespace JinEngine +{ + namespace Lua + { + + LuaRef::LuaRef(lua_State* L, int i) + : mL(L) + { + luax_pushvalue(mL, i); + mIndex = luax_ref(mL, LUA_REGISTRYINDEX); + } + + LuaRef::~LuaRef() + { + unref(); + } + + void LuaRef::unref() + { + luax_unref(mL, LUA_REGISTRYINDEX, mIndex); + } + + void LuaRef::push() + { + luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex); + } + + } +}
\ No newline at end of file diff --git a/src/lua/common/je_lua_reference.h b/src/lua/common/je_lua_reference.h index 28c3bc3..bde61b6 100644 --- a/src/lua/common/je_lua_reference.h +++ b/src/lua/common/je_lua_reference.h @@ -12,33 +12,18 @@ namespace JinEngine /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua /// variables. /// - class Reference + class LuaRef { public: - Reference(lua_State* L, unsigned int i) - : mL(L) - { - luax_pushvalue(mL, i); - mIndex = luax_ref(mL, LUA_REGISTRYINDEX); - } - - ~Reference() - { - unref(); - } - - void unref() - { - luax_unref(mL, LUA_REGISTRYINDEX, mIndex); - } + LuaRef(lua_State* L, int i); + ~LuaRef(); + + void unref(); /// /// Push value onto the stack. /// - void push() - { - luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex); - } + void push(); private: lua_State* const mL; diff --git a/src/lua/modules/ai/je_lua_state_machine.cpp b/src/lua/modules/ai/je_lua_state_machine.cpp index 9b95f43..82899b4 100644 --- a/src/lua/modules/ai/je_lua_state_machine.cpp +++ b/src/lua/modules/ai/je_lua_state_machine.cpp @@ -7,14 +7,14 @@ namespace JinEngine { namespace Lua { - + LUA_IMPLEMENT int l_addEnterCallback(lua_State* L) { - StateMachine* sm; - sm->addEnterListener("", [](void* p) -> void{ + //StateMachine* sm; + //sm->addEnterListener("", [](void* p) -> void{ - - }); + // + //}); return 0; } diff --git a/src/lua/modules/time/je_lua_timer.cpp b/src/lua/modules/time/je_lua_timer.cpp index c79c16b..239d1dd 100644 --- a/src/lua/modules/time/je_lua_timer.cpp +++ b/src/lua/modules/time/je_lua_timer.cpp @@ -1,4 +1,5 @@ #include "../types.h" +#include "lua/common/je_lua_function.h" #include "lua/common/je_lua_common.h" #include "je_lua_timer.h" @@ -23,23 +24,21 @@ namespace JinEngine delete param; } - Reference* func; - Reference* param; + LuaRef* func; + LuaRef* param; }; -#define TIMER_CALLBACK \ -[=](void* data)->void { \ - Callback* cbk = static_cast<Callback*>(data); \ - cbk->func->push(); \ - cbk->param->push(); \ - luax_call(L, 1, 0); \ -} + static Timer::TimerCallback Timer_Callback = [](void* data)->void + { + LuaFunc* func = static_cast<LuaFunc*>(data); + func->call(); + }; -#define FINISH_CALLBACK \ -[=](void* data) { \ - Callback* cbk = static_cast<Callback*>(data); \ - delete cbk; \ -} + static Timer::FinishCallback Finish_Callback = [](void* data)->void + { + LuaFunc* func = static_cast<LuaFunc*>(data); + delete func; + }; LUA_IMPLEMENT inline SharedTimer checkTimer(lua_State* L) { @@ -53,10 +52,10 @@ namespace JinEngine 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); + LuaFunc* func = new LuaFunc(L); + func->setFunc(3, 0); + func->pushParam(4); + Timer::Handler* handler = timer->every(s, Timer_Callback, func, Finish_Callback); Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER)); return 1; @@ -68,10 +67,10 @@ namespace JinEngine 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); + LuaFunc* func = new LuaFunc(L); + func->setFunc(3, 0); + func->pushParam(4); + Timer::Handler* handler = timer->after(s, Timer_Callback, func, Finish_Callback); Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER)); return 1; @@ -84,10 +83,10 @@ namespace JinEngine 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); + LuaFunc* func = new LuaFunc(L); + func->setFunc(4, 0); + func->pushParam(5); + Timer::Handler* handler = timer->repeat(s, count, Timer_Callback, func, Finish_Callback); Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); proxy->bind(new Shared<Timer::Handler>(handler, JIN_TIME_HANDLER)); return 1; |