1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include "rapidxml.hpp"
#include "Entities.h"
#include "LoadFromXml.hpp"
#include <fstream>
#include <iostream>
// This function loads a pinball table from an XML file.
bool LoadPinballTable(b2World &physics_world)
{
// Emit a warning to the user/developer that errors given by the parser are probably due to errors in the xml file.
std::cout << "Loading pinball table from XML file. If you get any errors beyond this point, it's probably in the XML file." << std::endl;
using namespace rapidxml;
std::string input_xml;
std::string line;
std::ifstream in("testtable.xml");
// read file into input_xml
while(getline(in,line))
{
input_xml += line;
}
// Run the RapidXML parser inputting the string that has just been loaded.
std::vector<char> xml_copy(input_xml.begin(), input_xml.end());
xml_copy.push_back('\0');
xml_document<> doc;
doc.parse<parse_full>(&xml_copy[0]);
xml_node<> *node = doc.first_node(0);
// Iterate over the DOM tree
while (node != 0)
{
// Determine what entity is specified in the XML file and spawn it.
// Static wall: A chain of vertices specifying an immobile solid wall.
if ((strcmp((node->name()), "staticwall") == 0))
{
// Make an array that's as long as specified in the xml tag
int num_vertices = atoi(node->first_attribute("num_verts")->value());
b2Vec2 verts[num_vertices];
xml_node<> *node2 = node->first_node("vertex");
// Make an array of the vertices
for (int i = 0; i<num_vertices; i++)
{
verts[i].Set(atof(node2->first_attribute("x")->value()),
atof(node2->first_attribute("y")->value()));
node2 = &(*node2->next_sibling());
}
entity_list.push_back(new StaticWall(verts,num_vertices,physics_world));
}
// Ball: The "ball" in "pinBALL". Usually spawned by a ball dispenser rather than through this function.
if ((strcmp((node->name()), "ball") == 0))
{
entity_list.push_back(new Ball(atof(node->first_attribute("x")->value()),
atof(node->first_attribute("y")->value()),
0,0,physics_world));
}
if ((strcmp((node->name()), "bumper") == 0))
{
entity_list.push_back(new Bumper(atof(node->first_attribute("x")->value()),
atof(node->first_attribute("y")->value()),
atof(node->first_attribute("radius")->value()),
atof(node->first_attribute("strenght")->value()),
555,
physics_world));
}
// The left flipper
else if ((strcmp((node->name()), "lflipper") == 0))
{
entity_list.push_back(new Flipper(atof(node->first_attribute("x")->value()),
atof(node->first_attribute("y")->value()),
0,0,'l',physics_world));
}
// And the other one
else if ((strcmp((node->name()), "rflipper") == 0))
{
entity_list.push_back(new Flipper(atof(node->first_attribute("x")->value()),
atof(node->first_attribute("y")->value()),
0,0,'r',physics_world));
}
// A plunger used to launch the ball
else if ((strcmp((node->name()), "launcher") == 0))
{
entity_list.push_back(new Launcher(atof(node->first_attribute("x")->value()),
atof(node->first_attribute("y")->value()),
physics_world));
}
// An invisible object that spawns a ball at the start of the game and whenever a ball falls through the drain.
else if ((strcmp((node->name()), "balldispenser") == 0))
{
entity_list.push_back(new BallDispenser(atof(node->first_attribute("x")->value()),
atof(node->first_attribute("y")->value()), 3));
}
node = node->next_sibling(); // Assign a reference of the xml node's next sibling to the node pointer for the next iteration.
// If the xml node has no next sibling, the returned adress will be 0, in which case the looping condidion will return false and stop looping.
}
return true;
}
|