diff --git a/src/drawing/Image.cpp b/src/drawing/Image.cpp index e4d88b8967..0fab47c9b9 100644 --- a/src/drawing/Image.cpp +++ b/src/drawing/Image.cpp @@ -14,8 +14,11 @@ *****************************************************************************/ #pragma endregion +#include #include +#include "../core/Console.hpp" #include "../core/Guard.hpp" +#include "../core/Memory.hpp" extern "C" { @@ -35,13 +38,34 @@ struct ImageList static bool _initialised = false; static std::list _freeLists; +#ifdef DEBUG +static std::list _allocatedLists; + +static bool AllocatedListContains(uint32 baseImageId, uint32 count) +{ + bool contains = std::any_of( + _allocatedLists.begin(), + _allocatedLists.end(), + [baseImageId, count](const ImageList &imageList) -> bool + { + return imageList.BaseId == baseImageId && imageList.Count == count; + }); + return contains; +} +#endif + static uint32 AllocateImageList(uint32 count) { + Guard::Assert(count != 0); + if (!_initialised) { _initialised = true; _freeLists.clear(); _freeLists.push_back({ BASE_IMAGE_ID, MAX_IMAGES }); +#ifdef DEBUG + _allocatedLists.clear(); +#endif } for (auto it = _freeLists.begin(); it != _freeLists.end(); it++) @@ -57,6 +81,9 @@ static uint32 AllocateImageList(uint32 count) _freeLists.push_back(remainder); } +#ifdef DEBUG + _allocatedLists.push_back({ imageList.BaseId, count }); +#endif return imageList.BaseId; } } @@ -68,6 +95,11 @@ static void FreeImageList(uint32 baseImageId, uint32 count) Guard::Assert(_initialised); Guard::Assert(baseImageId >= BASE_IMAGE_ID); +#ifdef DEBUG + bool contains = AllocatedListContains(baseImageId, count); + Guard::Assert(contains); +#endif + // TODO validate that this was an allocated list _freeLists.push_back({ baseImageId, count }); } diff --git a/src/object/LargeSceneryObject.cpp b/src/object/LargeSceneryObject.cpp index 7a76701fba..94e1679d10 100644 --- a/src/object/LargeSceneryObject.cpp +++ b/src/object/LargeSceneryObject.cpp @@ -81,7 +81,8 @@ void LargeSceneryObject::ReadLegacy(IReadObjectContext * context, IStream * stre void LargeSceneryObject::Load() { _legacyType.name = language_allocate_object_string(GetName()); - _legacyType.image = gfx_object_allocate_images(GetImageTable()->GetImages(), GetImageTable()->GetCount()); + _baseImageId = gfx_object_allocate_images(GetImageTable()->GetImages(), GetImageTable()->GetCount()); + _legacyType.image = _baseImageId; _legacyType.large_scenery.tiles = _tiles; @@ -112,7 +113,7 @@ void LargeSceneryObject::Load() void LargeSceneryObject::Unload() { language_free_object_string(_legacyType.name); - gfx_object_free_images(_legacyType.image, GetImageTable()->GetCount()); + gfx_object_free_images(_baseImageId, GetImageTable()->GetCount()); _legacyType.name = 0; _legacyType.image = 0; diff --git a/src/object/LargeSceneryObject.h b/src/object/LargeSceneryObject.h index 1034243988..00d67c3dbb 100644 --- a/src/object/LargeSceneryObject.h +++ b/src/object/LargeSceneryObject.h @@ -27,6 +27,7 @@ class LargeSceneryObject : public Object { private: rct_scenery_entry _legacyType = { 0 }; + uint32 _baseImageId = 0; rct_object_entry _sceneryTabEntry = { 0 }; rct_large_scenery_text * _3dFont = nullptr; rct_large_scenery_tile * _tiles = nullptr;