From 8142a9c87a13cecc7a3698e877f24d89f128c074 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 21 Apr 2018 14:50:52 -0400 Subject: Started working on prototype objects --- src/prototype_manager.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/prototype_manager.cpp (limited to 'src/prototype_manager.cpp') diff --git a/src/prototype_manager.cpp b/src/prototype_manager.cpp new file mode 100644 index 0000000..250aa59 --- /dev/null +++ b/src/prototype_manager.cpp @@ -0,0 +1,71 @@ +#include "prototype_manager.h" +#include +#include +#include "xml.h" + +PrototypeManager::PrototypeManager(std::string path) +{ + xmlDocPtr doc = xmlParseFile(path.c_str()); + if (doc == nullptr) + { + throw std::invalid_argument("Cannot find prototypes file"); + } + + xmlNodePtr top = xmlDocGetRootElement(doc); + if (top == nullptr) + { + throw std::invalid_argument("Error parsing prototypes file"); + } + + if (xmlStrcmp(top->name, reinterpret_cast("entities"))) + { + throw std::invalid_argument("Error parsing prototypes file"); + } + + xmlChar* key = nullptr; + + for (xmlNodePtr node = top->xmlChildrenNode; + node != nullptr; + node = node->next) + { + if (!xmlStrcmp(node->name, reinterpret_cast("entity"))) + { + key = getProp(node, "id"); + std::string prototypeName(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(node, "sprite"); + std::string spritePath(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(node, "width"); + int width = atoi(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(node, "height"); + int height = atoi(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(node, "action"); + std::string actionStr(reinterpret_cast(key)); + xmlFree(key); + + Prototype::Action pAction = Prototype::Action::none; + if (actionStr == "save") + { + pAction = Prototype::Action::save; + } + + prototypes_.emplace( + std::piecewise_construct, + std::tie(prototypeName), + std::tie( + width, + height, + spritePath, + pAction)); + } + } + + xmlFreeDoc(doc); +} -- cgit 1.4.1