From 48a21f6542e799e41735febbda76815e058954ac Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 28 Sep 2012 02:09:04 +0300 Subject: Override inc/dec ref to add/remove atlas images to resource cache. --- src/resources/atlasmanager.cpp | 69 ++++++++++++++++++++++++++++++++++++------ src/resources/atlasmanager.h | 6 ++++ src/resources/resource.h | 4 +-- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/resources/atlasmanager.cpp b/src/resources/atlasmanager.cpp index 308c8d9ba..a9745bd08 100644 --- a/src/resources/atlasmanager.cpp +++ b/src/resources/atlasmanager.cpp @@ -257,20 +257,21 @@ void AtlasManager::convertAtlas(TextureAtlas *atlas) void AtlasManager::injectToResources(AtlasResource *resource) { ResourceManager *const resman = ResourceManager::getInstance(); - int k = 0; for (std::vector::iterator it = resource->atlases.begin(), - it_end = resource->atlases.end(); it != it_end; ++ it, k ++) + it_end = resource->atlases.end(); it != it_end; ++ it) { // add each atlas image to resources TextureAtlas *const atlas = *it; - resman->addResource(atlas->name, atlas->atlasImage); if (atlas) { + resman->addResource(atlas->name, atlas->atlasImage); for (std::vector::iterator it2 = atlas->items.begin(), it2_end = atlas->items.end(); it2 != it2_end; ++ it2) { AtlasItem *const item = *it2; + if (!item) + continue; // add each atlas sub image to resources resman->addResource(item->name, item->image); } @@ -278,6 +279,42 @@ void AtlasManager::injectToResources(AtlasResource *resource) } } +void AtlasManager::moveToDeleted(AtlasResource *resource) +{ + ResourceManager *const resman = ResourceManager::getInstance(); + for (std::vector::iterator it = resource->atlases.begin(), + it_end = resource->atlases.end(); it != it_end; ++ it) + { + // move each atlas image to deleted + TextureAtlas *const atlas = *it; + if (atlas) + { + Image *const image = atlas->atlasImage; + if (image) + { + image->decRef(); + resman->moveToDeleted(image); + } + for (std::vector::iterator it2 = atlas->items.begin(), + it2_end = atlas->items.end(); + it2 != it2_end; ++ it2) + { + AtlasItem *const item = *it2; + if (item) + { + Image *const image2 = item->image; + if (image2) + { + image2->decRef(); + // move each atlas sub image to deleted + resman->moveToDeleted(image2); + } + } + } + } + } +} + AtlasResource::~AtlasResource() { for (std::vector::iterator it = atlases.begin(), @@ -293,18 +330,32 @@ AtlasResource::~AtlasResource() AtlasItem *const item = *it2; if (item) { - Image *const image = item->image; - if (image) - image->decRef(); +// Image *const image = item->image; +// if (image) +// image->decRef(); delete item; } } - Image *const image = atlas->atlasImage; - if (image) - image->decRef(); +// Image *const image = atlas->atlasImage; +// if (image) +// image->decRef(); delete atlas; } } } +void AtlasResource::incRef() +{ + if (!getRefCount()) + AtlasManager::injectToResources(this); + Resource::incRef(); +} + +void AtlasResource::decRef() +{ + Resource::decRef(); + if (!getRefCount()) + AtlasManager::moveToDeleted(this); +} + #endif diff --git a/src/resources/atlasmanager.h b/src/resources/atlasmanager.h index 41aae86e5..1606fc4c9 100644 --- a/src/resources/atlasmanager.h +++ b/src/resources/atlasmanager.h @@ -73,6 +73,10 @@ class AtlasResource : public Resource public: ~AtlasResource(); + void incRef(); + + void decRef(); + std::vector atlases; }; @@ -86,6 +90,8 @@ class AtlasManager static void injectToResources(AtlasResource *resource); + static void moveToDeleted(AtlasResource *resource); + private: static void loadImages(const StringVect &files, std::vector &images); diff --git a/src/resources/resource.h b/src/resources/resource.h index b73597d14..cf3555cf5 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -52,7 +52,7 @@ class Resource /** * Increments the internal reference count. */ - void incRef(); + virtual void incRef(); /** * Decrements the reference count and deletes the object @@ -61,7 +61,7 @@ class Resource * @return true if the object was deleted * false otherwise. */ - void decRef(); + virtual void decRef(); /** * Return the path identifying this resource. -- cgit v1.2.3-60-g2f50