Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // XML Game Engine
- // author: beefviper
- // date : May 19th, 2020
- #include <iostream>
- #include <string>
- #include <vector>
- #include <SFML/Graphics.hpp>
- #include <tinyxml2.h>
- #include "bvGameEngine.h"
- #include "xml.h"
- namespace tx = tinyxml2;
- struct GameInit {
- int width{};
- int height{};
- std::string title;
- };
- tx::XMLError loadHeadTag(GameInit& game, bv::XML& XML);
- int main()
- {
- bv::GameEngine game;
- bv::XML xml;
- xml.load("game.xml");
- GameInit gameInit;
- tx::XMLError eResult = loadHeadTag(gameInit, xml);
- if (eResult != tx::XML_SUCCESS)
- {
- std::cout << "Error: failed to load head tag";
- return tx::XML_ERROR_PARSING;
- }
- game.init(gameInit.width, gameInit.height, gameInit.title);
- while (game.getWindow().isOpen())
- {
- sf::Event event;
- while (game.getWindow().pollEvent(event))
- {
- switch (event.type)
- {
- case sf::Event::Closed:
- game.getWindow().close();
- break;
- default:
- break;
- }
- }
- game.getWindow().clear(sf::Color::Red);
- game.getWindow().display();
- }
- return EXIT_SUCCESS;
- }
- tx::XMLError loadHeadTag(GameInit& gameInit, bv::XML& xml)
- {
- tx::XMLNode* pRoot = xml.xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- std::cout << "Error: Root node game not found" << std::endl;
- return tx::XML_ERROR_FILE_READ_ERROR;
- }
- std::cout << pRoot->Value() << std::endl;
- tx::XMLNode* pNode = pRoot->FirstChild();
- if (pNode == nullptr)
- {
- std::cout << "Error: Element head not found" << std::endl;
- return tx::XML_ERROR_PARSING;
- }
- tx::XMLElement* pElement = pNode->FirstChildElement("title");
- if (pElement == nullptr)
- {
- std::cout << "Error: Element title not found" << std::endl;
- return tx::XML_ERROR_PARSING_ELEMENT;
- }
- const char* titletmp = pElement->GetText();
- std::string title;
- if (titletmp != nullptr)
- {
- title = titletmp;
- }
- pElement = pNode->FirstChildElement("screen");
- if (pNode == nullptr)
- {
- std::cout << "Error: Element screen not found" << std::endl;
- return tx::XML_ERROR_PARSING_ELEMENT;
- }
- int width{};
- int height{};
- tx::XMLError eResult = pElement->QueryIntAttribute("width", &width);
- if (eResult != tx::XML_SUCCESS)
- {
- std::cout << "Error: Unable to find screen.width attribute" << std::endl;
- return tx::XML_ERROR_PARSING_ATTRIBUTE;
- }
- eResult = pElement->QueryIntAttribute("height", &height);
- if (eResult != tx::XML_SUCCESS)
- {
- std::cout << "Error: Unable to find screen.height attribute" << std::endl;
- return tx::XML_ERROR_PARSING_ATTRIBUTE;
- }
- gameInit.width = width;
- gameInit.height = height;
- gameInit.title = title;
- return tx::XML_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement