From 866a71ffd576fd10c4e309195016d86f0c8ed635 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 3 Sep 2012 21:58:08 +0300 Subject: Add const to more classes. --- src/resources/spritedef.cpp | 86 ++++++++++++++++++++++++--------------------- src/resources/spritedef.h | 35 +++++++++--------- src/resources/subimage.cpp | 14 ++++---- src/resources/subimage.h | 9 ++--- src/resources/wallpaper.cpp | 12 +++---- src/resources/wallpaper.h | 2 +- 6 files changed, 85 insertions(+), 73 deletions(-) (limited to 'src/resources') diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index fd5178ec6..b55253c05 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -38,7 +38,8 @@ SpriteReference *SpriteReference::Empty = nullptr; extern int serverVersion; -Action *SpriteDef::getAction(std::string action, unsigned num) const +Action *SpriteDef::getAction(const std::string &action, + const unsigned num) const { Actions::const_iterator i = mActions.find(num); if (i == mActions.end() && num != 100) @@ -47,7 +48,7 @@ Action *SpriteDef::getAction(std::string action, unsigned num) const if (i == mActions.end() || !(*i).second) return nullptr; - ActionMap::const_iterator it = ((*i).second)->find(action); + const ActionMap::const_iterator it = ((*i).second)->find(action); if (it == ((*i).second)->end()) { @@ -58,13 +59,13 @@ Action *SpriteDef::getAction(std::string action, unsigned num) const return (*it).second; } -unsigned SpriteDef::findNumber(unsigned num) const +unsigned SpriteDef::findNumber(const unsigned num) const { unsigned min = 101; for (Actions::const_iterator it = mActions.begin(), it_end = mActions.end(); it != it_end; ++ it) { - unsigned n = (*it).first; + const unsigned n = (*it).first; if (n >= num && n < min) min = n; } @@ -73,7 +74,7 @@ unsigned SpriteDef::findNumber(unsigned num) const return min; } -SpriteDef *SpriteDef::load(const std::string &animationFile, int variant) +SpriteDef *SpriteDef::load(const std::string &animationFile, const int variant) { size_t pos = animationFile.find('|'); std::string palettes; @@ -95,7 +96,7 @@ SpriteDef *SpriteDef::load(const std::string &animationFile, int variant) return nullptr; } - SpriteDef *def = new SpriteDef; + SpriteDef *const def = new SpriteDef; def->mProcessedFiles.insert(animationFile); def->loadSprite(rootNode, variant, palettes); def->substituteActions(); @@ -109,11 +110,11 @@ void SpriteDef::fixDeadAction() for (ActionsIter it = mActions.begin(), it_end = mActions.end(); it != it_end; ++ it) { - ActionMap *d = (*it).second; + ActionMap *const d = (*it).second; if (!d) continue; - ActionMap::iterator i = d->find(SpriteAction::DEAD); - ActionMap::iterator i2 = d->find(SpriteAction::STAND); + const ActionMap::iterator i = d->find(SpriteAction::DEAD); + const ActionMap::iterator i2 = d->find(SpriteAction::STAND); // search dead action and check what it not same with stand action if (i != d->end() && i->second && i->second != i2->second) (i->second)->setLastFrameDelay(0); @@ -125,12 +126,12 @@ void SpriteDef::substituteAction(std::string complete, std::string with) for (ActionsConstIter it = mActions.begin(), it_end = mActions.end(); it != it_end; ++ it) { - ActionMap *d = (*it).second; + ActionMap *const d = (*it).second; if (!d) continue; if (d->find(complete) == d->end()) { - ActionMap::iterator i = d->find(with); + const ActionMap::iterator i = d->find(with); if (i != d->end()) (*d)[complete] = i->second; } @@ -152,7 +153,7 @@ void SpriteDef::substituteActions() substituteAction(SpriteAction::SPAWN, SpriteAction::STAND); } -void SpriteDef::loadSprite(XmlNodePtr spriteNode, int variant, +void SpriteDef::loadSprite(const XmlNodePtr spriteNode, const int variant, const std::string &palettes) { // Get the variant @@ -176,7 +177,8 @@ void SpriteDef::loadSprite(XmlNodePtr spriteNode, int variant, } } -void SpriteDef::loadImageSet(XmlNodePtr node, const std::string &palettes) +void SpriteDef::loadImageSet(const XmlNodePtr node, + const std::string &palettes) { const std::string name = XML::getProperty(node, "name", ""); @@ -190,8 +192,8 @@ void SpriteDef::loadImageSet(XmlNodePtr node, const std::string &palettes) std::string imageSrc = XML::getProperty(node, "src", ""); Dye::instantiate(imageSrc, palettes); - ResourceManager *resman = ResourceManager::getInstance(); - ImageSet *imageSet = resman->getImageSet(imageSrc, width, height); + ResourceManager *const resman = ResourceManager::getInstance(); + ImageSet *const imageSet = resman->getImageSet(imageSrc, width, height); if (!imageSet) { @@ -204,20 +206,20 @@ void SpriteDef::loadImageSet(XmlNodePtr node, const std::string &palettes) mImageSets[name] = imageSet; } -void SpriteDef::loadAction(XmlNodePtr node, int variant_offset) +void SpriteDef::loadAction(const XmlNodePtr node, const int variant_offset) { const std::string actionName = XML::getProperty(node, "name", ""); const std::string imageSetName = XML::getProperty(node, "imageset", ""); const unsigned hp = XML::getProperty(node, "hp", 100); - ImageSetIterator si = mImageSets.find(imageSetName); + const ImageSetIterator si = mImageSets.find(imageSetName); if (si == mImageSets.end()) { logger->log("Warning: imageset \"%s\" not defined in %s", imageSetName.c_str(), getIdPath().c_str()); return; } - ImageSet *imageSet = si->second; + ImageSet *const imageSet = si->second; if (actionName == SpriteAction::INVALID) { @@ -225,7 +227,7 @@ void SpriteDef::loadAction(XmlNodePtr node, int variant_offset) actionName.c_str(), getIdPath().c_str()); return; } - Action *action = new Action; + Action *const action = new Action; action->setNumber(hp); addAction(hp, actionName, action); @@ -234,7 +236,7 @@ void SpriteDef::loadAction(XmlNodePtr node, int variant_offset) addAction(hp, "attack", action); // When first action set it as default direction - Actions::const_iterator i = mActions.find(hp); + const Actions::const_iterator i = mActions.find(hp); if ((*i).second->size() == 1) addAction(hp, SpriteAction::DEFAULT, action); @@ -246,9 +248,9 @@ void SpriteDef::loadAction(XmlNodePtr node, int variant_offset) } } -void SpriteDef::loadAnimation(XmlNodePtr animationNode, - Action *action, ImageSet *imageSet, - int variant_offset) +void SpriteDef::loadAnimation(const XmlNodePtr animationNode, + Action *const action, ImageSet *const imageSet, + const int variant_offset) { if (!action || !imageSet) return; @@ -264,7 +266,7 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, return; } - Animation *animation = new Animation; + Animation *const animation = new Animation; action->setAnimation(directionType, animation); // Get animation frames @@ -276,7 +278,7 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, imageSet->getOffsetX(); int offsetY = XML::getProperty(frameNode, "offsetY", 0) + imageSet->getOffsetY(); - int rand = XML::getIntProperty(frameNode, "rand", 100, 0, 100); + const int rand = XML::getIntProperty(frameNode, "rand", 100, 0, 100); offsetY -= imageSet->getHeight() - 32; offsetX -= imageSet->getWidth() / 2 - 16; @@ -291,7 +293,7 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, continue; } - Image *img = imageSet->get(index + variant_offset); + Image *const img = imageSet->get(index + variant_offset); if (!img) { @@ -306,7 +308,8 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, const int start = XML::getProperty(frameNode, "start", -1); const int end = XML::getProperty(frameNode, "end", -1); const std::string value = XML::getProperty(frameNode, "value", ""); - int repeat = XML::getIntProperty(frameNode, "repeat", 1, 0, 100); + const int repeat = XML::getIntProperty( + frameNode, "repeat", 1, 0, 100); if (repeat < 1) { @@ -338,14 +341,14 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, } else if (idx != std::string::npos) { - int v1 = atoi(str.substr(0, idx).c_str()); - int v2 = atoi(str.substr(idx + 1).c_str()); + const int v1 = atoi(str.substr(0, idx).c_str()); + const int v2 = atoi(str.substr(idx + 1).c_str()); addSequence(v1, v2, delay, offsetX, offsetY, variant_offset, repeat, rand, imageSet, animation); } else { - Image *img = imageSet->get(atoi( + Image *const img = imageSet->get(atoi( str.c_str()) + variant_offset); if (img) { @@ -384,7 +387,7 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, } // for frameNode } -void SpriteDef::includeSprite(XmlNodePtr includeNode) +void SpriteDef::includeSprite(const XmlNodePtr includeNode) { std::string filename = XML::getProperty(includeNode, "file", ""); @@ -401,7 +404,7 @@ void SpriteDef::includeSprite(XmlNodePtr includeNode) mProcessedFiles.insert(filename); XML::Document doc(filename); - XmlNodePtr rootNode = doc.rootNode(); + const XmlNodePtr rootNode = doc.rootNode(); if (!rootNode || !xmlNameEqual(rootNode, "sprite")) { @@ -470,19 +473,22 @@ SpriteDirection SpriteDef::makeSpriteDirection(const std::string &direction) return DIRECTION_INVALID; } -void SpriteDef::addAction(unsigned hp, std::string name, Action *action) +void SpriteDef::addAction(const unsigned hp, const std::string &name, + Action *const action) { - Actions::const_iterator i = mActions.find(hp); + const Actions::const_iterator i = mActions.find(hp); if (i == mActions.end()) mActions[hp] = new ActionMap(); (*mActions[hp])[name] = action; } -bool SpriteDef::addSequence(int start, int end, int delay, - int offsetX, int offsetY, int variant_offset, - int repeat, int rand, ImageSet *imageSet, - Animation *animation) +bool SpriteDef::addSequence(const int start, const int end, const int delay, + const int offsetX, const int offsetY, + const int variant_offset, + int repeat, const int rand, + ImageSet *const imageSet, + Animation *const animation) const { if (start < 0 || end < 0) { @@ -497,7 +503,7 @@ bool SpriteDef::addSequence(int start, int end, int delay, int pos = start; while (end >= pos) { - Image *img = imageSet->get(pos + variant_offset); + Image *const img = imageSet->get(pos + variant_offset); if (!img) { @@ -521,7 +527,7 @@ bool SpriteDef::addSequence(int start, int end, int delay, int pos = start; while (end <= pos) { - Image *img = imageSet->get(pos + variant_offset); + Image *const img = imageSet->get(pos + variant_offset); if (!img) { diff --git a/src/resources/spritedef.h b/src/resources/spritedef.h index 461e60230..565c3a95a 100644 --- a/src/resources/spritedef.h +++ b/src/resources/spritedef.h @@ -44,7 +44,7 @@ struct SpriteReference sprite(""), variant(0) {} - SpriteReference(std::string sprite0, int variant0) : + SpriteReference(const std::string &sprite0, const int variant0) : sprite(sprite0), variant(variant0) { } @@ -113,14 +113,14 @@ class SpriteDef : public Resource /** * Loads a sprite definition file. */ - static SpriteDef *load(const std::string &file, int variant); + static SpriteDef *load(const std::string &file, const int variant); /** * Returns the specified action. */ - Action *getAction(std::string action, unsigned num) const; + Action *getAction(const std::string &action, const unsigned num) const; - unsigned findNumber(unsigned num) const; + unsigned findNumber(const unsigned num) const; /** * Converts a string into a SpriteDirection enum. @@ -129,12 +129,15 @@ class SpriteDef : public Resource makeSpriteDirection(const std::string &direction); - void addAction(unsigned hp, std::string name, Action *action); + void addAction(const unsigned hp, const std::string &name, + Action *const action); - bool addSequence(int start, int end, int delay, - int offsetX, int offsetY, int variant_offset, - int repeat, int rand, ImageSet *imageSet, - Animation *animation); + bool addSequence(const int start, const int end, const int delay, + const int offsetX, const int offsetY, + const int variant_offset, + int repeat, const int rand, + ImageSet *const imageSet, + Animation *const animation) const; private: /** @@ -151,30 +154,30 @@ class SpriteDef : public Resource /** * Loads a sprite element. */ - void loadSprite(XmlNodePtr spriteNode, int variant, + void loadSprite(const XmlNodePtr spriteNode, const int variant, const std::string &palettes = ""); /** * Loads an imageset element. */ - void loadImageSet(XmlNodePtr node, const std::string &palettes); + void loadImageSet(const XmlNodePtr node, const std::string &palettes); /** * Loads an action element. */ - void loadAction(XmlNodePtr node, int variant_offset); + void loadAction(const XmlNodePtr node, const int variant_offset); /** * Loads an animation element. */ - void loadAnimation(XmlNodePtr animationNode, - Action *action, ImageSet *imageSet, - int variant_offset); + void loadAnimation(const XmlNodePtr animationNode, + Action *const action, ImageSet *const imageSet, + const int variant_offset); /** * Include another sprite into this one. */ - void includeSprite(XmlNodePtr includeNode); + void includeSprite(const XmlNodePtr includeNode); /** * Complete missing actions by copying existing ones. diff --git a/src/resources/subimage.cpp b/src/resources/subimage.cpp index 2dc7d4a7a..138805d24 100644 --- a/src/resources/subimage.cpp +++ b/src/resources/subimage.cpp @@ -31,8 +31,9 @@ #include "debug.h" -SubImage::SubImage(Image *parent, SDL_Surface *image, - int x, int y, int width, int height): +SubImage::SubImage(Image *const parent, SDL_Surface *const image, + const int x, const int y, + const int width, const int height) : Image(image), mParent(parent) { @@ -77,9 +78,9 @@ SubImage::SubImage(Image *parent, SDL_Surface *image, } #ifdef USE_OPENGL -SubImage::SubImage(Image *parent, GLuint image, - int x, int y, int width, int height, - int texWidth, int texHeight): +SubImage::SubImage(Image *const parent, const GLuint image, + const int x, const int y, const int width, const int height, + const int texWidth, const int texHeight): Image(image, width, height, texWidth, texHeight), mParent(parent) { @@ -128,7 +129,8 @@ SubImage::~SubImage() } } -Image *SubImage::getSubImage(int x, int y, int w, int h) +Image *SubImage::getSubImage(const int x, const int y, + const int w, const int h) { if (mParent) return mParent->getSubImage(mBounds.x + x, mBounds.y + y, w, h); diff --git a/src/resources/subimage.h b/src/resources/subimage.h index 1ff268288..4b5478b71 100644 --- a/src/resources/subimage.h +++ b/src/resources/subimage.h @@ -51,11 +51,12 @@ class SubImage : public Image /** * Constructor. */ - SubImage(Image *parent, SDL_Surface *image, - int x, int y, int width, int height); + SubImage(Image *const parent, SDL_Surface *const image, + const int x, const int y, const int width, const int height); #ifdef USE_OPENGL - SubImage(Image *parent, GLuint image, int x, int y, - int width, int height, int texWidth, int textHeight); + SubImage(Image *const parent, const GLuint image, + const int x, const int y, const int width, const int height, + const int texWidth, const int textHeight); #endif /** diff --git a/src/resources/wallpaper.cpp b/src/resources/wallpaper.cpp index 9ee87e33a..3a5cd05b7 100644 --- a/src/resources/wallpaper.cpp +++ b/src/resources/wallpaper.cpp @@ -44,7 +44,7 @@ struct WallpaperData int height; }; -static bool wallpaperCompare(WallpaperData a, WallpaperData b); +static bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b); static std::vector wallpaperData; static bool haveBackup; // Is the backup (no size given) version available? @@ -55,7 +55,7 @@ static std::string wallpaperFile; // Search for the wallpaper path values sequentially.. static void initDefaultWallpaperPaths() { - ResourceManager *resman = ResourceManager::getInstance(); + ResourceManager *const resman = ResourceManager::getInstance(); // Init the path wallpaperPath = branding.getStringValue("wallpapersPath"); @@ -78,10 +78,10 @@ static void initDefaultWallpaperPaths() wallpaperFile = "login_wallpaper.png"; } -static bool wallpaperCompare(WallpaperData a, WallpaperData b) +static bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b) { - int aa = a.width * a.height; - int ab = b.width * b.height; + const int aa = a.width * a.height; + const int ab = b.width * b.height; return (aa > ab || (aa == ab && a.width > b.width)); } @@ -135,7 +135,7 @@ void Wallpaper::loadWallpapers() std::sort(wallpaperData.begin(), wallpaperData.end(), wallpaperCompare); } -std::string Wallpaper::getWallpaper(int width, int height) +std::string Wallpaper::getWallpaper(const int width, const int height) { WallpaperData wp; diff --git a/src/resources/wallpaper.h b/src/resources/wallpaper.h index 7123cfd96..517ace011 100644 --- a/src/resources/wallpaper.h +++ b/src/resources/wallpaper.h @@ -45,7 +45,7 @@ class Wallpaper * @param height the desired height * @return the file to use, or empty if no wallpapers are useable */ - static std::string getWallpaper(int width, int height); + static std::string getWallpaper(const int width, const int height); }; #endif // WALLPAPER_H -- cgit v1.2.3-60-g2f50