This repository has been archived by the owner on Mar 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateParser.cpp
73 lines (60 loc) · 2.37 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
#include "StateParser.h"
#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)
{
TiXmlDocument xmlDoc;
if (!xmlDoc.LoadFile(stateFile))
{
std::cerr << xmlDoc.ErrorDesc() << std::endl;
return false;
}
TiXmlElement *pRoot = xmlDoc.RootElement();
TiXmlElement *pStateRoot = nullptr;
for (TiXmlElement *e = pRoot->FirstChildElement(); e != nullptr; e = e->NextSiblingElement())
if (e->Value() == stateID)
pStateRoot = e;
TiXmlElement *pTextureRoot = nullptr;
for (TiXmlElement *e = pStateRoot->FirstChildElement(); e != nullptr; e = e->NextSiblingElement())
if (e->Value() == std::string("TEXTURES"))
pTextureRoot = e;
parseTextures(pTextureRoot, pTextureIDs);
TiXmlElement *pObjectRoot = nullptr;
for (TiXmlElement *e = pStateRoot->FirstChildElement(); e != nullptr; e = e->NextSiblingElement())
if (e->Value() == std::string("OBJECTS"))
pObjectRoot = e;
parseObjects(pObjectRoot, pObjects);
return true;
}
void StateParser::parseTextures(TiXmlElement *pStateRoot, std::vector<std::string> *pTextureIDs)
{
for (TiXmlElement *e = pStateRoot->FirstChildElement(); e != nullptr; e = e->NextSiblingElement())
{
std::string filenameAttribute = e->Attribute("filename");
std::string idAttribute = e->Attribute("ID");
pTextureIDs->push_back(idAttribute);
TheTextureManager::Instance()->load(filenameAttribute, idAttribute, TheGame::Instance()->getRenderer());
}
}
void StateParser::parseObjects(TiXmlElement *pStateRoot, std::vector<GameObject *> *pObjects)
{
for (TiXmlElement *e = pStateRoot->FirstChildElement(); e != nullptr; 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(std::unique_ptr<LoaderParams>(new LoaderParams(x, y, width, height, textureID, numFrames, callbackID,
animSpeed)));
pObjects->push_back(pGameObject);
}
}