diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-10-07 14:57:15 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-10-07 14:57:15 +0000 |
commit | e4530c6d0cd180ab18469bf8e32fb978229dedde (patch) | |
tree | 218175c6d472315e59c988b168a25b7452cc3b00 /src/resources/mapreader.cpp | |
parent | 7b9a9c55d6e73ba9151042e27ad4bc330d11617f (diff) | |
download | mana-e4530c6d0cd180ab18469bf8e32fb978229dedde.tar.gz mana-e4530c6d0cd180ab18469bf8e32fb978229dedde.tar.bz2 mana-e4530c6d0cd180ab18469bf8e32fb978229dedde.tar.xz mana-e4530c6d0cd180ab18469bf8e32fb978229dedde.zip |
Added support for gzip compressed map layer data. Increased version to 0.0.22.
Cleaned up overlay initialization a bit.
Diffstat (limited to 'src/resources/mapreader.cpp')
-rw-r--r-- | src/resources/mapreader.cpp | 130 |
1 files changed, 68 insertions, 62 deletions
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 2377a8f4..2aea3dc5 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -52,7 +52,7 @@ inflateMemory(unsigned char *in, unsigned int inLength, int ret; z_stream strm; - out = (unsigned char*)malloc(bufferSize); + out = (unsigned char*) malloc(bufferSize); strm.zalloc = Z_NULL; strm.zfree = Z_NULL; @@ -83,13 +83,13 @@ inflateMemory(unsigned char *in, unsigned int inLength, ret = Z_DATA_ERROR; case Z_DATA_ERROR: case Z_MEM_ERROR: - (void)inflateEnd(&strm); + (void) inflateEnd(&strm); return ret; } if (ret != Z_STREAM_END) { - out = (unsigned char*)realloc(out, bufferSize * 2); + out = (unsigned char*) realloc(out, bufferSize * 2); if (out == NULL) { @@ -106,10 +106,44 @@ inflateMemory(unsigned char *in, unsigned int inLength, assert(strm.avail_in == 0); outLength = bufferSize - strm.avail_out; - (void)inflateEnd(&strm); + (void) inflateEnd(&strm); return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; } +int +inflateMemory(unsigned char *in, unsigned int inLength, + unsigned char *&out) +{ + unsigned int outLength = 0; + int ret = inflateMemory(in, inLength, out, outLength); + + if (ret != Z_OK || out == NULL) + { + if (ret == Z_MEM_ERROR) + { + logger->log("Error: Out of memory while decompressing map data!"); + } + else if (ret == Z_VERSION_ERROR) + { + logger->log("Error: Incompatible zlib version!"); + } + else if (ret == Z_DATA_ERROR) + { + logger->log("Error: Incorrect zlib compressed data!"); + } + else + { + logger->log("Error: Unknown error while decompressing map data!"); + } + + free(out); + out = NULL; + outLength = 0; + } + + return outLength; +} + Map* MapReader::readMap(const std::string &filename) { @@ -127,33 +161,17 @@ MapReader::readMap(const std::string &filename) // Inflate the gzipped map data unsigned char *inflated; - unsigned int inflatedSize = 0; - int ret = inflateMemory((unsigned char*)buffer, - fileSize, inflated, inflatedSize); + unsigned int inflatedSize = inflateMemory((unsigned char*) buffer, + fileSize, inflated); free(buffer); - if (ret == Z_MEM_ERROR) - { - logger->log("Error: Out of memory while decompressing map data!"); - return NULL; - } - else if (ret == Z_VERSION_ERROR) - { - logger->log("Error: Incompatible zlib version!"); - return NULL; - } - else if (ret == Z_DATA_ERROR) + if (inflated == NULL) { - logger->log("Error: Incorrect zlib compressed data!"); - return NULL; - } - else if (ret != Z_OK || inflated == NULL) - { - logger->log("Error: Unknown error while decompressing map data!"); + logger->log("Could not decompress map file (%s)\n", filename.c_str()); return NULL; } - xmlDocPtr doc = xmlParseMemory((char*)inflated, inflatedSize); + xmlDocPtr doc = xmlParseMemory((char*) inflated, inflatedSize); free(inflated); // Parse the inflated map data @@ -215,39 +233,7 @@ MapReader::readMap(xmlNodePtr node, const std::string &path) } } - //set Overlays - ResourceManager *resman = ResourceManager::getInstance(); - for (int i = 0; ; i++) - { - const std::string name = "overlay" + toString(i); - - if (!map->hasProperty(name + "image")) - break; // Finished - - Image *img = resman->getImage(map->getProperty(name + "image")); - float scrollX = 0.0f; - float scrollY = 0.0f; - float parallax = 0.0f; - std::stringstream ss; - - if (map->hasProperty(name + "scrollX")) - { - ss.str(map->getProperty(name + "scrollX")); - ss >> scrollX; - } - if (map->hasProperty(name + "scrollY")) - { - ss.str(map->getProperty(name + "scrollY")); - ss >> scrollY; - } - if (map->hasProperty(name + "parallax")) - { - ss.str(map->getProperty(name + "parallax")); - ss >> parallax; - } - map->setOverlay(img, scrollX, scrollY, parallax); - img->decRef(); - } + map->initializeOverlays(); return map; } @@ -293,8 +279,8 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer) { xmlFree(encoding); - if (compression) { - logger->log("Warning: no layer compression supported!"); + if (compression && !xmlStrEqual(compression, BAD_CAST "gzip")) { + logger->log("Warning: only gzip layer compression supported!"); xmlFree(compression); return; } @@ -322,12 +308,32 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer) int binLen; unsigned char *binData = - php_base64_decode(charData, strlen((char*)charData), - &binLen); + php_base64_decode(charData, strlen((char*)charData), &binLen); delete[] charData; if (binData) { + if (compression) { + if (xmlStrEqual(compression, BAD_CAST "gzip")) { + // Inflate the gzipped layer data + unsigned char *inflated; + unsigned int inflatedSize = + inflateMemory(binData, binLen, inflated); + + free(binData); + binData = inflated; + binLen = inflatedSize; + + if (inflated == NULL) + { + logger->log("Error: Could not decompress layer!"); + xmlFree(compression); + return; + } + } + xmlFree(compression); + } + for (int i = 0; i < binLen - 3; i += 4) { int gid = binData[i] | binData[i + 1] << 8 | |