From ed73ad0514e018de2351c4d9223a56d61a8c1289 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Thu, 23 Jun 2005 01:05:12 +0000 Subject: Maps are now loaded through the resource manager. --- src/game.cpp | 42 ++++----------- src/gui/char_select.cpp | 2 +- src/resources/mapreader.cpp | 124 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 125 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index 60db563d..6927ee18 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -65,8 +65,8 @@ ConfirmDialog *exitConfirm = NULL; Being *target = NULL; -#define EMOTION_TIME 150 -#define MAX_TIME 10000 +const int EMOTION_TIME = 150; /**< Duration of emotion icon */ +const int MAX_TIME = 10000; /** * Listener used for handling death message. @@ -369,34 +369,14 @@ void do_input() { switch (player_node->direction) { - case NORTH: - y--; - break; - case SOUTH: - y++; - break; - case WEST: - x--; - break; - case EAST: - x++; - break; - case NW: - x--; - y--; - break; - case NE: - x++; - y--; - break; - case SW: - x--; - y++; - break; - case SE: - x++; - y++; - break; + case NORTH: y--; break; + case SOUTH: y++; break; + case WEST: x--; break; + case EAST: x++; break; + case NW: x--; y--; break; + case NE: x++; y--; break; + case SW: x--; y++; break; + case SE: x++; y++; break; } id = find_floor_item_by_cor(x, y); WFIFOW(0) = net_w_value(0x009f); @@ -1071,7 +1051,7 @@ void do_parse() // Warp case 0x0091: memset(map_path, '\0', 480); - strcat(map_path, TMW_DATADIR "data/maps/"); + strcat(map_path, "maps/"); strncat(map_path, RFIFOP(2), 497 - strlen(map_path)); logger->log("Warping to %s (%d, %d)", map_path, RFIFOW(18), RFIFOW(20)); diff --git a/src/gui/char_select.cpp b/src/gui/char_select.cpp index bfd9d03f..ebe1a3c8 100644 --- a/src/gui/char_select.cpp +++ b/src/gui/char_select.cpp @@ -225,7 +225,7 @@ void CharSelectDialog::serverCharSelect() } char_ID = RFIFOL(2); memset(map_path, '\0', 480); - strcat(map_path, TMW_DATADIR "data/maps/"); + strcat(map_path, "maps/"); strncat(map_path, RFIFOP(6), 479 - strlen(map_path)); map_address = RFIFOL(22); map_port = RFIFOW(26); diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index abd269d0..bf6ecdce 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -28,10 +28,11 @@ #include "../graphic/spriteset.h" #include "../base64.h" +#include #include -#define DEFAULT_TILE_WIDTH 32 -#define DEFAULT_TILE_HEIGHT 32 +const unsigned int DEFAULT_TILE_WIDTH = 32; +const unsigned int DEFAULT_TILE_HEIGHT = 32; // MSVC libxml2 at the moment doesn't work right when using MinGW, missing this // function at link time. @@ -40,6 +41,77 @@ #define xmlFree(x) ; #endif +/** + * Inflates either zlib or gzip deflated memory. The inflated memory is + * expected to be freed by the caller. + */ +int +inflateMemory( + unsigned char *in, unsigned int inLength, + unsigned char *&out, unsigned int &outLength) +{ + int bufferSize = 256 * 1024; + int ret; + z_stream strm; + + out = (unsigned char*)malloc(bufferSize); + + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.next_in = in; + strm.avail_in = inLength; + strm.next_out = out; + strm.avail_out = bufferSize; + + ret = inflateInit2(&strm, 15 + 32); + + if (ret != Z_OK) + return ret; + + do + { + if (strm.next_out == NULL) + { + inflateEnd(&strm); + return Z_MEM_ERROR; + } + + ret = inflate(&strm, Z_NO_FLUSH); + assert(ret != Z_STREAM_ERROR); + + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void)inflateEnd(&strm); + return ret; + } + + if (ret != Z_STREAM_END) + { + out = (unsigned char*)realloc(out, bufferSize * 2); + + if (out == NULL) + { + inflateEnd(&strm); + return Z_MEM_ERROR; + } + + strm.next_out = out + bufferSize; + strm.avail_out = bufferSize; + bufferSize *= 2; + } + } + while (ret != Z_STREAM_END); + assert(strm.avail_in == 0); + + outLength = bufferSize - strm.avail_out; + (void)inflateEnd(&strm); + return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; +} + std::vector MapReader::tilesets; Tileset::Tileset(Image *img, int w, int h, int firstGid): @@ -56,24 +128,54 @@ int Tileset::getFirstGid() Map *MapReader::readMap(const std::string &filename) { - std::string name = /*std::string("data/") +*/ filename; + // Load the file through resource manager + ResourceManager *resman = ResourceManager::getInstance(); + int fileSize; + void *buffer = resman->loadFile(filename, fileSize); + + if (buffer == NULL) + { + logger->log("Map file not found (%s)\n", filename.c_str()); + return NULL; + } - // Check that file exists before trying to parse it - std::fstream fin; - fin.open(name.c_str(), std::ios::in); - if (!fin.is_open()) { - logger->log("No such file!"); + // Inflate the gzipped map data + unsigned char *inflated; + unsigned int inflatedSize = 0; + int ret = inflateMemory( + (unsigned char*)buffer, fileSize, inflated, inflatedSize); + 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) + { + 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!"); return NULL; } - fin.close(); - xmlDocPtr doc = xmlParseFile(name.c_str()); + xmlDocPtr doc = xmlParseMemory((char*)inflated, inflatedSize); + free(inflated); + // Parse the inflated map data if (doc) { xmlNodePtr node = xmlDocGetRootElement(doc); if (!node || !xmlStrEqual(node->name, BAD_CAST "map")) { - logger->log("Warning: Not a map file (%s)!", filename.c_str()); + logger->log("Error: Not a map file (%s)!", filename.c_str()); return NULL; } -- cgit v1.2.3-60-g2f50