Skip to content

Commit

Permalink
Added textureCutFactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Dec 28, 2023
1 parent 656bfde commit 221be90
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 14 deletions.
5 changes: 0 additions & 5 deletions engine/core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions engine/core/component/ImageComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Supernova{
int patchMarginTop = 0;
int patchMarginBottom = 0;

float textureCutFactor = 0.0;

bool needUpdatePatches = true;
};

Expand Down
2 changes: 2 additions & 0 deletions engine/core/component/SpriteComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions engine/core/component/TilemapComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace Supernova{
bool automaticFlipY = true;
bool flipY = false;

float textureCutFactor = 0.0;

unsigned int reserveTiles = 10;
unsigned int numTiles = 0;

Expand Down
15 changes: 15 additions & 0 deletions engine/core/object/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ bool Sprite::isFlipY() const{
return spritecomp.flipY;
}

void Sprite::setTextureCutFactor(float textureCutFactor){
SpriteComponent& spritecomp = getComponent<SpriteComponent>();

if (spritecomp.textureCutFactor != textureCutFactor){
spritecomp.textureCutFactor = textureCutFactor;
spritecomp.needUpdateSprite = true;
}
}

float Sprite::getTextureCutFactor() const{
SpriteComponent& spritecomp = getComponent<SpriteComponent>();

return spritecomp.textureCutFactor;
}

void Sprite::setTextureRect(float x, float y, float width, float height){
setTextureRect(Rect(x, y, width, height));
}
Expand Down
3 changes: 3 additions & 0 deletions engine/core/object/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions engine/core/object/Tilemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ int Tilemap::findTileByString(std::string name){
return -1;
}

void Tilemap::setTextureCutFactor(float textureCutFactor){
TilemapComponent& tilemap = getComponent<TilemapComponent>();

if (tilemap.textureCutFactor != textureCutFactor){
tilemap.textureCutFactor = textureCutFactor;
tilemap.needUpdateTilemap = true;
}
}

float Tilemap::getTextureCutFactor() const{
TilemapComponent& tilemap = getComponent<TilemapComponent>();

return tilemap.textureCutFactor;
}

void Tilemap::addRect(int id, std::string name, std::string texture, TextureFilter texFilter, Rect rect){
MeshComponent& mesh = getComponent<MeshComponent>();
TilemapComponent& tilemap = getComponent<TilemapComponent>();
Expand Down
3 changes: 3 additions & 0 deletions engine/core/object/Tilemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions engine/core/object/ui/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ void Image::setTexture(Framebuffer* framebuffer){
ui.needUpdateTexture = true;
}

void Image::setTextureCutFactor(float textureCutFactor){
ImageComponent& img = getComponent<ImageComponent>();

if (img.textureCutFactor != textureCutFactor){
img.textureCutFactor = textureCutFactor;
img.needUpdatePatches = true;
}
}

float Image::getTextureCutFactor() const{
ImageComponent& img = getComponent<ImageComponent>();

return img.textureCutFactor;
}

void Image::setColor(Vector4 color){
UIComponent& ui = getComponent<UIComponent>();

Expand Down
3 changes: 3 additions & 0 deletions engine/core/object/ui/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions engine/core/script/binding/ECSClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions engine/core/script/binding/ObjectClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){
.addConstructor <void (*) (Scene*)> ()
.addFunction("findRectByString", &Tilemap::findRectByString)
.addFunction("findTileByString", &Tilemap::findTileByString)
.addProperty("textureCutFactor", &Tilemap::getTextureCutFactor, &Tilemap::setTextureCutFactor)
.addFunction("addRect",
luabridge::overload<int, std::string, std::string, TextureFilter, Rect>(&Tilemap::addRect),
luabridge::overload<int, std::string, std::string, Rect>(&Tilemap::addRect),
Expand Down Expand Up @@ -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)
Expand All @@ -441,7 +443,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){
.deriveClass<UILayout, Object>("UILayout")
.addConstructor <void (*) (Scene*)> ()
.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)
Expand All @@ -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<UILayoutComponent>)
.addFunction("getUILayoutComponent", &UILayout::getComponent<UILayoutComponent>)
.endClass();

luabridge::getGlobalNamespace(L)
Expand Down Expand Up @@ -527,6 +529,7 @@ void LuaBinding::registerObjectClasses(lua_State *L){
.addFunction("setTexture",
luabridge::overload<std::string>(&Image::setTexture),
luabridge::overload<Framebuffer*>(&Image::setTexture))
.addProperty("textureCutFactor", &Image::getTextureCutFactor, &Image::setTextureCutFactor)
.addProperty("color", &Image::getColor, (void(Image::*)(Vector4))&Image::setColor)
.addFunction("setColor",
luabridge::overload<const float, const float, const float>(&Image::setColor),
Expand Down
8 changes: 4 additions & 4 deletions engine/core/subsystem/MeshSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions engine/core/subsystem/UISystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 221be90

Please sign in to comment.