Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // game.cpp
- // XML Game Engine
- // author: beefviper
- // date: May 21, 2020
- // Wrote some helper functions
- // Looks better than the first attempt: https://pastebin.com/DJYFV715
- #include "game.h"
- void xge::Game::init(std::string filename)
- {
- tx::XMLError xmlLoadFile = xml.LoadFile(filename.c_str());
- checkXMLResult(xmlLoadFile);
- tx::XMLElement* xGame = getXMLElement(xml.FirstChildElement("game"));
- tx::XMLElement* xHead = getXMLElement(xGame, "head");
- tx::XMLElement* xTitle = getXMLElement(xHead,"title");
- tx::XMLElement* xScreen = getXMLElement(xHead,"screen");
- tx::XMLElement* xBody = getXMLElement(xGame,"body");
- tx::XMLElement* xObjects = getXMLElement(xBody, "objects");
- tx::XMLElement* xStates = getXMLElement(xBody, "states");
- title = getXMLText(xTitle);
- width = getXMLAttributeInt(xScreen, "width");
- height = getXMLAttributeInt(xScreen, "height");
- std::cout << "title = " << title << std::endl;
- std::cout << "screen.width = " << width << std::endl;
- std::cout << "screen.height = " << height << std::endl;
- }
- void xge::Game::checkXMLResult(tx::XMLError result)
- {
- if (result != tx::XML_SUCCESS)
- {
- std::cout << xml.ErrorStr() << std::endl;
- exit(EXIT_FAILURE);
- }
- }
- tx::XMLElement* xge::Game::getXMLElement(tx::XMLElement* element, std::string tag)
- {
- tx::XMLElement* newElement = nullptr;
- newElement = tag.size() ? element->FirstChildElement(tag.c_str()) : element;
- if (newElement == nullptr)
- {
- std::cout << "Error: parsing <" << (tag.size() ? tag : "game") << ">" << std::endl;
- exit(EXIT_FAILURE);
- }
- return newElement;
- }
- std::string xge::Game::getXMLText(tx::XMLElement* element)
- {
- return (element->GetText() ? element->GetText() : "");
- }
- std::string xge::Game::getXMLAttribute(tx::XMLElement* element, std::string attribute)
- {
- const char* attributeText = nullptr;
- attributeText = element->Attribute(attribute.c_str());
- if (attributeText == nullptr)
- {
- std::cout << "Error: finding <" << element->Name() << "> attribute \"" << attribute << "\"" << std::endl;
- exit(EXIT_FAILURE);
- }
- std::string attributeString = attributeText;
- return attributeString;
- }
- int xge::Game::getXMLAttributeInt(tx::XMLElement* element, std::string attribute)
- {
- int attributeInt{};
- tx::XMLError xError = element->QueryIntAttribute(attribute.c_str(), &attributeInt);
- if (xError != tx::XML_SUCCESS )
- {
- std::cout << "Error: parsing <" << element->Name() << "> int attribute \"" << attribute << "\"" << std::endl;
- exit(EXIT_FAILURE);
- }
- return attributeInt;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement