Moved loading of the config.xml to the loader class.

This commit is contained in:
Steffen 2015-02-07 20:08:59 +01:00
parent b6a9e2f180
commit dbf5a88ddb
4 changed files with 126 additions and 85 deletions

View File

@ -1,10 +1,10 @@
#include "application.hh"
#include "loader.hh"
using namespace tinyxml2;
Application::Application() {
Loader loader = Loader();
//load the config.xml
loadConfig();
loader.loadConfig(this);
graphics = Graphics(glm::uvec2(windowWidth, windowHeight), 0.1f, farPlane);
}
@ -79,86 +79,42 @@ bool Application::isLocked() {
return cameraLock;
}
void Application::loadConfig() {
XMLDocument* config = new XMLDocument();
const char* xmlFile = "../data/config.xml";
config->LoadFile(xmlFile);
if (config->ErrorID()!=0){
printf("Could not open config.xml!\n");
exit(-1);
}
XMLElement* resolution = config->FirstChildElement("resolution");
errorCheck(resolution->FirstChildElement("width")->QueryIntText(&windowWidth));
errorCheck(resolution->FirstChildElement("height")->QueryIntText(&windowHeight));
errorCheck(config->FirstChildElement("farPlane")->QueryFloatText(&farPlane));
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();
if(charHeightmapPath == NULL){
printf("XMLError: No heightmapPath found.\n");
exit(-1);
}
heightmapPath = charHeightmapPath;
const char* charLevelXmlPath = config->FirstChildElement("levelXmlPath")->GetText();
if(charLevelXmlPath == NULL){
printf("XMLError: No levelXmlPath found.\n");
exit(-1);
}
levelXmlPath = charLevelXmlPath;
void Application::setWindowWidth(int windowWidth) {
this->windowWidth = windowWidth;
}
void Application::errorCheck(XMLError error){
if (error) {
printf("XMLError: ");
if (error == XML_WRONG_ATTRIBUTE_TYPE) {
printf("Wrong attribute type.\n");
}
else if (error == XML_NO_ATTRIBUTE) {
printf("No attribute.\n");
}
else if (error == XML_CAN_NOT_CONVERT_TEXT) {
printf("Can not convert text.\n");
}
else if (error == XML_NO_TEXT_NODE) {
printf("No text.\n");
}
else {
printf("Unknown error.\n");
}
exit(-1);
}
void Application::setWindowHeight(int windowHeight) {
this->windowHeight = windowHeight;
}
void Application::setFarPlane(float farPlane) {
this->farPlane = farPlane;
}
void Application::setCompositionsPath(std::string compositionsPath) {
this->compositionsPath = compositionsPath;
}
void Application::setShaderPath(std::string shaderPath) {
this->shaderPath = shaderPath;
}
void Application::setGeometryPath(std::string geometryPath) {
this->geometryPath = geometryPath;
}
void Application::setTexturePath(std::string texturePath) {
this->texturePath = texturePath;
}
void Application::setScriptPath(std::string scriptPath) {
this->scriptPath = scriptPath;
}
void Application::setHeightmapPath(std::string heightmapPath) {
this->heightmapPath = heightmapPath;
}
void Application::setLevelXmlPath(std::string levelXmlPath) {
this->levelXmlPath = levelXmlPath;
}

View File

@ -4,7 +4,6 @@
#include "physics.hh"
#include "graphics.hh"
#include "level.hh"
#include "tinyxml2.hh"
class Application {
public:
@ -19,9 +18,17 @@ class Application {
void ignoreNextMouseUpdate();
int getIgnoredMouseUpdates();
void ignoredOneMouseUpdate();
void setWindowWidth(int windowWidth);
void setWindowHeight(int windowHeight);
void setFarPlane(float farPlane);
void setCompositionsPath(std::string compositionsPath);
void setShaderPath(std::string shaderPath);
void setGeometryPath(std::string geometryPath);
void setTexturePath(std::string texturePath);
void setScriptPath(std::string scriptPath);
void setHeightmapPath(std::string heightmapPath);
void setLevelXmlPath(std::string levelXmlPath);
private:
void loadConfig();
void errorCheck(tinyxml2::XMLError error);
int ignoredMouseUpdates;
bool focused;
bool cameraLock;

View File

@ -4,6 +4,82 @@ using namespace tinyxml2;
Loader::Loader() {
}
void Loader::loadConfig(Application* application) {
int windowWidth, windowHeight;
float farPlane;
std::string compositionsPath, shaderPath, geometryPath, texturePath, scriptPath, heightmapPath, levelXmlPath;
XMLDocument* config = new XMLDocument();
const char* xmlFile = "../data/config.xml";
config->LoadFile(xmlFile);
if (config->ErrorID()!=0){
printf("Could not open config.xml!\n");
exit(-1);
}
XMLElement* resolution = config->FirstChildElement("resolution");
errorCheck(resolution->FirstChildElement("width")->QueryIntText(&windowWidth));
errorCheck(resolution->FirstChildElement("height")->QueryIntText(&windowHeight));
errorCheck(config->FirstChildElement("farPlane")->QueryFloatText(&farPlane));
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();
if(charHeightmapPath == NULL){
printf("XMLError: No heightmapPath found.\n");
exit(-1);
}
heightmapPath = charHeightmapPath;
const char* charLevelXmlPath = config->FirstChildElement("levelXmlPath")->GetText();
if(charLevelXmlPath == NULL){
printf("XMLError: No levelXmlPath found.\n");
exit(-1);
}
levelXmlPath = charLevelXmlPath;
application->setWindowWidth(windowWidth);
application->setWindowHeight(windowHeight);
application->setFarPlane(farPlane);
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) {
//Loading from xml:
XMLDocument* doc = new XMLDocument();

View File

@ -1,5 +1,6 @@
#pragma once
#include "application.hh"
#include "level.hh"
#include "tinyxml2.hh"
@ -7,6 +8,7 @@ class Loader {
public:
Loader();
void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath);
void loadConfig(Application* application);
private:
void errorCheck(tinyxml2::XMLError error);
};