summaryrefslogtreecommitdiff
path: root/src/resources/resourcemanager.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-07-24 00:13:24 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-07-24 00:13:24 +0000
commit4789ebb11407eb9402c385f3cc6fa242d91214c2 (patch)
tree6bf5bed52073ae19ef4b5b08e7fe92af603f04d8 /src/resources/resourcemanager.cpp
parent1b7ff9ad4160fd0ff79d2da658eca8d8c8abf815 (diff)
downloadmana-4789ebb11407eb9402c385f3cc6fa242d91214c2.tar.gz
mana-4789ebb11407eb9402c385f3cc6fa242d91214c2.tar.bz2
mana-4789ebb11407eb9402c385f3cc6fa242d91214c2.tar.xz
mana-4789ebb11407eb9402c385f3cc6fa242d91214c2.zip
Implemented caching of spritesets, including a lot of cleanups to the new
animation system. Action now refers to the Spriteset directly and AnimatedSprite refers to the current Action directly instead of using the std::map with a std::string constantly. Some methods and parameters are marked as const. The READ_PROP macro was replaced by static methods. Warnings are logged when unnamed actions are defined or when actions refer to undefined imagesets. Code is more tolerant towards missing actions.
Diffstat (limited to 'src/resources/resourcemanager.cpp')
-rw-r--r--src/resources/resourcemanager.cpp74
1 files changed, 50 insertions, 24 deletions
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index a6dc692b..26899d77 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -30,11 +30,10 @@
#include "image.h"
#include "music.h"
#include "soundeffect.h"
+#include "spriteset.h"
#include "../log.h"
-#include "../graphic/spriteset.h"
-
ResourceManager *ResourceManager::instance = NULL;
@@ -44,25 +43,39 @@ ResourceManager::ResourceManager()
ResourceManager::~ResourceManager()
{
- // Create our resource iterator.
+ // Release any remaining spritesets first because they depend on images
ResourceIterator iter = mResources.begin();
+ while (iter != mResources.end())
+ {
+ if (dynamic_cast<Spriteset*>(iter->second) != NULL)
+ {
+ cleanUp(iter->second);
+ ResourceIterator toErase = iter;
+ ++iter;
+ mResources.erase(toErase);
+ }
+ else
+ {
+ ++iter;
+ }
+ }
- // Iterate through and release references until objects are deleted.
+ // Release remaining resources, logging the number of dangling references.
while (!mResources.empty())
{
- Resource *res = mResources.begin()->second;
- std::string id = res->getIdPath();
- int references = 0;
-
- references += res->mRefCount;
- release(res->mIdPath);
- delete res;
-
- logger->log("ResourceManager::~ResourceManager() cleaned up %d "
- "references to %s", references, id.c_str());
+ cleanUp(mResources.begin()->second);
+ mResources.erase(mResources.begin());
}
}
+void
+ResourceManager::cleanUp(Resource *res)
+{
+ logger->log("ResourceManager::~ResourceManager() cleaning up %d "
+ "references to %s", res->mRefCount, res->mIdPath.c_str());
+ delete res;
+}
+
bool
ResourceManager::setWriteDir(const std::string &path)
{
@@ -151,42 +164,55 @@ ResourceManager::get(const E_RESOURCE_TYPE &type, const std::string &idPath)
mResources[idPath] = resource;
}
- // Return NULL if the object could not be created.
+ // Returns NULL if the object could not be created.
return resource;
}
Image*
ResourceManager::getImage(const std::string &idPath)
{
- return (Image*)get(IMAGE, idPath);
+ return dynamic_cast<Image*>(get(IMAGE, idPath));
}
Music*
ResourceManager::getMusic(const std::string &idPath)
{
- return (Music*)get(MUSIC, idPath);
+ return dynamic_cast<Music*>(get(MUSIC, idPath));
}
SoundEffect*
ResourceManager::getSoundEffect(const std::string &idPath)
{
- return (SoundEffect*)get(SOUND_EFFECT, idPath);
+ return dynamic_cast<SoundEffect*>(get(SOUND_EFFECT, idPath));
}
-Spriteset* ResourceManager::createSpriteset(const std::string &imagePath,
- int w, int h)
+Spriteset*
+ResourceManager::getSpriteset(const std::string &imagePath, int w, int h)
{
- Image *img;
+ std::stringstream ss;
+ ss << imagePath << "[" << w << "x" << h << "]";
+ const std::string idPath = ss.str();
+
+ ResourceIterator resIter = mResources.find(idPath);
+
+ if (resIter != mResources.end()) {
+ resIter->second->incRef();
+ return dynamic_cast<Spriteset*>(resIter->second);
+ }
+
+ Image *img = getImage(imagePath);
- if (!(img = getImage(imagePath))) {
+ if (!img) {
return NULL;
}
- Spriteset *result = new Spriteset(img, w, h);
+ Spriteset *spriteset = new Spriteset(idPath, img, w, h);
+ spriteset->incRef();
+ mResources[idPath] = spriteset;
img->decRef();
- return result;
+ return spriteset;
}
void