Added xml query functions to the loader class, using them in loadConfig().

This commit is contained in:
Steffen 2015-02-18 20:10:30 +01:00
parent af9f6d6933
commit 99d8a45380
2 changed files with 116 additions and 66 deletions

172
loader.cc
View File

@ -16,73 +16,19 @@ void Loader::loadConfig(Application* application) {
exit(-1); exit(-1);
} }
XMLElement* resolution = config->FirstChildElement("resolution"); XMLElement* resolution = config->FirstChildElement("resolution");
errorCheck(resolution->FirstChildElement("width")->QueryIntText(&windowWidth));
errorCheck(resolution->FirstChildElement("height")->QueryIntText(&windowHeight));
errorCheck(config->FirstChildElement("shadowCubeSize")->QueryIntText(&shadowCubeSize));
errorCheck(config->FirstChildElement("farPlane")->QueryFloatText(&farPlane));
errorCheck(config->FirstChildElement("maxShadowRenderCount")->QueryIntText(&maxShadowRenderCount));
const char* charCompositionsPath = config->FirstChildElement("compositionsPath")->GetText();
if(charCompositionsPath == NULL){
printf("XMLError: No compositionsPath found.\n");
exit(-1);
}
compositionsPath = charCompositionsPath;
const char* charShaderPath = config->FirstChildElement("shaderPath")->GetText();
if(charShaderPath == NULL){
printf("XMLError: No shaderPath found.\n");
exit(-1);
}
shaderPath = charShaderPath;
const char* charGeometryPath = config->FirstChildElement("geometryPath")->GetText();
if(charGeometryPath == NULL){
printf("XMLError: No geometryPath found.\n");
exit(-1);
}
geometryPath = charGeometryPath;
const char* charTexturePath = config->FirstChildElement("texturePath")->GetText();
if(charTexturePath == NULL){
printf("XMLError: No texturePath found.\n");
exit(-1);
}
texturePath = charTexturePath;
const char* charScriptPath = config->FirstChildElement("scriptPath")->GetText();
if(charScriptPath == NULL){
printf("XMLError: No scriptPath found.\n");
exit(-1);
}
scriptPath = charScriptPath;
const char* charHeightmapPath = config->FirstChildElement("heightmapPath")->GetText(); application->setWindowWidth(queryInt(resolution, "width"));
if(charHeightmapPath == NULL){ application->setWindowHeight(queryInt(resolution, "height"));
printf("XMLError: No heightmapPath found.\n"); application->setShadowCubeSize(queryInt(config, "shadowCubeSize"));
exit(-1); application->setFarPlane(queryFloat(config, "farPlane"));
} application->setMaxShadowRenderCount(queryInt(config, "maxShadowRenderCount"));
heightmapPath = charHeightmapPath; application->setCompositionsPath(queryString(config, "compositionsPath"));
application->setShaderPath(queryString(config, "shaderPath"));
const char* charLevelXmlPath = config->FirstChildElement("levelXmlPath")->GetText(); application->setGeometryPath(queryString(config, "geometryPath"));
if(charLevelXmlPath == NULL){ application->setTexturePath(queryString(config, "texturePath"));
printf("XMLError: No levelXmlPath found.\n"); application->setScriptPath(queryString(config, "scriptPath"));
exit(-1); application->setHeightmapPath(queryString(config, "heightmapPath"));
} application->setLevelXmlPath(queryString(config, "levelXmlPath"));
levelXmlPath = charLevelXmlPath;
application->setWindowWidth(windowWidth);
application->setWindowHeight(windowHeight);
application->setShadowCubeSize(shadowCubeSize);
application->setFarPlane(farPlane);
application->setMaxShadowRenderCount(maxShadowRenderCount);
application->setCompositionsPath(compositionsPath);
application->setShaderPath(shaderPath);
application->setGeometryPath(geometryPath);
application->setTexturePath(texturePath);
application->setScriptPath(scriptPath);
application->setHeightmapPath(heightmapPath);
application->setLevelXmlPath(levelXmlPath);
} }
void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath) { void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath) {
@ -551,7 +497,101 @@ glm::vec3 Loader::reloadPlayerPosition(std::string filePath, Level* level){
exit(-1); exit(-1);
} }
float Loader::queryFloat(XMLElement* element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
float ret;
errorCheck(attributeElement->QueryFloatText(&ret));
return ret;
}
float Loader::queryFloat(XMLDocument*& element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
float ret;
errorCheck(attributeElement->QueryFloatText(&ret));
return ret;
}
int Loader::queryInt(XMLElement* element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
int ret;
errorCheck(attributeElement->QueryIntText(&ret));
return ret;
}
int Loader::queryInt(XMLDocument*& element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
int ret;
errorCheck(attributeElement->QueryIntText(&ret));
return ret;
}
bool Loader::queryBool(XMLElement* element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
bool ret;
errorCheck(attributeElement->QueryBoolText(&ret));
return ret;
}
bool Loader::queryBool(XMLDocument*& element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
bool ret;
errorCheck(attributeElement->QueryBoolText(&ret));
return ret;
}
std::string Loader::queryString(XMLElement* element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
const char* charRet = attributeElement->GetText();
if(charRet == NULL){
std::cout << "XMLError: Attribute " << attribute << " could not be loaded." << std::endl;
exit(-1);
}
std::string ret = charRet;
return ret;
}
std::string Loader::queryString(XMLDocument*& element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
const char* charRet = attributeElement->GetText();
if(charRet == NULL){
std::cout << "XMLError: Attribute " << attribute << " could not be loaded." << std::endl;
exit(-1);
}
std::string ret = charRet;
return ret;
}
void Loader::errorCheck(XMLError error){ void Loader::errorCheck(XMLError error){
if (error) { if (error) {

View File

@ -4,6 +4,8 @@
#include "level.hh" #include "level.hh"
#include "tinyxml2.hh" #include "tinyxml2.hh"
using namespace tinyxml2;
class Loader { class Loader {
public: public:
Loader(); Loader();
@ -11,5 +13,13 @@ class Loader {
void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath); void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath);
glm::vec3 reloadPlayerPosition(std::string filePath, Level* level); glm::vec3 reloadPlayerPosition(std::string filePath, Level* level);
private: private:
float queryFloat(XMLElement* element, const char* attribute);
float queryFloat(XMLDocument*& element, const char* attribute);
int queryInt(XMLElement* element, const char* attribute);
int queryInt(XMLDocument*& element, const char* attribute);
bool queryBool(XMLElement* element, const char* attribute);
bool queryBool(XMLDocument*& element, const char* attribute);
std::string queryString(XMLElement* element, const char* attribute);
std::string queryString(XMLDocument*& element, const char* attribute);
void errorCheck(tinyxml2::XMLError error); void errorCheck(tinyxml2::XMLError error);
}; };