diff --git a/engine/core/Engine.h b/engine/core/Engine.h index d3442e04..bf6c2ecd 100644 --- a/engine/core/Engine.h +++ b/engine/core/Engine.h @@ -53,11 +53,6 @@ #define MAX_EXTERNAL_BUFFERS 10 #endif -// to prevent tiled texture getting part of neighborhood tile -#ifndef TEXTURE_CUT_FACTOR -#define TEXTURE_CUT_FACTOR 1.0 -#endif - #include "util/FunctionSubscribe.h" #include "math/Rect.h" #include "util/ThreadUtils.h" diff --git a/engine/core/component/ImageComponent.h b/engine/core/component/ImageComponent.h index 5e254206..e58b46f8 100644 --- a/engine/core/component/ImageComponent.h +++ b/engine/core/component/ImageComponent.h @@ -14,6 +14,8 @@ namespace Supernova{ int patchMarginTop = 0; int patchMarginBottom = 0; + float textureCutFactor = 0.0; + bool needUpdatePatches = true; }; diff --git a/engine/core/component/SpriteComponent.h b/engine/core/component/SpriteComponent.h index 85f671b8..b94ade2b 100644 --- a/engine/core/component/SpriteComponent.h +++ b/engine/core/component/SpriteComponent.h @@ -25,6 +25,8 @@ namespace Supernova{ bool automaticFlipY = true; bool flipY = false; + float textureCutFactor = 0.0; + FrameData framesRect[MAX_SPRITE_FRAMES]; PivotPreset pivotPreset = PivotPreset::BOTTOM_LEFT; diff --git a/engine/core/component/TilemapComponent.h b/engine/core/component/TilemapComponent.h index 477b7519..787501eb 100644 --- a/engine/core/component/TilemapComponent.h +++ b/engine/core/component/TilemapComponent.h @@ -30,6 +30,8 @@ namespace Supernova{ bool automaticFlipY = true; bool flipY = false; + float textureCutFactor = 0.0; + unsigned int reserveTiles = 10; unsigned int numTiles = 0; diff --git a/engine/core/object/Sprite.cpp b/engine/core/object/Sprite.cpp index dde382cf..ac92690d 100644 --- a/engine/core/object/Sprite.cpp +++ b/engine/core/object/Sprite.cpp @@ -98,6 +98,21 @@ bool Sprite::isFlipY() const{ return spritecomp.flipY; } +void Sprite::setTextureCutFactor(float textureCutFactor){ + SpriteComponent& spritecomp = getComponent(); + + if (spritecomp.textureCutFactor != textureCutFactor){ + spritecomp.textureCutFactor = textureCutFactor; + spritecomp.needUpdateSprite = true; + } +} + +float Sprite::getTextureCutFactor() const{ + SpriteComponent& spritecomp = getComponent(); + + return spritecomp.textureCutFactor; +} + void Sprite::setTextureRect(float x, float y, float width, float height){ setTextureRect(Rect(x, y, width, height)); } diff --git a/engine/core/object/Sprite.h b/engine/core/object/Sprite.h index bdb5988c..1e1656ea 100644 --- a/engine/core/object/Sprite.h +++ b/engine/core/object/Sprite.h @@ -36,6 +36,9 @@ namespace Supernova{ void setFlipY(bool flipY); bool isFlipY() const; + void setTextureCutFactor(float textureCutFactor); + float getTextureCutFactor() const; + void setTextureRect(float x, float y, float width, float height); void setTextureRect(Rect textureRect); Rect getTextureRect() const; diff --git a/engine/core/object/Tilemap.cpp b/engine/core/object/Tilemap.cpp index 61deb3c3..dc03c67d 100644 --- a/engine/core/object/Tilemap.cpp +++ b/engine/core/object/Tilemap.cpp @@ -46,6 +46,21 @@ int Tilemap::findTileByString(std::string name){ return -1; } +void Tilemap::setTextureCutFactor(float textureCutFactor){ + TilemapComponent& tilemap = getComponent(); + + if (tilemap.textureCutFactor != textureCutFactor){ + tilemap.textureCutFactor = textureCutFactor; + tilemap.needUpdateTilemap = true; + } +} + +float Tilemap::getTextureCutFactor() const{ + TilemapComponent& tilemap = getComponent(); + + return tilemap.textureCutFactor; +} + void Tilemap::addRect(int id, std::string name, std::string texture, TextureFilter texFilter, Rect rect){ MeshComponent& mesh = getComponent(); TilemapComponent& tilemap = getComponent(); diff --git a/engine/core/object/Tilemap.h b/engine/core/object/Tilemap.h index 62418447..9b75db9c 100644 --- a/engine/core/object/Tilemap.h +++ b/engine/core/object/Tilemap.h @@ -20,6 +20,9 @@ namespace Supernova{ int findRectByString(std::string name); int findTileByString(std::string name); + void setTextureCutFactor(float textureCutFactor); + float getTextureCutFactor() const; + void addRect(int id, std::string name, std::string texture, TextureFilter texFilter, Rect rect); void addRect(int id, std::string name, std::string texture, Rect rect); void addRect(int id, std::string name, Rect rect); diff --git a/engine/core/object/ui/Image.cpp b/engine/core/object/ui/Image.cpp index f69f1ef7..ea647c02 100644 --- a/engine/core/object/ui/Image.cpp +++ b/engine/core/object/ui/Image.cpp @@ -123,6 +123,21 @@ void Image::setTexture(Framebuffer* framebuffer){ ui.needUpdateTexture = true; } +void Image::setTextureCutFactor(float textureCutFactor){ + ImageComponent& img = getComponent(); + + if (img.textureCutFactor != textureCutFactor){ + img.textureCutFactor = textureCutFactor; + img.needUpdatePatches = true; + } +} + +float Image::getTextureCutFactor() const{ + ImageComponent& img = getComponent(); + + return img.textureCutFactor; +} + void Image::setColor(Vector4 color){ UIComponent& ui = getComponent(); diff --git a/engine/core/object/ui/Image.h b/engine/core/object/ui/Image.h index 3113374b..ab1f07d3 100644 --- a/engine/core/object/ui/Image.h +++ b/engine/core/object/ui/Image.h @@ -31,6 +31,9 @@ namespace Supernova{ void setTexture(std::string path); void setTexture(Framebuffer* framebuffer); + void setTextureCutFactor(float textureCutFactor); + float getTextureCutFactor() const; + void setColor(Vector4 color); void setColor(const float red, const float green, const float blue, const float alpha); void setColor(const float red, const float green, const float blue); diff --git a/engine/core/script/binding/ECSClassesLua.cpp b/engine/core/script/binding/ECSClassesLua.cpp index 0e6ed8fc..1218bdaa 100644 --- a/engine/core/script/binding/ECSClassesLua.cpp +++ b/engine/core/script/binding/ECSClassesLua.cpp @@ -490,6 +490,7 @@ void LuaBinding::registerECSClasses(lua_State *L){ .addProperty("height", &TilemapComponent::height) .addProperty("automaticFlipY", &TilemapComponent::automaticFlipY) .addProperty("flipY", &TilemapComponent::flipY) + .addProperty("textureCutFactor", &TilemapComponent::textureCutFactor) .addProperty("reserveTiles", &TilemapComponent::reserveTiles) .addProperty("numTiles", &TilemapComponent::numTiles) //.addProperty("tilesRect", &TilemapComponent::tilesRect) diff --git a/engine/core/script/binding/ObjectClassesLua.cpp b/engine/core/script/binding/ObjectClassesLua.cpp index d5554e5c..1aa0faf6 100644 --- a/engine/core/script/binding/ObjectClassesLua.cpp +++ b/engine/core/script/binding/ObjectClassesLua.cpp @@ -324,6 +324,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){ .addConstructor () .addFunction("findRectByString", &Tilemap::findRectByString) .addFunction("findTileByString", &Tilemap::findTileByString) + .addProperty("textureCutFactor", &Tilemap::getTextureCutFactor, &Tilemap::setTextureCutFactor) .addFunction("addRect", luabridge::overload(&Tilemap::addRect), luabridge::overload(&Tilemap::addRect), @@ -415,6 +416,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){ .addProperty("width", &Sprite::getWidth, &Sprite::setWidth) .addProperty("height", &Sprite::getHeight, &Sprite::setHeight) .addProperty("flipY", &Sprite::isFlipY, &Sprite::setFlipY) + .addProperty("textureCutFactor", &Sprite::getTextureCutFactor, &Sprite::setTextureCutFactor) .addProperty("textureRect", &Sprite::getTextureRect, (void(Sprite::*)(Rect))&Sprite::setTextureRect) .addFunction("setTextureRect",(void(Sprite::*)(float, float, float, float)) &Sprite::setTextureRect) .addProperty("pivotPreset", &Sprite::getPivotPreset, &Sprite::setPivotPreset) @@ -441,7 +443,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){ .deriveClass("UILayout") .addConstructor () .addFunction("setSize", &UILayout::setSize) - .addProperty("width", &UILayout::getWidth, &Image::setWidth) + .addProperty("width", &UILayout::getWidth, &UILayout::setWidth) .addProperty("height", &UILayout::getHeight, &UILayout::setHeight) .addFunction("setAnchorPoints", &UILayout::setAnchorPoints) .addProperty("anchorPointLeft", &UILayout::getAnchorPointLeft, &UILayout::setAnchorPointLeft) @@ -456,7 +458,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){ .addProperty("anchorPreset", &UILayout::getAnchorPreset, &UILayout::setAnchorPreset) .addProperty("usingAnchors", &UILayout::isUsingAnchors, &UILayout::setUsingAnchors) .addProperty("ignoreScissor", &UILayout::isIgnoreScissor, &UILayout::setIgnoreScissor) - .addFunction("getUILayoutComponent", &Image::getComponent) + .addFunction("getUILayoutComponent", &UILayout::getComponent) .endClass(); luabridge::getGlobalNamespace(L) @@ -527,6 +529,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){ .addFunction("setTexture", luabridge::overload(&Image::setTexture), luabridge::overload(&Image::setTexture)) + .addProperty("textureCutFactor", &Image::getTextureCutFactor, &Image::setTextureCutFactor) .addProperty("color", &Image::getColor, (void(Image::*)(Vector4))&Image::setColor) .addFunction("setColor", luabridge::overload(&Image::setColor), diff --git a/engine/core/subsystem/MeshSystem.cpp b/engine/core/subsystem/MeshSystem.cpp index 23a6bccd..81e2b5e4 100644 --- a/engine/core/subsystem/MeshSystem.cpp +++ b/engine/core/subsystem/MeshSystem.cpp @@ -91,8 +91,8 @@ void MeshSystem::createSprite(SpriteComponent& sprite, MeshComponent& mesh, Came float texCutRatioW = 0; float texCutRatioH = 0; if (texWidth != 0 && texHeight != 0){ - texCutRatioW = 1.0 / texWidth * TEXTURE_CUT_FACTOR; - texCutRatioH = 1.0 / texHeight * TEXTURE_CUT_FACTOR; + texCutRatioW = 1.0 / texWidth * sprite.textureCutFactor; + texCutRatioH = 1.0 / texHeight * sprite.textureCutFactor; } if (!sprite.flipY){ @@ -260,8 +260,8 @@ void MeshSystem::createTilemap(TilemapComponent& tilemap, MeshComponent& mesh){ float texCutRatioW = 0; float texCutRatioH = 0; if (texWidth != 0 && texHeight != 0){ - texCutRatioW = 1.0 / texWidth * TEXTURE_CUT_FACTOR; - texCutRatioH = 1.0 / texHeight * TEXTURE_CUT_FACTOR; + texCutRatioW = 1.0 / texWidth * tilemap.textureCutFactor; + texCutRatioH = 1.0 / texHeight * tilemap.textureCutFactor; } Attribute* attTexcoord = mesh.buffer.getAttribute(AttributeType::TEXCOORD1); diff --git a/engine/core/subsystem/UISystem.cpp b/engine/core/subsystem/UISystem.cpp index 7d79d9fe..f2b29f58 100644 --- a/engine/core/subsystem/UISystem.cpp +++ b/engine/core/subsystem/UISystem.cpp @@ -29,7 +29,9 @@ UISystem::~UISystem(){ bool UISystem::createImagePatches(ImageComponent& img, UIComponent& ui, UILayoutComponent& layout){ - ui.texture.load(); + if (!ui.texture.load()) + return false; + unsigned int texWidth = ui.texture.getWidth(); unsigned int texHeight = ui.texture.getHeight(); @@ -83,8 +85,8 @@ bool UISystem::createImagePatches(ImageComponent& img, UIComponent& ui, UILayout float texCutRatioW = 0; float texCutRatioH = 0; if (texWidth != 0 && texHeight != 0){ - texCutRatioW = 1.0 / texWidth * TEXTURE_CUT_FACTOR; - texCutRatioH = 1.0 / texHeight * TEXTURE_CUT_FACTOR; + texCutRatioW = 1.0 / texWidth * img.textureCutFactor; + texCutRatioH = 1.0 / texHeight * img.textureCutFactor; } float x0 = texCutRatioW;