From 8b984259c0d7d7af5394defc89a3b64d7b479425 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 30 Sep 2012 15:47:51 +0300 Subject: Some fixes in atlases deletion. --- src/resources/atlasmanager.cpp | 24 +++++++++----------- src/resources/resource.h | 2 +- src/resources/resourcemanager.cpp | 47 ++++++++++++++++++++++++++++++++++----- src/resources/resourcemanager.h | 2 ++ src/resources/subimage.cpp | 2 ++ 5 files changed, 56 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/resources/atlasmanager.cpp b/src/resources/atlasmanager.cpp index a9745bd08..5a43454b4 100644 --- a/src/resources/atlasmanager.cpp +++ b/src/resources/atlasmanager.cpp @@ -92,16 +92,21 @@ AtlasResource *AtlasManager::loadTextureAtlas(const std::string &name, void AtlasManager::loadImages(const StringVect &files, std::vector &images) { - const ResourceManager *const resman = ResourceManager::getInstance(); + ResourceManager *const resman = ResourceManager::getInstance(); for (StringVectCIter it = files.begin(), it_end = files.end(); it != it_end; ++ it) { const std::string str = *it; - if (resman->isInCache(str)) + // check is image with same name already in cache + // and if yes, move it to deleted set + Resource *const res = resman->getTempResource(str); + if (res) { - logger->log("Resource %s already in cache", str.c_str()); - continue; + //logger->log("Resource %s already in cache", str.c_str()); + // increase counter because in moveToDeleted it will be decreased. + res->incRef(); + resman->moveToDeleted(res); } SDL_RWops *rw = PHYSFSRWOPS_openRead(str.c_str()); @@ -292,7 +297,7 @@ void AtlasManager::moveToDeleted(AtlasResource *resource) Image *const image = atlas->atlasImage; if (image) { - image->decRef(); + // move each atlas image to deleted resman->moveToDeleted(image); } for (std::vector::iterator it2 = atlas->items.begin(), @@ -305,7 +310,6 @@ void AtlasManager::moveToDeleted(AtlasResource *resource) Image *const image2 = item->image; if (image2) { - image2->decRef(); // move each atlas sub image to deleted resman->moveToDeleted(image2); } @@ -329,16 +333,8 @@ AtlasResource::~AtlasResource() { AtlasItem *const item = *it2; if (item) - { -// Image *const image = item->image; -// if (image) -// image->decRef(); delete item; - } } -// Image *const image = atlas->atlasImage; -// if (image) -// image->decRef(); delete atlas; } } diff --git a/src/resources/resource.h b/src/resources/resource.h index cf3555cf5..d9b220338 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -96,11 +96,11 @@ class Resource virtual ~Resource(); std::string mIdPath; /**< Path identifying this resource. */ + std::string mSource; private: time_t mTimeStamp; /**< Time at which the resource was orphaned. */ unsigned mRefCount; /**< Reference count. */ - std::string mSource; #ifdef DEBUG_DUMP_LEAKS bool mDumped; #endif diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 2bc269503..e479a9609 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -289,6 +289,11 @@ void ResourceManager::clearDeleted() while (resDelIter != mDeletedResources.end()) { logResource(*resDelIter); + + // for debug only +// delete *resDelIter; + // for debug only + ++ resDelIter; } } @@ -446,6 +451,18 @@ bool ResourceManager::isInCache(const std::string &idPath) const return (resIter != mResources.end() && resIter->second); } +Resource *ResourceManager::getTempResource(const std::string &idPath) +{ + const ResourceCIterator &resIter = mResources.find(idPath); + if (resIter != mResources.end()) + { + Resource *const res = resIter->second; + if (resIter->second) + return res; + } + return nullptr; +} + Resource *ResourceManager::getFromCache(const std::string &idPath) { // Check if the id exists, and return the value if it does. @@ -714,7 +731,7 @@ struct AtlasLoader const AtlasLoader *const rl = static_cast(v); AtlasResource *const resource = AtlasManager::loadTextureAtlas( rl->name, *rl->files); - AtlasManager::injectToResources(resource); +// AtlasManager::injectToResources(resource); return resource; } }; @@ -788,17 +805,35 @@ void ResourceManager::release(Resource *const res) void ResourceManager::moveToDeleted(Resource *const res) { + if (!res) + return; + + bool found(false); + const int count = res->getRefCount(); + if (count == 1) + logResource(res); + res->decRef(); ResourceIterator resIter = mResources.find(res->mIdPath); if (resIter != mResources.end() && resIter->second == res) { - mDeletedResources.insert(res); mResources.erase(resIter); + found = true; } - resIter = mOrphanedResources.find(res->mIdPath); - if (resIter != mOrphanedResources.end() && resIter->second == res) + else { - mDeletedResources.insert(res); - mOrphanedResources.erase(resIter); + resIter = mOrphanedResources.find(res->mIdPath); + if (resIter != mOrphanedResources.end() && resIter->second == res) + { + mOrphanedResources.erase(resIter); + found = true; + } + } + if (found) + { + if (count > 1) + mDeletedResources.insert(res); + else + delete res; } } diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 5699fc7fc..432d404b6 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -303,6 +303,8 @@ class ResourceManager final bool isInCache(const std::string &idPath) const; + Resource *getTempResource(const std::string &idPath); + static void addDelayedAnimation(AnimationDelayLoad *const animation) { mDelayedAnimations.push_back(animation); } diff --git a/src/resources/subimage.cpp b/src/resources/subimage.cpp index 138805d24..a2d6a8c33 100644 --- a/src/resources/subimage.cpp +++ b/src/resources/subimage.cpp @@ -44,6 +44,7 @@ SubImage::SubImage(Image *const parent, SDL_Surface *const image, mHasAlphaChannel = mParent->hasAlphaChannel(); mIsAlphaVisible = mHasAlphaChannel; mAlphaChannel = mParent->SDLgetAlphaChannel(); + mSource = parent->getIdPath(); #ifdef DEBUG_BIND_TEXTURE mIdPath = parent->getIdPath(); #endif @@ -98,6 +99,7 @@ SubImage::SubImage(Image *const parent, const GLuint image, mInternalBounds.y = mParent->mBounds.y; mInternalBounds.w = mParent->mBounds.w; mInternalBounds.h = mParent->mBounds.h; + mSource = parent->getIdPath(); #ifdef DEBUG_BIND_TEXTURE mIdPath = parent->getIdPath(); #endif -- cgit v1.2.3-60-g2f50