diff options
author | chai <chaifix@163.com> | 2018-11-14 00:51:15 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-11-14 00:51:15 +0800 |
commit | 0bfff69053e27fbb6e541a6b8afa6c8e61a62403 (patch) | |
tree | d3bd41f8defef8e08f80850d56eaf7a50de3c05a | |
parent | d2058dce75ceb67f27c50e9f27d755a4d30c9003 (diff) |
*shared 增加依赖
30 files changed, 229 insertions, 229 deletions
diff --git a/bin/Jin.exe b/bin/Jin.exe Binary files differindex e648fd4..6f56539 100644 --- a/bin/Jin.exe +++ b/bin/Jin.exe diff --git a/bin/SDL2.dll b/bin/SDL2.dll Binary files differindex 81f8ae4..97527fa 100644 --- a/bin/SDL2.dll +++ b/bin/SDL2.dll diff --git a/bin/game/0.png b/bin/game/0.png Binary files differnew file mode 100644 index 0000000..fa62576 --- /dev/null +++ b/bin/game/0.png diff --git a/bin/game/1.png b/bin/game/1.png Binary files differnew file mode 100644 index 0000000..7e3fe49 --- /dev/null +++ b/bin/game/1.png diff --git a/bin/game/SDL.jpg b/bin/game/SDL.jpg Binary files differnew file mode 100644 index 0000000..dcd530a --- /dev/null +++ b/bin/game/SDL.jpg diff --git a/bin/game/config.lua b/bin/game/config.lua new file mode 100644 index 0000000..380dd1b --- /dev/null +++ b/bin/game/config.lua @@ -0,0 +1,5 @@ +return +{ + title = "window form", + icon = "splash.png", +}
\ No newline at end of file diff --git a/bin/game/forest.ogg b/bin/game/forest.ogg Binary files differnew file mode 100644 index 0000000..d3059e1 --- /dev/null +++ b/bin/game/forest.ogg diff --git a/bin/game/icon.ico b/bin/game/icon.ico Binary files differnew file mode 100644 index 0000000..3043332 --- /dev/null +++ b/bin/game/icon.ico diff --git a/bin/game/main.lua b/bin/game/main.lua new file mode 100644 index 0000000..2c9036f --- /dev/null +++ b/bin/game/main.lua @@ -0,0 +1,93 @@ +io.stdout:setvbuf("no") +local shader = [[ +#VERTEX_SHADER +Vertex vert(Vertex v) +{ + return v; +} +#END_VERTEX_SHADER +#FRAGMENT_SHADER +Color frag(Color col, Texture tex, Vertex v) +{ + return col; +} +#END_FRAGMENT_SHADER +]] + +local shader2 = [[ +#VERTEX_SHADER +Vertex vert(Vertex v) +{ + return v; +} +#END_VERTEX_SHADER +#FRAGMENT_SHADER +Color frag(Color col, Texture tex, Vertex v) +{ + Color c = texel(tex, v.uv); + return c; +} +#END_FRAGMENT_SHADER +]] +music = nil +local tex = nil +local shader_program = nil +local shader_program2 = nil +local timer = nil +local tb = {x = 1, y = 2} +local t = 0 +local spr = nil +function jin.core.onLoad() + jin.graphics.setClearColor(100, 100, 100, 255) + shader_program = jin.graphics.newShader(shader) + shader_program2 = jin.graphics.newShader(shader2) + tex = jin.graphics.newTexture("1.png") + spr = jin.graphics.newSprite() + spr:setShader(shader_program2) + spr:setGraphic(tex) + tex = nil + spr:setScale(1, 1) + spr:setOrigin(jin.graphics.SpriteOrigin.MIDDLERIGHT) + music = jin.audio.newSource("forest.ogg") + music:setVolume(0.5) + music:setLoop(true) + music:play() + jin.graphics.clear() + jin.graphics.showWindow() + timer = jin.time.newTimer() + local h = timer:every(0.5, function(sp) + local x, y = spr:getPosition() + spr:setPosition(x + 1, y) + end, spr) + timer:after(3, function(p) + --timer:cancel(h) + end, h) +end +local stop = false + +function jin.core.onEvent(e) + if e.type == "Quit" then + jin.core.stop() + end +end + +function jin.core.onUpdate() + tb.x = t + t = t + jin.time.getDelta() +end + +function jin.core.onDraw() + timer:update(jin.time.getDelta()) + jin.graphics.useShader(shader_program) + jin.graphics.setColor(255, 0, 255, 255) + jin.graphics.rect(jin.graphics.RenderMode.FILL, 30, 50, 100, 200) + jin.graphics.setColor(255, 255, 255, 255) + jin.graphics.unuseShader() + spr:render() + --jin.graphics.useShader(shader_program2) + --jin.graphics.draw(tex, 0, 0,0.2, 0.2) + --jin.graphics.unuseShader() + if stop then + jin.graphics.print("Quit", 100, 300) + end +end
\ No newline at end of file diff --git a/bin/game/splash.png b/bin/game/splash.png Binary files differnew file mode 100644 index 0000000..b0c4256 --- /dev/null +++ b/bin/game/splash.png diff --git a/build/vc++/jin.vcxproj b/build/vc++/jin.vcxproj index b28f79b..6b9449f 100644 --- a/build/vc++/jin.vcxproj +++ b/build/vc++/jin.vcxproj @@ -198,7 +198,7 @@ <ClInclude Include="..\..\src\lua\jin.h" /> <ClInclude Include="..\..\src\lua\libraries\luax\luax.h" /> <ClInclude Include="..\..\src\lua\luax.h" /> - <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_sprite.h" /> + <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_page.h" /> <ClInclude Include="..\..\src\lua\modules\luax.h" /> <ClInclude Include="..\..\src\lua\modules\net\je_lua_buffer.h" /> <ClInclude Include="..\..\src\lua\modules\thread\je_lua_thread.h" /> diff --git a/build/vc++/jin.vcxproj.filters b/build/vc++/jin.vcxproj.filters index 960d7a3..7b8a9a9 100644 --- a/build/vc++/jin.vcxproj.filters +++ b/build/vc++/jin.vcxproj.filters @@ -254,7 +254,7 @@ <ClInclude Include="..\..\src\lua\common\je_lua_callback.h"> <Filter>source\common</Filter> </ClInclude> - <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_sprite.h"> + <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_page.h"> <Filter>source\modules\graphics</Filter> </ClInclude> </ItemGroup> diff --git a/build/vc++/libjin/libjin.vcxproj b/build/vc++/libjin/libjin.vcxproj index 552272d..a1bfa66 100644 --- a/build/vc++/libjin/libjin.vcxproj +++ b/build/vc++/libjin/libjin.vcxproj @@ -171,7 +171,6 @@ <ClInclude Include="..\..\..\src\libjin\Graphics\je_color.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_graphic.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_graphics.h" /> - <ClInclude Include="..\..\..\src\libjin\graphics\je_graphic_manager.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_image.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_mesh.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_gl.h" /> @@ -245,7 +244,6 @@ <ClCompile Include="..\..\..\src\libjin\Graphics\je_canvas.cpp" /> <ClCompile Include="..\..\..\src\libjin\Graphics\je_color.cpp" /> <ClCompile Include="..\..\..\src\libjin\Graphics\je_graphic.cpp" /> - <ClCompile Include="..\..\..\src\libjin\graphics\je_graphic_manager.cpp" /> <ClCompile Include="..\..\..\src\libjin\Graphics\je_image.cpp" /> <ClCompile Include="..\..\..\src\libjin\Graphics\je_mesh.cpp" /> <ClCompile Include="..\..\..\src\libjin\Graphics\je_gl.cpp" /> diff --git a/build/vc++/libjin/libjin.vcxproj.filters b/build/vc++/libjin/libjin.vcxproj.filters index c70aa25..f68943f 100644 --- a/build/vc++/libjin/libjin.vcxproj.filters +++ b/build/vc++/libjin/libjin.vcxproj.filters @@ -246,9 +246,6 @@ <ClInclude Include="..\..\..\src\libjin\graphics\je_sprite_sheet.h"> <Filter>source\graphics</Filter> </ClInclude> - <ClInclude Include="..\..\..\src\libjin\graphics\je_graphic_manager.h"> - <Filter>source\graphics</Filter> - </ClInclude> <ClInclude Include="..\..\..\src\libjin\graphics\shaders\built-in\je_default.shader.h"> <Filter>source\graphics\shaders\built-in</Filter> </ClInclude> @@ -436,9 +433,6 @@ <ClCompile Include="..\..\..\src\libjin\graphics\je_sprite_sheet.cpp"> <Filter>source\graphics</Filter> </ClCompile> - <ClCompile Include="..\..\..\src\libjin\graphics\je_graphic_manager.cpp"> - <Filter>source\graphics</Filter> - </ClCompile> <ClCompile Include="..\..\..\src\libjin\graphics\shaders\je_jsl_compiler.cpp"> <Filter>source\graphics\shaders</Filter> </ClCompile> diff --git a/src/3rdparty/buildvm/buildvm.exe b/src/3rdparty/buildvm/buildvm.exe Binary files differindex 00cd94d..f60b37b 100644 --- a/src/3rdparty/buildvm/buildvm.exe +++ b/src/3rdparty/buildvm/buildvm.exe diff --git a/src/3rdparty/minilua/minilua.exe b/src/3rdparty/minilua/minilua.exe Binary files differindex 003f2a1..1070809 100644 --- a/src/3rdparty/minilua/minilua.exe +++ b/src/3rdparty/minilua/minilua.exe diff --git a/src/libjin/Graphics/je_graphic_manager.cpp b/src/libjin/Graphics/je_graphic_manager.cpp deleted file mode 100644 index e69de29..0000000 --- a/src/libjin/Graphics/je_graphic_manager.cpp +++ /dev/null diff --git a/src/libjin/Graphics/je_graphic_manager.h b/src/libjin/Graphics/je_graphic_manager.h deleted file mode 100644 index e69de29..0000000 --- a/src/libjin/Graphics/je_graphic_manager.h +++ /dev/null diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h index 96b2093..9f55490 100644 --- a/src/lua/common/je_lua_proxy.h +++ b/src/lua/common/je_lua_proxy.h @@ -35,12 +35,6 @@ namespace JinEngine shared->retain(); } - void setUserdata(void* data) - { - if (shared != nullptr) - shared->setUserdata(data); - } - template<class T> Shared<T>& getShared() { diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp index 876fe28..d6ea357 100644 --- a/src/lua/common/je_lua_shared.hpp +++ b/src/lua/common/je_lua_shared.hpp @@ -1,6 +1,9 @@ #ifndef __JIN_COMMON_SHARED_H #define __JIN_COMMON_SHARED_H +#include <map> +#include <vector> + namespace JinEngine { namespace Lua @@ -11,32 +14,77 @@ namespace JinEngine public: void retain() { - ++count; + ++mCount; } void release() { - if (--count <= 0) + if (--mCount <= 0) delete this; } - // Object type string. + // Object type. const char* const type; - void setUserdata(void* data) + void setDependency(int key, SharedBase* shared) + { + removeDependency(key); + shared->retain(); + mDependencies.insert(std::pair<int, SharedBase*>(key, shared)); + } + + void removeDependency(int key) { - userdata = data; + if (!isDependOn(key)) + return; + DepMap::iterator it = mDependencies.find(key); + it->second->release(); + mDependencies.erase(it); } - void* getUserdata() + void removeDependency(SharedBase* dep) { - return userdata; + for (DepMap::iterator it = mDependencies.begin(); it != mDependencies.end();) + { + if (it->second == dep) + { + it->second->release(); + mDependencies.erase(it); + } + else + ++it; + } + } + + bool isDependOn(int key) + { + return mDependencies.find(key) != mDependencies.end(); + } + + bool isDependOn(SharedBase* shared) + { + for (std::pair<int, SharedBase*> dep : mDependencies) + { + if (dep.second == shared) + return true; + } + return false; + } + + void clearDependencies() + { + for (std::pair<int, SharedBase*> dep : mDependencies) + dep.second->release(); + mDependencies.clear(); } protected: + + using DepMap = std::map<int, SharedBase*>; + SharedBase(void* obj, const char* t) - : count(1) - , object(obj) + : mCount(1) + , mObject(obj) , type(t) { } @@ -45,11 +93,12 @@ namespace JinEngine virtual ~SharedBase() { + clearDependencies(); } - void* object; - int count; - void* userdata; + void* mObject; + int mCount; + DepMap mDependencies; }; template<class T> @@ -63,12 +112,12 @@ namespace JinEngine T* operator->() { - return static_cast<T*>(object); + return static_cast<T*>(mObject); } T* getObject() { - return static_cast<T*>(object); + return static_cast<T*>(mObject); } private: @@ -78,7 +127,7 @@ namespace JinEngine // Make shared only be able created with new. ~Shared() { - T* obj = static_cast<T*>(object); + T* obj = static_cast<T*>(mObject); delete obj; } 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<Lua::Sprite>(new Lua::Sprite(), JIN_GRAPHICS_SPRITE)); + p->bind(new Shared<Sprite>(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>* page = &proxy->getShared<Page>(); - 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>* shader) - { - if (mShaderShared != nullptr) - mShaderShared->release(); - mShaderShared = shader; - mShaderShared->retain(); - Parent::setShader(mShaderShared->getObject()); - } - - void Lua::Sprite::setGraphic(Shared<Graphic>* 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<Lua::Sprite>& SharedSprite; + typedef Shared<Sprite>& SharedSprite; LUA_IMPLEMENT inline SharedSprite checkSprite(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SPRITE); - return proxy->getShared<Lua::Sprite>(); + return proxy->getShared<Sprite>(); } 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<Lua::Sprite::Origin>(origin)); + sprite->setOrigin(static_cast<Sprite::Origin>(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* shader = proxy->getObject<Shader>(); + sprite->setShader(shader); + sprite.setDependency(DEP_SHADER, &proxy->getShared<Shader>()); 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<Graphic>()); + if (p != nullptr) + { + sprite->setGraphic(p->getObject<Graphic>()); + sprite.setDependency(DEP_GRAPHIC, &p->getShared<Graphic>()); + } 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<Graphics::Shaders::Shader>* shader); - - void setGraphic(Shared<Graphics::Graphic>* graphic); - - private: - - using Parent = Graphics::Sprite; - - /// - /// - /// - Shared<Graphics::Graphic>* mGraphicShared; - - /// - /// - /// - Shared<Graphics::Shaders::Shader>* 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<TextureFont>(); + Proxy* pxyTexFont = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + Shared<TextureFont>& shrTexFont = pxyTexFont->getShared<TextureFont>(); + TextureFont* tf = pxyTexFont->getObject<TextureFont>(); 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<Text>(); page = tf->typeset(*text, lineheight, spacing); } - Proxy* proxy = luax_newinstance(L, JIN_GRAPHICS_PAGE); - Shared<Page>* refPage = new Shared<Page>(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Shared<TextureFont>& refTF = p->getShared<TextureFont>(); - refTF.retain(); - refPage->setUserdata(&refTF); - } - proxy->bind(refPage); + Proxy* pxyPage = luax_newinstance(L, JIN_GRAPHICS_PAGE); + Shared<Page>* shrPage = new Shared<Page>(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>* ttf = &proxy->getShared<TTF>(); - 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<TTF>(); + Proxy* pxyTTF = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); + Shared<TTF>& shrTTF = pxyTTF->getShared<TTF>(); + TTF* ttf = pxyTTF->getObject<TTF>(); 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<Text>(); + Proxy* pxyText = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + Text* text = pxyText->getObject<Text>(); page = ttf->typeset(*text, lineheight, spacing); } - Proxy* proxy = luax_newinstance(L, JIN_GRAPHICS_PAGE); + Proxy* pxyPage = luax_newinstance(L, JIN_GRAPHICS_PAGE); Shared<Page>* refPage = new Shared<Page>(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Shared<TTF>& refTTF = p->getShared<TTF>(); - 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<Graphics::Fonts::TTFData>* 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<TTFData>& refFontData = p->getShared<TTFData>(); - TTFData* fontData = refFontData.getObject(); - Proxy* proxy = luax_newinstance(L, JIN_GRAPHICS_TTF); + Shared<TTFData>& shrFontData = pxyTTFData->getShared<TTFData>(); + TTFData* fontData = shrFontData.getObject(); + Proxy* pxyTTF = luax_newinstance(L, JIN_GRAPHICS_TTF); TTF* font = fontData->createTTF(fontsize); - Shared<TTF>* refTTF = new Shared<TTF>(font, JIN_GRAPHICS_TTF); - { - Shared<TTFData>& refTTFData = p->getShared<TTFData>(); - refTTFData.retain(); - refTTF->setUserdata(&refTTFData); - } - proxy->bind(refTTF); + Shared<TTF>* shrTTF = new Shared<TTF>(font, JIN_GRAPHICS_TTF); + shrTTF->setDependency(DEP_TTFDATA, &shrFontData); + pxyTTF->bind(shrTTF); return 1; } |