From 75792c15480d3d99b2ba7e79e143e4b569b22611 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 13 Nov 2018 08:27:26 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9lua=20ref?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/3rdparty/buildvm/buildvm.exe | Bin 121856 -> 123392 bytes src/3rdparty/minilua/minilua.exe | Bin 209920 -> 219136 bytes src/libjin/Graphics/je_gl.cpp | 18 +++++++ src/libjin/Graphics/je_gl.h | 10 ++++ src/libjin/Graphics/je_sprite.cpp | 63 +++++++++++++++++++++++-- src/libjin/Graphics/je_sprite.h | 34 ++++++++------ src/lua/common/je_lua_callback.cpp | 43 +++++++++++++++++ src/lua/common/je_lua_callback.h | 67 +++++++++++++++++++++++++++ src/lua/common/je_lua_function.cpp | 45 ------------------ src/lua/common/je_lua_function.h | 46 ------------------ src/lua/common/je_lua_reference.h | 17 +++++++ src/lua/common/je_lua_shared.hpp | 8 ++-- src/lua/jin.cpp | 47 +++++++++---------- src/lua/luax.h | 4 ++ src/lua/modules/audio/je_lua_audio.cpp | 4 +- src/lua/modules/graphics/je_lua_graphics.cpp | 36 +++++--------- src/lua/modules/net/je_lua_net.cpp | 7 +-- src/lua/modules/time/je_lua_time.cpp | 5 +- src/lua/modules/time/je_lua_timer.cpp | 57 ++++++++++------------- 19 files changed, 303 insertions(+), 208 deletions(-) create mode 100644 src/lua/common/je_lua_callback.cpp create mode 100644 src/lua/common/je_lua_callback.h delete mode 100644 src/lua/common/je_lua_function.cpp delete mode 100644 src/lua/common/je_lua_function.h (limited to 'src') diff --git a/src/3rdparty/buildvm/buildvm.exe b/src/3rdparty/buildvm/buildvm.exe index 9cacff6..00cd94d 100644 Binary files a/src/3rdparty/buildvm/buildvm.exe and b/src/3rdparty/buildvm/buildvm.exe differ diff --git a/src/3rdparty/minilua/minilua.exe b/src/3rdparty/minilua/minilua.exe index 512e93b..003f2a1 100644 Binary files a/src/3rdparty/minilua/minilua.exe and b/src/3rdparty/minilua/minilua.exe differ diff --git a/src/libjin/Graphics/je_gl.cpp b/src/libjin/Graphics/je_gl.cpp index 9ef1460..f72ce37 100644 --- a/src/libjin/Graphics/je_gl.cpp +++ b/src/libjin/Graphics/je_gl.cpp @@ -1,5 +1,6 @@ #define OGL2D_IMPLEMENT #include "je_gl.h" +#include "je_color.h" namespace JinEngine { @@ -8,5 +9,22 @@ namespace JinEngine OpenGL gl; + void OpenGL::setColor(Channel r, Channel g, Channel b, Channel a) + { + setColor(Color(r, g, b, a)); + } + + void OpenGL::setColor(Color c) + { + mCurrentColor = c; + glColor4f(c.r / 255.f, c.g / 255.f, c.b / 255.f, c.a / 255.f); + } + + Color OpenGL::getColor() + { + return mCurrentColor; + } + + } // namespace Graphics } // namespace JinEngine diff --git a/src/libjin/Graphics/je_gl.h b/src/libjin/Graphics/je_gl.h index 48dccc2..331d899 100644 --- a/src/libjin/Graphics/je_gl.h +++ b/src/libjin/Graphics/je_gl.h @@ -3,6 +3,7 @@ #include "../math/je_matrix.h" +#include "je_color.h" #include "GLee/GLee.h" #include "ogl/OpenGL.h" @@ -31,6 +32,15 @@ namespace JinEngine { } + void setColor(Channel r, Channel g, Channel b, Channel a); + + void setColor(Color c); + + Color getColor(); + + private: + Color mCurrentColor; + }; extern OpenGL gl; diff --git a/src/libjin/Graphics/je_sprite.cpp b/src/libjin/Graphics/je_sprite.cpp index 4d4fa12..4ef32a4 100644 --- a/src/libjin/Graphics/je_sprite.cpp +++ b/src/libjin/Graphics/je_sprite.cpp @@ -1,16 +1,69 @@ +#include "shaders/je_shader.h" + #include "je_sprite.h" +using namespace JinEngine::Graphics::Shaders; + namespace JinEngine { namespace Graphics { - void Sprite::onRender() + Sprite::Sprite() + : mShader(nullptr) + , mGraphic(nullptr) + { + } + + void Sprite::setRotation(float r) + { + mRotation = r; + } + + void Sprite::setOrigin(Origin origin) + { + + } + + void Sprite::setOrigin(int x, int y) + { + + } + + void Sprite::setPosition(int x, int y) + { + + } + + void Sprite::setScale(float x, float y) + { + + } + + void Sprite::setColor(Color color) + { + + } + + void Sprite::setShader(const Shader* shader) + { + + } + + void Sprite::setGraphic(const Graphic* graphic) + { + + } + + void Sprite::render() { - if (mShader != nullptr) - mShader->use(); - - mShader->unuse(); + Shader* shader = Shader::getCurrentShader(); + Color c = gl.getColor(); + gl.setColor(mColor); + mShader->use(); + mGraphic->render(mPosition.x, mPosition.y, mScale.x, mScale.y, mRotation, mOrigin.x, mOrigin.y); + shader->use(); + gl.setColor(c); } } // namespace Graphics diff --git a/src/libjin/Graphics/je_sprite.h b/src/libjin/Graphics/je_sprite.h index 76ef30a..4fb7ebf 100644 --- a/src/libjin/Graphics/je_sprite.h +++ b/src/libjin/Graphics/je_sprite.h @@ -11,19 +11,6 @@ namespace JinEngine { namespace Graphics { - /** Determines position of the sprite in its bounds. */ - enum SpriteAnchor - { - SA_TopLeft, - SA_TopCenter, - SA_TopRight, - SA_MiddleLeft, - SA_MiddleCenter, - SA_MiddleRight, - SA_BottomLeft, - SA_BottomCenter, - SA_BottomRight - }; /// /// A sprite is unit of rendering. Animation is based on sprite, but not texture or other graphic stuff. @@ -31,6 +18,24 @@ namespace JinEngine class Sprite { public: + Sprite(); + virtual ~Sprite(); + + enum Origin + { + TopLeft, + TopCenter, + TopRight, + MiddleLeft, + MiddleCenter, + MiddleRight, + BottomLeft, + BottomCenter, + BottomRight + }; + + void setRotation(float r); + void setOrigin(Origin origin); void setOrigin(int x, int y); void setPosition(int x, int y); void setScale(float x, float y); @@ -41,7 +46,7 @@ namespace JinEngine /// /// Render callback. /// - virtual void onRender(); + virtual void render(); private: /// @@ -50,6 +55,7 @@ namespace JinEngine Math::Vector2 mPosition; Math::Vector2 mOrigin; Math::Vector2 mScale; + float mRotation; Color mColor; Shaders::Shader* mShader; Graphic* mGraphic; diff --git a/src/lua/common/je_lua_callback.cpp b/src/lua/common/je_lua_callback.cpp new file mode 100644 index 0000000..392f919 --- /dev/null +++ b/src/lua/common/je_lua_callback.cpp @@ -0,0 +1,43 @@ +#include "je_lua_callback.h" + +namespace JinEngine +{ + namespace Lua + { + + LuaCallback::LuaCallback(lua_State* L) + : mLuaFunc(nullptr) + , mParams(0) + , mL(L) + { + } + + LuaCallback::~LuaCallback() + { + delete mLuaFunc; + for (auto p : mParams) + delete p; + } + + void LuaCallback::setFunc(int i) + { + if (mLuaFunc != nullptr) + delete mLuaFunc; + mLuaFunc = new LuaRef(mL, i); + } + + void LuaCallback::pushParam(int i) + { + mParams.push_back(new LuaRef(mL, i)); + } + + void LuaCallback::call() + { + mLuaFunc->push(); + for (auto p : mParams) + p->push(); + luax_call(mL, mParams.size(), 0); + } + + } +} \ No newline at end of file diff --git a/src/lua/common/je_lua_callback.h b/src/lua/common/je_lua_callback.h new file mode 100644 index 0000000..f3301fc --- /dev/null +++ b/src/lua/common/je_lua_callback.h @@ -0,0 +1,67 @@ +#ifndef __JIN_COMMON_FUNCTION_H +#define __JIN_COMMON_FUNCTION_H + +#include + +#include "libjin/jin.h" +#include "../luax.h" +#include "je_lua_reference.h" + +namespace JinEngine +{ + namespace Lua + { + + /// + /// + /// + class LuaCallback + { + public: + /// + /// + /// + LuaCallback(lua_State* L); + + /// + /// + /// + ~LuaCallback(); + + /// + /// + /// + void setFunc(int i); + + /// + /// + /// + void pushParam(int i); + + /// + /// + /// + void call(); + + private: + /// + /// + /// + LuaRef* mLuaFunc; + + /// + /// + /// + std::vector mParams; + + /// + /// + /// + lua_State* const mL; + + }; + + } // namespace Lua +} // namespace JinEngine + +#endif // __JIN_COMMON_REFERENCE_H \ No newline at end of file diff --git a/src/lua/common/je_lua_function.cpp b/src/lua/common/je_lua_function.cpp deleted file mode 100644 index e202d99..0000000 --- a/src/lua/common/je_lua_function.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#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 deleted file mode 100644 index a2d5ccc..0000000 --- a/src/lua/common/je_lua_function.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef __JIN_COMMON_FUNCTION_H -#define __JIN_COMMON_FUNCTION_H - -#include - -#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 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.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(object); } T* getObject() { - return (T*)object; + return static_cast(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& 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(data); + LuaCallback* func = static_cast(data); func->call(); }; - static Timer::FinishCallback Finish_Callback = [](void* data)->void + static Timer::FinishCallback finishCallback = [](void* data)->void { - LuaFunc* func = static_cast(data); + LuaCallback* func = static_cast(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(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(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(handler, JIN_TIME_HANDLER)); return 1; @@ -140,6 +130,7 @@ namespace JinEngine }; luax_newtype(L, JIN_TIME_TIMER, f); + return 0; } -- cgit v1.1-26-g67d0