summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/image.h2
-rw-r--r--src/resources/itemmanager.cpp4
-rw-r--r--src/resources/mapreader.cpp214
-rw-r--r--src/resources/openglsdlimageloader.cpp6
4 files changed, 106 insertions, 120 deletions
diff --git a/src/resources/image.h b/src/resources/image.h
index 1f67fcae..78751394 100644
--- a/src/resources/image.h
+++ b/src/resources/image.h
@@ -24,6 +24,8 @@
#ifndef _TMW_IMAGE_H
#define _TMW_IMAGE_H
+#include "../main.h"
+
#include <SDL.h>
#ifdef USE_OPENGL
#include <SDL_opengl.h>
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: <property name="name" value="value"/>
-
- xmlChar *name = xmlGetProp(node, BAD_CAST "name");
- xmlChar *value = xmlGetProp(node, BAD_CAST "value");
+ // Example: <property name="name" value="value"/>
+ 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;
}
diff --git a/src/resources/openglsdlimageloader.cpp b/src/resources/openglsdlimageloader.cpp
index c7ddec74..b3e1601e 100644
--- a/src/resources/openglsdlimageloader.cpp
+++ b/src/resources/openglsdlimageloader.cpp
@@ -21,14 +21,16 @@
* $Id: sdlimageloader.cpp 2121 2006-01-31 02:55:26Z der_doener $
*/
-#ifdef USE_OPENGL
-
#include "openglsdlimageloader.h"
#include <string>
#include "resourcemanager.h"
+#include "../main.h"
+
+#ifdef USE_OPENGL
+
SDL_Surface* OpenGLSDLImageLoader::loadSDLSurface(const std::string& filename)
{
ResourceManager *resman = ResourceManager::getInstance();