void ConfigSingleton::configure(std::string xml_file_path) { //Open XML config file and try to load it XMLDocument doc; if(doc.LoadFile(xml_file_path.c_str()) != XML_SUCCESS) { std::cerr << "Cannot reach configuration file: " << xml_file_path << "!" << std::endl; return; } //Look for <config> element XMLElement* pConfig = doc.FirstChildElement("root")->FirstChildElement("config"); if(pConfig==nullptr) { std::cerr << "Invalid configuration file: " << xml_file_path << "!" << std::endl; return; } //Iterate attribute list and set parameter values //Version 2 of TinyXML won't let me iterate over Attribute list this way by returning //const XMLAttribute*. I'm forced to const_cast before I find an elegant way of doing this. XMLAttribute* pAttrib = const_cast<XMLAttribute*>(pConfig->FirstAttribute()); while(pAttrib != nullptr) { set_parameter(pAttrib->Name(),pAttrib->Value()); pAttrib = const_cast<XMLAttribute*>(pAttrib->Next()); } }
void cSDL2DSceneManager::addLayerObjects(c2DLayer *Layer, XMLElement *Element) { cSceneObject* Object = new cSceneObject(); unsigned int r=0; unsigned int g=0; unsigned int b=0; if(!Object) return; for(XMLAttribute* ElementAttrib = const_cast<XMLAttribute*>(Element->FirstAttribute()); ElementAttrib; ElementAttrib = const_cast<XMLAttribute*>(ElementAttrib->Next())) { std::string AttribName = ElementAttrib->Name(); std::string AttribValue = ElementAttrib->Value(); if(AttribName=="resourceID") { cResourceManager* ResourceManager = cResourceManager::GetResourceManager(); Object->setResourceObject((cRenderResource*) ResourceManager->findResourcebyID(atoi(AttribValue.c_str()))); } if(AttribName=="posx") { Object->m_PosX = atof(AttribValue.c_str()); } if(AttribName=="posy") { Object->m_PosY = atof(AttribValue.c_str()); } if(AttribName=="colorkey") { if(AttribValue=="true") Object->m_bColorKeyEnabled=true; else Object->m_bColorKeyEnabled=false; } if(AttribName=="r") { r = atoi(AttribValue.c_str()); } if(AttribName=="g") { g = atoi(AttribValue.c_str()); } if(AttribName=="b") { b = atoi(AttribValue.c_str()); } } if(Object->m_bColorKeyEnabled) Object->setColorKey(r,g,b); Layer->m_SceneObjects.push_back(Object); }
bool cSDL2DSceneManager::loadFromXMLFile(std::string Filename) { XMLDocument doc(Filename.c_str()); std::list<c2DLayer*> List; if(doc.LoadFile(Filename.c_str()) == XML_NO_ERROR) { //Find resources node XMLNode* ResourceTree = doc.RootElement(); if(ResourceTree) { //Enumerate resource objects for(XMLNode* child = ResourceTree->FirstChild(); child; child = child->NextSibling()) { XMLElement *Element = child->ToElement(); if(Element) { c2DLayer *Layer = new c2DLayer(); Layer->m_ZOrder = m_Layers.size(); for(XMLAttribute* ElementAttrib = const_cast<XMLAttribute*>(Element->FirstAttribute()); ElementAttrib; ElementAttrib = const_cast<XMLAttribute*>(ElementAttrib->Next())) { //Examine layers std::string AttribName = ElementAttrib->Name(); std::string AttribValue = ElementAttrib->Value(); //Detect resource type. Graphic? Audio? Text? if(AttribName=="name") { Layer->m_Name=AttribValue; continue; } if(AttribName=="posx") { Layer->m_PosX = atof(AttribValue.c_str()); } if(AttribName=="posy") { Layer->m_PosY = atof(AttribValue.c_str()); } if(AttribName=="visible") { if(AttribValue=="true") Layer->m_bVisible=true; else Layer->m_bVisible=false; } } m_Layers.push_back(Layer); //Cycle through layer objects for(XMLNode* objs = child->FirstChild(); objs; objs = objs->NextSibling()) { if(std::string(objs->Value())=="objects") { for(XMLNode* obj = objs->FirstChild(); obj; obj = obj->NextSibling()) { XMLElement *ObjElement = obj->ToElement(); addLayerObjects(Layer, ObjElement); } } } } } sortLayers(); return true; } } return false; }