From 0f02cdb4aa0a0e29bea414583a6db8a5ae389f49 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sat, 14 Mar 2015 10:11:19 -0400 Subject: MADS: Fix crash in stream crossing death, simplify SpriteSets class --- engines/mads/game.cpp | 2 +- engines/mads/player.cpp | 2 +- engines/mads/scene.cpp | 2 +- engines/mads/sprites.cpp | 52 +++++++++++++++++++++++++----------------------- engines/mads/sprites.h | 8 +++++--- 5 files changed, 35 insertions(+), 31 deletions(-) (limited to 'engines') diff --git a/engines/mads/game.cpp b/engines/mads/game.cpp index 74c2a3f416..b601a12c82 100644 --- a/engines/mads/game.cpp +++ b/engines/mads/game.cpp @@ -160,7 +160,7 @@ void Game::gameLoop() { sectionLoop(); _player.releasePlayerSprites(); - assert(_scene._sprites._assetCount == 0); + assert(_scene._sprites.size() == 0); _vm->_palette->unlock(); _vm->_events->waitCursor(); diff --git a/engines/mads/player.cpp b/engines/mads/player.cpp index 38e8638415..f61a5a1592 100644 --- a/engines/mads/player.cpp +++ b/engines/mads/player.cpp @@ -709,7 +709,7 @@ void Player::releasePlayerSprites() { _spritesLoaded = false; _spritesChanged = true; - if (scene._sprites._assetCount > 0) { + if (scene._sprites.size() > 0) { warning("Player::releasePlayerSprites(): leftover sprites remain, clearing list"); scene._sprites.clear(); } diff --git a/engines/mads/scene.cpp b/engines/mads/scene.cpp index bbc83a791b..463d4a62fd 100644 --- a/engines/mads/scene.cpp +++ b/engines/mads/scene.cpp @@ -676,7 +676,7 @@ void Scene::freeCurrentScene() { } void Scene::removeSprites() { - for (int idx = _sprites._assetCount - 1; idx >= _spritesCount; --idx) + for (int idx = _sprites.size() - 1; idx >= _spritesCount; --idx) _sprites.remove(idx); } diff --git a/engines/mads/sprites.cpp b/engines/mads/sprites.cpp index 19742e22b0..6488add86c 100644 --- a/engines/mads/sprites.cpp +++ b/engines/mads/sprites.cpp @@ -368,19 +368,18 @@ SpriteSets::~SpriteSets() { } int SpriteSets::add(SpriteAsset *asset, int idx) { - if (idx) - idx = idx + 49; - else - idx = _assetCount++; + if (idx) { + assert(idx == 1); + delete _uiSprites; + _uiSprites = asset; - if (idx >= (int)size()) - resize(idx + 1); + return SPRITE_SLOTS_MAX_SIZE; + } else { + assert(size() < SPRITE_SLOTS_MAX_SIZE); + push_back(asset); - if ((*this)[idx]) - delete (*this)[idx]; - - (*this)[idx] = asset; - return idx; + return (int)size() - 1; + } } int SpriteSets::addSprites(const Common::String &resName, int flags) { @@ -390,26 +389,29 @@ int SpriteSets::addSprites(const Common::String &resName, int flags) { void SpriteSets::clear() { for (uint i = 0; i < size(); ++i) delete (*this)[i]; - - _assetCount = 0; Common::Array::clear(); + + delete _uiSprites; + _uiSprites = nullptr; } void SpriteSets::remove(int idx) { - if (idx >= 0) { - if (idx < ((int)size() - 1)) { - delete (*this)[idx]; - (*this)[idx] = nullptr; - } else { - do { - delete (*this)[size() - 1]; - remove_at(size() - 1); - } while (size() > 0 && (*this)[size() - 1] == nullptr); - } + if (idx == SPRITE_SLOTS_MAX_SIZE) { + delete _uiSprites; + _uiSprites = nullptr; + } else if (idx >= 0) { + delete (*this)[idx]; - if (idx < 50 && _assetCount > 0) - --_assetCount; + if (idx == ((int)size() - 1)) + remove_at(size() - 1); + else + (*this)[idx] = nullptr; } } +SpriteAsset *&SpriteSets::operator[](int idx) { + return (idx == SPRITE_SLOTS_MAX_SIZE) ? _uiSprites : + Common::Array::operator[](idx); +} + } // End of namespace MADS diff --git a/engines/mads/sprites.h b/engines/mads/sprites.h index 6ea3c9e52e..bb5fdbe964 100644 --- a/engines/mads/sprites.h +++ b/engines/mads/sprites.h @@ -202,12 +202,12 @@ class SpriteSets : public Common::Array { private: MADSEngine *_vm; public: - int _assetCount; - + SpriteAsset *_uiSprites; +public: /** * Constructor */ - SpriteSets(MADSEngine *vm) : _vm(vm), _assetCount(0) {} + SpriteSets(MADSEngine *vm) : _vm(vm), _uiSprites(nullptr) {} /** * Destructor @@ -233,6 +233,8 @@ public: * Remove an asset from the list */ void remove(int idx); + + SpriteAsset *&operator[](int idx); }; } // End of namespace MADS -- cgit v1.2.3