From 44324ba5d6cac01048cc5cbecbff255ee56f2fc0 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 14 Mar 2015 16:13:11 -0400 Subject: Wrote simple factory to read map and entity data from XML files --- src/map.cpp | 158 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 124 insertions(+), 34 deletions(-) (limited to 'src/map.cpp') diff --git a/src/map.cpp b/src/map.cpp index 3976b63..8c1c6f0 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -2,43 +2,111 @@ #include "game.h" #include #include +#include +#include +#include "entityfactory.h" + +static std::map maps; Map::Map() { - + title = (char*) calloc(1, sizeof(char)); + mapdata = (int*) calloc(1, sizeof(int)); } -Map::Map(const char* filename) +Map::Map(const std::string name) { - FILE* f = fopen(filename, "r"); + xmlDocPtr doc = xmlParseFile(("../maps/" + name + ".xml").c_str()); + if (doc == nullptr) + { + fprintf(stderr, "Error reading map %s\n", name.c_str()); + exit(-1); + } - m_mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); - for (int i=0; iname, (const xmlChar*) "map-def")) + { + fprintf(stderr, "Invalid map definition %s\n", name.c_str()); + exit(-1); + } + + for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) + { + if (!xmlStrcmp(node->name, (const xmlChar*) "name")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + title = (char*) calloc(xmlStrlen(key) + 1, sizeof(char)); + strcpy(title, (char*) key); + xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); + mapdata[0] = atoi(strtok((char*) key, ",\n")); + for (int i=1; i<(MAP_WIDTH*(MAP_HEIGHT-1)); i++) + { + mapdata[i] = atoi(strtok(NULL, ",\n")); + } + xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "entities")) { - fscanf(f, "%d,", &(m_mapdata[i*MAP_WIDTH + j])); + for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) + { + if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) + { + EntityData data; + for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) + { + if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) + { + xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); + data.name = std::string((char*) key); + xmlFree(key); + } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) + { + xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); + sscanf((char*) key, "%lf,%lf", &(data.position.first), &(data.position.second)); + xmlFree(key); + } + } + + entities.push_back(data); + } + } + } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + leftMap = &Map::getNamedMap(std::string((char*) key)); + xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + rightMap = &Map::getNamedMap(std::string((char*) key)); + xmlFree(key); } - - fgetc(f); } - m_title = (char*) calloc(41, sizeof(char)); - fgets(m_title, 41, f); - - fclose(f); + xmlFreeDoc(doc); } -Map::Map(Map& map) +Map::Map(const Map& map) { - m_mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); - memcpy(m_mapdata, map.m_mapdata, MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); + mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); + memcpy(mapdata, map.mapdata, MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); + + title = (char*) malloc((MAP_WIDTH+1)*sizeof(char)); + strncpy(title, map.title, MAP_WIDTH+1); - m_title = (char*) malloc((MAP_WIDTH+1)*sizeof(char)); - strncpy(m_title, map.m_title, MAP_WIDTH+1); + leftMap = map.leftMap; + rightMap = map.rightMap; - m_leftMap = map.m_leftMap; - m_rightMap = map.m_rightMap; + entities = map.entities; } Map::Map(Map&& map) : Map() @@ -48,8 +116,8 @@ Map::Map(Map&& map) : Map() Map::~Map() { - free(m_mapdata); - free(m_title); + free(mapdata); + free(title); } Map& Map::operator= (Map map) @@ -61,38 +129,60 @@ Map& Map::operator= (Map map) void swap(Map& first, Map& second) { - std::swap(first.m_mapdata, second.m_mapdata); - std::swap(first.m_title, second.m_title); - std::swap(first.m_leftMap, second.m_leftMap); - std::swap(first.m_rightMap, second.m_rightMap); + std::swap(first.mapdata, second.mapdata); + std::swap(first.title, second.title); + std::swap(first.leftMap, second.leftMap); + std::swap(first.rightMap, second.rightMap); + std::swap(first.entities, second.entities); } -const int* Map::mapdata() const +const int* Map::getMapdata() const { - return m_mapdata; + return mapdata; } -const char* Map::title() const +const char* Map::getTitle() const { - return m_title; + return title; } const Map* Map::getLeftMap() const { - return m_leftMap; + return leftMap; } const Map* Map::getRightMap() const { - return m_rightMap; + return rightMap; } void Map::setLeftMap(const Map* m) { - m_leftMap = m; + leftMap = m; } void Map::setRightMap(const Map* m) { - m_rightMap = m; + rightMap = m; +} + +void Map::createEntities(std::list>& entities) const +{ + for (auto data : this->entities) + { + auto entity = EntityFactory::createNamedEntity(data.name, *this); + entity->position = data.position; + + entities.push_back(entity); + } +} + +Map& Map::getNamedMap(const std::string name) +{ + if (maps.count(name) == 0) + { + maps[name] = Map {name}; + } + + return maps[name]; } -- cgit 1.4.1