From 77be863f4f15d2481a64e4e8dadb4060a6e4e590 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 11 Feb 2018 12:34:52 -0500 Subject: Implemented map rendering and basic collision Only wall and platform collision currently works, and map edges are not currently implemented. --- src/world.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/world.cpp (limited to 'src/world.cpp') diff --git a/src/world.cpp b/src/world.cpp new file mode 100644 index 0000000..9b1e4f6 --- /dev/null +++ b/src/world.cpp @@ -0,0 +1,99 @@ +#include "world.h" +#include +#include +#include +#include "consts.h" + +inline xmlChar* getProp(xmlNodePtr node, const char* attr) +{ + xmlChar* key = xmlGetProp(node, reinterpret_cast(attr)); + if (key == nullptr) + { + throw std::invalid_argument("Error parsing world file"); + } + + return key; +} + +World::World(std::string filename) +{ + xmlDocPtr doc = xmlParseFile(filename.c_str()); + if (doc == nullptr) + { + throw std::invalid_argument("Cannot find world file"); + } + + xmlNodePtr top = xmlDocGetRootElement(doc); + if (top == nullptr) + { + throw std::invalid_argument("Error parsing world file"); + } + + if (xmlStrcmp(top->name, reinterpret_cast("world"))) + { + throw std::invalid_argument("Error parsing world file"); + } + + xmlChar* key = nullptr; + + key = getProp(top, "startx"); + startX_ = atoi(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(top, "starty"); + startY_ = atoi(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(top, "startmap"); + startMap_ = atoi(reinterpret_cast(key)); + xmlFree(key); + + for (xmlNodePtr node = top->xmlChildrenNode; + node != nullptr; + node = node->next) + { + if (!xmlStrcmp(node->name, reinterpret_cast("map"))) + { + key = getProp(node, "id"); + size_t mapId = atoi(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(node, "title"); + std::string mapTitle(reinterpret_cast(key)); + xmlFree(key); + + std::vector mapTiles; + + for (xmlNodePtr mapNode = node->xmlChildrenNode; + mapNode != nullptr; + mapNode = mapNode->next) + { + if (!xmlStrcmp( + mapNode->name, + reinterpret_cast("environment"))) + { + key = xmlNodeGetContent(mapNode); + + mapTiles.clear(); + mapTiles.push_back(atoi(strtok(reinterpret_cast(key), ",\n"))); + for (size_t i = 1; i < (MAP_WIDTH * MAP_HEIGHT); i++) + { + mapTiles.push_back(atoi(strtok(nullptr, ",\n"))); + } + + xmlFree(key); + } + } + + maps_.emplace( + std::piecewise_construct, + std::forward_as_tuple(mapId), + std::forward_as_tuple( + mapId, + std::move(mapTiles), + std::move(mapTitle))); + } + } + + xmlFreeDoc(doc); +} -- cgit 1.4.1