summaryrefslogtreecommitdiff
path: root/src/resources/resourcemanager.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-02-08 22:09:07 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-02-09 23:47:51 +0100
commit32996cee607c52ecef9be4638df554dd89b39c24 (patch)
treeb3c9987ea39b52e46e5969f938c3aa9002a4bd4c /src/resources/resourcemanager.cpp
parent8aeb42b16430c85e4bc4052d881b8335d4a2ff36 (diff)
downloadmana-32996cee607c52ecef9be4638df554dd89b39c24.tar.gz
mana-32996cee607c52ecef9be4638df554dd89b39c24.tar.bz2
mana-32996cee607c52ecef9be4638df554dd89b39c24.tar.xz
mana-32996cee607c52ecef9be4638df554dd89b39c24.zip
Fixed wallpaper prescaling issues
Image::SDLgetScaledImage was changed so that it tries to find an existing scaled version of the image first, and generates it when none exists. When it needs to generate one, this resource is added to the resource manager, partly to avoid duplicating the work later but mainly to keep memory management straightforward. This function also used to leak the scaled SDL_Surface since it wrongly assumed that Image::load would free it. To avoid filling up the memory with scaled wallpapers that are waiting 30 seconds until they will be deleted, the Resource::decRef function was extended with a parameter that allows telling it what to do with orphans. Calling decRef with Resource::DeleteImmediately will delete the resource immediately in case the resource is orphaned. Reviewed-by: Yohann Ferreira
Diffstat (limited to 'src/resources/resourcemanager.cpp')
-rw-r--r--src/resources/resourcemanager.cpp28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index 15d7c8eb..a9e7e565 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -117,9 +117,11 @@ void ResourceManager::cleanOrphans()
timeval tv;
gettimeofday(&tv, NULL);
// Delete orphaned resources after 30 seconds.
- time_t oldest = tv.tv_sec, threshold = oldest - 30;
+ time_t oldest = tv.tv_sec;
+ time_t threshold = oldest - 30;
- if (mOrphanedResources.empty() || mOldestOrphan >= threshold) return;
+ if (mOrphanedResources.empty() || mOldestOrphan >= threshold)
+ return;
ResourceIterator iter = mOrphanedResources.begin();
while (iter != mOrphanedResources.end())
@@ -128,7 +130,8 @@ void ResourceManager::cleanOrphans()
time_t t = res->mTimeStamp;
if (t >= threshold)
{
- if (t < oldest) oldest = t;
+ if (t < oldest)
+ oldest = t;
++iter;
}
else
@@ -234,6 +237,17 @@ bool ResourceManager::addResource(const std::string &idPath,
return false;
}
+Resource *ResourceManager::get(const std::string &idPath)
+{
+ ResourceIterator resIter = mResources.find(idPath);
+ if (resIter != mResources.end())
+ {
+ resIter->second->incRef();
+ return resIter->second;
+ }
+ return 0;
+}
+
Resource *ResourceManager::get(const std::string &idPath, generator fun,
void *data)
{
@@ -392,12 +406,18 @@ void ResourceManager::release(Resource *res)
time_t timestamp = tv.tv_sec;
res->mTimeStamp = timestamp;
- if (mOrphanedResources.empty()) mOldestOrphan = timestamp;
+ if (mOrphanedResources.empty())
+ mOldestOrphan = timestamp;
mOrphanedResources.insert(*resIter);
mResources.erase(resIter);
}
+void ResourceManager::remove(Resource *res)
+{
+ mResources.erase(res->mIdPath);
+}
+
ResourceManager *ResourceManager::getInstance()
{
// Create a new instance if necessary.