From 0bfff69053e27fbb6e541a6b8afa6c8e61a62403 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Nov 2018 00:51:15 +0800 Subject: =?UTF-8?q?*shared=20=E5=A2=9E=E5=8A=A0=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/graphics/je_lua_canvas.cpp | 2 +- src/lua/modules/graphics/je_lua_graphics.cpp | 4 +- src/lua/modules/graphics/je_lua_page.cpp | 6 --- src/lua/modules/graphics/je_lua_page.h | 18 +++++++ src/lua/modules/graphics/je_lua_sprite.cpp | 60 +++++++----------------- src/lua/modules/graphics/je_lua_sprite.h | 42 ----------------- src/lua/modules/graphics/je_lua_texture_font.cpp | 19 ++++---- src/lua/modules/graphics/je_lua_ttf.cpp | 50 ++++---------------- src/lua/modules/graphics/je_lua_ttf.h | 39 --------------- src/lua/modules/graphics/je_lua_ttf_data.cpp | 23 ++++----- 10 files changed, 65 insertions(+), 198 deletions(-) create mode 100644 src/lua/modules/graphics/je_lua_page.h delete mode 100644 src/lua/modules/graphics/je_lua_sprite.h delete mode 100644 src/lua/modules/graphics/je_lua_ttf.h (limited to 'src/lua/modules') diff --git a/src/lua/modules/graphics/je_lua_canvas.cpp b/src/lua/modules/graphics/je_lua_canvas.cpp index 8d8630f..70edfd1 100644 --- a/src/lua/modules/graphics/je_lua_canvas.cpp +++ b/src/lua/modules/graphics/je_lua_canvas.cpp @@ -62,4 +62,4 @@ namespace JinEngine } } // namespace Lua -} // namespace JinEngine \ No newline at end of file +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp index 78674dd..5255de7 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -6,8 +6,6 @@ #include "lua/modules/types.h" #include "lua/common/je_lua_common.h" -#include "je_lua_sprite.h" - using namespace std; using namespace JinEngine; using namespace JinEngine::Graphics; @@ -664,7 +662,7 @@ namespace JinEngine LUA_IMPLEMENT int l_newSprite(lua_State* L) { Proxy* p = luax_newinstance(L, JIN_GRAPHICS_SPRITE); - p->bind(new Shared(new Lua::Sprite(), JIN_GRAPHICS_SPRITE)); + p->bind(new Shared(new Sprite(), JIN_GRAPHICS_SPRITE)); return 1; } diff --git a/src/lua/modules/graphics/je_lua_page.cpp b/src/lua/modules/graphics/je_lua_page.cpp index 7e9b6d1..3ebd557 100644 --- a/src/lua/modules/graphics/je_lua_page.cpp +++ b/src/lua/modules/graphics/je_lua_page.cpp @@ -25,12 +25,6 @@ namespace JinEngine LUA_IMPLEMENT int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); - { - /* release font */ - Shared* page = &proxy->getShared(); - SharedBase* font = (SharedBase*)page->getUserdata(); - font->release(); - } proxy->release(); return 0; } diff --git a/src/lua/modules/graphics/je_lua_page.h b/src/lua/modules/graphics/je_lua_page.h new file mode 100644 index 0000000..d2d9f22 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_page.h @@ -0,0 +1,18 @@ +#ifndef __JE_LUA_PAGE_H__ +#define __JE_LUA_PAGE_H__ + +namespace JinEngine +{ + namespace Lua + { + + enum PageDependency + { + DEP_TTF = 1, + DEP_TEXTURE_FONT = 2, + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_sprite.cpp b/src/lua/modules/graphics/je_lua_sprite.cpp index f02d1cc..b81b66b 100644 --- a/src/lua/modules/graphics/je_lua_sprite.cpp +++ b/src/lua/modules/graphics/je_lua_sprite.cpp @@ -2,7 +2,6 @@ #include "lua/modules/types.h" #include "lua/common/je_lua_common.h" #include "libjin/jin.h" -#include "je_lua_sprite.h" using namespace JinEngine::Graphics; using namespace JinEngine::Graphics::Shaders; @@ -11,52 +10,20 @@ namespace JinEngine { namespace Lua { - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - // Sprite class. - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - Lua::Sprite::~Sprite() + // Sprite dependency slots. + enum SpriteDependency { - if (mGraphicShared != nullptr) - { - mGraphicShared->release(); - mGraphicShared = nullptr; - } - if (mShaderShared != nullptr) - { - mShaderShared->release(); - mShaderShared = nullptr; - } - } - - void Lua::Sprite::setShader(Shared* shader) - { - if (mShaderShared != nullptr) - mShaderShared->release(); - mShaderShared = shader; - mShaderShared->retain(); - Parent::setShader(mShaderShared->getObject()); - } - - void Lua::Sprite::setGraphic(Shared* graphic) - { - if (mGraphicShared != nullptr) - mGraphicShared = graphic; - mGraphicShared = graphic; - mGraphicShared->retain(); - Parent::setGraphic(mGraphicShared->getObject()); - } + DEP_GRAPHIC = 1, + DEP_SHADER = 2, + }; - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - // Register Sprite class. - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - - typedef Shared& SharedSprite; + typedef Shared& SharedSprite; LUA_IMPLEMENT inline SharedSprite checkSprite(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SPRITE); - return proxy->getShared(); + return proxy->getShared(); } LUA_IMPLEMENT int l_gc(lua_State* L) @@ -82,7 +49,7 @@ namespace JinEngine case 2: { int origin = luax_checkinteger(L, 2); - sprite->setOrigin(static_cast(origin)); + sprite->setOrigin(static_cast(origin)); } break; case 3: @@ -129,7 +96,9 @@ namespace JinEngine { SharedSprite sprite = checkSprite(L); Proxy* proxy = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_SHADER); - sprite->setShader(&proxy->getShared()); + Shader* shader = proxy->getObject(); + sprite->setShader(shader); + sprite.setDependency(DEP_SHADER, &proxy->getShared()); return 0; } @@ -142,8 +111,11 @@ namespace JinEngine p = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXTURE); else if (luax_istype(L, 2, JIN_GRAPHICS_CANVAS)) p = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_CANVAS); - if(p != nullptr) - sprite->setGraphic(&p->getShared()); + if (p != nullptr) + { + sprite->setGraphic(p->getObject()); + sprite.setDependency(DEP_GRAPHIC, &p->getShared()); + } return 0; } diff --git a/src/lua/modules/graphics/je_lua_sprite.h b/src/lua/modules/graphics/je_lua_sprite.h deleted file mode 100644 index ded237e..0000000 --- a/src/lua/modules/graphics/je_lua_sprite.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef __JE_LUA_SPRITE_H__ -#define __JE_LUA_SPRITE_H__ - -#include "../../common/je_lua_shared.hpp" - -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - class Sprite - : public Graphics::Sprite - { - public: - ~Sprite(); - - void setShader(Shared* shader); - - void setGraphic(Shared* graphic); - - private: - - using Parent = Graphics::Sprite; - - /// - /// - /// - Shared* mGraphicShared; - - /// - /// - /// - Shared* mShaderShared; - - }; - - } -} - -#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_texture_font.cpp b/src/lua/modules/graphics/je_lua_texture_font.cpp index 1cdbfff..dc48ce4 100644 --- a/src/lua/modules/graphics/je_lua_texture_font.cpp +++ b/src/lua/modules/graphics/je_lua_texture_font.cpp @@ -2,6 +2,7 @@ #include "lua/modules/types.h" #include "lua/common/je_lua_common.h" #include "libjin/jin.h" +#include "je_lua_page.h" using namespace JinEngine::Graphics; using namespace JinEngine::Graphics::Fonts; @@ -21,8 +22,9 @@ namespace JinEngine /* typeset(Text | string, lineheight, spacing) */ LUA_IMPLEMENT int l_typeset(lua_State* L) { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - TextureFont* tf = p->getObject(); + Proxy* pxyTexFont = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + Shared& shrTexFont = pxyTexFont->getShared(); + TextureFont* tf = pxyTexFont->getObject(); int lineheight = luax_checkinteger(L, 3); int spacing = luax_optnumber(L, 4, 0); Page* page = nullptr; @@ -39,15 +41,10 @@ namespace JinEngine Text* text = p2->getObject(); page = tf->typeset(*text, lineheight, spacing); } - Proxy* proxy = luax_newinstance(L, JIN_GRAPHICS_PAGE); - Shared* refPage = new Shared(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Shared& refTF = p->getShared(); - refTF.retain(); - refPage->setUserdata(&refTF); - } - proxy->bind(refPage); + Proxy* pxyPage = luax_newinstance(L, JIN_GRAPHICS_PAGE); + Shared* shrPage = new Shared(page, JIN_GRAPHICS_PAGE); + shrPage->setDependency(DEP_TEXTURE_FONT, &shrTexFont); + pxyPage->bind(shrPage); return 1; } diff --git a/src/lua/modules/graphics/je_lua_ttf.cpp b/src/lua/modules/graphics/je_lua_ttf.cpp index 31af310..6cade75 100644 --- a/src/lua/modules/graphics/je_lua_ttf.cpp +++ b/src/lua/modules/graphics/je_lua_ttf.cpp @@ -2,7 +2,7 @@ #include "lua/modules/types.h" #include "lua/common/je_lua_common.h" #include "libjin/jin.h" -#include "je_lua_ttf.h" +#include "je_lua_page.h" using namespace JinEngine::Graphics; using namespace JinEngine::Graphics::Fonts; @@ -12,37 +12,9 @@ namespace JinEngine namespace Lua { - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - // TTFData - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - - TTF* TTFData::createTTF(unsigned fontSize) - { - TTF* ttf; - try - { - ttf = new TTF(this, fontSize); - } - catch (...) - { - return nullptr; - } - return ttf; - } - - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - // TTF - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - LUA_IMPLEMENT int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); - { - /* release ttf data */ - Shared* ttf = &proxy->getShared(); - SharedBase* data = (SharedBase*)ttf->getUserdata(); - data->release(); - } proxy->release(); return 0; } @@ -50,8 +22,9 @@ namespace JinEngine /* typeset(Text | string, lineheight, spacing) */ LUA_IMPLEMENT int l_typeset(lua_State* L) { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); - TTF* ttf = p->getObject(); + Proxy* pxyTTF = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); + Shared& shrTTF = pxyTTF->getShared(); + TTF* ttf = pxyTTF->getObject(); int lineheight = luax_optnumber(L, 3, ttf->getFontSize()); int spacing = luax_optnumber(L, 4, 0); Page* page = nullptr; @@ -64,19 +37,14 @@ namespace JinEngine } else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) { - Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = p2->getObject(); + Proxy* pxyText = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + Text* text = pxyText->getObject(); page = ttf->typeset(*text, lineheight, spacing); } - Proxy* proxy = luax_newinstance(L, JIN_GRAPHICS_PAGE); + Proxy* pxyPage = luax_newinstance(L, JIN_GRAPHICS_PAGE); Shared* refPage = new Shared(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Shared& refTTF = p->getShared(); - refTTF.retain(); - refPage->setUserdata(&refTTF); - } - proxy->bind(refPage); + refPage->setDependency(DEP_TTF, &shrTTF); + pxyPage->bind(refPage); return 1; } diff --git a/src/lua/modules/graphics/je_lua_ttf.h b/src/lua/modules/graphics/je_lua_ttf.h deleted file mode 100644 index 7673d67..0000000 --- a/src/lua/modules/graphics/je_lua_ttf.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef __JE_LUA_TTF_H__ -#define __JE_LUA_TTF_H__ - -#include "../../common/je_lua_shared.hpp" - -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - class TTFData - : public Graphics::Fonts::TTFData - { - public: - TTF * createTTF(unsigned fontSize); - - private: - using Parent = Graphics::Fonts::TTFData; - - }; - - class TTF - : public Graphics::Fonts::TTF - { - public: - - private: - using Parent = Graphics::Fonts::TTF; - - Shared* ttfDataShared; - - }; - - } -} - -#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_ttf_data.cpp b/src/lua/modules/graphics/je_lua_ttf_data.cpp index cc91d60..4df2f6e 100644 --- a/src/lua/modules/graphics/je_lua_ttf_data.cpp +++ b/src/lua/modules/graphics/je_lua_ttf_data.cpp @@ -11,21 +11,22 @@ namespace JinEngine namespace Lua { + enum TTFDependency + { + DEP_TTFDATA = 1, + }; + LUA_IMPLEMENT int l_newTTF(lua_State* L) { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); + Proxy* pxyTTFData = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); int fontsize = luax_checkinteger(L, 2); - Shared& refFontData = p->getShared(); - TTFData* fontData = refFontData.getObject(); - Proxy* proxy = luax_newinstance(L, JIN_GRAPHICS_TTF); + Shared& shrFontData = pxyTTFData->getShared(); + TTFData* fontData = shrFontData.getObject(); + Proxy* pxyTTF = luax_newinstance(L, JIN_GRAPHICS_TTF); TTF* font = fontData->createTTF(fontsize); - Shared* refTTF = new Shared(font, JIN_GRAPHICS_TTF); - { - Shared& refTTFData = p->getShared(); - refTTFData.retain(); - refTTF->setUserdata(&refTTFData); - } - proxy->bind(refTTF); + Shared* shrTTF = new Shared(font, JIN_GRAPHICS_TTF); + shrTTF->setDependency(DEP_TTFDATA, &shrFontData); + pxyTTF->bind(shrTTF); return 1; } -- cgit v1.1-26-g67d0