diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-11-20 12:27:56 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-11-20 12:27:56 +0000 |
commit | ca58ec1faedca0081ecd233f2cefa1ba783cebf4 (patch) | |
tree | 11216c01bb8867a9bd4ad6d4b8bdefc3c61a952a /src | |
parent | 5a7abdafdac8f6ddd7972cadbc7e20563a0a29fc (diff) | |
download | mana-ca58ec1faedca0081ecd233f2cefa1ba783cebf4.tar.gz mana-ca58ec1faedca0081ecd233f2cefa1ba783cebf4.tar.bz2 mana-ca58ec1faedca0081ecd233f2cefa1ba783cebf4.tar.xz mana-ca58ec1faedca0081ecd233f2cefa1ba783cebf4.zip |
Merged revisions 3642,3662-3664,3667 via svnmerge from
https://themanaworld.svn.sourceforge.net/svnroot/themanaworld/tmw/trunk
........
r3642 | gmelquio | 2007-10-19 19:46:46 +0200 (Fri, 19 Oct 2007) | 1 line
Factored code between resource handlers. Implemented failure-friendly sprite loader.
........
r3662 | gmelquio | 2007-10-21 21:01:16 +0200 (Sun, 21 Oct 2007) | 1 line
Added persistent positioning.
........
r3663 | gmelquio | 2007-10-21 21:03:43 +0200 (Sun, 21 Oct 2007) | 1 line
Fixed missing pixels at bottom and right.
........
r3664 | gmelquio | 2007-10-21 21:05:56 +0200 (Sun, 21 Oct 2007) | 1 line
Changed to use default values when restoring missing settings.
........
r3667 | gmelquio | 2007-10-21 22:09:08 +0200 (Sun, 21 Oct 2007) | 1 line
Fixed invisible text in dropboxes and shopboxes.
........
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/listbox.cpp | 1 | ||||
-rw-r--r-- | src/gui/menuwindow.cpp | 9 | ||||
-rw-r--r-- | src/gui/ministatus.cpp | 1 | ||||
-rw-r--r-- | src/gui/shoplistbox.cpp | 1 | ||||
-rw-r--r-- | src/gui/trade.cpp | 2 | ||||
-rw-r--r-- | src/gui/window.cpp | 15 | ||||
-rw-r--r-- | src/gui/window.h | 7 | ||||
-rw-r--r-- | src/resources/image.cpp | 35 | ||||
-rw-r--r-- | src/resources/image.h | 12 | ||||
-rw-r--r-- | src/resources/imageloader.cpp | 2 | ||||
-rw-r--r-- | src/resources/imageset.cpp | 5 | ||||
-rw-r--r-- | src/resources/imageset.h | 2 | ||||
-rw-r--r-- | src/resources/music.cpp | 23 | ||||
-rw-r--r-- | src/resources/music.h | 8 | ||||
-rw-r--r-- | src/resources/resource.cpp | 6 | ||||
-rw-r--r-- | src/resources/resource.h | 2 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 144 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 34 | ||||
-rw-r--r-- | src/resources/soundeffect.cpp | 14 | ||||
-rw-r--r-- | src/resources/soundeffect.h | 6 | ||||
-rw-r--r-- | src/resources/spritedef.cpp | 47 | ||||
-rw-r--r-- | src/resources/spritedef.h | 27 | ||||
-rw-r--r-- | src/tileset.h | 2 |
23 files changed, 176 insertions, 229 deletions
diff --git a/src/gui/listbox.cpp b/src/gui/listbox.cpp index a7f6df8d..94e8b38c 100644 --- a/src/gui/listbox.cpp +++ b/src/gui/listbox.cpp @@ -52,6 +52,7 @@ void ListBox::draw(gcn::Graphics *graphics) } // Draw the list elements + graphics->setColor(gcn::Color(0, 0, 0)); for (int i = 0, y = 0; i < mListModel->getNumberOfElements(); ++i, y += fontHeight) diff --git a/src/gui/menuwindow.cpp b/src/gui/menuwindow.cpp index e6b35f5a..2043b7d1 100644 --- a/src/gui/menuwindow.cpp +++ b/src/gui/menuwindow.cpp @@ -66,22 +66,19 @@ MenuWindow::MenuWindow(): "Setup", 0 }; - int x = 0, y = 3, h = 0; + int x = 0, h = 0; for (const char **curBtn = buttonNames; *curBtn; curBtn++) { gcn::Button *btn = new Button(*curBtn, *curBtn, &listener); - btn->setPosition(x, y); + btn->setPosition(x, 0); add(btn); x += btn->getWidth() + 3; h = btn->getHeight(); } + setPosition(windowContainer->getWidth() - x - 3, 3); setContentSize(x - 3, h); - setDefaultSize(windowContainer->getWidth() - getWidth() - 1, - 0, - x - 3, - y + h); } void MenuWindow::draw(gcn::Graphics *graphics) diff --git a/src/gui/ministatus.cpp b/src/gui/ministatus.cpp index 694a6f32..8f121368 100644 --- a/src/gui/ministatus.cpp +++ b/src/gui/ministatus.cpp @@ -78,6 +78,7 @@ MiniStatusWindow::MiniStatusWindow(): setDefaultSize(0, 0, mXpBar->getX() + mXpBar->getWidth(), mXpBar->getY() + mXpBar->getHeight()); + loadWindowState(); } void MiniStatusWindow::update() diff --git a/src/gui/shoplistbox.cpp b/src/gui/shoplistbox.cpp index 4efd6e64..610ad76c 100644 --- a/src/gui/shoplistbox.cpp +++ b/src/gui/shoplistbox.cpp @@ -95,6 +95,7 @@ void ShopListBox::draw(gcn::Graphics *gcnGraphics) graphics->drawImage(icon, 1, y); } } + graphics->setColor(gcn::Color(0, 0, 0)); graphics->drawText(mListModel->getElementAt(i), ITEM_ICON_SIZE + 5, y + (ITEM_ICON_SIZE - getFont()->getHeight()) / 2); } diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index 06daf3ef..744afe1e 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -99,6 +99,8 @@ TradeWindow::TradeWindow(Network *network): add(mMoneyField); add(mMoneyLabel); + loadWindowState(); + gcn::Rectangle area = getChildrenArea(); int width = area.width; int height = area.height; diff --git a/src/gui/window.cpp b/src/gui/window.cpp index 8317684a..90031545 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -526,17 +526,21 @@ Window::loadWindowState() { const std::string &name = mWindowName; - setPosition((int) config.getValue(name + "WinX", getX()), - (int) config.getValue(name + "WinY", getY())); + setPosition((int) config.getValue(name + "WinX", mDefaultX), + (int) config.getValue(name + "WinY", mDefaultY)); if (mGrip) { - setSize((int) config.getValue(name + "WinWidth", getWidth()), - (int) config.getValue(name + "WinHeight", getHeight())); + setSize((int) config.getValue(name + "WinWidth", mDefaultWidth), + (int) config.getValue(name + "WinHeight", mDefaultHeight)); const gcn::Rectangle area = getChildrenArea(); mChrome->setSize(area.width, area.height); } + else + { + setContentSize(mDefaultWidth, mDefaultHeight); + } } void Window::setDefaultSize(int defaultX, int defaultY, @@ -546,9 +550,6 @@ void Window::setDefaultSize(int defaultX, int defaultY, mDefaultY = defaultY; mDefaultWidth = defaultWidth; mDefaultHeight = defaultHeight; - - setPosition(mDefaultX, mDefaultY); - setContentSize(mDefaultWidth, mDefaultHeight); } void Window::resetToDefaultSize() diff --git a/src/gui/window.h b/src/gui/window.h index 8e3fdef9..18f97cec 100644 --- a/src/gui/window.h +++ b/src/gui/window.h @@ -255,10 +255,9 @@ class Window : public gcn::Window getWindowName() { return mWindowName; } /** - * Read the x, y, and width and height for resizables in the config - * based on the name of the window. - * That function let the values set with set{X, Y, Height, width}() - * if no config value is found. + * Reads the position (and the size for resizable windows) in the + * configuration based on the given string. + * Uses the default values when config values are missing. * Don't forget to set these default values and resizable before * calling this function. */ diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 995e34b0..7a394edb 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -33,8 +33,7 @@ int Image::mTextureType = 0; int Image::mTextureSize = 0; #endif -Image::Image(const std::string &idPath, SDL_Surface *image): - Resource(idPath), +Image::Image(SDL_Surface *image): #ifdef USE_OPENGL mGLImage(0), #endif @@ -48,9 +47,8 @@ Image::Image(const std::string &idPath, SDL_Surface *image): } #ifdef USE_OPENGL -Image::Image(const std::string &idPath, GLuint glimage, int width, int height, +Image::Image(GLuint glimage, int width, int height, int texWidth, int texHeight): - Resource(idPath), mGLImage(glimage), mTexWidth(texWidth), mTexHeight(texHeight), @@ -69,36 +67,25 @@ Image::~Image() unload(); } -Image* Image::load(void *buffer, unsigned int bufferSize, - const std::string &idPath) +Resource *Image::load(void *buffer, unsigned bufferSize) { // Load the raw file data from the buffer in an RWops structure SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); - SDL_Surface *tmpImage; + SDL_Surface *tmpImage = IMG_Load_RW(rw, 1); - // Use SDL_Image to load the raw image data and have it free the data - if (!idPath.compare(idPath.length() - 4, 4, ".tga")) + if (!tmpImage) { - tmpImage = IMG_LoadTyped_RW(rw, 1, const_cast<char*>("TGA")); - } - else - { - tmpImage = IMG_Load_RW(rw, 1); - } - - if (!tmpImage) { logger->log("Error, image load failed: %s", IMG_GetError()); return NULL; } - Image *image = load(tmpImage, idPath); + Image *image = load(tmpImage); SDL_FreeSurface(tmpImage); - return image; } -Image *Image::load(SDL_Surface *tmpImage, std::string const &idPath) +Image *Image::load(SDL_Surface *tmpImage) { #ifdef USE_OPENGL if (mUseOpenGL) @@ -198,7 +185,7 @@ Image *Image::load(SDL_Surface *tmpImage, std::string const &idPath) return NULL; } - return new Image(idPath, texture, width, height, realWidth, realHeight); + return new Image(texture, width, height, realWidth, realHeight); } #endif @@ -238,7 +225,7 @@ Image *Image::load(SDL_Surface *tmpImage, std::string const &idPath) return NULL; } - return new Image(idPath, image); + return new Image(image); } void Image::unload() @@ -324,7 +311,7 @@ Image::powerOfTwo(int input) SubImage::SubImage(Image *parent, SDL_Surface *image, int x, int y, int width, int height): - Image("", image), mParent(parent) + Image(image), mParent(parent) { mParent->incRef(); @@ -339,7 +326,7 @@ SubImage::SubImage(Image *parent, SDL_Surface *image, SubImage::SubImage(Image *parent, GLuint image, int x, int y, int width, int height, int texWidth, int texHeight): - Image("", image, width, height, texWidth, texHeight), mParent(parent) + Image(image, width, height, texWidth, texHeight), mParent(parent) { mParent->incRef(); diff --git a/src/resources/image.h b/src/resources/image.h index 34515dda..485ca227 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -62,18 +62,16 @@ class Image : public Resource * * @param buffer The memory buffer containing the image data. * @param bufferSize The size of the memory buffer in bytes. - * @param idPath The path identifying the resource. * * @return <code>NULL</code> if the an error occurred, a valid pointer * otherwise. */ - static Image* - load(void* buffer, unsigned int bufferSize, const std::string &idPath); + static Resource *load(void *buffer, unsigned bufferSize); /** * Loads an image from an SDL surface. */ - static Image *load(SDL_Surface *, std::string const &idPath); + static Image *load(SDL_Surface *); /** * Frees the resources created by SDL. @@ -129,8 +127,8 @@ class Image : public Resource * Constructor. */ #ifdef USE_OPENGL - Image(const std::string &idPath, GLuint glimage, int width, int height, - int texWidth, int texHeight); + Image(GLuint glimage, int width, int height, + int texWidth, int texHeight); /** * Returns the first power of two equal or bigger than the input. @@ -138,7 +136,7 @@ class Image : public Resource static int powerOfTwo(int input); #endif - Image(const std::string &idPath, SDL_Surface *image); + Image(SDL_Surface *image); SDL_Rect mBounds; bool mLoaded; diff --git a/src/resources/imageloader.cpp b/src/resources/imageloader.cpp index 81a5cf63..d6539b05 100644 --- a/src/resources/imageloader.cpp +++ b/src/resources/imageloader.cpp @@ -85,7 +85,7 @@ void ProxyImage::convertToDisplayFormat() font. Get rid of them. */ SDL_SetColorKey(mSDLImage, SDL_SRCCOLORKEY, SDL_MapRGB(mSDLImage->format, 255, 0, 255)); - mImage = ::Image::load(mSDLImage, std::string()); + mImage = ::Image::load(mSDLImage); SDL_FreeSurface(mSDLImage); mSDLImage = NULL; } diff --git a/src/resources/imageset.cpp b/src/resources/imageset.cpp index 677c024b..565e8860 100644 --- a/src/resources/imageset.cpp +++ b/src/resources/imageset.cpp @@ -29,10 +29,7 @@ #include "../utils/dtor.h" -ImageSet::ImageSet(const std::string& idPath, - Image *img, - int width, int height): - Resource(idPath) +ImageSet::ImageSet(Image *img, int width, int height) { for (int y = 0; y + height <= img->getHeight(); y += height) { diff --git a/src/resources/imageset.h b/src/resources/imageset.h index 3469a3bb..15c21d90 100644 --- a/src/resources/imageset.h +++ b/src/resources/imageset.h @@ -40,7 +40,7 @@ class ImageSet : public Resource /* * Cuts the passed image in a grid of sub images. */ - ImageSet(const std::string &idPath, Image *img, int w, int h); + ImageSet(Image *img, int w, int h); /** * Destructor. diff --git a/src/resources/music.cpp b/src/resources/music.cpp index 09e2752a..161d8b01 100644 --- a/src/resources/music.cpp +++ b/src/resources/music.cpp @@ -23,8 +23,9 @@ #include "music.h" -Music::Music(const std::string &idPath, Mix_Chunk *music): - Resource(idPath), +#include "../log.h" + +Music::Music(Mix_Chunk *music): mChunk(music), mChannel(-1) { @@ -36,20 +37,24 @@ Music::~Music() Mix_FreeChunk(mChunk); } -Music* -Music::load(void *buffer, unsigned int bufferSize, const std::string &idPath) +Resource *Music::load(void *buffer, unsigned bufferSize) { // Load the raw file data from the buffer in an RWops structure SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); // Use Mix_LoadMUS to load the raw music data //Mix_Music* music = Mix_LoadMUS_RW(rw); Need to be implemeted - Mix_Chunk *tmpMusic = Mix_LoadWAV_RW(rw, 0); - - // Now free the SDL_RWops data - SDL_FreeRW(rw); + Mix_Chunk *tmpMusic = Mix_LoadWAV_RW(rw, 1); - return new Music(idPath, tmpMusic); + if (tmpMusic) + { + return new Music(tmpMusic); + } + else + { + logger->log("Error, failed to load music: %s", Mix_GetError()); + return NULL; + } } bool diff --git a/src/resources/music.h b/src/resources/music.h index 2888eaa0..72e76295 100644 --- a/src/resources/music.h +++ b/src/resources/music.h @@ -40,17 +40,15 @@ class Music : public Resource virtual ~Music(); /** - * Loads an image from a buffer in memory. + * Loads a music from a buffer in memory. * * @param buffer The memory buffer containing the music data. * @param bufferSize The size of the memory buffer in bytes. - * @param idPath The path identifying the resource. * * @return <code>NULL</code> if the an error occurred, a valid pointer * otherwise. */ - static Music* - load(void* buffer, unsigned int bufferSize, const std::string &idPath); + static Resource *load(void *buffer, unsigned bufferSize); /** * Plays the music. @@ -73,7 +71,7 @@ class Music : public Resource /** * Constructor. */ - Music(const std::string &idPath, Mix_Chunk *music); + Music(Mix_Chunk *music); //Mix_Music *music; Mix_Chunk *mChunk; diff --git a/src/resources/resource.cpp b/src/resources/resource.cpp index bd8cd437..07f31b8f 100644 --- a/src/resources/resource.cpp +++ b/src/resources/resource.cpp @@ -27,12 +27,6 @@ #include "resourcemanager.h" - -Resource::Resource(const std::string &idPath): - mRefCount(0), mIdPath(idPath) -{ -} - Resource::~Resource() { } diff --git a/src/resources/resource.h b/src/resources/resource.h index d8b93e2d..7c39f176 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -37,7 +37,7 @@ class Resource /** * Constructor */ - Resource(const std::string &idPath); + Resource(): mRefCount(0) {} /** * Increments the internal reference count. diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 5e3f3bc7..e507835a 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -157,56 +157,23 @@ ResourceManager::isDirectory(const std::string &path) return PHYSFS_isDirectory(path.c_str()); } -Resource* -ResourceManager::get(const E_RESOURCE_TYPE &type, const std::string &idPath) +Resource *ResourceManager::get(std::string const &idPath, generator fun, void *data) { // Check if the id exists, and return the value if it does. ResourceIterator resIter = mResources.find(idPath); - if (resIter != mResources.end() && resIter->second) { + if (resIter != mResources.end()) + { resIter->second->incRef(); return resIter->second; } - int fileSize; - void *buffer = loadFile(idPath, fileSize); - if (!buffer) { - return NULL; - } - - Resource *resource = NULL; - - // Create an object of the specified type. - switch (type) - { - case MUSIC: - { - // Let the music class load it - resource = Music::load(buffer, fileSize, idPath); - } - break; - case IMAGE: - { - // Let the image class load it - resource = Image::load(buffer, fileSize, idPath); - } - break; - case SOUND_EFFECT: - { - // Let the sound effect class load it - resource = SoundEffect::load(buffer, fileSize, idPath); - } - break; - default: - /* Nothing to do here, just avoid compiler warnings... */ - break; - } - - free(buffer); + Resource *resource = fun(data); if (resource) { resource->incRef(); + resource->mIdPath = idPath; mResources[idPath] = resource; } @@ -214,79 +181,90 @@ ResourceManager::get(const E_RESOURCE_TYPE &type, const std::string &idPath) return resource; } +struct ResourceLoader +{ + ResourceManager *manager; + std::string path; + ResourceManager::loader fun; + static Resource *load(void *v) + { + ResourceLoader *l = static_cast< ResourceLoader * >(v); + int fileSize; + void *buffer = l->manager->loadFile(l->path, fileSize); + if (!buffer) return NULL; + Resource *res = l->fun(buffer, fileSize); + free(buffer); + return res; + } +}; + +Resource *ResourceManager::load(std::string const &path, loader fun) +{ + ResourceLoader l = { this, path, fun }; + return get(path, ResourceLoader::load, &l); +} + Image* ResourceManager::getImage(const std::string &idPath) { - return dynamic_cast<Image*>(get(IMAGE, idPath)); + return static_cast<Image*>(load(idPath, Image::load)); } Music* ResourceManager::getMusic(const std::string &idPath) { - return dynamic_cast<Music*>(get(MUSIC, idPath)); + return static_cast<Music*>(load(idPath, Music::load)); } SoundEffect* ResourceManager::getSoundEffect(const std::string &idPath) { - return dynamic_cast<SoundEffect*>(get(SOUND_EFFECT, idPath)); + return static_cast<SoundEffect*>(load(idPath, SoundEffect::load)); } +struct ImageSetLoader +{ + ResourceManager *manager; + std::string path; + int w, h; + static Resource *load(void *v) + { + ImageSetLoader *l = static_cast< ImageSetLoader * >(v); + Image *img = l->manager->getImage(l->path); + if (!img) return NULL; + ImageSet *res = new ImageSet(img, l->w, l->h); + img->decRef(); + return res; + } +}; + ImageSet* ResourceManager::getImageSet(const std::string &imagePath, int w, int h) { + ImageSetLoader l = { this, imagePath, w, h }; 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<ImageSet*>(resIter->second); - } - - Image *img = getImage(imagePath); + return static_cast<ImageSet*>(get(ss.str(), ImageSetLoader::load, &l)); +} - if (!img) { - return NULL; +struct SpriteDefLoader +{ + std::string path; + int variant; + static Resource *load(void *v) + { + SpriteDefLoader *l = static_cast< SpriteDefLoader * >(v); + return SpriteDef::load(l->path, l->variant); } - - ImageSet *imageSet = new ImageSet(idPath, img, w, h); - imageSet->incRef(); - mResources[idPath] = imageSet; - - img->decRef(); - - return imageSet; -} +}; SpriteDef* ResourceManager::getSprite(const std::string &path, int variant) { + SpriteDefLoader l = { path, variant }; std::stringstream ss; ss << path << "[" << variant << "]"; - const std::string idPath = ss.str(); - - ResourceIterator resIter = mResources.find(idPath); - - if (resIter != mResources.end()) { - resIter->second->incRef(); - return dynamic_cast<SpriteDef*>(resIter->second); - } - - // FIXME: modify SpriteDef so that it gracefully fails on missing sprite. - if (!exists(path) || isDirectory(path)) - { - logger->log("Failed to load file: %s", path.c_str()); - return NULL; - } - - SpriteDef *sprite = new SpriteDef(idPath, path, variant); - sprite->incRef(); - mResources[idPath] = sprite; - - return sprite; + return static_cast<SpriteDef*>(get(ss.str(), SpriteDefLoader::load, &l)); } void diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index db29e6d3..150b773c 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -42,18 +42,8 @@ class SpriteDef; class ResourceManager { public: - /** - * An enumeration of resource types. - */ - enum E_RESOURCE_TYPE - { - //MAP, - MUSIC, - IMAGE, - //SCRIPT, - //TILESET, - SOUND_EFFECT - }; + typedef Resource *(*loader)(void *, unsigned); + typedef Resource *(*generator)(void *); /** * Constructor. @@ -109,17 +99,25 @@ class ResourceManager isDirectory(const std::string &path); /** - * Creates a resource and adds it to the resource map. The idPath is - * converted into the appropriate path for the current operating system - * and the resource is loaded. + * Creates a resource and adds it to the resource map. * - * @param type The type of resource to load. * @param idPath The resource identifier path. + * @param fun A function for generating the resource. + * @param data Extra parameters for the generator. + * @return A valid resource or <code>NULL</code> if the resource could + * not be generated. + */ + Resource *get(std::string const &idPath, generator fun, void *data); + + /** + * Loads a resource from a file and adds it to the resource map. + * + * @param path The file name. + * @param fun A function for parsing the file. * @return A valid resource or <code>NULL</code> if the resource could * not be loaded. */ - Resource* - get(const E_RESOURCE_TYPE &type, const std::string &idPath); + Resource *load(std::string const &path, loader fun); /** * Convenience wrapper around ResourceManager::get for loading diff --git a/src/resources/soundeffect.cpp b/src/resources/soundeffect.cpp index bb35218e..ec9bc65c 100644 --- a/src/resources/soundeffect.cpp +++ b/src/resources/soundeffect.cpp @@ -25,20 +25,12 @@ #include "../log.h" -SoundEffect::SoundEffect(const std::string &idPath, Mix_Chunk *soundEffect): - Resource(idPath), - mChunk(soundEffect) -{ -} - SoundEffect::~SoundEffect() { Mix_FreeChunk(mChunk); } -SoundEffect* -SoundEffect::load(void *buffer, unsigned int bufferSize, - const std::string &idPath) +Resource *SoundEffect::load(void *buffer, unsigned bufferSize) { // Load the raw file data from the buffer in an RWops structure SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); @@ -48,11 +40,11 @@ SoundEffect::load(void *buffer, unsigned int bufferSize, if (tmpSoundEffect) { - return new SoundEffect(idPath, tmpSoundEffect); + return new SoundEffect(tmpSoundEffect); } else { - logger->log("Error while loading sound effect (%s)", idPath.c_str()); + logger->log("Error, failed to load sound effect: %s", Mix_GetError()); return NULL; } } diff --git a/src/resources/soundeffect.h b/src/resources/soundeffect.h index 007f5a77..866c53ec 100644 --- a/src/resources/soundeffect.h +++ b/src/resources/soundeffect.h @@ -45,13 +45,11 @@ class SoundEffect : public Resource * * @param buffer The memory buffer containing the sample data. * @param bufferSize The size of the memory buffer in bytes. - * @param idPath The path identifying the resource. * * @return <code>NULL</code> if the an error occurred, a valid pointer * otherwise. */ - static SoundEffect* - load(void* buffer, unsigned int bufferSize, const std::string &idPath); + static Resource *load(void *buffer, unsigned bufferSize); /** * Plays the sample. @@ -69,7 +67,7 @@ class SoundEffect : public Resource /** * Constructor. */ - SoundEffect(const std::string &idPath, Mix_Chunk *soundEffect); + SoundEffect(Mix_Chunk *soundEffect): mChunk(soundEffect) {} Mix_Chunk *mChunk; }; diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index 6eb2365b..45a52d2e 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -35,16 +35,6 @@ #include "../utils/xml.h" -SpriteDef::SpriteDef(const std::string &idPath, - const std::string &file, int variant): - Resource(idPath), - mAction(NULL), - mDirection(DIRECTION_DOWN), - mLastTime(0) -{ - load(file, variant); -} - Action* SpriteDef::getAction(SpriteAction action) const { @@ -59,29 +49,29 @@ SpriteDef::getAction(SpriteAction action) const return i->second; } -void -SpriteDef::load(const std::string &animationFile, int variant) +SpriteDef *SpriteDef::load(std::string const &animationFile, int variant) { int size; ResourceManager *resman = ResourceManager::getInstance(); char *data = (char*) resman->loadFile(animationFile.c_str(), size); - if (!data) { - logger->error("Animation: Could not find " + animationFile + "!"); - } + if (!data) return NULL; xmlDocPtr doc = xmlParseMemory(data, size); free(data); - if (!doc) { - logger->error( - "Animation: Error while parsing " + animationFile + " file!"); + if (!doc) + { + logger->log("Error, failed to parse %s.", animationFile.c_str()); + return NULL; } xmlNodePtr rootNode = xmlDocGetRootElement(doc); - if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite")) { - logger->error( - "Animation: this is not a valid " + animationFile + " file!"); + if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite")) + { + logger->log("Error, failed to parse %s.", animationFile.c_str()); + xmlFreeDoc(doc); + return NULL; } // Get the variant @@ -93,25 +83,32 @@ SpriteDef::load(const std::string &animationFile, int variant) variant_offset = variant * XML::getProperty(rootNode, "variant_offset", 0); } + SpriteDef *def = new SpriteDef; + for_each_xml_child_node(node, rootNode) { if (xmlStrEqual(node->name, BAD_CAST "imageset")) { - loadImageSet(node); + def->loadImageSet(node); } else if (xmlStrEqual(node->name, BAD_CAST "action")) { - loadAction(node, variant_offset); + def->loadAction(node, variant_offset); } else if (xmlStrEqual(node->name, BAD_CAST "include")) { - includeSprite(node); + def->includeSprite(node); } } xmlFreeDoc(doc); - // Complete missing actions + def->substituteActions(); + return def; +} + +void SpriteDef::substituteActions() +{ substituteAction(ACTION_STAND, ACTION_DEFAULT); substituteAction(ACTION_WALK, ACTION_STAND); substituteAction(ACTION_WALK, ACTION_RUN); diff --git a/src/resources/spritedef.h b/src/resources/spritedef.h index 55d7f459..121f23cc 100644 --- a/src/resources/spritedef.h +++ b/src/resources/spritedef.h @@ -71,28 +71,26 @@ class SpriteDef : public Resource { public: /** - * Constructor. + * Loads a sprite definition file. */ - SpriteDef(const std::string &idPath, - const std::string &file, int variant); + static SpriteDef *load(std::string const &file, int variant); /** - * Destructor. + * Returns the specified action. */ - ~SpriteDef(); + Action *getAction(SpriteAction action) const; + + private: /** - * Returns the specified action. + * Constructor. */ - Action* - getAction(SpriteAction action) const; + SpriteDef(): mAction(NULL), mDirection(DIRECTION_DOWN), mLastTime(0) {} - private: /** - * Loads a sprite definition file. + * Destructor. */ - void - load(const std::string &file, int variant); + ~SpriteDef(); /** * Loads an imageset element. @@ -121,6 +119,11 @@ class SpriteDef : public Resource includeSprite(xmlNodePtr includeNode); /** + * Complete missing actions by copying existing ones. + */ + void substituteActions(); + + /** * When there are no animations defined for the action "complete", its * animations become a copy of those of the action "with". */ diff --git a/src/tileset.h b/src/tileset.h index 7521a3e5..6af69235 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -36,7 +36,7 @@ class Tileset : public ImageSet * Constructor. */ Tileset(Image *img, int w, int h, int firstGid): - ImageSet("", img, w, h), + ImageSet(img, w, h), mFirstGid(firstGid) { } |