diff --git a/es-core/src/resources/TextureData.cpp b/es-core/src/resources/TextureData.cpp index e9c05e198..28d1dc154 100644 --- a/es-core/src/resources/TextureData.cpp +++ b/es-core/src/resources/TextureData.cpp @@ -15,6 +15,7 @@ #include "Log.h" #include "renderers/Renderer.h" #include "resources/ResourceManager.h" +#include "utils/StringUtil.h" #include "nanosvg.h" #include "nanosvgrast.h" @@ -31,7 +32,6 @@ TextureData::TextureData(bool tile) , mHeight{0} , mSourceWidth{0.0f} , mSourceHeight{0.0f} - , mScaleDuringLoad{1.0f} , mScalable{false} , mLinearMagnify{false} , mForceRasterization{false} @@ -71,7 +71,7 @@ bool TextureData::initSVGFromMemory(const std::string& fileData) bool rasterize{true}; // If there is no image size defined yet, then don't rasterize unless mForceRasterization has - // been set (this is only used by NinePatchComponent to avoid flickering menus). + // been set. if (mSourceWidth == 0.0f && mSourceHeight == 0.0f) { if (!mForceRasterization) rasterize = false; @@ -80,8 +80,8 @@ bool TextureData::initSVGFromMemory(const std::string& fileData) mSourceHeight = 64.0f * (svgImage->height / svgImage->width); } - mWidth = static_cast<int>(std::round(mSourceWidth * mScaleDuringLoad)); - mHeight = static_cast<int>(std::round(mSourceHeight * mScaleDuringLoad)); + mWidth = static_cast<int>(std::round(mSourceWidth)); + mHeight = static_cast<int>(std::round(mSourceHeight)); if (mWidth == 0) { // Auto scale width to keep aspect ratio. @@ -176,7 +176,7 @@ bool TextureData::load() std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance(); const ResourceData& data = rm->getFileData(mPath); // Is it an SVG? - if (mPath.substr(mPath.size() - 4, std::string::npos) == ".svg") { + if (Utils::String::toLower(mPath.substr(mPath.size() - 4, std::string::npos)) == ".svg") { mScalable = true; std::string dataString; dataString.assign(std::string(reinterpret_cast<char*>(data.ptr.get()), data.length)); @@ -243,22 +243,14 @@ size_t TextureData::width() { if (mWidth == 0) load(); - // If it's an SVG image, the size was correctly set to the scaled-up values during the - // rasterization, so only multiply by the scale factor if it's a raster file. - if (!mScalable) - return static_cast<size_t>(mWidth * mScaleDuringLoad); - else - return mWidth; + return static_cast<size_t>(mWidth); } size_t TextureData::height() { if (mHeight == 0) load(); - if (!mScalable) - return static_cast<size_t>(mHeight * mScaleDuringLoad); - else - return mHeight; + return static_cast<size_t>(mHeight); } float TextureData::sourceWidth() diff --git a/es-core/src/resources/TextureData.h b/es-core/src/resources/TextureData.h index 1717bf101..6562643c1 100644 --- a/es-core/src/resources/TextureData.h +++ b/es-core/src/resources/TextureData.h @@ -57,8 +57,6 @@ public: void setSourceSize(float width, float height); glm::vec2 getSize() { return glm::vec2{mWidth, mHeight}; } - // Define a factor for scaling the file when loading it (1.0f = no scaling). - void setScaleDuringLoad(float scale) { mScaleDuringLoad = scale; } // Whether to use linear filtering when magnifying the texture. void setLinearMagnify(bool setting) { mLinearMagnify = setting; } // Whether to rasterize the image even if a size has not been set yet. @@ -81,7 +79,6 @@ private: int mHeight; float mSourceWidth; float mSourceHeight; - float mScaleDuringLoad; bool mScalable; bool mLinearMagnify; bool mReloadable; diff --git a/es-core/src/resources/TextureResource.cpp b/es-core/src/resources/TextureResource.cpp index 8217a9f58..9d35d964b 100644 --- a/es-core/src/resources/TextureResource.cpp +++ b/es-core/src/resources/TextureResource.cpp @@ -16,12 +16,8 @@ std::map<TextureResource::TextureKeyType, std::weak_ptr<TextureResource>> TextureResource::sTextureMap; std::set<TextureResource*> TextureResource::sAllTextures; -TextureResource::TextureResource(const std::string& path, - bool tile, - bool dynamic, - bool linearMagnify, - bool forceRasterization, - float scaleDuringLoad) +TextureResource::TextureResource( + const std::string& path, bool tile, bool dynamic, bool linearMagnify, bool forceRasterization) : mTextureData(nullptr) , mForceLoad(false) { @@ -33,8 +29,6 @@ TextureResource::TextureResource(const std::string& path, if (dynamic) { data = sTextureDataManager.add(this, tile); data->initFromPath(path); - if (scaleDuringLoad != 1.0f) - data->setScaleDuringLoad(scaleDuringLoad); data->setLinearMagnify(linearMagnify); data->setForceRasterization(forceRasterization); // Force the texture manager to load it using a blocking load. @@ -44,8 +38,6 @@ TextureResource::TextureResource(const std::string& path, mTextureData = std::shared_ptr<TextureData>(new TextureData(tile)); data = mTextureData; data->initFromPath(path); - if (scaleDuringLoad != 1.0f) - data->setScaleDuringLoad(scaleDuringLoad); data->setLinearMagnify(linearMagnify); data->setForceRasterization(forceRasterization); // Load it so we can read the width/height. @@ -154,15 +146,14 @@ std::shared_ptr<TextureResource> TextureResource::get(const std::string& path, bool forceLoad, bool dynamic, bool linearMagnify, - bool forceRasterization, - float scaleDuringLoad) + bool forceRasterization) { std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance(); const std::string canonicalPath = Utils::FileSystem::getCanonicalPath(path); if (canonicalPath.empty()) { - std::shared_ptr<TextureResource> tex(new TextureResource( - "", tile, false, linearMagnify, forceRasterization, scaleDuringLoad)); + std::shared_ptr<TextureResource> tex( + new TextureResource("", tile, false, linearMagnify, forceRasterization)); // Make sure we get properly deinitialized even though we do nothing on reinitialization. rm->addReloadable(tex); return tex; @@ -178,8 +169,8 @@ std::shared_ptr<TextureResource> TextureResource::get(const std::string& path, // Need to create it. std::shared_ptr<TextureResource> tex; - tex = std::shared_ptr<TextureResource>(new TextureResource( - key.first, tile, dynamic, linearMagnify, forceRasterization, scaleDuringLoad)); + tex = std::shared_ptr<TextureResource>( + new TextureResource(key.first, tile, dynamic, linearMagnify, forceRasterization)); std::shared_ptr<TextureData> data = sTextureDataManager.get(tex.get()); // Is it an SVG? @@ -219,6 +210,9 @@ void TextureResource::rasterizeAt(float width, float height) data->setSourceSize(static_cast<float>(width), static_cast<float>(height)); if (mForceLoad || mTextureData != nullptr) data->load(); + + mSize.x = width; + mSize.y = height; } size_t TextureResource::getTotalMemUsage() diff --git a/es-core/src/resources/TextureResource.h b/es-core/src/resources/TextureResource.h index 7ae64728b..b36118f2f 100644 --- a/es-core/src/resources/TextureResource.h +++ b/es-core/src/resources/TextureResource.h @@ -31,8 +31,7 @@ public: bool forceLoad = false, bool dynamic = true, bool linearMagnify = false, - bool forceRasterization = false, - float scaleDuringLoad = 1.0f); + bool forceRasterization = false); void initFromPixels(const unsigned char* dataRGBA, size_t width, size_t height); virtual void initFromMemory(const char* data, size_t length); static void manualUnload(std::string path, bool tile); @@ -48,10 +47,6 @@ public: std::string getTextureFilePath(); - // For SVG graphics this function effectively rescales the image to the defined size. - // It does unload and re-rasterize the texture though which may cause flickering in some - // situations. An alternative is to set a scaling factor directly when loading the texture - // using get(), by using the scaleDuringLoad parameter (which also works for raster graphics). void rasterizeAt(float width, float height); glm::vec2 getSourceImageSize() const { return mSourceSize; } @@ -72,8 +67,7 @@ protected: bool tile, bool dynamic, bool linearMagnify, - bool forceRasterization, - float scaleDuringLoad); + bool forceRasterization); virtual void unload(std::shared_ptr<ResourceManager>& rm); virtual void reload(std::shared_ptr<ResourceManager>& rm);