-
Notifications
You must be signed in to change notification settings - Fork 9
/
StateParser.cpp
99 lines (82 loc) · 2.76 KB
/
StateParser.cpp
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
#include "StateParser.h"
#include <iostream>
#include "TextureManager.h"
#include "Game.h"
#include "GameObjectFactory.h"
bool StateParser::parseState(const char* stateFile, std::string stateID, std::vector<GameObject*> *pObjects, std::vector<std::string> *pTextureIDs)
{
//create the XML document
TiXmlDocument xmlDoc;
//load the state file
if (!xmlDoc.LoadFile(stateFile))
{
std::cerr << xmlDoc.ErrorDesc() << "\n";
return false;
}
//get the root element
TiXmlElement* pRoot = xmlDoc.RootElement(); // <STATES>
//pre declare the states root node
TiXmlElement* pStateRoot = 0;
//get this states root node and assign it to pStateRoot
for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
{
if (e->Value() == stateID)
{
pStateRoot = e;
}
}
//pre declare the texture root
TiXmlElement* pTextureRoot = 0;
//get the root of the textre elements
for (TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
{
if (e->Value() == std::string("TEXTURES"))
{
pTextureRoot = e;
}
}
//now parse the textures
parseTextures(pTextureRoot, pTextureIDs);
//pre declare the object root node
TiXmlElement* pObjectRoot = 0;
//get the root node and assign it to pObjectRoot
for (TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
{
if (e->Value() == std::string("OBJECTS"))
{
pObjectRoot = e;
}
}
//now parse the objects
parseObjects(pObjectRoot, pObjects);
return true;
}
void StateParser::parseTextures(TiXmlElement* pStateRoot, std::vector<std::string> *pTextureIDs)
{
for (TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
{
std::string filenameAttribute = e->Attribute("filename");
std::string idAttribute = e->Attribute("ID");
pTextureIDs->push_back(idAttribute); //push into list
TheTextureManager::Instance()->load(filenameAttribute, idAttribute, TheGame::Instance()->getRenderer());
}
}
void StateParser::parseObjects(TiXmlElement* pStateRoot, std::vector<GameObject*> *pObjects)
{
for (TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
{
int x, y, width, height, numFrames, callbackID, animSpeed;
std::string textureID;
e->Attribute("x", &x);
e->Attribute("y", &y);
e->Attribute("width", &width);
e->Attribute("height", &height);
e->Attribute("numFrames", &numFrames);
e->Attribute("callbackID", &callbackID);
e->Attribute("animSpeed", &animSpeed);
textureID = e->Attribute("textureID");
GameObject* pGameObject = TheGameObjectFactory::instance()->create(e->Attribute("type"));
pGameObject->load(new LoaderParams(x, y, width, height, textureID, numFrames, callbackID, animSpeed));
pObjects->push_back(pGameObject);
}
}