diff options
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/animation.h | 1 | ||||
-rw-r--r-- | src/resources/image.cpp | 124 | ||||
-rw-r--r-- | src/resources/image.h | 6 | ||||
-rw-r--r-- | src/resources/imageset.cpp (renamed from src/resources/spriteset.cpp) | 18 | ||||
-rw-r--r-- | src/resources/imageset.h (renamed from src/resources/spriteset.h) | 23 | ||||
-rw-r--r-- | src/resources/monsterdb.h | 2 | ||||
-rw-r--r-- | src/resources/monsterinfo.h | 157 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 22 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 8 | ||||
-rw-r--r-- | src/resources/spritedef.cpp | 34 | ||||
-rw-r--r-- | src/resources/spritedef.h | 10 |
11 files changed, 193 insertions, 212 deletions
diff --git a/src/resources/animation.h b/src/resources/animation.h index 54142bcb..d0d11c69 100644 --- a/src/resources/animation.h +++ b/src/resources/animation.h @@ -29,7 +29,6 @@ #include <libxml/tree.h> class Image; -class Spriteset; /** * A single frame in an animation, with a delay and an offset. diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 58325213..a27783d4 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -80,7 +80,7 @@ Image* Image::load(void *buffer, unsigned int bufferSize, tmpImage = IMG_Load_RW(rw, 1); } - if (tmpImage == NULL) { + if (!tmpImage) { logger->log("Error, image load failed: %s", IMG_GetError()); return NULL; } @@ -99,87 +99,28 @@ Image* Image::load(void *buffer, unsigned int bufferSize, amask = 0xff000000; #endif - // Convert the image to a 32 bit software surface for processing - SDL_Surface *formatImage = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, 32, - rmask, gmask, bmask, amask); - - if (formatImage == NULL) { - logger->log("Error, image load failed: not enough memory"); - SDL_FreeSurface(tmpImage); - return NULL; - } - - SDL_Surface *image = SDL_ConvertSurface( - tmpImage, formatImage->format, SDL_SWSURFACE); - - SDL_FreeSurface(formatImage); - - if (image == NULL) { - logger->log("Error, image load failed: not enough memory"); - return NULL; - } - - bool hasPink = false; - bool hasAlpha = false; - int i; - Uint32 pink = SDL_MapRGB(image->format, 255, 0, 255); - - // Figure out whether the image has pink pixels - for (i = 0; i < image->w * image->h; ++i) - { - if (((Uint32*)image->pixels)[i] == pink) - { - hasPink = true; - break; - } - } - - // Figure out whether the image uses its alpha layer - for (i = 0; i < image->w * image->h; ++i) - { - Uint8 r, g, b, a; - SDL_GetRGBA( - ((Uint32*)image->pixels)[i], - image->format, - &r, &g, &b, &a); - - if (a != 255) - { - hasAlpha = true; - break; - } - } - - SDL_FreeSurface(image); - - if (hasPink && !hasAlpha) { - SDL_SetColorKey(tmpImage, SDL_SRCCOLORKEY, - SDL_MapRGB(tmpImage->format, 255, 0, 255)); - } else if (hasAlpha) { - SDL_SetAlpha(tmpImage, SDL_SRCALPHA, SDL_ALPHA_OPAQUE); - } - #ifdef USE_OPENGL if (mUseOpenGL) { int width = tmpImage->w; int height = tmpImage->h; - int realWidth = 1, realHeight = 1; + int realWidth = powerOfTwo(width); + int realHeight = powerOfTwo(height); - while (realWidth < width && realWidth < 1024) { - realWidth *= 2; - } - - while (realHeight < height && realHeight < 1024) { - realHeight *= 2; + if (realWidth < width || realHeight < height) + { + logger->log("Warning: image too large, cropping to %dx%d texture!", + tmpImage->w, tmpImage->h); } + // Make sure the alpha channel is not used, but copied to destination SDL_SetAlpha(tmpImage, 0, SDL_ALPHA_OPAQUE); + SDL_Surface *oldImage = tmpImage; - tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, 32, - rmask, gmask, bmask, amask); + tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, + 32, rmask, gmask, bmask, amask); - if (tmpImage == NULL) { + if (!tmpImage) { logger->log("Error, image convert failed: out of memory"); return NULL; } @@ -189,8 +130,6 @@ Image* Image::load(void *buffer, unsigned int bufferSize, GLuint texture; glGenTextures(1, &texture); - logger->log("Binding texture %d (%dx%d)", - texture, tmpImage->w, tmpImage->h); glBindTexture(GL_TEXTURE_2D, texture); if (SDL_MUSTLOCK(tmpImage)) { @@ -246,8 +185,27 @@ Image* Image::load(void *buffer, unsigned int bufferSize, } #endif - // Set color key and alpha blending optins, and convert the surface to the - // current display format + bool hasAlpha = false; + + // Figure out whether the image uses its alpha layer + for (int i = 0; i < tmpImage->w * tmpImage->h; ++i) + { + Uint8 r, g, b, a; + SDL_GetRGBA( + ((Uint32*) tmpImage->pixels)[i], + tmpImage->format, + &r, &g, &b, &a); + + if (a != 255) + { + hasAlpha = true; + break; + } + } + + SDL_Surface *image; + + // Convert the surface to the current display format if (hasAlpha) { image = SDL_DisplayFormatAlpha(tmpImage); } @@ -256,7 +214,7 @@ Image* Image::load(void *buffer, unsigned int bufferSize, } SDL_FreeSurface(tmpImage); - if (image == NULL) { + if (!image) { logger->log("Error: Image convert failed."); return NULL; } @@ -315,10 +273,22 @@ float Image::getAlpha() } #ifdef USE_OPENGL -void Image::setLoadAsOpenGL(bool useOpenGL) +void +Image::setLoadAsOpenGL(bool useOpenGL) { Image::mUseOpenGL = useOpenGL; } + +int +Image::powerOfTwo(int input) +{ + int value = 1; + while (value < input && value < 1024) + { + value <<= 1; + } + return value; +} #endif //============================================================================ diff --git a/src/resources/image.h b/src/resources/image.h index 03bf0cbc..cad21dcd 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -126,6 +126,12 @@ class Image : public Resource #ifdef USE_OPENGL Image(const std::string &idPath, GLuint glimage, int width, int height, int texWidth, int texHeight); + + /** + * Returns the first power of two equal or bigger than the input. + */ + static int + powerOfTwo(int input); #endif Image(const std::string &idPath, SDL_Surface *image); diff --git a/src/resources/spriteset.cpp b/src/resources/imageset.cpp index 96bcef0c..677c024b 100644 --- a/src/resources/spriteset.cpp +++ b/src/resources/imageset.cpp @@ -21,7 +21,7 @@ * $Id$ */ -#include "spriteset.h" +#include "imageset.h" #include "../log.h" @@ -29,7 +29,7 @@ #include "../utils/dtor.h" -Spriteset::Spriteset(const std::string& idPath, +ImageSet::ImageSet(const std::string& idPath, Image *img, int width, int height): Resource(idPath) @@ -38,28 +38,28 @@ Spriteset::Spriteset(const std::string& idPath, { for (int x = 0; x + width <= img->getWidth(); x += width) { - mSpriteset.push_back(img->getSubImage(x, y, width, height)); + mImages.push_back(img->getSubImage(x, y, width, height)); } } mWidth = width; mHeight = height; } -Spriteset::~Spriteset() +ImageSet::~ImageSet() { - for_each(mSpriteset.begin(), mSpriteset.end(), make_dtor(mSpriteset)); + for_each(mImages.begin(), mImages.end(), make_dtor(mImages)); } Image* -Spriteset::get(size_type i) +ImageSet::get(size_type i) { - if (i >= mSpriteset.size()) + if (i >= mImages.size()) { - logger->log("Warning: Sprite #%i does not exist in this spriteset", i); + logger->log("Warning: Sprite #%i does not exist in this image set", i); return NULL; } else { - return mSpriteset[i]; + return mImages[i]; } } diff --git a/src/resources/spriteset.h b/src/resources/imageset.h index 7f6b42df..3469a3bb 100644 --- a/src/resources/spriteset.h +++ b/src/resources/imageset.h @@ -21,8 +21,8 @@ * $Id$ */ -#ifndef _TMW_SPRITESET_H -#define _TMW_SPRITESET_H +#ifndef _TMW_IMAGESET_H +#define _TMW_IMAGESET_H #include <vector> @@ -33,21 +33,19 @@ class Image; /** * Stores a set of subimages originating from a single image. - * - * TODO: Should probably be renamed to ImageSet or TileSet. */ -class Spriteset : public Resource +class ImageSet : public Resource { public: /* * Cuts the passed image in a grid of sub images. */ - Spriteset(const std::string &idPath, Image *img, int w, int h); + ImageSet(const std::string &idPath, Image *img, int w, int h); /** * Destructor. */ - ~Spriteset(); + ~ImageSet(); int getWidth() { return mWidth; }; @@ -56,14 +54,13 @@ class Spriteset : public Resource typedef std::vector<Image*>::size_type size_type; Image* get(size_type i); - size_type size() { return mSpriteset.size(); } + size_type size() { return mImages.size(); } private: - // Vector storing the whole spriteset. - std::vector<Image*> mSpriteset; - // Height and width of the images in the spriteset - int mHeight; - int mWidth; + std::vector<Image*> mImages; + + int mHeight; /**< Height of the images in the image set. */ + int mWidth; /**< Width of the images in the image set. */ }; #endif diff --git a/src/resources/monsterdb.h b/src/resources/monsterdb.h index 048638c2..46a33b06 100644 --- a/src/resources/monsterdb.h +++ b/src/resources/monsterdb.h @@ -39,7 +39,7 @@ namespace MonsterDB void unload(); - const MonsterInfo& get (int id); + const MonsterInfo& get(int id); typedef std::map<int, MonsterInfo*> MonsterInfos; typedef MonsterInfos::iterator MonsterInfoIterator; diff --git a/src/resources/monsterinfo.h b/src/resources/monsterinfo.h index 413dafa0..05d4c014 100644 --- a/src/resources/monsterinfo.h +++ b/src/resources/monsterinfo.h @@ -1,74 +1,83 @@ -/*
- * 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: monsterinfo.h 2650 2006-09-03 15:00:47Z b_lindeijer $
- */
-
-#ifndef _TMW_MONSTERINFO_H_
-#define _TMW_MONSTERINFO_H_
-
-#include <map>
-#include <string>
-#include <vector>
-
-
-enum SoundEvent
-{
- EVENT_HIT,
- EVENT_MISS,
- EVENT_HURT,
- EVENT_DIE
-};
-
-
-class MonsterInfo
-{
- public:
- MonsterInfo();
-
- ~MonsterInfo();
-
- void
- setName(std::string name) { mName = name; } ;
-
- void
- setSprite(std::string filename) { mSprite = filename; }
-
- void
- addSound (SoundEvent event, std::string filename);
-
- const std::string&
- getName () const { return mName; };
-
- const std::string&
- getSprite () const { return mSprite; };
-
- std::string
- getSound (SoundEvent event) const;
-
- private:
-
- std::string mName;
- std::string mSprite;
-
- std::map<SoundEvent, std::vector<std::string>* > mSounds;
-};
-
-#endif
+/* + * 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: monsterinfo.h 2650 2006-09-03 15:00:47Z b_lindeijer $ + */ + +#ifndef _TMW_MONSTERINFO_H_ +#define _TMW_MONSTERINFO_H_ + +#include <map> +#include <string> +#include <vector> + +enum SoundEvent +{ + EVENT_HIT, + EVENT_MISS, + EVENT_HURT, + EVENT_DIE +}; + +/** + * Holds information about a certain type of monster. This includes the name + * of the monster, the sprite to display and the sounds the monster makes. + * + * @see MonsterDB + */ +class MonsterInfo +{ + public: + /** + * Constructor. + */ + MonsterInfo(); + + /** + * Destructor. + */ + ~MonsterInfo(); + + void + setName(std::string name) { mName = name; } ; + + void + setSprite(std::string filename) { mSprite = filename; } + + void + addSound (SoundEvent event, std::string filename); + + const std::string& + getName () const { return mName; }; + + const std::string& + getSprite () const { return mSprite; }; + + std::string + getSound (SoundEvent event) const; + + private: + std::string mName; + std::string mSprite; + + std::map<SoundEvent, std::vector<std::string>* > mSounds; +}; + +#endif diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 2059a5c3..448e7f80 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -31,7 +31,7 @@ #include "image.h" #include "music.h" #include "soundeffect.h" -#include "spriteset.h" +#include "imageset.h" #include "spritedef.h" #include "../log.h" @@ -46,7 +46,7 @@ ResourceManager::ResourceManager() ResourceManager::~ResourceManager() { - // Release any remaining spritedefs first because they depend on spritesets + // Release any remaining spritedefs first because they depend on image sets ResourceIterator iter = mResources.begin(); while (iter != mResources.end()) { @@ -63,11 +63,11 @@ ResourceManager::~ResourceManager() } } - // Release any remaining spritesets first because they depend on images + // Release any remaining image sets first because they depend on images iter = mResources.begin(); while (iter != mResources.end()) { - if (dynamic_cast<Spriteset*>(iter->second) != 0) + if (dynamic_cast<ImageSet*>(iter->second) != 0) { cleanUp(iter->second); ResourceIterator toErase = iter; @@ -232,8 +232,8 @@ ResourceManager::getSoundEffect(const std::string &idPath) return dynamic_cast<SoundEffect*>(get(SOUND_EFFECT, idPath)); } -Spriteset* -ResourceManager::getSpriteset(const std::string &imagePath, int w, int h) +ImageSet* +ResourceManager::getImageSet(const std::string &imagePath, int w, int h) { std::stringstream ss; ss << imagePath << "[" << w << "x" << h << "]"; @@ -243,7 +243,7 @@ ResourceManager::getSpriteset(const std::string &imagePath, int w, int h) if (resIter != mResources.end()) { resIter->second->incRef(); - return dynamic_cast<Spriteset*>(resIter->second); + return dynamic_cast<ImageSet*>(resIter->second); } Image *img = getImage(imagePath); @@ -252,13 +252,13 @@ ResourceManager::getSpriteset(const std::string &imagePath, int w, int h) return NULL; } - Spriteset *spriteset = new Spriteset(idPath, img, w, h); - spriteset->incRef(); - mResources[idPath] = spriteset; + ImageSet *imageSet = new ImageSet(idPath, img, w, h); + imageSet->incRef(); + mResources[idPath] = imageSet; img->decRef(); - return spriteset; + return imageSet; } SpriteDef* diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index e176e337..db29e6d3 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -33,7 +33,7 @@ class Resource; class Image; class Music; class SoundEffect; -class Spriteset; +class ImageSet; class SpriteDef; /** @@ -143,11 +143,11 @@ class ResourceManager getSoundEffect(const std::string &idPath); /** - * Creates a spriteset based on the image referenced by the given + * Creates a image set based on the image referenced by the given * path and the supplied sprite sizes */ - Spriteset* - getSpriteset(const std::string &imagePath, int w, int h); + ImageSet* + getImageSet(const std::string &imagePath, int w, int h); /** * Creates a sprite definition based on a given path and the supplied diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index feb6f8f8..d29bd847 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -28,7 +28,7 @@ #include "animation.h" #include "action.h" #include "resourcemanager.h" -#include "spriteset.h" +#include "imageset.h" #include "image.h" #include "../utils/xml.h" @@ -135,30 +135,30 @@ SpriteDef::loadImageSet(xmlNodePtr node) std::string imageSrc = XML::getProperty(node, "src", ""); ResourceManager *resman = ResourceManager::getInstance(); - Spriteset *spriteset = resman->getSpriteset(imageSrc, width, height); + ImageSet *imageSet = resman->getImageSet(imageSrc, width, height); - if (!spriteset) + if (!imageSet) { logger->error("Couldn't load imageset!"); } - mSpritesets[name] = spriteset; + mImageSets[name] = imageSet; } void SpriteDef::loadAction(xmlNodePtr node, int variant_offset) { const std::string actionName = XML::getProperty(node, "name", ""); - const std::string imagesetName = XML::getProperty(node, "imageset", ""); + const std::string imageSetName = XML::getProperty(node, "imageset", ""); - SpritesetIterator si = mSpritesets.find(imagesetName); - if (si == mSpritesets.end()) + ImageSetIterator si = mImageSets.find(imageSetName); + if (si == mImageSets.end()) { logger->log("Warning: imageset \"%s\" not defined in %s", - imagesetName.c_str(), getIdPath().c_str()); + imageSetName.c_str(), getIdPath().c_str()); return; } - Spriteset *imageset = si->second; + ImageSet *imageSet = si->second; SpriteAction actionType = makeSpriteAction(actionName); if (actionType == ACTION_INVALID) @@ -183,14 +183,14 @@ SpriteDef::loadAction(xmlNodePtr node, int variant_offset) { if (xmlStrEqual(animationNode->name, BAD_CAST "animation")) { - loadAnimation(animationNode, action, imageset, variant_offset); + loadAnimation(animationNode, action, imageSet, variant_offset); } } } void SpriteDef::loadAnimation(xmlNodePtr animationNode, - Action *action, Spriteset *imageset, + Action *action, ImageSet *imageSet, int variant_offset) { std::string directionName = @@ -215,8 +215,8 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, int delay = XML::getProperty(frameNode, "delay", 0); int offsetX = XML::getProperty(frameNode, "offsetX", 0); int offsetY = XML::getProperty(frameNode, "offsetY", 0); - offsetY -= imageset->getHeight() - 32; - offsetX -= imageset->getWidth() / 2 - 16; + offsetY -= imageSet->getHeight() - 32; + offsetX -= imageSet->getWidth() / 2 - 16; if (xmlStrEqual(frameNode->name, BAD_CAST "frame")) { @@ -228,7 +228,7 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, continue; } - Image *img = imageset->get(index + variant_offset); + Image *img = imageSet->get(index + variant_offset); if (!img) { @@ -251,7 +251,7 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, while (end >= start) { - Image *img = imageset->get(start + variant_offset); + Image *img = imageSet->get(start + variant_offset); if (!img) { @@ -296,8 +296,8 @@ SpriteDef::substituteAction(SpriteAction complete, SpriteAction with) SpriteDef::~SpriteDef() { - for (SpritesetIterator i = mSpritesets.begin(); - i != mSpritesets.end(); ++i) + for (ImageSetIterator i = mImageSets.begin(); + i != mImageSets.end(); ++i) { i->second->decRef(); } diff --git a/src/resources/spritedef.h b/src/resources/spritedef.h index 057129ad..6d335b02 100644 --- a/src/resources/spritedef.h +++ b/src/resources/spritedef.h @@ -32,7 +32,7 @@ #include <libxml/tree.h> class Action; -class Spriteset; +class ImageSet; enum SpriteAction { @@ -111,7 +111,7 @@ class SpriteDef : public Resource */ void loadAnimation(xmlNodePtr animationNode, - Action *action, Spriteset *imageset, + Action *action, ImageSet *imageSet, int variant_offset); /** @@ -140,12 +140,12 @@ class SpriteDef : public Resource makeSpriteDirection(const std::string &direction); - typedef std::map<std::string, Spriteset*> Spritesets; - typedef Spritesets::iterator SpritesetIterator; + typedef std::map<std::string, ImageSet*> ImageSets; + typedef ImageSets::iterator ImageSetIterator; typedef std::map<SpriteAction, Action*> Actions; - Spritesets mSpritesets; + ImageSets mImageSets; Actions mActions; Action *mAction; SpriteDirection mDirection; |