From 036937ee02dcd3063598a2668b02524703971854 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 18 Jul 2022 21:14:39 +0200 Subject: [PATCH] Store RenderManager textures by value --- .../myguiplatform/myguirendermanager.cpp | 27 +++++-------------- .../myguiplatform/myguirendermanager.hpp | 6 ++--- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/components/myguiplatform/myguirendermanager.cpp b/components/myguiplatform/myguirendermanager.cpp index 9d48a0d35a..e0e8e245a3 100644 --- a/components/myguiplatform/myguirendermanager.cpp +++ b/components/myguiplatform/myguirendermanager.cpp @@ -380,8 +380,6 @@ RenderManager::~RenderManager() mSceneRoot = nullptr; mViewer = nullptr; - destroyAllResources(); - MYGUI_PLATFORM_LOG(Info, getClassTypeName()<<" successfully shutdown"); mIsInitialise = false; } @@ -532,16 +530,13 @@ bool RenderManager::isFormatSupported(MyGUI::PixelFormat /*format*/, MyGUI::Text MyGUI::ITexture* RenderManager::createTexture(const std::string &name) { - MapTexture::iterator item = mTextures.find(name); + auto item = mTextures.find(name); if (item != mTextures.end()) - { - delete item->second; mTextures.erase(item); - } - OSGTexture* texture = new OSGTexture(name, mImageManager); - mTextures.insert(std::make_pair(name, texture)); - return texture; + item = mTextures.emplace_hint(item, name, OSGTexture(name, mImageManager)); + + return &item->second; } void RenderManager::destroyTexture(MyGUI::ITexture *texture) @@ -549,11 +544,10 @@ void RenderManager::destroyTexture(MyGUI::ITexture *texture) if(texture == nullptr) return; - MapTexture::iterator item = mTextures.find(texture->getName()); + const auto item = mTextures.find(texture->getName()); MYGUI_PLATFORM_ASSERT(item != mTextures.end(), "Texture '"<getName()<<"' not found"); mTextures.erase(item); - delete texture; } MyGUI::ITexture* RenderManager::getTexture(const std::string &name) @@ -561,21 +555,14 @@ MyGUI::ITexture* RenderManager::getTexture(const std::string &name) if (name.empty()) return nullptr; - MapTexture::const_iterator item = mTextures.find(name); + const auto item = mTextures.find(name); if(item == mTextures.end()) { MyGUI::ITexture* tex = createTexture(name); tex->loadFromFile(name); return tex; } - return item->second; -} - -void RenderManager::destroyAllResources() -{ - for (MapTexture::iterator it = mTextures.begin(); it != mTextures.end(); ++it) - delete it->second; - mTextures.clear(); + return &item->second; } bool RenderManager::checkTexture(MyGUI::ITexture* _texture) diff --git a/components/myguiplatform/myguirendermanager.hpp b/components/myguiplatform/myguirendermanager.hpp index 0d1ad4fb41..b6723db283 100644 --- a/components/myguiplatform/myguirendermanager.hpp +++ b/components/myguiplatform/myguirendermanager.hpp @@ -32,6 +32,7 @@ namespace osgMyGUI { class Drawable; +class OSGTexture; class RenderManager : public MyGUI::RenderManager, public MyGUI::IRenderTarget { @@ -45,8 +46,7 @@ class RenderManager : public MyGUI::RenderManager, public MyGUI::IRenderTarget MyGUI::VertexColourType mVertexFormat; MyGUI::RenderTargetInfo mInfo; - typedef std::map MapTexture; - MapTexture mTextures; + std::map mTextures; bool mIsInitialise; @@ -56,8 +56,6 @@ class RenderManager : public MyGUI::RenderManager, public MyGUI::IRenderTarget osg::StateSet* mInjectState; - void destroyAllResources(); - public: RenderManager(osgViewer::Viewer *viewer, osg::Group *sceneroot, Resource::ImageManager* imageManager, float scalingFactor); virtual ~RenderManager();