diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-07-24 00:13:24 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-07-24 00:13:24 +0000 |
commit | 4789ebb11407eb9402c385f3cc6fa242d91214c2 (patch) | |
tree | 6bf5bed52073ae19ef4b5b08e7fe92af603f04d8 /src/resources | |
parent | 1b7ff9ad4160fd0ff79d2da658eca8d8c8abf815 (diff) | |
download | mana-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')
-rw-r--r-- | src/resources/mapreader.cpp | 3 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 74 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 8 | ||||
-rw-r--r-- | src/resources/spriteset.cpp | 65 | ||||
-rw-r--r-- | src/resources/spriteset.h | 67 |
5 files changed, 190 insertions, 27 deletions
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 48f23aea..af79480a 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -28,14 +28,13 @@ #include "resourcemanager.h" #include "image.h" +#include "spriteset.h" #include "../base64.h" #include "../log.h" #include "../map.h" #include "../tileset.h" -#include "../graphic/spriteset.h" - const unsigned int DEFAULT_TILE_WIDTH = 32; const unsigned int DEFAULT_TILE_HEIGHT = 32; 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 diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 8d60ae8c..f254a8b4 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -136,7 +136,7 @@ class ResourceManager * Creates a spriteset based on the image referenced by the given * path and the supplied sprite sizes */ - Spriteset* createSpriteset(const std::string &imagePath, int w, int h); + Spriteset* getSpriteset(const std::string &imagePath, int w, int h); /** * Releases a resource, removing it from the set of loaded resources. @@ -177,6 +177,12 @@ class ResourceManager deleteInstance(); private: + /** + * Deletes the resource after logging a cleanup message. + */ + static void + cleanUp(Resource *resource); + static ResourceManager *instance; typedef std::map<std::string, Resource*> Resources; typedef Resources::iterator ResourceIterator; diff --git a/src/resources/spriteset.cpp b/src/resources/spriteset.cpp new file mode 100644 index 00000000..9b09f1e5 --- /dev/null +++ b/src/resources/spriteset.cpp @@ -0,0 +1,65 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "spriteset.h" + +#include "../log.h" + +#include "image.h" + +#include "../utils/dtor.h" + +Spriteset::Spriteset(const std::string& idPath, + Image *img, + int width, int height): + Resource(idPath) +{ + for (int y = 0; y + height <= img->getHeight(); y += height) + { + for (int x = 0; x + width <= img->getWidth(); x += width) + { + mSpriteset.push_back(img->getSubImage(x, y, width, height)); + } + } + mWidth = width; + mHeight = height; +} + +Spriteset::~Spriteset() +{ + for_each(mSpriteset.begin(), mSpriteset.end(), make_dtor(mSpriteset)); +} + +Image* +Spriteset::get(size_type i) +{ + if (i > mSpriteset.size()) + { + logger->log("Warning: Sprite #%i does not exist in this spriteset", i); + return NULL; + } + else + { + return mSpriteset[i]; + } +} diff --git a/src/resources/spriteset.h b/src/resources/spriteset.h new file mode 100644 index 00000000..c51e6a75 --- /dev/null +++ b/src/resources/spriteset.h @@ -0,0 +1,67 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMW_SPRITESET_H +#define _TMW_SPRITESET_H + +#include <vector> + +#include "resource.h" + +class Image; + + +/** + * Stores a complete set of sprites. + */ +class Spriteset : public Resource +{ + public: + /* + * Cuts the passed image in a grid of sub images. + */ + Spriteset(const std::string& idPath, Image *img, int w, int h); + + /** + * Destructor. + */ + ~Spriteset(); + + int getWidth() { return mWidth; }; + + int getHeight() { return mHeight; }; + + typedef std::vector<Image*>::size_type size_type; + Image* get(size_type i); + + size_type size() { return mSpriteset.size(); } + + private: + // Vector storing the whole spriteset. + std::vector<Image*> mSpriteset; + // Height and width of the images in the spriteset + int mHeight; + int mWidth; +}; + +#endif |