From dff191a7c21faa0213f6d5af548595d8c2b542c8 Mon Sep 17 00:00:00 2001 From: Nimetu Date: Sat, 17 Jul 2021 15:12:27 +0300 Subject: [PATCH] Move loading texture from file/memory into own functions --- nel/include/nel/gui/view_renderer.h | 3 + nel/src/gui/view_renderer.cpp | 125 +++++++++++++++++----------- 2 files changed, 79 insertions(+), 49 deletions(-) diff --git a/nel/include/nel/gui/view_renderer.h b/nel/include/nel/gui/view_renderer.h index 35d806aab..d1ad48d43 100644 --- a/nel/include/nel/gui/view_renderer.h +++ b/nel/include/nel/gui/view_renderer.h @@ -479,6 +479,9 @@ namespace NLGUI // \name Texture management // *************************************************************************** + bool loadTextureFromString(SGlobalTexture *gt, const std::string &data); + bool loadTextureFromFile(SGlobalTexture *gt, const std::string &filename); + // SImage accessors SImage *getSImage(sint32 textureId) { diff --git a/nel/src/gui/view_renderer.cpp b/nel/src/gui/view_renderer.cpp index 796479e82..0f0676cfc 100644 --- a/nel/src/gui/view_renderer.cpp +++ b/nel/src/gui/view_renderer.cpp @@ -999,6 +999,74 @@ namespace NLGUI ite->Texture = externalTexture; } + bool CViewRenderer::loadTextureFromString(CViewRenderer::SGlobalTexture *gt, const std::string &data) + { + size_t pos = data.find(";base64,"); + if (pos == std::string::npos) + { + nlwarning("Data does not have 'data:image/...;base64,...' format '%s'", data.c_str()); + return false; + } + + std::string decoded = base64::decode(data.substr(pos + 8)); + if (decoded.empty()) + { + nlwarning("base64 decoding failed '%s", data.substr(pos + 8).c_str()); + return false; + } + + CMemStream buf; + if (buf.isReading()) buf.invert(); + buf.serialBuffer((uint8 *)(decoded.data()), decoded.size()); + buf.invert(); + + CBitmap btm; + btm.load(buf); + + gt->Width = gt->DefaultWidth = btm.getWidth();; + gt->Height = gt->DefaultHeight = btm.getHeight(); + + if (gt->Width == 0 || gt->Height == 0) + { + nlwarning("Decoded image has width==0 || height==0, check image format. '%s'", data.c_str()); + return false; + } + + UTextureMem *texture = driver->createTextureMem(btm.getWidth(), btm.getHeight(), CBitmap::RGBA); + if (!texture) + { + nlwarning("Failed to create mem texture (%d,%d)", btm.getWidth(), btm.getHeight()); + return false; + } + + memcpy(texture->getPointer(), btm.getPixels().getPtr(), btm.getSize() * 4); + gt->Texture = texture; + gt->FromGlobaleTexture = false; + + return true; + } + + bool CViewRenderer::loadTextureFromFile(CViewRenderer::SGlobalTexture *gt, const std::string &filename) + { + // load new file + CIFile ifTmp; + if (ifTmp.open(filename)) + { + CBitmap::loadSize (ifTmp, gt->Width, gt->Height); + gt->DefaultWidth = gt->Width; + gt->DefaultHeight = gt->Height; + if (gt->Width == 0 || gt->Height == 0) + { + nlwarning("Failed to load the texture '%s', please check image format", filename.c_str()); + return false; + } + } + + gt->Texture = driver->createTextureFile(filename); + gt->FromGlobaleTexture = false; + + return true; + } /* * createTexture */ @@ -1030,27 +1098,20 @@ namespace NLGUI // If global texture not exists create it if (ite == _GlobalTextures.end()) { - SGlobalTexture gtTmp; - gtTmp.FromGlobaleTexture = false; string filename = CPath::lookup (sLwrGTName, false); if (filename.empty() ) return -1; - CIFile ifTmp; - if (ifTmp.open(filename)) - { - CBitmap::loadSize (ifTmp, gtTmp.Width, gtTmp.Height); - gtTmp.DefaultWidth = gtTmp.Width; - gtTmp.DefaultHeight = gtTmp.Height; - if (gtTmp.Width == 0 || gtTmp.Height == 0) - { - nlwarning("Failed to load the texture '%s', please check image format", filename.c_str()); - } - } - gtTmp.Texture = driver->createTextureFile (sLwrGTName); + + SGlobalTexture gtTmp; gtTmp.Name = sLwrGTName; + + if (!loadTextureFromFile(>Tmp, filename)) + return -1; + gtTmp.Texture->setFilterMode(UTexture::Nearest, UTexture::NearestMipMapOff); if(uploadDXTC) gtTmp.Texture->setUploadFormat(UTexture::DXTC5); gtTmp.Texture->setReleasable(bReleasable); + _GlobalTextures.push_back(gtTmp); ite = _GlobalTextures.end(); ite--; @@ -1105,44 +1166,10 @@ namespace NLGUI // If global texture not exists create it if (ite == _GlobalTextures.end()) { - std::string decoded = base64::decode(data.substr(pos + 8)); - if (decoded.empty()) - { - nlwarning("base64 decode failed '%s'", data.substr(pos + 8).c_str()); - return -1; - } - - // - CMemStream buf; - if (buf.isReading()) buf.invert(); - buf.serialBuffer((uint8 *)(decoded.data()), decoded.size()); - buf.invert(); - - CBitmap btm; - btm.load(buf); - SGlobalTexture gtTmp; - gtTmp.FromGlobaleTexture = false; - - gtTmp.Width = gtTmp.DefaultWidth = btm.getWidth();; - gtTmp.Height = gtTmp.DefaultHeight = btm.getHeight(); - - if (gtTmp.Width == 0 || gtTmp.Height == 0) - { - nlwarning("Failed to load the texture '%s', please check image format", data.c_str()); - return -1; - } - - UTextureMem *texture = driver->createTextureMem(btm.getWidth(), btm.getHeight(), CBitmap::RGBA); - if (!texture) - { - nlwarning("Failed to create mem texture (%d,%d)", btm.getWidth(), btm.getHeight()); + if (!loadTextureFromString(>Tmp, data)) return -1; - } - - memcpy(texture->getPointer(), btm.getPixels().getPtr(), btm.getSize() * 4); - gtTmp.Texture = texture; gtTmp.Name = md5hash; gtTmp.Texture->setFilterMode(UTexture::Nearest, UTexture::NearestMipMapOff); gtTmp.Texture->setReleasable(bReleasable);