From eb61ec5578bf45d454d9d0c9fba523b7803a5ed9 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 15 Mar 2015 19:47:51 -0400 Subject: Fixed error with empty left and right maps --- src/components/map_render.cpp | 6 +++--- src/map.cpp | 31 +++++++++++++++---------------- src/map.h | 8 ++++---- 3 files changed, 22 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/components/map_render.cpp b/src/components/map_render.cpp index 6fdfcc3..3b6b9c4 100644 --- a/src/components/map_render.cpp +++ b/src/components/map_render.cpp @@ -23,9 +23,9 @@ MapRenderComponent::MapRenderComponent(const Map& map) : screen(GAME_WIDTH, GAME } Texture font("../res/font.bmp"); - const char* map_name = map.getTitle(); - int start_x = (40/2) - (strlen(map_name)/2); - for (size_t i=0; i maps; Map::Map() { - title = (char*) calloc(1, sizeof(char)); mapdata = (int*) calloc(1, sizeof(int)); } @@ -44,14 +43,7 @@ Map::Map(const std::string name) if (!xmlStrcmp(node->name, (const xmlChar*) "name")) { xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - int len = xmlStrlen(key); - title = (char*) calloc(len + 1, sizeof(char)); - - if (len > 0) - { - strcpy(title, (char*) key); - } - + title = (char*) key; xmlFree(key); } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) { @@ -91,13 +83,23 @@ Map::Map(const std::string name) } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) { xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - leftMap = &Map::getNamedMap(std::string((char*) key)); + std::string mapname = (char*) key; xmlFree(key); + + if (mapname.length() > 0) + { + leftMap = &Map::getNamedMap(mapname); + } } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) { xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - rightMap = &Map::getNamedMap(std::string((char*) key)); + std::string mapname = (char*) key; xmlFree(key); + + if (mapname.length() > 0) + { + rightMap = &Map::getNamedMap(mapname); + } } } @@ -109,9 +111,7 @@ Map::Map(const Map& map) mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); - title = (char*) malloc((MAP_WIDTH+1)*sizeof(char)); - strncpy(title, map.title, MAP_WIDTH+1); - + title = map.title; leftMap = map.leftMap; rightMap = map.rightMap; @@ -128,7 +128,6 @@ Map::Map(Map&& map) : Map() Map::~Map() { free(mapdata); - free(title); } Map& Map::operator= (Map map) @@ -153,7 +152,7 @@ const int* Map::getMapdata() const return mapdata; } -const char* Map::getTitle() const +std::string Map::getTitle() const { return title; } diff --git a/src/map.h b/src/map.h index a562bec..3b3d42c 100644 --- a/src/map.h +++ b/src/map.h @@ -9,17 +9,17 @@ class Entity; class Map { public: Map(); - Map(const std::string name); + Map(std::string name); Map(const Map& map); Map(Map&& map); ~Map(); Map& operator= (Map other); friend void swap(Map& first, Map& second); - static Map& getNamedMap(const std::string name); + static Map& getNamedMap(std::string name); const int* getMapdata() const; - const char* getTitle() const; + std::string getTitle() const; const Map* getLeftMap() const; const Map* getRightMap() const; void setLeftMap(const Map* m); @@ -34,7 +34,7 @@ class Map { }; int* mapdata; - char* title; + std::string title; std::string name; const Map* leftMap = nullptr; const Map* rightMap = nullptr; -- cgit 1.4.1