diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-06-20 00:34:39 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-06-21 00:59:10 +0300 |
commit | aa68511ad3d339be8c8f42fc6c083b696d8e687b (patch) | |
tree | 1bf138439c28b4a879c6b35e4361801bd5e1d246 /src/resources | |
parent | 12002b81544038bc5855189c74aca761d0c08f1b (diff) | |
download | plus-aa68511ad3d339be8c8f42fc6c083b696d8e687b.tar.gz plus-aa68511ad3d339be8c8f42fc6c083b696d8e687b.tar.bz2 plus-aa68511ad3d339be8c8f42fc6c083b696d8e687b.tar.xz plus-aa68511ad3d339be8c8f42fc6c083b696d8e687b.zip |
Add delayed images load.
Can work for now only in OpenGL modes.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/resourcemanager.cpp | 69 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 18 |
2 files changed, 83 insertions, 4 deletions
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 37975cf16..16214661c 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -22,6 +22,7 @@ #include "resources/resourcemanager.h" +#include "animationdelayload.h" #include "client.h" #include "configuration.h" #include "logger.h" @@ -52,6 +53,7 @@ #include "debug.h" ResourceManager *ResourceManager::instance = nullptr; +DelayedAnim ResourceManager::mDelayedAnimations; ResourceManager::ResourceManager() : mOldestOrphan(0), @@ -354,8 +356,15 @@ bool ResourceManager::addResource(const std::string &idPath, return false; } -Resource *ResourceManager::get(const std::string &idPath, generator fun, - void *data) +Resource *ResourceManager::getFromCache(const std::string &filename, + int variant) +{ + std::stringstream ss; + ss << filename << "[" << variant << "]"; + return getFromCache(ss.str()); +} + +Resource *ResourceManager::getFromCache(const std::string &idPath) { // Check if the id exists, and return the value if it does. ResourceIterator resIter = mResources.find(idPath); @@ -376,8 +385,17 @@ Resource *ResourceManager::get(const std::string &idPath, generator fun, res->incRef(); return res; } + return nullptr; +} - Resource *resource = fun(data); +Resource *ResourceManager::get(const std::string &idPath, generator fun, + void *data) +{ + Resource *resource = getFromCache(idPath); + if (resource) + return resource; + + resource = fun(data); if (resource) { @@ -745,3 +763,48 @@ Image *ResourceManager::getRescaled(Image *image, int width, int height) Image *img = static_cast<Image*>(get(idPath, RescaledLoader::load, &rl)); return img; } + +void ResourceManager::delayedLoad() +{ + static int loadTime = 0; + if (loadTime < cur_time) + { +// loadTime = tick_time + 10; + loadTime = tick_time; + + int k = 0; + DelayedAnimIter it = mDelayedAnimations.begin(); + DelayedAnimIter it_end = mDelayedAnimations.end(); + while (it != it_end && k < 1) + { + (*it)->load(); + AnimationDelayLoad *tmp = *it; + it = mDelayedAnimations.erase(it); + delete tmp; + k ++; + } + const int time2 = tick_time; +// if (time2 != loadTime) +// { +// logger->log("diff %d", time2 - loadTime); +// } + if (time2 > loadTime) + loadTime = time2 + (time2 - loadTime) * 2 + 10; +// loadTime += 10 - time2; + else + loadTime = time2 + 3; + } +} + +void ResourceManager::removeDelayLoad(AnimationDelayLoad *delayedLoad) +{ + for(DelayedAnimIter it = mDelayedAnimations.begin(), + it_end = mDelayedAnimations.end(); it != it_end; ++ it) + { + if (*it == delayedLoad) + { + mDelayedAnimations.erase(it); + return; + } + } +} diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index ca6bdb373..4900b7ce3 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -28,9 +28,11 @@ #include "utils/stringvector.h" #include <ctime> +#include <list> #include <map> #include <set> +class AnimationDelayLoad; class Image; class ImageSet; class Music; @@ -41,6 +43,9 @@ class SpriteDef; struct SDL_Surface; struct SDL_RWops; +typedef std::list<AnimationDelayLoad*> DelayedAnim; +typedef DelayedAnim::iterator DelayedAnimIter; + /** * A class for loading and managing resources. */ @@ -139,6 +144,10 @@ class ResourceManager */ Resource *get(const std::string &idPath, generator fun, void *data); + Resource *getFromCache(const std::string &idPath); + + Resource *getFromCache(const std::string &filename, int variant); + /** * Loads a resource from a file and adds it to the resource map. * @@ -269,13 +278,19 @@ class ResourceManager void cleanOrphans(bool always = false); + static void addDelayedAnimation(AnimationDelayLoad *animation) + { mDelayedAnimations.push_back(animation); } + + static void delayedLoad(); + + static void removeDelayLoad(AnimationDelayLoad *delayedLoad); + private: /** * Deletes the resource after logging a cleanup message. */ static void cleanUp(Resource *resource); - static ResourceManager *instance; std::set<SDL_Surface*> deletedSurfaces; Resources mResources; @@ -284,6 +299,7 @@ class ResourceManager std::string mSelectedSkin; std::string mSkinName; bool mDestruction; + static DelayedAnim mDelayedAnimations; }; #endif |