mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-04-10 19:15:13 +00:00
Added support for scaling textures during load for raster files.
This commit is contained in:
parent
574feadc68
commit
0bc707a828
|
|
@ -26,7 +26,7 @@ TextureData::TextureData(
|
||||||
: mTile(tile),
|
: mTile(tile),
|
||||||
mTextureID(0),
|
mTextureID(0),
|
||||||
mDataRGBA({}),
|
mDataRGBA({}),
|
||||||
mScaleSVG(1.0f),
|
mScaleDuringLoad(1.0f),
|
||||||
mScalable(false),
|
mScalable(false),
|
||||||
mWidth(0),
|
mWidth(0),
|
||||||
mHeight(0),
|
mHeight(0),
|
||||||
|
|
@ -71,8 +71,8 @@ bool TextureData::initSVGFromMemory(const std::string& fileData)
|
||||||
mSourceHeight = svgImage->height;
|
mSourceHeight = svgImage->height;
|
||||||
}
|
}
|
||||||
|
|
||||||
mWidth = static_cast<size_t>(std::round(mSourceWidth * mScaleSVG));
|
mWidth = static_cast<size_t>(std::round(mSourceWidth * mScaleDuringLoad));
|
||||||
mHeight = static_cast<size_t>(std::round(mSourceHeight * mScaleSVG));
|
mHeight = static_cast<size_t>(std::round(mSourceHeight * mScaleDuringLoad));
|
||||||
|
|
||||||
if (mWidth == 0) {
|
if (mWidth == 0) {
|
||||||
// Auto scale width to keep aspect ratio.
|
// Auto scale width to keep aspect ratio.
|
||||||
|
|
@ -107,7 +107,8 @@ bool TextureData::initSVGFromMemory(const std::string& fileData)
|
||||||
|
|
||||||
bool TextureData::initImageFromMemory(const unsigned char* fileData, size_t length)
|
bool TextureData::initImageFromMemory(const unsigned char* fileData, size_t length)
|
||||||
{
|
{
|
||||||
size_t width, height;
|
size_t width;
|
||||||
|
size_t height;
|
||||||
|
|
||||||
// If already initialized then don't process it again.
|
// If already initialized then don't process it again.
|
||||||
{
|
{
|
||||||
|
|
@ -141,7 +142,7 @@ bool TextureData::initFromRGBA(const unsigned char* dataRGBA, size_t width, size
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
mDataRGBA.reserve(width * height * 4);
|
mDataRGBA.reserve(width * height * 4);
|
||||||
mDataRGBA.insert(mDataRGBA.begin(), dataRGBA, dataRGBA+(width * height * 4));
|
mDataRGBA.insert(mDataRGBA.begin(), dataRGBA, dataRGBA + (width * height * 4));
|
||||||
|
|
||||||
mWidth = width;
|
mWidth = width;
|
||||||
mHeight = height;
|
mHeight = height;
|
||||||
|
|
@ -193,9 +194,9 @@ bool TextureData::uploadAndBind()
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Upload texture.
|
// Upload texture.
|
||||||
mTextureID = Renderer::createTexture(Renderer::Texture::RGBA, true,
|
mTextureID = Renderer::createTexture(Renderer::Texture::RGBA, true, mTile,
|
||||||
mTile, static_cast<const unsigned int>(mWidth),
|
static_cast<const unsigned int>(mWidth), static_cast<const unsigned int>(mHeight),
|
||||||
static_cast<const unsigned int>(mHeight), mDataRGBA.data());
|
mDataRGBA.data());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -223,14 +224,22 @@ size_t TextureData::width()
|
||||||
{
|
{
|
||||||
if (mWidth == 0)
|
if (mWidth == 0)
|
||||||
load();
|
load();
|
||||||
return mWidth;
|
// 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 mWidth * mScaleDuringLoad;
|
||||||
|
else
|
||||||
|
return mWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t TextureData::height()
|
size_t TextureData::height()
|
||||||
{
|
{
|
||||||
if (mHeight == 0)
|
if (mHeight == 0)
|
||||||
load();
|
load();
|
||||||
return mHeight;
|
if (!mScalable)
|
||||||
|
return mHeight * mScaleDuringLoad;
|
||||||
|
else
|
||||||
|
return mHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
float TextureData::sourceWidth()
|
float TextureData::sourceWidth()
|
||||||
|
|
|
||||||
|
|
@ -54,8 +54,8 @@ public:
|
||||||
float sourceHeight();
|
float sourceHeight();
|
||||||
void setSourceSize(float width, float height);
|
void setSourceSize(float width, float height);
|
||||||
|
|
||||||
// Define a factor for scaling the SVG graphics when loading it (1.0f = no scaling).
|
// Define a factor for scaling the file when loading it (1.0f = no scaling).
|
||||||
void setScaleSVGDuringLoad(float scale) { mScaleSVG = scale; }
|
void setScaleDuringLoad(float scale) { mScaleDuringLoad = scale; }
|
||||||
|
|
||||||
bool tiled() { return mTile; }
|
bool tiled() { return mTile; }
|
||||||
|
|
||||||
|
|
@ -69,7 +69,7 @@ private:
|
||||||
size_t mHeight;
|
size_t mHeight;
|
||||||
float mSourceWidth;
|
float mSourceWidth;
|
||||||
float mSourceHeight;
|
float mSourceHeight;
|
||||||
float mScaleSVG;
|
float mScaleDuringLoad;
|
||||||
bool mScalable;
|
bool mScalable;
|
||||||
bool mReloadable;
|
bool mReloadable;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ TextureResource::TextureResource(
|
||||||
const std::string& path,
|
const std::string& path,
|
||||||
bool tile,
|
bool tile,
|
||||||
bool dynamic,
|
bool dynamic,
|
||||||
float scaleSVG)
|
float scaleDuringLoad)
|
||||||
: mTextureData(nullptr),
|
: mTextureData(nullptr),
|
||||||
mForceLoad(false)
|
mForceLoad(false)
|
||||||
{
|
{
|
||||||
|
|
@ -32,8 +32,8 @@ TextureResource::TextureResource(
|
||||||
if (dynamic) {
|
if (dynamic) {
|
||||||
data = sTextureDataManager.add(this, tile);
|
data = sTextureDataManager.add(this, tile);
|
||||||
data->initFromPath(path);
|
data->initFromPath(path);
|
||||||
if (scaleSVG != 1.0f)
|
if (scaleDuringLoad != 1.0f)
|
||||||
data->setScaleSVGDuringLoad(scaleSVG);
|
data->setScaleDuringLoad(scaleDuringLoad);
|
||||||
// Force the texture manager to load it using a blocking load.
|
// Force the texture manager to load it using a blocking load.
|
||||||
sTextureDataManager.load(data, true);
|
sTextureDataManager.load(data, true);
|
||||||
}
|
}
|
||||||
|
|
@ -41,8 +41,8 @@ TextureResource::TextureResource(
|
||||||
mTextureData = std::shared_ptr<TextureData>(new TextureData(tile));
|
mTextureData = std::shared_ptr<TextureData>(new TextureData(tile));
|
||||||
data = mTextureData;
|
data = mTextureData;
|
||||||
data->initFromPath(path);
|
data->initFromPath(path);
|
||||||
if (scaleSVG != 1.0f)
|
if (scaleDuringLoad != 1.0f)
|
||||||
data->setScaleSVGDuringLoad(scaleSVG);
|
data->setScaleDuringLoad(scaleDuringLoad);
|
||||||
// Load it so we can read the width/height.
|
// Load it so we can read the width/height.
|
||||||
data->load();
|
data->load();
|
||||||
}
|
}
|
||||||
|
|
@ -134,13 +134,13 @@ std::shared_ptr<TextureResource> TextureResource::get(
|
||||||
bool tile,
|
bool tile,
|
||||||
bool forceLoad,
|
bool forceLoad,
|
||||||
bool dynamic,
|
bool dynamic,
|
||||||
float scaleSVG)
|
float scaleDuringLoad)
|
||||||
{
|
{
|
||||||
std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance();
|
std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance();
|
||||||
|
|
||||||
const std::string canonicalPath = Utils::FileSystem::getCanonicalPath(path);
|
const std::string canonicalPath = Utils::FileSystem::getCanonicalPath(path);
|
||||||
if (canonicalPath.empty()) {
|
if (canonicalPath.empty()) {
|
||||||
std::shared_ptr<TextureResource> tex(new TextureResource("", tile, false, scaleSVG));
|
std::shared_ptr<TextureResource> tex(new TextureResource("", tile, false, scaleDuringLoad));
|
||||||
// Make sure we get properly deinitialized even though we do nothing on reinitialization.
|
// Make sure we get properly deinitialized even though we do nothing on reinitialization.
|
||||||
rm->addReloadable(tex);
|
rm->addReloadable(tex);
|
||||||
return tex;
|
return tex;
|
||||||
|
|
@ -156,7 +156,8 @@ std::shared_ptr<TextureResource> TextureResource::get(
|
||||||
|
|
||||||
// Need to create it.
|
// Need to create it.
|
||||||
std::shared_ptr<TextureResource> tex;
|
std::shared_ptr<TextureResource> tex;
|
||||||
tex = std::shared_ptr<TextureResource>(new TextureResource(key.first, tile, dynamic, scaleSVG));
|
tex = std::shared_ptr<TextureResource>(
|
||||||
|
new TextureResource(key.first, tile, dynamic, scaleDuringLoad));
|
||||||
std::shared_ptr<TextureData> data = sTextureDataManager.get(tex.get());
|
std::shared_ptr<TextureData> data = sTextureDataManager.get(tex.get());
|
||||||
|
|
||||||
// Is it an SVG?
|
// Is it an SVG?
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ public:
|
||||||
bool tile = false,
|
bool tile = false,
|
||||||
bool forceLoad = false,
|
bool forceLoad = false,
|
||||||
bool dynamic = true,
|
bool dynamic = true,
|
||||||
float scaleSVG = 1.0f);
|
float scaleDuringLoad = 1.0f);
|
||||||
void initFromPixels(const unsigned char* dataRGBA, size_t width, size_t height);
|
void initFromPixels(const unsigned char* dataRGBA, size_t width, size_t height);
|
||||||
virtual void initFromMemory(const char* data, size_t length);
|
virtual void initFromMemory(const char* data, size_t length);
|
||||||
static void manualUnload(std::string path, bool tile);
|
static void manualUnload(std::string path, bool tile);
|
||||||
|
|
@ -38,7 +38,7 @@ public:
|
||||||
// For SVG graphics this function effectively rescales the image to the defined size.
|
// 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
|
// 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
|
// situations. An alternative is to set a scaling factor directly when loading the texture
|
||||||
// using get(), by using the scaleSVG parameter.
|
// using get(), by using the scaleDuringLoad parameter (which also works for raster graphics).
|
||||||
void rasterizeAt(size_t width, size_t height);
|
void rasterizeAt(size_t width, size_t height);
|
||||||
Vector2f getSourceImageSize() const;
|
Vector2f getSourceImageSize() const;
|
||||||
|
|
||||||
|
|
@ -56,7 +56,7 @@ public:
|
||||||
static size_t getTotalTextureSize();
|
static size_t getTotalTextureSize();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TextureResource(const std::string& path, bool tile, bool dynamic, float scaleSVG);
|
TextureResource(const std::string& path, bool tile, bool dynamic, float scaleDuringLoad);
|
||||||
virtual void unload(std::shared_ptr<ResourceManager>& rm);
|
virtual void unload(std::shared_ptr<ResourceManager>& rm);
|
||||||
virtual void reload(std::shared_ptr<ResourceManager>& rm);
|
virtual void reload(std::shared_ptr<ResourceManager>& rm);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue