diff options
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/action.h | 2 | ||||
-rw-r--r-- | src/resources/colordb.cpp | 65 | ||||
-rw-r--r-- | src/resources/colordb.h | 4 | ||||
-rw-r--r-- | src/resources/emotedb.cpp | 5 | ||||
-rw-r--r-- | src/resources/image.cpp | 49 | ||||
-rw-r--r-- | src/resources/image.h | 23 | ||||
-rw-r--r-- | src/resources/imageset.h | 4 | ||||
-rw-r--r-- | src/resources/iteminfo.h | 5 | ||||
-rw-r--r-- | src/resources/music.cpp | 51 | ||||
-rw-r--r-- | src/resources/music.h | 22 | ||||
-rw-r--r-- | src/resources/resource.h | 2 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 39 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 10 | ||||
-rw-r--r-- | src/resources/soundeffect.cpp | 8 | ||||
-rw-r--r-- | src/resources/soundeffect.h | 8 |
15 files changed, 146 insertions, 151 deletions
diff --git a/src/resources/action.h b/src/resources/action.h index 1e3965363..3951cc02c 100644 --- a/src/resources/action.h +++ b/src/resources/action.h @@ -43,7 +43,7 @@ class Action Animation *getAnimation(int direction) const; - unsigned getNumber() + unsigned getNumber() const { return mNumber; } void setNumber(unsigned n) diff --git a/src/resources/colordb.cpp b/src/resources/colordb.cpp index 1ffe507b2..2ddf27ac3 100644 --- a/src/resources/colordb.cpp +++ b/src/resources/colordb.cpp @@ -30,7 +30,7 @@ namespace { - ColorDB::Colors mHairColors; + int mHairColorsSize = 0; bool mLoaded = false; std::string mFail = "#ffffff"; ColorDB::ColorLists mColorLists; @@ -44,10 +44,22 @@ void ColorDB::load() loadHair(); if (serverVersion >= 1) loadColorLists(); + + ColorListsIterator it = mColorLists.find("hair"); + if (it != mColorLists.end()) + mHairColorsSize = (*it).second.size(); + else + mHairColorsSize = 0; } void ColorDB::loadHair() { + std::map <int, ItemColor> colors; + ColorListsIterator it = mColorLists.find("hair"); + + if (it != mColorLists.end()) + colors = it->second; + XML::Document *doc = new XML::Document("hair.xml"); XmlNodePtr root = doc->rootNode(); bool hairXml = true; @@ -65,7 +77,7 @@ void ColorDB::loadHair() if (!root || !xmlNameEqual(root, "colors")) { logger->log1("ColorDB: Failed to find any color files."); - mHairColors[0] = mFail; + colors[0] = ItemColor(0, "", ""); mLoaded = true; delete doc; @@ -80,17 +92,17 @@ void ColorDB::loadHair() { int id = XML::getProperty(node, "id", 0); - if (mHairColors.find(id) != mHairColors.end()) + if (colors.find(id) != colors.end()) logger->log("ColorDB: Redefinition of dye ID %d", id); - mHairColors[id] = hairXml ? - XML::getProperty(node, "value", "#FFFFFF") : - XML::getProperty(node, "dye", "#FFFFFF"); + colors[id] = ItemColor(id, XML::getProperty(node, "name", ""), + XML::getProperty(node, hairXml ? "value" : "dye", "#FFFFFF")); } } delete doc; + mColorLists["hair"] = colors; mLoaded = true; } @@ -139,7 +151,6 @@ void ColorDB::unload() { logger->log1("Unloading color database..."); - mHairColors.clear(); mColorLists.clear(); mLoaded = false; } @@ -149,22 +160,54 @@ std::string &ColorDB::getHairColor(int id) if (!mLoaded) load(); - ColorIterator i = mHairColors.find(id); + ColorListsIterator it = mColorLists.find("hair"); + if (it == mColorLists.end()) + { + logger->log1("ColorDB: Error, hair colors list empty"); + return mFail; + } + + ColorIterator i = (*it).second.find(id); + + if (i == (*it).second.end()) + { + logger->log("ColorDB: Error, unknown dye ID# %d", id); + return mFail; + } + else + { + return i->second.color; + } +} + +std::string &ColorDB::getHairColorName(int id) +{ + if (!mLoaded) + load(); + + ColorListsIterator it = mColorLists.find("hair"); + if (it == mColorLists.end()) + { + logger->log1("ColorDB: Error, hair colors list empty"); + return mFail; + } + + ColorIterator i = (*it).second.find(id); - if (i == mHairColors.end()) + if (i == (*it).second.end()) { logger->log("ColorDB: Error, unknown dye ID# %d", id); return mFail; } else { - return i->second; + return i->second.name; } } int ColorDB::getHairSize() { - return static_cast<int>(mHairColors.size()); + return mHairColorsSize; } std::map <int, ColorDB::ItemColor> *ColorDB::getColorsList(std::string name) diff --git a/src/resources/colordb.h b/src/resources/colordb.h index 36907095e..ade4227f8 100644 --- a/src/resources/colordb.h +++ b/src/resources/colordb.h @@ -70,12 +70,14 @@ namespace ColorDB std::string &getHairColor(int id); + std::string &getHairColorName(int id); + int getHairSize(); std::map <int, ItemColor> *getColorsList(std::string name); // Color DB - typedef std::map<int, std::string> Colors; + typedef std::map<int, ItemColor> Colors; typedef Colors::iterator ColorIterator; typedef std::map <std::string, std::map <int, ItemColor> > ColorLists; typedef ColorLists::iterator ColorListsIterator; diff --git a/src/resources/emotedb.cpp b/src/resources/emotedb.cpp index c6126a57b..07e192cd4 100644 --- a/src/resources/emotedb.cpp +++ b/src/resources/emotedb.cpp @@ -67,6 +67,10 @@ void EmoteDB::load() continue; int id = XML::getProperty(emoteNode, "id", -1); + // skip hight images + if (id > 19) + continue; + if (id == -1) { logger->log1("Emote Database: Emote with missing ID in " @@ -104,7 +108,6 @@ void EmoteDB::load() mLastEmote = id; } - XML::Document doc2("graphics/sprites/manaplus_emotes.xml"); rootNode = doc2.rootNode(); diff --git a/src/resources/image.cpp b/src/resources/image.cpp index d94967631..6f9042029 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -79,6 +79,8 @@ Image::Image(SDL_Surface *image, bool hasAlphaChannel0, Uint8 *alphaChannel): { logger->log( "Image::Image(SDL_Surface*): Couldn't load invalid Surface!"); + mBounds.w = 0; + mBounds.h = 0; } } @@ -119,10 +121,8 @@ Image::~Image() unload(); } -Resource *Image::load(void *buffer, unsigned bufferSize) +Resource *Image::load(SDL_RWops *rw) { - // Load the raw file data from the buffer in an RWops structure - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); SDL_Surface *tmpImage = IMG_Load_RW(rw, 1); if (!tmpImage) @@ -137,9 +137,8 @@ Resource *Image::load(void *buffer, unsigned bufferSize) return image; } -Resource *Image::load(void *buffer, unsigned bufferSize, Dye const &dye) +Resource *Image::load(SDL_RWops *rw, Dye const &dye) { - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); SDL_Surface *tmpImage = IMG_Load_RW(rw, 1); if (!tmpImage) @@ -162,7 +161,7 @@ Resource *Image::load(void *buffer, unsigned bufferSize, Dye const &dye) SDL_Surface *surf = SDL_ConvertSurface(tmpImage, &rgba, SDL_SWSURFACE); SDL_FreeSurface(tmpImage); - Uint32 *pixels = static_cast< Uint32 * >(surf->pixels); + Uint32 *pixels = static_cast<Uint32 *>(surf->pixels); for (Uint32 *p_end = pixels + surf->w * surf->h; pixels != p_end; ++pixels) { const Uint32 p = *pixels; @@ -210,10 +209,11 @@ Image *Image::createTextSurface(SDL_Surface *tmpImage, float alpha) bool hasAlpha = false; bool converted = false; + const int sz = tmpImage->w * tmpImage->h; + // The alpha channel to be filled with alpha values - Uint8 *alphaChannel = new Uint8[tmpImage->w * tmpImage->h]; + Uint8 *alphaChannel = new Uint8[sz]; - const int sz = tmpImage->w * tmpImage->h; const SDL_PixelFormat * const fmt = tmpImage->format; if (fmt->Amask) { @@ -466,7 +466,7 @@ Image* Image::SDLmerge(Image *image, int x, int y) SDL_LockSurface(surface); SDL_LockSurface(mSDLSurface); - const int x0 = (y * getWidth()) + x; + const int x0 = (y * mBounds.w) + x; const int maxX = std::min(image->mBounds.w, static_cast<Uint16>(mBounds.w - x)); const int maxY = std::min(image->mBounds.w, @@ -479,7 +479,7 @@ Image* Image::SDLmerge(Image *image, int x, int y) for (offset_y = ((y > 0) ? 0 : -y); offset_y < maxY; offset_y++) { // Computing offset on both images - current_offset = (offset_y * getWidth()) + x1; + current_offset = (offset_y * mBounds.w) + x1; surface_offset = offset_y * surface->w + offset_x; // Retrieving a pixel to merge @@ -546,7 +546,7 @@ Image* Image::SDLgetScaledImage(int width, int height) return nullptr; // No scaling when there is ... no different given size ... - if (width == getWidth() && height == getHeight()) + if (width == mBounds.w && height == mBounds.h) return nullptr; Image* scaledImage = nullptr; @@ -555,8 +555,8 @@ Image* Image::SDLgetScaledImage(int width, int height) if (mSDLSurface) { scaledSurface = zoomSurface(mSDLSurface, - static_cast<double>(width) / getWidth(), - static_cast<double>(height) / getHeight(), + static_cast<double>(width) / mBounds.w, + static_cast<double>(height) / mBounds.h, 1); // The load function takes care of the SDL<->OpenGL implementation @@ -626,22 +626,20 @@ Image *Image::_SDLload(SDL_Surface *tmpImage) bool hasAlpha = false; bool converted = false; - // The alpha channel to be filled with alpha values - Uint8 *alphaChannel = new Uint8[tmpImage->w * tmpImage->h]; - if (tmpImage->format->BitsPerPixel != 32) { tmpImage = convertTo32Bit(tmpImage); if (!tmpImage) - { - delete[] alphaChannel; return nullptr; - } converted = true; } const int sz = tmpImage->w * tmpImage->h; + + // The alpha channel to be filled with alpha values + Uint8 *alphaChannel = new Uint8[sz]; + // Figure out whether the image uses its alpha layer if (!tmpImage->format->palette) { @@ -663,19 +661,19 @@ Image *Image::_SDLload(SDL_Surface *tmpImage) else { if (SDL_ALPHA_OPAQUE != 255) + { hasAlpha = true; - memset(alphaChannel, SDL_ALPHA_OPAQUE, sz); -// for (int i = 0; i < sz; ++ i) -// alphaChannel[i] = SDL_ALPHA_OPAQUE; + memset(alphaChannel, SDL_ALPHA_OPAQUE, sz); + } } } else { if (SDL_ALPHA_OPAQUE != 255) + { hasAlpha = true; - memset(alphaChannel, SDL_ALPHA_OPAQUE, sz); -// for (int i = 0; i < sz; ++ i) -// alphaChannel[i] = SDL_ALPHA_OPAQUE; + memset(alphaChannel, SDL_ALPHA_OPAQUE, sz); + } } SDL_Surface *image; @@ -705,7 +703,6 @@ Image *Image::_SDLload(SDL_Surface *tmpImage) if (converted) SDL_FreeSurface(tmpImage); -// SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, 0); return new Image(image, hasAlpha, alphaChannel); } diff --git a/src/resources/image.h b/src/resources/image.h index a9f5722cd..68ab09e58 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -66,28 +66,25 @@ class Image : public Resource virtual ~Image(); /** - * Loads an image from a buffer in memory. + * Loads an image from an SDL_RWops structure. * - * @param buffer The memory buffer containing the image data. - * @param bufferSize The size of the memory buffer in bytes. + * @param rw The SDL_RWops to load the image from. * * @return <code>NULL</code> if an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize); + static Resource *load(SDL_RWops *rw); /** - * Loads an image from a buffer in memory and recolors it. + * Loads an image from an SDL_RWops structure and recolors it. * - * @param buffer The memory buffer containing the image data. - * @param bufferSize The size of the memory buffer in bytes. + * @param rw The SDL_RWops to load the image from. * @param dye The dye used to recolor the image. * * @return <code>NULL</code> if an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize, - Dye const &dye); + static Resource *load(SDL_RWops *rw, Dye const &dye); /** * Loads an image from an SDL surface. @@ -106,7 +103,7 @@ class Image : public Resource /** * Tells is the image is loaded */ - bool isLoaded() + bool isLoaded() const { return mLoaded; } /** @@ -220,16 +217,16 @@ class Image : public Resource static int mTextureType; #endif - bool isHasAlphaChannel() + bool isHasAlphaChannel() const { return mHasAlphaChannel; } - bool isAlphaVisible() + bool isAlphaVisible() const { return mIsAlphaVisible; } void setAlphaVisible(bool b) { mIsAlphaVisible = b; } - bool isAlphaCalculated() + bool isAlphaCalculated() const { return mIsAlphaCalculated; } void setAlphaCalculated(bool b) diff --git a/src/resources/imageset.h b/src/resources/imageset.h index 69ebebdc0..edfc4b123 100644 --- a/src/resources/imageset.h +++ b/src/resources/imageset.h @@ -63,13 +63,13 @@ class ImageSet : public Resource size_type size() const { return mImages.size(); } - int getOffsetX() + int getOffsetX() const { return mOffsetX; } void setOffsetX(int n) { mOffsetX = n; } - int getOffsetY() + int getOffsetY() const { return mOffsetY; } void setOffsetY(int n) diff --git a/src/resources/iteminfo.h b/src/resources/iteminfo.h index 8c5e2dd8f..5f789663f 100644 --- a/src/resources/iteminfo.h +++ b/src/resources/iteminfo.h @@ -148,7 +148,8 @@ class ItemInfo void setEffect(const std::string &effect) { mEffect = effect; } - const std::string &getEffect() const { return mEffect; } + const std::string &getEffect() const + { return mEffect; } void setType(ItemType type) { mType = type; } @@ -250,7 +251,7 @@ class ItemInfo void setColorsList(std::string name); - bool isHaveColors() + bool isHaveColors() const { return !mColorList.empty(); } const std::string replaceColors(std::string str, diff --git a/src/resources/music.cpp b/src/resources/music.cpp index 6e752ab60..b13812f18 100644 --- a/src/resources/music.cpp +++ b/src/resources/music.cpp @@ -26,30 +26,21 @@ #include "debug.h" -Music::Music(Mix_Chunk *music): - mChunk(music), - mChannel(-1) +Music::Music(Mix_Music *music) : + mMusic(music) { } Music::~Music() { - //Mix_FreeMusic(music); - Mix_FreeChunk(mChunk); + Mix_FreeMusic(mMusic); } -Resource *Music::load(void *buffer, unsigned bufferSize) +Resource *Music::load(SDL_RWops *rw) { - // 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, 1); - - if (tmpMusic) + if (Mix_Music *music = Mix_LoadMUS_RW(rw)) { - return new Music(tmpMusic); + return new Music(music); } else { @@ -58,30 +49,10 @@ Resource *Music::load(void *buffer, unsigned bufferSize) } } -bool Music::play(int loops) -{ - /* - * Warning: loops should be always set to -1 (infinite) with current - * implementation to avoid halting the playback of other samples - */ - - /*if (Mix_PlayMusic(music, loops)) - return true;*/ - Mix_VolumeChunk(mChunk, 120); - mChannel = Mix_PlayChannel(-1, mChunk, loops); - - return mChannel != -1; -} - -void Music::stop() +bool Music::play(int loops, int fadeIn) { - /* - * Warning: very dungerous trick, it could try to stop channels occupied - * by samples rather than the current music file - */ - - //Mix_HaltMusic(); - - if (mChannel != -1) - Mix_HaltChannel(mChannel); + if (fadeIn > 0) + return Mix_FadeInMusic(mMusic, loops, fadeIn); + else + return Mix_PlayMusic(mMusic, loops); } diff --git a/src/resources/music.h b/src/resources/music.h index 428c02572..88cc752bc 100644 --- a/src/resources/music.h +++ b/src/resources/music.h @@ -41,38 +41,32 @@ class Music : public Resource /** * 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 rw The SDL_RWops to load the music data from. * * @return <code>NULL</code> if the an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize); + static Resource *load(SDL_RWops *rw); /** * Plays the music. * - * @param loops Number of times to repeat the playback. + * @param loops Number of times to repeat the playback (-1 means + * forever). + * @param fadeIn Duration in milliseconds to fade in the music. * * @return <code>true</code> if the playback started properly * <code>false</code> otherwise. */ - virtual bool play(int loops); - - /** - * Stops the music. - */ - virtual void stop(); + bool play(int loops = -1, int fadeIn = 0); protected: /** * Constructor. */ - Music(Mix_Chunk *music); + Music(Mix_Music *music); - //Mix_Music *music; - Mix_Chunk *mChunk; - int mChannel; + Mix_Music *mMusic; }; #endif diff --git a/src/resources/resource.h b/src/resources/resource.h index b8e06aaa7..c0ea7ca23 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -73,7 +73,7 @@ class Resource { return mRefCount; } #ifdef DEBUG_DUMP_LEAKS - bool getDumped() + bool getDumped() const { return mDumped; } void setDumped(bool n) diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 3376394bc..c26526b97 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -35,6 +35,7 @@ #include "resources/spritedef.h" #include "utils/mkdir.h" +#include "utils/physfsrwops.h" #include <physfs.h> #include <SDL_image.h> @@ -42,6 +43,7 @@ #include <fstream> #include <iostream> #include <sstream> +#include <zlib.h> #include <sys/stat.h> #include <sys/time.h> @@ -397,19 +399,16 @@ struct ResourceLoader ResourceManager *manager; std::string path; ResourceManager::loader fun; + static Resource *load(void *v) { if (!v) return nullptr; ResourceLoader *l = static_cast< ResourceLoader * >(v); - int fileSize; - if (!l->manager) + SDL_RWops *rw = PHYSFSRWOPS_openRead(l->path.c_str()); + if (!rw) return nullptr; - void *buffer = l->manager->loadFile(l->path, fileSize); - if (!buffer) - return nullptr; - Resource *res = l->fun(buffer, fileSize); - free(buffer); + Resource *res = l->fun(rw); return res; } }; @@ -451,16 +450,14 @@ struct DyedImageLoader d = new Dye(path.substr(p + 1)); path = path.substr(0, p); } - int fileSize; - void *buffer = l->manager->loadFile(path, fileSize); - if (!buffer) + SDL_RWops *rw = PHYSFSRWOPS_openRead(path.c_str()); + if (!rw) { delete d; return nullptr; } - Resource *res = d ? Image::load(buffer, fileSize, *d) - : Image::load(buffer, fileSize); - free(buffer); + Resource *res = d ? Image::load(rw, *d) + : Image::load(rw); delete d; return res; } @@ -695,18 +692,10 @@ void ResourceManager::saveTextFile(std::string path, std::string name, SDL_Surface *ResourceManager::loadSDLSurface(const std::string &filename) { - int fileSize; - void *buffer = loadFile(filename, fileSize); - SDL_Surface *tmp = nullptr; - - if (buffer) - { - SDL_RWops *rw = SDL_RWFromMem(buffer, fileSize); - tmp = IMG_Load_RW(rw, 1); - ::free(buffer); - } - - return tmp; + SDL_Surface *surface = nullptr; + if (SDL_RWops *rw = PHYSFSRWOPS_openRead(filename.c_str())) + surface = IMG_Load_RW(rw, 1); + return surface; } void ResourceManager::scheduleDelete(SDL_Surface* surface) diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index f0146b8b4..9df96d354 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -37,7 +37,9 @@ class Music; class Resource; class SoundEffect; class SpriteDef; + struct SDL_Surface; +struct SDL_RWops; /** * A class for loading and managing resources. @@ -48,7 +50,7 @@ class ResourceManager public: - typedef Resource *(*loader)(void *, unsigned); + typedef Resource *(*loader)(SDL_RWops *); typedef Resource *(*generator)(void *); ResourceManager(); @@ -252,17 +254,17 @@ class ResourceManager */ static void deleteInstance(); - int size() + int size() const { return mResources.size(); } typedef std::map<std::string, Resource*> Resources; typedef Resources::iterator ResourceIterator; #ifdef DEBUG_DUMP_LEAKS - Resources* getResources() + Resources* getResources() const { return &mResources; } - Resources* getOrphanedResources() + Resources* getOrphanedResources() const { return &mOrphanedResources; } #endif diff --git a/src/resources/soundeffect.cpp b/src/resources/soundeffect.cpp index eaa323bd6..6a3a980a7 100644 --- a/src/resources/soundeffect.cpp +++ b/src/resources/soundeffect.cpp @@ -31,14 +31,10 @@ SoundEffect::~SoundEffect() Mix_FreeChunk(mChunk); } -Resource *SoundEffect::load(void *buffer, unsigned bufferSize) +Resource *SoundEffect::load(SDL_RWops *rw) { - if (!buffer) + if (!rw) return nullptr; - - // Load the raw file data from the buffer in an RWops structure - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); - // Load the music data and free the RWops structure Mix_Chunk *tmpSoundEffect = Mix_LoadWAV_RW(rw, 1); diff --git a/src/resources/soundeffect.h b/src/resources/soundeffect.h index 91ca3bb59..b8c9e8735 100644 --- a/src/resources/soundeffect.h +++ b/src/resources/soundeffect.h @@ -41,13 +41,12 @@ class SoundEffect : public Resource /** * Loads a sample from a buffer in memory. * - * @param buffer The memory buffer containing the sample data. - * @param bufferSize The size of the memory buffer in bytes. + * @param rw The SDL_RWops to load the sample data from. * * @return <code>NULL</code> if the an error occurred, a valid pointer * otherwise. */ - static Resource *load(void *buffer, unsigned bufferSize); + static Resource *load(SDL_RWops *rw); /** * Plays the sample. @@ -65,7 +64,8 @@ class SoundEffect : public Resource /** * Constructor. */ - SoundEffect(Mix_Chunk *soundEffect): mChunk(soundEffect) + SoundEffect(Mix_Chunk *soundEffect) : + mChunk(soundEffect) { } Mix_Chunk *mChunk; |