diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 54e673eb65..fb54a05b04 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -111,9 +111,6 @@ CGUI::~CGUI() { for (const std::pair& p : m_pAllObjects) delete p.second; - - for (const std::pair& p : m_Sprites) - delete p.second; } InReaction CGUI::HandleEvent(const SDL_Event_* ev) @@ -991,7 +988,7 @@ void CGUI::Xeromyces_ReadScript(const XMBData& xmb, XMBElement element, std::uno void CGUI::Xeromyces_ReadSprite(const XMBData& xmb, XMBElement element) { - CGUISprite* Sprite = new CGUISprite; + auto sprite = std::make_unique(); // Get name, we know it exists because of DTD requirements CStr name = element.GetAttributes().GetNamedItem(xmb.GetAttributeID("name")); @@ -1006,7 +1003,7 @@ void CGUI::Xeromyces_ReadSprite(const XMBData& xmb, XMBElement element) { std::string_view ElementName(xmb.GetElementStringView(child.GetNodeName())); if (ElementName == "image") - Xeromyces_ReadImage(xmb, child, *Sprite); + Xeromyces_ReadImage(xmb, child, *sprite); else if (ElementName == "effect") { if (effects) @@ -1025,13 +1022,13 @@ void CGUI::Xeromyces_ReadSprite(const XMBData& xmb, XMBElement element) // different effects) if (effects) { - for (const std::unique_ptr& image : Sprite->m_Images) + for (const std::unique_ptr& image : sprite->m_Images) if (!image->m_Effects) image->m_Effects = effects; } m_Sprites.erase(name); - m_Sprites.emplace(name, Sprite); + m_Sprites.emplace(name, std::move(sprite)); } void CGUI::Xeromyces_ReadImage(const XMBData& xmb, XMBElement element, CGUISprite& parent) diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index 1bbb3909f4..dfc68cc414 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -35,6 +35,7 @@ #include "scriptinterface/ScriptForward.h" #include +#include #include #include #include @@ -670,7 +671,7 @@ private: std::map m_PreDefinedColors; // Sprites - std::map m_Sprites; + std::map> m_Sprites; // Styles std::map m_Styles; diff --git a/source/gui/CGUISprite.cpp b/source/gui/CGUISprite.cpp index b47e2deaa2..268b573422 100644 --- a/source/gui/CGUISprite.cpp +++ b/source/gui/CGUISprite.cpp @@ -26,7 +26,7 @@ void CGUISprite::AddImage(std::unique_ptr image) m_Images.emplace_back(std::move(image)); } -void CGUISpriteInstance::Draw(CGUI& pGUI, CCanvas2D& canvas, const CRect& Size, std::map& Sprites) const +void CGUISpriteInstance::Draw(CGUI& pGUI, CCanvas2D& canvas, const CRect& Size, std::map>& Sprites) const { if (m_CachedSize != Size) { diff --git a/source/gui/CGUISprite.h b/source/gui/CGUISprite.h index 91ebecaa86..8feead0ac8 100644 --- a/source/gui/CGUISprite.h +++ b/source/gui/CGUISprite.h @@ -139,7 +139,7 @@ public: CGUISpriteInstance(); CGUISpriteInstance(const CStr& SpriteName); - void Draw(CGUI& pGUI, CCanvas2D& canvas, const CRect& Size, std::map& Sprites) const; + void Draw(CGUI& pGUI, CCanvas2D& canvas, const CRect& Size, std::map>& Sprites) const; /** * Whether this Sprite has no texture name set. diff --git a/source/gui/GUIRenderer.cpp b/source/gui/GUIRenderer.cpp index 57a8d4491d..6832b351f6 100644 --- a/source/gui/GUIRenderer.cpp +++ b/source/gui/GUIRenderer.cpp @@ -60,7 +60,7 @@ DrawCalls& DrawCalls::operator=(const DrawCalls&) } -void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, std::map& Sprites) +void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, std::map>& Sprites) { // This is called only when something has changed (like the size of the // sprite), so it doesn't need to be particularly efficient. @@ -75,7 +75,7 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const return; - std::map::iterator it(Sprites.find(SpriteName)); + std::map>::iterator it(Sprites.find(SpriteName)); if (it == Sprites.end()) { /* @@ -96,7 +96,7 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const LOGERROR("Trying to use a sprite that doesn't exist (\"%s\").", SpriteName.c_str()); return; } - CGUISprite* Sprite = new CGUISprite; + auto sprite = std::make_unique(); VfsPath TextureName = VfsPath("art/textures/ui") / wstring_from_utf8(SpriteName.AfterLast(":")); if (SpriteName.Find("stretched:") != -1) { @@ -110,8 +110,8 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const image->m_Effects->m_Greyscale = true; } - Sprite->AddImage(std::move(image)); - Sprites[SpriteName] = Sprite; + sprite->AddImage(std::move(image)); + Sprites[SpriteName] = std::move(sprite); } else if (SpriteName.Find("cropped:") != -1) { @@ -135,8 +135,8 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const image->m_Effects->m_Greyscale = true; } - Sprite->AddImage(std::move(image)); - Sprites[SpriteName] = Sprite; + sprite->AddImage(std::move(image)); + Sprites[SpriteName] = std::move(sprite); } if (SpriteName.Find("color:") != -1) { @@ -164,15 +164,15 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const return; } - Sprite->AddImage(std::move(image)); - Sprites[SpriteName] = Sprite; + sprite->AddImage(std::move(image)); + Sprites[SpriteName] = std::move(sprite); } it = Sprites.find(SpriteName); // Otherwise, just complain and give up: if (it == Sprites.end()) { - SAFE_DELETE(Sprite); + sprite.reset(); LOGERROR("Trying to use a sprite that doesn't exist (\"%s\").", SpriteName.c_str()); return; } diff --git a/source/gui/GUIRenderer.h b/source/gui/GUIRenderer.h index eddf28b34b..f7cb11b0fe 100644 --- a/source/gui/GUIRenderer.h +++ b/source/gui/GUIRenderer.h @@ -65,7 +65,7 @@ namespace GUIRenderer DrawCalls& operator=(const DrawCalls&); }; - void UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const CStr8& SpriteName, const CRect& Size, std::map& Sprites); + void UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const CStr8& SpriteName, const CRect& Size, std::map>& Sprites); void Draw(DrawCalls& Calls, CCanvas2D& canvas); }