Advertisement
beefviper

XML Game Engine - game.cpp

May 21st, 2020
1,449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. // game.cpp
  2. // XML Game Engine
  3. // author: beefviper
  4. // date: May 21, 2020
  5.  
  6. // Wrote some helper functions
  7. // Looks better than the first attempt: https://pastebin.com/DJYFV715
  8.  
  9. #include "game.h"
  10.  
  11. void xge::Game::init(std::string filename)
  12. {
  13.     tx::XMLError xmlLoadFile = xml.LoadFile(filename.c_str());
  14.     checkXMLResult(xmlLoadFile);
  15.  
  16.     tx::XMLElement* xGame = getXMLElement(xml.FirstChildElement("game"));
  17.     tx::XMLElement* xHead = getXMLElement(xGame, "head");
  18.     tx::XMLElement* xTitle = getXMLElement(xHead,"title");
  19.     tx::XMLElement* xScreen = getXMLElement(xHead,"screen");
  20.     tx::XMLElement* xBody = getXMLElement(xGame,"body");
  21.     tx::XMLElement* xObjects = getXMLElement(xBody, "objects");
  22.     tx::XMLElement* xStates = getXMLElement(xBody, "states");
  23.  
  24.     title = getXMLText(xTitle);
  25.     width = getXMLAttributeInt(xScreen, "width");
  26.     height = getXMLAttributeInt(xScreen, "height");
  27.  
  28.     std::cout << "title = " << title << std::endl;
  29.     std::cout << "screen.width = " << width << std::endl;
  30.     std::cout << "screen.height = " << height << std::endl;
  31. }
  32.  
  33. void xge::Game::checkXMLResult(tx::XMLError result)
  34. {
  35.     if (result != tx::XML_SUCCESS)
  36.     {
  37.         std::cout << xml.ErrorStr() << std::endl;
  38.         exit(EXIT_FAILURE);
  39.     }
  40. }
  41.  
  42. tx::XMLElement* xge::Game::getXMLElement(tx::XMLElement* element, std::string tag)
  43. {
  44.     tx::XMLElement* newElement = nullptr;
  45.     newElement = tag.size() ? element->FirstChildElement(tag.c_str()) : element;
  46.  
  47.     if (newElement == nullptr)
  48.     {
  49.         std::cout << "Error: parsing <" << (tag.size() ? tag : "game") << ">"  << std::endl;
  50.         exit(EXIT_FAILURE);
  51.     }
  52.    
  53.     return newElement;
  54. }
  55.  
  56. std::string xge::Game::getXMLText(tx::XMLElement* element)
  57. {
  58.     return (element->GetText() ? element->GetText() : "");
  59. }
  60.  
  61. std::string xge::Game::getXMLAttribute(tx::XMLElement* element, std::string attribute)
  62. {
  63.     const char* attributeText = nullptr;
  64.  
  65.     attributeText = element->Attribute(attribute.c_str());
  66.     if (attributeText == nullptr)
  67.     {
  68.         std::cout << "Error: finding <" << element->Name() << "> attribute \"" << attribute << "\"" << std::endl;
  69.         exit(EXIT_FAILURE);
  70.     }
  71.  
  72.     std::string attributeString = attributeText;
  73.  
  74.     return attributeString;
  75. }
  76.  
  77. int xge::Game::getXMLAttributeInt(tx::XMLElement* element, std::string attribute)
  78. {
  79.     int attributeInt{};
  80.    
  81.     tx::XMLError xError = element->QueryIntAttribute(attribute.c_str(), &attributeInt);
  82.     if (xError != tx::XML_SUCCESS )
  83.     {
  84.         std::cout << "Error: parsing <" << element->Name() << "> int attribute \"" << attribute << "\"" << std::endl;
  85.         exit(EXIT_FAILURE);
  86.     }
  87.  
  88.     return  attributeInt;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement