diff options
author | chai <chaifix@163.com> | 2018-11-12 08:04:11 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-11-12 08:04:11 +0800 |
commit | 72e45f0062d727cedd576d1e1251f6722454a119 (patch) | |
tree | 736594b79e71c66a668d99d96c3ce464618e50ca /src | |
parent | 7c2f33bdf37de7acf9b0728a115377081344db1c (diff) |
*修改代码结构
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/Time/je_timer.cpp | 31 | ||||
-rw-r--r-- | src/libjin/Time/je_timer.h | 22 | ||||
-rw-r--r-- | src/lua/common/je_lua_common.h | 1 | ||||
-rw-r--r-- | src/lua/common/je_lua_proxy.h | 29 | ||||
-rw-r--r-- | src/lua/common/je_lua_reference.h | 52 | ||||
-rw-r--r-- | src/lua/common/je_lua_shared.hpp | 9 | ||||
-rw-r--r-- | src/lua/embed/scripts/boot.lua.h | 12 | ||||
-rw-r--r-- | src/lua/libraries/luax/luax.h | 11 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_graphics.cpp | 24 | ||||
-rw-r--r-- | src/lua/modules/thread/je_lua_thread.cpp | 8 | ||||
-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 | ||||
-rw-r--r-- | src/lua/modules/types.h | 1 |
14 files changed, 299 insertions, 90 deletions
diff --git a/src/libjin/Time/je_timer.cpp b/src/libjin/Time/je_timer.cpp index 8c48634..3315abf 100644 --- a/src/libjin/Time/je_timer.cpp +++ b/src/libjin/Time/je_timer.cpp @@ -20,17 +20,21 @@ namespace JinEngine delete mHandlers[i]; } - void Timer::update(int ms) + void Timer::update(float dt) { // Process handler. std::vector<Handler*>::iterator it = mHandlers.begin(); for (; it != mHandlers.end(); ++it) - (*it)->process(ms); + (*it)->process(dt); // Erase canceled handler. for (it = mHandlers.begin(); it != mHandlers.end();) { if ((*it)->canceled) + { + Handler* h = *it; it = mHandlers.erase(it); + delete h; + } else ++it; } @@ -48,28 +52,28 @@ namespace JinEngine cancel(h); } - Timer::Handler* Timer::every(int ms, TimerCallback callback, void* p) + Timer::Handler* Timer::every(float dt, TimerCallback callback, void* p, FinishCallback finish) { - Handler* t = new Handler(Handler::EVERY, ms, 0, callback, p); + Handler* t = new Handler(Handler::EVERY, dt, 0, callback, p, finish); mHandlers.push_back(t); return t; } - Timer::Handler* Timer::after(int ms, TimerCallback callback, void* p) + Timer::Handler* Timer::after(float dt, TimerCallback callback, void* p, FinishCallback finish) { - Handler* t = new Handler(Handler::AFTER, ms, 0, callback, p); + Handler* t = new Handler(Handler::AFTER, dt, 0, callback, p, finish); mHandlers.push_back(t); return t; } - Timer::Handler* Timer::repeat(int ms, int count, TimerCallback callback, void* p) + Timer::Handler* Timer::repeat(float dt, int count, TimerCallback callback, void* p, FinishCallback finish) { - Handler* t = new Handler(Handler::REPEAT, ms, count, callback, p); + Handler* t = new Handler(Handler::REPEAT, dt, count, callback, p, finish); mHandlers.push_back(t); return t; } - Timer::Handler::Handler(Type t, int d, int c, TimerCallback f, void* p) + Timer::Handler::Handler(Type t, float d, int c, TimerCallback f, void* p, FinishCallback finishcallback) : type(t) , duration(d) , count(c) @@ -78,19 +82,22 @@ namespace JinEngine , callback(f) , paramters(p) , canceled(false) + , finishCallback(finishcallback) { } Timer::Handler::~Handler() { + if (finishCallback) + finishCallback(paramters); } - void Timer::Handler::process(int ms) + void Timer::Handler::process(float dt) { - tickdown -= ms; + tickdown -= dt; if (tickdown <= 0) { - tickdown = duration; + tickdown += duration; if (!canceled && callback != nullptr) callback(paramters); if (type == EVERY) diff --git a/src/libjin/Time/je_timer.h b/src/libjin/Time/je_timer.h index fabde28..31cd322 100644 --- a/src/libjin/Time/je_timer.h +++ b/src/libjin/Time/je_timer.h @@ -22,12 +22,14 @@ namespace JinEngine typedef std::function<void(void*)> TimerCallback; + typedef std::function<void(void*)> FinishCallback; + /// /// /// class Handler { - private: + public: friend class Timer; enum Type { @@ -35,16 +37,18 @@ namespace JinEngine AFTER, REPEAT, }; - Handler(Type type, int duration, int count = 0, TimerCallback callback = nullptr, void* paramters = nullptr); + Handler(Type type, float duration, int count = 0, TimerCallback callback = nullptr, void* paramters = nullptr, FinishCallback finishcallback = nullptr); virtual ~Handler(); - void process(int ms); + void process(float dt); - int duration; + protected: int count; - int tickdown; int countdown; + float duration; + float tickdown; Type type; TimerCallback callback; + FinishCallback finishCallback; void* paramters; bool canceled; }; @@ -62,22 +66,22 @@ namespace JinEngine /// /// /// - void update(int ms); + void update(float dt); /// /// /// - Handler* every(int ms, TimerCallback callback, void* paramters); + Handler* every(float dt, TimerCallback callback, void* paramters, FinishCallback finish); /// /// /// - Handler* after(int ms, TimerCallback callback, void* paramters); + Handler* after(float dt, TimerCallback callback, void* paramters, FinishCallback finish); /// /// /// - Handler* repeat(int ms, int count, TimerCallback callback, void* paramters); + Handler* repeat(float dt, int count, TimerCallback callback, void* paramters, FinishCallback finish); /// /// diff --git a/src/lua/common/je_lua_common.h b/src/lua/common/je_lua_common.h index 82c550a..5b217a2 100644 --- a/src/lua/common/je_lua_common.h +++ b/src/lua/common/je_lua_common.h @@ -5,5 +5,6 @@ #include "je_lua_proxy.h" #include "je_lua_shared.hpp" #include "je_lua_error.h" +#include "je_lua_reference.h" #endif
\ No newline at end of file diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h index b476aa6..96b2093 100644 --- a/src/lua/common/je_lua_proxy.h +++ b/src/lua/common/je_lua_proxy.h @@ -13,40 +13,43 @@ namespace JinEngine class Proxy { public: - void bind(SharedBase* shared) + void bind(SharedBase* s) { - if (shared == nullptr) + if (s == nullptr) return; - reference = shared; + shared = s; } void release() { - if (reference != nullptr) + if (shared != nullptr) { - reference->release(); - reference = nullptr; + shared->release(); + shared = nullptr; } } void retain() { - if (reference != nullptr) - reference->retain(); + if (shared != nullptr) + shared->retain(); } void setUserdata(void* data) { - if (reference != nullptr) - reference->setUserdata(data); + if (shared != nullptr) + shared->setUserdata(data); } template<class T> Shared<T>& getShared() { - return *(Shared<T>*) reference; + return *(Shared<T>*)shared; } + /// + /// For convenience. + /// template<class T> T* getObject() { @@ -56,10 +59,10 @@ namespace JinEngine const char* getObjectType() { - return reference->type; + return shared->type; } - SharedBase* reference; + SharedBase* shared; }; diff --git a/src/lua/common/je_lua_reference.h b/src/lua/common/je_lua_reference.h new file mode 100644 index 0000000..28c3bc3 --- /dev/null +++ b/src/lua/common/je_lua_reference.h @@ -0,0 +1,52 @@ +#ifndef __JIN_COMMON_REFERENCE_H +#define __JIN_COMMON_REFERENCE_H + +#include "../luax.h" + +namespace JinEngine +{ + namespace Lua + { + + /// + /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua + /// variables. + /// + class Reference + { + 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); + } + + /// + /// Push value onto the stack. + /// + void push() + { + luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex); + } + + private: + lua_State* const mL; + int mIndex; + + }; + + } // namespace Lua +} // namespace JinEngine + +#endif // __JIN_COMMON_REFERENCE_H
\ No newline at end of file diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp index 53f557a..7ac8923 100644 --- a/src/lua/common/je_lua_shared.hpp +++ b/src/lua/common/je_lua_shared.hpp @@ -1,5 +1,5 @@ -#ifndef __JIN_COMMON_REFERENCE_H -#define __JIN_COMMON_REFERENCE_H +#ifndef __JIN_COMMON_SHARED_H +#define __JIN_COMMON_SHARED_H namespace JinEngine { @@ -75,6 +75,7 @@ namespace JinEngine // Disable copy constructor. Shared(const Shared<T>& shared); + // Make shared only be able created with new. ~Shared() { T* obj = static_cast<T*>(object); @@ -83,7 +84,7 @@ namespace JinEngine }; - } -} + } // namespace Lua +} // namespace JinEngine #endif
\ No newline at end of file diff --git a/src/lua/embed/scripts/boot.lua.h b/src/lua/embed/scripts/boot.lua.h index cffeca6..af81c16 100644 --- a/src/lua/embed/scripts/boot.lua.h +++ b/src/lua/embed/scripts/boot.lua.h @@ -31,12 +31,13 @@ local function call(func, ...) end end +local step = jin.time.step +jin.time.step = nil + function jin.core.run() jin.graphics.reset() call(jin.core.onLoad) local dt = 0 - local previous = jin.time.second() - local current = previous while jin.core.running() do for _, e in pairs(jin.event.poll()) do if e.type == "KeyDown" then @@ -46,10 +47,9 @@ function jin.core.run() end call(jin.core.onEvent, e) end - previous = current - current = jin.time.second() - dt = current - previous - call(jin.core.onUpdate, dt) + step() + dt = jin.time.getDelta() + call(jin.core.onUpdate) jin.graphics.clear() call(jin.core.onDraw) jin.graphics.present() diff --git a/src/lua/libraries/luax/luax.h b/src/lua/libraries/luax/luax.h index 8a01a05..450805b 100644 --- a/src/lua/libraries/luax/luax.h +++ b/src/lua/libraries/luax/luax.h @@ -40,8 +40,8 @@ #define luax_pcall lua_pcall #define luax_setglobal lua_setglobal #define luax_setglobali(L, i, name)\ -lua_pushvalue(L, i);\ -lua_setglobal(L, name); + lua_pushvalue(L, i);\ + lua_setglobal(L, name); #define luax_pop lua_pop #define luax_newtable lua_newtable #define luax_getglobal lua_getglobal @@ -90,6 +90,8 @@ inline bool luax_checkbool(lua_State *L, int numArg) /* get value and leaves it on top of stack */ #define luax_rawgetnumber(L, i, k) (lua_rawgeti(L,i, k), lua_tonumber(L, -1)) +#define luax_rawgeti lua_rawgeti + /** * */ @@ -111,6 +113,7 @@ inline bool luax_checkbool(lua_State *L, int numArg) #define luax_pushboolean lua_pushboolean #define luax_pushlightuserdata lua_pushlightuserdata #define luax_pushnil lua_pushnil +#define luax_pushvalue lua_pushvalue //inline void luax_pushuserdata(lua_State* L, void* p) //{ @@ -168,6 +171,10 @@ inline char luax_getfieldbool(lua_State* L, int I, const char* N) char bin = lua_toboolean(L, -1); return bin; } +/** +* +*/ +#define luax_call lua_call /** * Set raw diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp index 24cdf59..3ff5710 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -482,7 +482,7 @@ namespace JinEngine } if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); Shared<Shader>& jsl = proxy->getShared<Shader>(); jsl->use(); } @@ -499,16 +499,6 @@ namespace JinEngine return 0; } - LUA_IMPLEMENT RenderMode strtomode(const char* str) - { - std::string s = std::string(str); - if (s == "fill") - return RenderMode::FILL; - else if (s == "line") - return RenderMode::LINE; - else return RenderMode::NONE; - } - LUA_IMPLEMENT int l_point(lua_State* L) { int x = luax_checknumber(L, 1); @@ -531,8 +521,7 @@ namespace JinEngine LUA_IMPLEMENT int l_rect(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); + RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1)); if (mode != RenderMode::NONE) { int x = luax_checknumber(L, 2); @@ -552,8 +541,7 @@ namespace JinEngine LUA_IMPLEMENT int l_circle(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); + RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1)); if (mode != RenderMode::NONE) { int x = luax_checknumber(L, 2); @@ -572,8 +560,7 @@ namespace JinEngine LUA_IMPLEMENT int l_triangle(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); + RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1)); if (mode != RenderMode::NONE) { int x = luax_checknumber(L, 2); @@ -598,9 +585,8 @@ namespace JinEngine LUA_IMPLEMENT int l_polygon(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); + RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1)); int n = luax_checknumber(L, 2); - RenderMode mode = strtomode(modestr); if (mode != RenderMode::NONE) { if (!luax_istable(L, 3)) diff --git a/src/lua/modules/thread/je_lua_thread.cpp b/src/lua/modules/thread/je_lua_thread.cpp index dfe45e5..8b8d1d5 100644 --- a/src/lua/modules/thread/je_lua_thread.cpp +++ b/src/lua/modules/thread/je_lua_thread.cpp @@ -126,8 +126,8 @@ namespace JinEngine case Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = luax_newinstance(L, p->getObjectType()); - p->reference->retain(); - proxy->bind(p->reference); + p->retain(); + proxy->bind(p->shared); break; } @@ -161,8 +161,8 @@ namespace JinEngine Proxy* p = (Proxy*)v.pointer; const char* objType = p->getObjectType(); Proxy* proxy = luax_newinstance(L, objType); - p->reference->retain(); - proxy->bind(p->reference); + p->retain(); + proxy->bind(p->shared); break; } 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 - { - - }; - } } diff --git a/src/lua/modules/types.h b/src/lua/modules/types.h index c6600dc..1ed1016 100644 --- a/src/lua/modules/types.h +++ b/src/lua/modules/types.h @@ -24,5 +24,6 @@ // time module #define JIN_TIME_TIMER "Timer" +#define JIN_TIME_HANDLER "Handler" #endif
\ No newline at end of file |