diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-15 19:47:51 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-15 19:47:51 -0400 |
commit | eb61ec5578bf45d454d9d0c9fba523b7803a5ed9 (patch) | |
tree | 1e775a62313618a7dc8f1dfc6b1687e579ab3b13 | |
parent | dca2aea57957c6af1af535f23ae392e7517ebd51 (diff) | |
download | therapy-eb61ec5578bf45d454d9d0c9fba523b7803a5ed9.tar.gz therapy-eb61ec5578bf45d454d9d0c9fba523b7803a5ed9.tar.bz2 therapy-eb61ec5578bf45d454d9d0c9fba523b7803a5ed9.zip |
Fixed error with empty left and right maps
-rw-r--r-- | src/components/map_render.cpp | 6 | ||||
-rw-r--r-- | src/map.cpp | 31 | ||||
-rw-r--r-- | src/map.h | 8 |
3 files changed, 22 insertions, 23 deletions
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 | |||
23 | } | 23 | } |
24 | 24 | ||
25 | Texture font("../res/font.bmp"); | 25 | Texture font("../res/font.bmp"); |
26 | const char* map_name = map.getTitle(); | 26 | std::string map_name = map.getTitle(); |
27 | int start_x = (40/2) - (strlen(map_name)/2); | 27 | int start_x = (40/2) - (map_name.length()/2); |
28 | for (size_t i=0; i<strlen(map_name); i++) | 28 | for (size_t i=0; i<map_name.length(); i++) |
29 | { | 29 | { |
30 | Rectangle srcRect {map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8}; | 30 | Rectangle srcRect {map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8}; |
31 | Rectangle dstRect {(start_x + (int)i)*8, 24*8, 8, 8}; | 31 | Rectangle dstRect {(start_x + (int)i)*8, 24*8, 8, 8}; |
diff --git a/src/map.cpp b/src/map.cpp index ef0d6c3..36baab6 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
@@ -11,7 +11,6 @@ static std::map<std::string, Map> maps; | |||
11 | 11 | ||
12 | Map::Map() | 12 | Map::Map() |
13 | { | 13 | { |
14 | title = (char*) calloc(1, sizeof(char)); | ||
15 | mapdata = (int*) calloc(1, sizeof(int)); | 14 | mapdata = (int*) calloc(1, sizeof(int)); |
16 | } | 15 | } |
17 | 16 | ||
@@ -44,14 +43,7 @@ Map::Map(const std::string name) | |||
44 | if (!xmlStrcmp(node->name, (const xmlChar*) "name")) | 43 | if (!xmlStrcmp(node->name, (const xmlChar*) "name")) |
45 | { | 44 | { |
46 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | 45 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); |
47 | int len = xmlStrlen(key); | 46 | title = (char*) key; |
48 | title = (char*) calloc(len + 1, sizeof(char)); | ||
49 | |||
50 | if (len > 0) | ||
51 | { | ||
52 | strcpy(title, (char*) key); | ||
53 | } | ||
54 | |||
55 | xmlFree(key); | 47 | xmlFree(key); |
56 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) | 48 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) |
57 | { | 49 | { |
@@ -91,13 +83,23 @@ Map::Map(const std::string name) | |||
91 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) | 83 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) |
92 | { | 84 | { |
93 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | 85 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); |
94 | leftMap = &Map::getNamedMap(std::string((char*) key)); | 86 | std::string mapname = (char*) key; |
95 | xmlFree(key); | 87 | xmlFree(key); |
88 | |||
89 | if (mapname.length() > 0) | ||
90 | { | ||
91 | leftMap = &Map::getNamedMap(mapname); | ||
92 | } | ||
96 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) | 93 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) |
97 | { | 94 | { |
98 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | 95 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); |
99 | rightMap = &Map::getNamedMap(std::string((char*) key)); | 96 | std::string mapname = (char*) key; |
100 | xmlFree(key); | 97 | xmlFree(key); |
98 | |||
99 | if (mapname.length() > 0) | ||
100 | { | ||
101 | rightMap = &Map::getNamedMap(mapname); | ||
102 | } | ||
101 | } | 103 | } |
102 | } | 104 | } |
103 | 105 | ||
@@ -109,9 +111,7 @@ Map::Map(const Map& map) | |||
109 | mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | 111 | mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); |
110 | memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | 112 | memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); |
111 | 113 | ||
112 | title = (char*) malloc((MAP_WIDTH+1)*sizeof(char)); | 114 | title = map.title; |
113 | strncpy(title, map.title, MAP_WIDTH+1); | ||
114 | |||
115 | leftMap = map.leftMap; | 115 | leftMap = map.leftMap; |
116 | rightMap = map.rightMap; | 116 | rightMap = map.rightMap; |
117 | 117 | ||
@@ -128,7 +128,6 @@ Map::Map(Map&& map) : Map() | |||
128 | Map::~Map() | 128 | Map::~Map() |
129 | { | 129 | { |
130 | free(mapdata); | 130 | free(mapdata); |
131 | free(title); | ||
132 | } | 131 | } |
133 | 132 | ||
134 | Map& Map::operator= (Map map) | 133 | Map& Map::operator= (Map map) |
@@ -153,7 +152,7 @@ const int* Map::getMapdata() const | |||
153 | return mapdata; | 152 | return mapdata; |
154 | } | 153 | } |
155 | 154 | ||
156 | const char* Map::getTitle() const | 155 | std::string Map::getTitle() const |
157 | { | 156 | { |
158 | return title; | 157 | return title; |
159 | } | 158 | } |
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; | |||
9 | class Map { | 9 | class Map { |
10 | public: | 10 | public: |
11 | Map(); | 11 | Map(); |
12 | Map(const std::string name); | 12 | Map(std::string name); |
13 | Map(const Map& map); | 13 | Map(const Map& map); |
14 | Map(Map&& map); | 14 | Map(Map&& map); |
15 | ~Map(); | 15 | ~Map(); |
16 | Map& operator= (Map other); | 16 | Map& operator= (Map other); |
17 | friend void swap(Map& first, Map& second); | 17 | friend void swap(Map& first, Map& second); |
18 | 18 | ||
19 | static Map& getNamedMap(const std::string name); | 19 | static Map& getNamedMap(std::string name); |
20 | 20 | ||
21 | const int* getMapdata() const; | 21 | const int* getMapdata() const; |
22 | const char* getTitle() const; | 22 | std::string getTitle() const; |
23 | const Map* getLeftMap() const; | 23 | const Map* getLeftMap() const; |
24 | const Map* getRightMap() const; | 24 | const Map* getRightMap() const; |
25 | void setLeftMap(const Map* m); | 25 | void setLeftMap(const Map* m); |
@@ -34,7 +34,7 @@ class Map { | |||
34 | }; | 34 | }; |
35 | 35 | ||
36 | int* mapdata; | 36 | int* mapdata; |
37 | char* title; | 37 | std::string title; |
38 | std::string name; | 38 | std::string name; |
39 | const Map* leftMap = nullptr; | 39 | const Map* leftMap = nullptr; |
40 | const Map* rightMap = nullptr; | 40 | const Map* rightMap = nullptr; |