Advertisement
beefviper

XML Game Engine

May 19th, 2020
1,984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. // XML Game Engine
  2. // author: beefviper
  3. // date  : May 19th, 2020
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <SFML/Graphics.hpp>
  9. #include <tinyxml2.h>
  10. #include "bvGameEngine.h"
  11. #include "xml.h"
  12.  
  13. namespace tx = tinyxml2;
  14.  
  15. struct GameInit {
  16.     int width{};
  17.     int height{};
  18.     std::string title;
  19. };
  20.  
  21. tx::XMLError loadHeadTag(GameInit& game, bv::XML& XML);
  22.  
  23. int main()
  24. {
  25.     bv::GameEngine game;
  26.  
  27.     bv::XML xml;
  28.     xml.load("game.xml");
  29.  
  30.     GameInit gameInit;
  31.     tx::XMLError eResult = loadHeadTag(gameInit, xml);
  32.     if (eResult != tx::XML_SUCCESS)
  33.     {
  34.         std::cout << "Error: failed to load head tag";
  35.         return tx::XML_ERROR_PARSING;
  36.     }
  37.    
  38.     game.init(gameInit.width, gameInit.height, gameInit.title);
  39.  
  40.  
  41.     while (game.getWindow().isOpen())
  42.     {
  43.         sf::Event event;
  44.         while (game.getWindow().pollEvent(event))
  45.         {
  46.             switch (event.type)
  47.             {
  48.             case sf::Event::Closed:
  49.                 game.getWindow().close();
  50.                 break;
  51.  
  52.             default:
  53.                 break;
  54.             }
  55.         }
  56.  
  57.         game.getWindow().clear(sf::Color::Red);
  58.  
  59.         game.getWindow().display();
  60.     }
  61.  
  62.     return EXIT_SUCCESS;
  63. }
  64.  
  65. tx::XMLError loadHeadTag(GameInit& gameInit, bv::XML& xml)
  66. {
  67.     tx::XMLNode* pRoot = xml.xmlDoc.FirstChild();
  68.     if (pRoot == nullptr)
  69.     {
  70.         std::cout << "Error: Root node game not found" << std::endl;
  71.         return tx::XML_ERROR_FILE_READ_ERROR;
  72.     }
  73.     std::cout << pRoot->Value() << std::endl;
  74.  
  75.     tx::XMLNode* pNode = pRoot->FirstChild();
  76.     if (pNode == nullptr)
  77.     {
  78.         std::cout << "Error: Element head not found" << std::endl;
  79.         return tx::XML_ERROR_PARSING;
  80.     }
  81.  
  82.     tx::XMLElement* pElement = pNode->FirstChildElement("title");
  83.     if (pElement == nullptr)
  84.     {
  85.         std::cout << "Error: Element title not found" << std::endl;
  86.         return tx::XML_ERROR_PARSING_ELEMENT;
  87.     }
  88.     const char* titletmp = pElement->GetText();
  89.  
  90.     std::string title;
  91.     if (titletmp != nullptr)
  92.     {
  93.         title = titletmp;
  94.     }
  95.  
  96.     pElement = pNode->FirstChildElement("screen");
  97.     if (pNode == nullptr)
  98.     {
  99.         std::cout << "Error: Element screen not found" << std::endl;
  100.         return tx::XML_ERROR_PARSING_ELEMENT;
  101.     }
  102.  
  103.     int width{};
  104.     int height{};
  105.  
  106.     tx::XMLError eResult = pElement->QueryIntAttribute("width", &width);
  107.     if (eResult != tx::XML_SUCCESS)
  108.     {
  109.         std::cout << "Error: Unable to find screen.width attribute" << std::endl;
  110.         return tx::XML_ERROR_PARSING_ATTRIBUTE;
  111.     }
  112.  
  113.     eResult = pElement->QueryIntAttribute("height", &height);
  114.     if (eResult != tx::XML_SUCCESS)
  115.     {
  116.         std::cout << "Error: Unable to find screen.height attribute" << std::endl;
  117.         return tx::XML_ERROR_PARSING_ATTRIBUTE;
  118.     }
  119.  
  120.     gameInit.width = width;
  121.     gameInit.height = height;
  122.     gameInit.title = title;
  123.  
  124.     return tx::XML_SUCCESS;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement