From d25e1c9b0dac7fcfe803083af1189489d5a9ad88 Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Sat, 5 Aug 2006 02:20:34 +0000 Subject: Some cleanups, mostly lowering indentation levels. --- src/being.cpp | 40 ++++---- src/configuration.cpp | 56 +++++------ src/engine.cpp | 9 +- src/game.cpp | 27 ++---- src/player.cpp | 20 ++-- src/resources/itemmanager.cpp | 4 +- src/resources/mapreader.cpp | 214 +++++++++++++++++++----------------------- 7 files changed, 162 insertions(+), 208 deletions(-) (limited to 'src') diff --git a/src/being.cpp b/src/being.cpp index a688aec5..1ca8929a 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -205,18 +205,18 @@ Being::setAction(Uint8 action) for (int i = 0; i < VECTOREND_SPRITE; i++) { - if (mSprites[i] != NULL) - { - if (currentAction == ACTION_ATTACK || + if (!mSprites[i]) + continue; + + if (currentAction == ACTION_ATTACK || currentAction == ACTION_ATTACK_STAB || currentAction == ACTION_ATTACK_BOW) - { - mSprites[i]->play(currentAction, mAttackSpeed); - } - else - { - mSprites[i]->play(currentAction); - } + { + mSprites[i]->play(currentAction, mAttackSpeed); + } + else + { + mSprites[i]->play(currentAction); } } @@ -231,7 +231,8 @@ Being::setDirection(Uint8 direction) for (int i = 0; i < VECTOREND_SPRITE; i++) { - if (mSprites[i] != NULL) mSprites[i]->setDirection(dir); + if (mSprites[i] != NULL) + mSprites[i]->setDirection(dir); } } @@ -332,10 +333,6 @@ Being::draw(Graphics *graphics, int offsetX, int offsetY) int px = mPx + offsetX; int py = mPy + offsetY; - //what are these two lines good for? please add a comment. - unsigned char dir = 0; - while (!(mDirection & (1 << dir))) dir++; - for (int i = 0; i < VECTOREND_SPRITE; i++) { if (mSprites[i] != NULL) @@ -348,14 +345,13 @@ Being::draw(Graphics *graphics, int offsetX, int offsetY) void Being::drawEmotion(Graphics *graphics, Sint32 offsetX, Sint32 offsetY) { - int px = mPx + offsetX; - int py = mPy + offsetY; + if (!mEmotion) + return; - if (mEmotion) - { - graphics->drawImage(emotionset->get(mEmotion - 1), - px + 3, py - 60); - } + int px = mPx + offsetX + 3; + int py = mPy + offsetY - 60; + + graphics->drawImage(emotionset->get(mEmotion - 1), px, py); } void diff --git a/src/configuration.cpp b/src/configuration.cpp index 4085b20b..53f6785e 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -57,18 +57,18 @@ void Configuration::init(const std::string &filename) for (node = node->xmlChildrenNode; node != NULL; node = node->next) { - if (xmlStrEqual(node->name, BAD_CAST "option")) - { - xmlChar *name = xmlGetProp(node, BAD_CAST "name"); - xmlChar *value = xmlGetProp(node, BAD_CAST "value"); + if (!xmlStrEqual(node->name, BAD_CAST "option")) + continue; - if (name && value) { - mOptions[(const char*)name] = (const char*)value; - } + xmlChar *name = xmlGetProp(node, BAD_CAST "name"); + xmlChar *value = xmlGetProp(node, BAD_CAST "value"); - if (name) xmlFree(name); - if (value) xmlFree(value); + if (name && value) { + mOptions[(const char*)name] = (const char*)value; } + + if (name) xmlFree(name); + if (value) xmlFree(value); } xmlFreeDoc(doc); @@ -87,28 +87,28 @@ void Configuration::write() xmlTextWriterPtr writer = xmlNewTextWriterFilename(mConfigPath.c_str(), 0); - if (writer) - { - xmlTextWriterSetIndent(writer, 1); - xmlTextWriterStartDocument(writer, NULL, NULL, NULL); - xmlTextWriterStartElement(writer, BAD_CAST "configuration"); + if (!writer) + return; - for (OptionIterator i = mOptions.begin(); i != mOptions.end(); i++) - { - logger->log("Configuration::write(%s, \"%s\")", - i->first.c_str(), i->second.c_str()); - - xmlTextWriterStartElement(writer, BAD_CAST "option"); - xmlTextWriterWriteAttribute(writer, - BAD_CAST "name", BAD_CAST i->first.c_str()); - xmlTextWriterWriteAttribute(writer, - BAD_CAST "value", BAD_CAST i->second.c_str()); - xmlTextWriterEndElement(writer); - } + xmlTextWriterSetIndent(writer, 1); + xmlTextWriterStartDocument(writer, NULL, NULL, NULL); + xmlTextWriterStartElement(writer, BAD_CAST "configuration"); - xmlTextWriterEndDocument(writer); - xmlFreeTextWriter(writer); + for (OptionIterator i = mOptions.begin(); i != mOptions.end(); i++) + { + logger->log("Configuration::write(%s, \"%s\")", + i->first.c_str(), i->second.c_str()); + + xmlTextWriterStartElement(writer, BAD_CAST "option"); + xmlTextWriterWriteAttribute(writer, + BAD_CAST "name", BAD_CAST i->first.c_str()); + xmlTextWriterWriteAttribute(writer, + BAD_CAST "value", BAD_CAST i->second.c_str()); + xmlTextWriterEndElement(writer); } + + xmlTextWriterEndDocument(writer); + xmlFreeTextWriter(writer); } void Configuration::setValue(const std::string &key, std::string value) diff --git a/src/engine.cpp b/src/engine.cpp index b57201a0..01bbbaa2 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -101,11 +101,8 @@ Engine::~Engine() emotionset->decRef(); itemset->decRef(); - std::vector::iterator iter; - for (iter = weaponset.begin(); iter != weaponset.end(); ++iter) - { - (*iter)->decRef(); - } + std::for_each(weaponset.begin(), weaponset.end(), + std::mem_fun(&Spriteset::decRef)); weaponset.clear(); delete itemDb; @@ -214,12 +211,12 @@ void Engine::draw(Graphics *graphics) player_node->mX, player_node->mY, mouseTileX, mouseTileY); + graphics->setColor(gcn::Color(255, 0, 0)); for (PathIterator i = debugPath.begin(); i != debugPath.end(); i++) { int squareX = i->x * 32 - map_x + 12; int squareY = i->y * 32 - map_y + 12; - graphics->setColor(gcn::Color(255, 0, 0)); graphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); graphics->drawText( toString(mCurrentMap->getMetaTile(i->x, i->y)->Gcost), diff --git a/src/game.cpp b/src/game.cpp index 51b541a6..0d834468 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -359,29 +359,16 @@ void Game::logic() gameTime = tick_time; fpsLimit = (int)config.getValue("fpslimit", 50); - if (fpsLimit) - { - delta = 1000 / fpsLimit; - } - else - { - delta = 0; - } + delta = fpsLimit ? 1000 / fpsLimit : 0; // Update the screen when application is active, delay otherwise - if (SDL_GetAppState() & SDL_APPACTIVE) + if (SDL_GetAppState() & SDL_APPACTIVE && + (abs(tick_time * 10 - drawTime) >= delta)) { - if (abs(tick_time * 10 - drawTime) >= delta) - { - frame++; - engine->draw(graphics); - graphics->updateScreen(); - drawTime += delta; - } - else - { - SDL_Delay(10); - } + frame++; + engine->draw(graphics); + graphics->updateScreen(); + drawTime += delta; } else { diff --git a/src/player.cpp b/src/player.cpp index 2e119e2d..c626fd09 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -107,11 +107,7 @@ Player::setHairColor(Uint16 color) { if (color != mHairColor && mHairStyle > 0) { - if (mSprites[HAIR_SPRITE]) - { - delete mSprites[HAIR_SPRITE]; - } - + delete mSprites[HAIR_SPRITE]; AnimatedSprite *newHairSprite = new AnimatedSprite( "graphics/sprites/hairstyle" + toString(mHairStyle) + ".xml", color - 1); @@ -130,11 +126,7 @@ Player::setHairStyle(Uint16 style) { if (style != mHairStyle && mHairColor > 0) { - if (mSprites[HAIR_SPRITE]) - { - delete mSprites[HAIR_SPRITE]; - } - + delete mSprites[HAIR_SPRITE]; AnimatedSprite *newHairSprite = new AnimatedSprite( "graphics/sprites/hairstyle" + toString(style) + ".xml", mHairColor - 1); @@ -164,11 +156,11 @@ Player::setVisibleEquipment(Uint8 slot, Uint8 id) position = TOPCLOTHES_SPRITE; break; } + + delete mSprites[position]; + mSprites[position] = NULL; + // id = 0 means unequip - if (mSprites[position]) { - delete mSprites[position]; - mSprites[position] = NULL; - } if (id) { char stringId[4]; sprintf(stringId, "%03i", id); diff --git a/src/resources/itemmanager.cpp b/src/resources/itemmanager.cpp index 56ac6354..63c0b036 100644 --- a/src/resources/itemmanager.cpp +++ b/src/resources/itemmanager.cpp @@ -30,6 +30,8 @@ #include "../log.h" +#include "../utils/dtor.h" + #define READ_PROP(node, prop, name, target, cast) \ prop = xmlGetProp(node, BAD_CAST name); \ if (prop) { \ @@ -56,14 +58,12 @@ ItemManager::ItemManager() if (!doc) { logger->error("Item Manager: Error while parsing item database (items.xml)!"); - return; } xmlNodePtr node = xmlDocGetRootElement(doc); if (!node || !xmlStrEqual(node->name, BAD_CAST "items")) { logger->error("Item Manager: items.xml is not a valid database file!"); - return; } for (node = node->xmlChildrenNode; node != NULL; node = node->next) diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index af79480a..382b0797 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -28,7 +28,6 @@ #include "resourcemanager.h" #include "image.h" -#include "spriteset.h" #include "../base64.h" #include "../log.h" @@ -220,34 +219,26 @@ MapReader::readMap(xmlNodePtr node, const std::string &path) void MapReader::readProperties(xmlNodePtr node, Properties* props) { - node = node->xmlChildrenNode; + for (node = node->xmlChildrenNode; node; node = node->next) { + if (!xmlStrEqual(node->name, BAD_CAST "property")) + continue; - while (node != NULL) - { - if (xmlStrEqual(node->name, BAD_CAST "property")) - { - // Example: - - xmlChar *name = xmlGetProp(node, BAD_CAST "name"); - xmlChar *value = xmlGetProp(node, BAD_CAST "value"); + // Example: + xmlChar *name = xmlGetProp(node, BAD_CAST "name"); + xmlChar *value = xmlGetProp(node, BAD_CAST "value"); - if (name && value) - { - props->setProperty((const char*) name, (const char*) value); - } - - if (name) xmlFree(name); - if (value) xmlFree(value); + if (name && value) { + props->setProperty((const char*)name, (const char*)value); } - node = node->next; + if (name) xmlFree(name); + if (value) xmlFree(value); } } void MapReader::readLayer(xmlNodePtr node, Map *map, int layer) { - node = node->xmlChildrenNode; int h = map->getHeight(); int w = map->getWidth(); int x = 0; @@ -255,89 +246,86 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer) // Load the tile data. Layers are assumed to be map size, with (0,0) as // origin. - while (node != NULL) - { - if (xmlStrEqual(node->name, BAD_CAST "data")) + for (node = node->xmlChildrenNode; node; node = node->next) { + if (!xmlStrEqual(node->name, BAD_CAST "data")) + continue; + + xmlChar *encoding = xmlGetProp(node, BAD_CAST "encoding"); + xmlChar *compression = xmlGetProp(node, BAD_CAST "compression"); + + if (encoding && xmlStrEqual(encoding, BAD_CAST "base64")) { - xmlChar *encoding = xmlGetProp(node, BAD_CAST "encoding"); - xmlChar *compression = xmlGetProp(node, BAD_CAST "compression"); + xmlFree(encoding); - if (encoding && xmlStrEqual(encoding, BAD_CAST "base64")) - { - xmlFree(encoding); + if (compression) { + logger->log("Warning: no layer compression supported!"); + xmlFree(compression); + return; + } - if (compression) { - logger->log("Warning: no layer compression supported!"); - xmlFree(compression); - return; - } + // Read base64 encoded map file + xmlNodePtr dataChild = node->xmlChildrenNode; + if (!dataChild) + continue; - // Read base64 encoded map file - xmlNodePtr dataChild = node->xmlChildrenNode; - if (!dataChild) continue; - - int len = strlen((const char*)dataChild->content) + 1; - unsigned char *charData = new unsigned char[len + 1]; - const char *charStart = (const char*)dataChild->content; - unsigned char *charIndex = charData; - - while (*charStart) { - if (*charStart != ' ' && *charStart != '\t' && - *charStart != '\n') - { - *charIndex = *charStart; - charIndex++; - } - charStart++; + int len = strlen((const char*)dataChild->content) + 1; + unsigned char *charData = new unsigned char[len + 1]; + const char *charStart = (const char*)dataChild->content; + unsigned char *charIndex = charData; + + while (*charStart) { + if (*charStart != ' ' && *charStart != '\t' && + *charStart != '\n') + { + *charIndex = *charStart; + charIndex++; } - *charIndex = '\0'; + charStart++; + } + *charIndex = '\0'; - int binLen; - unsigned char *binData = - php_base64_decode(charData, strlen((char*)charData), - &binLen); + int binLen; + unsigned char *binData = + php_base64_decode(charData, strlen((char*)charData), + &binLen); - delete[] charData; + delete[] charData; - if (binData) { - for (int i = 0; i < binLen - 3; i += 4) { - int gid = binData[i] | - binData[i + 1] << 8 | - binData[i + 2] << 16 | - binData[i + 3] << 24; + if (binData) { + for (int i = 0; i < binLen - 3; i += 4) { + int gid = binData[i] | + binData[i + 1] << 8 | + binData[i + 2] << 16 | + binData[i + 3] << 24; - map->setTileWithGid(x, y, layer, gid); + map->setTileWithGid(x, y, layer, gid); - x++; - if (x == w) {x = 0; y++;} - } - free(binData); + x++; + if (x == w) {x = 0; y++;} } + free(binData); } - else { - // Read plain XML map file - xmlNodePtr n2 = node->xmlChildrenNode; - - while (n2 != NULL) - { - if (xmlStrEqual(n2->name, BAD_CAST "tile") && y < h) - { - int gid = getProperty(n2, "gid", -1); - map->setTileWithGid(x, y, layer, gid); - - x++; - if (x == w) {x = 0; y++;} - } - - n2 = n2->next; + } + else { + // Read plain XML map file + for (xmlNodePtr n2 = node->xmlChildrenNode; n2; n2 = n2->next) { + if (!xmlStrEqual(n2->name, BAD_CAST "tile")) + continue; + + int gid = getProperty(n2, "gid", -1); + map->setTileWithGid(x, y, layer, gid); + + x++; + if (x == w) { + x = 0; y++; + if (y >= h) + break; } } - - // There can be only one data element - break; } - node = node->next; + // There can be only one data element + break; } } @@ -356,38 +344,33 @@ MapReader::readTileset(xmlNodePtr node, int tw = getProperty(node, "tilewidth", map->getTileWidth()); int th = getProperty(node, "tileheight", map->getTileHeight()); - node = node->xmlChildrenNode; + for (node = node->xmlChildrenNode; node; node = node->next) { + if (!xmlStrEqual(node->name, BAD_CAST "image")) + continue; - while (node != NULL) - { - if (xmlStrEqual(node->name, BAD_CAST "image")) - { - xmlChar* source = xmlGetProp(node, BAD_CAST "source"); + xmlChar* source = xmlGetProp(node, BAD_CAST "source"); - if (source) - { - std::string sourceStr = std::string((const char*)source); - sourceStr.erase(0, 3); // Remove "../" + if (source) + { + std::string sourceStr = std::string((const char*)source); + sourceStr.erase(0, 3); // Remove "../" - ResourceManager *resman = ResourceManager::getInstance(); - Image* tilebmp = resman->getImage(sourceStr); + ResourceManager *resman = ResourceManager::getInstance(); + Image* tilebmp = resman->getImage(sourceStr); - if (tilebmp) - { - Tileset *set = new Tileset(tilebmp, tw, th, firstGid); - tilebmp->decRef(); - xmlFree(source); - return set; - } - else { - logger->log("Warning: Failed to load tileset (%s)", source); - } + if (tilebmp) + { + Tileset *set = new Tileset(tilebmp, tw, th, firstGid); + tilebmp->decRef(); + xmlFree(source); + return set; + } + else { + logger->log("Warning: Failed to load tileset (%s)", source); } - - break; } - node = node->next; + break; } return NULL; @@ -396,13 +379,12 @@ MapReader::readTileset(xmlNodePtr node, int MapReader::getProperty(xmlNodePtr node, const char* name, int def) { + int &ret = def; + xmlChar *prop = xmlGetProp(node, BAD_CAST name); if (prop) { - int val = atoi((char*)prop); + ret = atoi((char*)prop); xmlFree(prop); - return val; - } - else { - return def; } + return ret; } -- cgit v1.2.3-70-g09d2