From 5acaaed860dd206528726462eb72e2273e1988e2 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Mon, 14 Oct 2019 21:30:17 +0200 Subject: GRAPHICS: MACGUI: Crash-proof window management. --- graphics/macgui/macwindowmanager.cpp | 27 ++++++++++++++++----------- graphics/macgui/macwindowmanager.h | 4 ++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/graphics/macgui/macwindowmanager.cpp b/graphics/macgui/macwindowmanager.cpp index 1e8a4bac77..cd72d5cf1f 100644 --- a/graphics/macgui/macwindowmanager.cpp +++ b/graphics/macgui/macwindowmanager.cpp @@ -184,8 +184,8 @@ MacWindowManager::MacWindowManager() { } MacWindowManager::~MacWindowManager() { - for (int i = 0; i < _lastId; i++) - delete _windows[i]; + for (Common::HashMap::iterator it = _windows.begin(); it != _windows.end(); it++) + delete it->_value; delete _fontMan; delete _screenCopy; @@ -223,14 +223,14 @@ MacTextWindow *MacWindowManager::addTextWindow(const MacFont *font, int fgcolor, void MacWindowManager::addWindowInitialized(MacWindow *macwindow) { - _windows.push_back(macwindow); + _windows[macwindow->getId()] = macwindow; _windowStack.push_back(macwindow); } MacMenu *MacWindowManager::addMenu() { _menu = new MacMenu(getNextId(), _screen->getBounds(), this); - _windows.push_back(_menu); + _windows[_menu->getId()] = _menu; return _menu; } @@ -431,7 +431,13 @@ void MacWindowManager::removeMarked() { } _windowsToRemove.clear(); _needsRemoval = false; - _lastId = _windows.size(); + + // Do we need compact lastid? + _lastId = 0; + for (Common::HashMap::iterator lit = _windows.begin(); lit != _windows.end(); lit++) { + if (lit->_key > (uint)_lastId) + _lastId = lit->_key; + } } void MacWindowManager::removeFromStack(BaseMacWindow *target) { @@ -445,14 +451,13 @@ void MacWindowManager::removeFromStack(BaseMacWindow *target) { } void MacWindowManager::removeFromWindowList(BaseMacWindow *target) { - int size = _windows.size(); - int ndx = 0; - for (int i = 0; i < size; i++) { - if (_windows[i] == target) { - ndx = i; + // _windows.erase(target->getId()); // Is applicable? + for (Common::HashMap::iterator it = _windows.begin(); it != _windows.end(); it++) { + if (it->_value == target) { + _windows.erase(it); + break; } } - _windows.remove_at(ndx); } ///////////////// diff --git a/graphics/macgui/macwindowmanager.h b/graphics/macgui/macwindowmanager.h index f288a09fd8..1719bee317 100644 --- a/graphics/macgui/macwindowmanager.h +++ b/graphics/macgui/macwindowmanager.h @@ -23,7 +23,7 @@ #ifndef GRAPHICS_MACGUI_MACWINDOWMANAGER_H #define GRAPHICS_MACGUI_MACWINDOWMANAGER_H -#include "common/array.h" +#include "common/hashmap.h" #include "common/list.h" #include "common/events.h" @@ -243,7 +243,7 @@ public: private: Common::List _windowStack; - Common::Array _windows; + Common::HashMap _windows; Common::List _windowsToRemove; bool _needsRemoval; -- cgit v1.2.3