From f741c14337dd0850a7201f5aa2185e40a625353a Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Mon, 21 Feb 2005 00:03:11 +0000 Subject: Updates to change log, file lists, lots of doxygen comments and small changes. --- src/resources/image.h | 15 +++++-- src/resources/mapreader.cpp | 95 +++++++++++++++++++++++++++-------------- src/resources/mapreader.h | 12 ++++-- src/resources/resourcemanager.h | 3 +- 4 files changed, 86 insertions(+), 39 deletions(-) (limited to 'src/resources') diff --git a/src/resources/image.h b/src/resources/image.h index ed1e0905..9102f62c 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -64,7 +64,12 @@ class Image : public Resource /** * Loads an image. + * * @param filePath The path to the image file to load. + * @param flags These flags allow controlling the way the image is + * loaded. Currently only IMG_ALPHA is supported, which + * causes alpha layer to be preserved. + * * @return NULL if the an error occurred, a * valid pointer otherwise. */ @@ -72,10 +77,12 @@ class Image : public Resource /** * Loads an image from a buffer in memory. - * @param buffer The memory buffer containing the image - * data. - * @return NULL if the an error occurred, a - * valid pointer otherwise. + * + * @param buffer The memory buffer containing the image data. + * @param bufferSize The size of the memory buffer in bytes. + * + * @return NULL if the an error occurred, a valid pointer + * otherwise. */ static Image *load(const char* buffer, unsigned int bufferSize); diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 32b71d9c..b28515c1 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -31,6 +31,13 @@ #define DEFAULT_TILE_WIDTH 32 #define DEFAULT_TILE_HEIGHT 32 +// MSVC libxml2 at the moment doesn't work right when using MinGW, missing this +// function at link time. +#ifdef WIN32 +#undef xmlFree +#define xmlFree(x) ; +#endif + std::vector MapReader::tilesets; Tileset::Tileset(Image *img, int w, int h, int firstGid): @@ -100,9 +107,7 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path) std::string pathDir = path.substr(0, path.rfind("/") + 1); prop = xmlGetProp(node, BAD_CAST "version"); -#ifndef WIN32 xmlFree(prop); -#endif int w = getProperty(node, "width", 0); int h = getProperty(node, "height", 0); @@ -144,41 +149,49 @@ void MapReader::readLayer(xmlNodePtr node, Map *map, int layer) // origin. while (node != NULL) { - if (xmlStrEqual(node->name, BAD_CAST "tile") && y < h) + if (xmlStrEqual(node->name, BAD_CAST "data")) { - int gid = getProperty(node, "gid", -1); - Image *img = NULL; + xmlChar *encoding = xmlGetProp(node, BAD_CAST "encoding"); + xmlChar *compression = xmlGetProp(node, BAD_CAST "compression"); - if (gid > -1) { - std::vector::iterator i; - Tileset *set = NULL; + if (encoding && xmlStrEqual(encoding, BAD_CAST "base64")) + { + xmlFree(encoding); - // Find the tileset with the highest firstGid below/eq to gid - for (i = tilesets.begin(); i != tilesets.end(); ++i) { - if ((*i)->getFirstGid() <= gid) { - set = (*i); - } + if (compression) { + log("Warning: no layer compression supported!"); + xmlFree(compression); + return; } - if (set && (gid - set->getFirstGid()) < - (int)set->spriteset.size()) + // Read base64 encoded map file + + } + else { + // Read plain XML map file + xmlNodePtr n2 = node->xmlChildrenNode; + + while (n2 != NULL) { - img = set->spriteset[gid - set->getFirstGid()]; + if (xmlStrEqual(n2->name, BAD_CAST "tile") && y < h) + { + int gid = getProperty(n2, "gid", -1); + if (layer == 0) map->setWalk(x, y, true); + map->setTile(x, y, layer, getTileWithGid(gid)); + + x++; + if (x == w) {x = 0; y++;} + } + + n2 = n2->next; } } - if (layer == 0) map->setWalk(x, y, true); - map->setTile(x, y, layer, img); - - x++; - if (x == w) {x = 0; y++;} - } - - if (xmlStrEqual(node->name, BAD_CAST "data")) { - node = node->xmlChildrenNode; - } else { - node = node->next; + // There can be only one data element + break; } + + node = node->next; } } @@ -211,9 +224,7 @@ Tileset* MapReader::readTileset( if (tilebmp) { Tileset *set = new Tileset(tilebmp, tw, th, firstGid); -#ifndef WIN32 xmlFree(source); -#endif return set; } else { @@ -235,12 +246,34 @@ int MapReader::getProperty(xmlNodePtr node, const char* name, int def) xmlChar *prop = xmlGetProp(node, BAD_CAST name); if (prop) { int val = atoi((char*)prop); -#ifndef WIN32 xmlFree(prop); -#endif return val; } else { return def; } } + +Image *MapReader::getTileWithGid(int gid) +{ + std::vector::iterator i; + Tileset *set = NULL; + + // Find the tileset with the highest firstGid below/eq to gid + for (i = tilesets.begin(); i != tilesets.end(); ++i) + { + if ((*i)->getFirstGid() <= gid) { + set = (*i); + } + else { + break; + } + } + + if (set && (gid - set->getFirstGid()) < (int)set->spriteset.size()) + { + return set->spriteset[gid - set->getFirstGid()]; + } + + return NULL; +} diff --git a/src/resources/mapreader.h b/src/resources/mapreader.h index b2a076ec..69c531de 100644 --- a/src/resources/mapreader.h +++ b/src/resources/mapreader.h @@ -68,21 +68,27 @@ class MapReader private: /** - * Helper function that handles reading a map layer. + * Reads a map layer. */ static void readLayer(xmlNodePtr node, Map *map, int layer); /** - * Helper function that handles reading a tile set. + * Reads a tile set. */ static Tileset *readTileset(xmlNodePtr node, const std::string &path, Map *map); /** - * Helper function to get an integer property. + * Gets an integer property from an xmlNodePtr. */ static int getProperty(xmlNodePtr node, const char* name, int def); + /** + * Converts a global tile id to the Image* pointing to the associated + * tile image. + */ + static Image *getTileWithGid(int gid); + static std::vector tilesets; }; diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index c3ed8904..961c000a 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -74,8 +74,9 @@ class ResourceManager * converted into the appropriate path for the current operating system * and the resource is loaded. * - * @param type The type of resource to load. + * @param type The type of resource to load. * @param idPath The resource identifier path. + * @param flags Flags to control the loading of certain resources. * @return A valid resource or NULL if the resource could * not be loaded. */ -- cgit v1.2.3-70-g09d2