summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/components/map_render.cpp6
-rw-r--r--src/map.cpp31
-rw-r--r--src/map.h8
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
12Map::Map() 12Map::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()
128Map::~Map() 128Map::~Map()
129{ 129{
130 free(mapdata); 130 free(mapdata);
131 free(title);
132} 131}
133 132
134Map& Map::operator= (Map map) 133Map& Map::operator= (Map map)
@@ -153,7 +152,7 @@ const int* Map::getMapdata() const
153 return mapdata; 152 return mapdata;
154} 153}
155 154
156const char* Map::getTitle() const 155std::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;
9class Map { 9class 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;