From 370135fca87cfbd4e24647877e771eefc9eaa6a7 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 14 Mar 2015 13:00:47 +0100 Subject: [PATCH] Made heightmap path configurable from level. --- data/levels/Level1.xml | 1 + game/application.cc | 6 ++---- game/level.cc | 7 +++++-- game/level.hh | 3 ++- game/loader.cc | 15 ++++++++++++--- game/loader.hh | 4 +++- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/data/levels/Level1.xml b/data/levels/Level1.xml index fbac9fe..a99fbb7 100644 --- a/data/levels/Level1.xml +++ b/data/levels/Level1.xml @@ -13208,6 +13208,7 @@ + heightmapLvl1.png sand.png 0.13 0.8 diff --git a/game/application.cc b/game/application.cc index 2dea84d..e72afa6 100644 --- a/game/application.cc +++ b/game/application.cc @@ -19,13 +19,11 @@ void Application::init() graphics.renderLoadingScreen(); // init random generator std::srand(std::time(NULL)); - // choose Level TODO: Choose this in a menu } void Application::initLevel() { - std::string heightmapFilePath = heightmapPath + "heightmapLvl1.png"; std::string levelXmlFilePath = levelXmlPath + "Level1.xml"; - this->level = Level(heightmapFilePath, levelXmlFilePath); + this->level = Level(levelXmlFilePath); level.getPhysics()->init(geometryPath); // Don't change this! ignoredMouseUpdates = 0; @@ -38,7 +36,7 @@ void Application::initLevel() { level.load(); Loader loader = Loader(); - loader.load(levelXmlFilePath, &level, compositionsPath, scriptPath, geometryPath, texturePath); + loader.load(levelXmlFilePath, &level, compositionsPath, scriptPath, geometryPath, texturePath, heightmapPath); graphics.init(&level); // just in case: check for errors diff --git a/game/level.cc b/game/level.cc index 14f1118..a036d12 100644 --- a/game/level.cc +++ b/game/level.cc @@ -2,8 +2,7 @@ #include "loader.hh" #include -Level::Level(std::string heightmapFilePath, std::string xmlFilePath){ - this->terrain = Terrain(heightmapFilePath); +Level::Level(std::string xmlFilePath){ // default value skydomeSize = 50.0f; physics = Physics(); @@ -341,3 +340,7 @@ void Level::activateEndgame(){ void Level::setWaterPlane(Object* water) { this->waterPlane = water; } + +void Level::setTerrain(Terrain terrain) { + this->terrain = terrain; +} diff --git a/game/level.hh b/game/level.hh index fc4e0bd..f989b27 100644 --- a/game/level.hh +++ b/game/level.hh @@ -22,7 +22,7 @@ extern "C" { class Level { public: - Level(std::string heightmapFilePath, std::string xmlFilePath); + Level(std::string xmlFilePath); Level(); ~Level(); void load(); @@ -73,6 +73,7 @@ class Level { void setPlayerIndex(int index); void forceMove(float x, float y, float z, unsigned indice); void activateEndgame(); + void setTerrain(Terrain terrain); private: lua_State* luaState=nullptr; std::vector objects; diff --git a/game/loader.cc b/game/loader.cc index 4a8ee97..9c1c921 100644 --- a/game/loader.cc +++ b/game/loader.cc @@ -52,7 +52,9 @@ void Loader::loadConfig(Application* application) { } } -void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath, std::string globalGeometryPath, std::string globalTexturePath) { +void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath, + std::string globalGeometryPath, std::string globalTexturePath, std::string heightmapPath) { + struct stat buf; //Loading from xml: XMLDocument* doc = new XMLDocument(); const char* xmlFile = filePath.c_str(); @@ -68,15 +70,22 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa level->setStrength(strength); // load the terrain + XMLElement* terrainElement = doc->FirstChildElement("terrain"); + std::string levelHeightmapPath = heightmapPath + queryString(terrainElement, "heightmap"); + if(stat(levelHeightmapPath.c_str(), &buf) != 0){ + std::cout << "The heightmap file " << levelHeightmapPath << " does not exist." << std::endl; + exit(-1); + } + Terrain terrain = Terrain(levelHeightmapPath); + level->setTerrain(terrain); + // Because object gets copied a lot load it when it has reached it's final destination level->getTerrain()->load(); Model terrainModel = Model(level->getTerrain()->getModel()); - XMLElement* terrainElement = doc->FirstChildElement("terrain"); std::string terrainTexture = queryString(terrainElement, "texture"); float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor"); float terrainDiffuseFactor = queryFloat(terrainElement, "diffuseFactor"); float terrainSpecularFactor = queryFloat(terrainElement, "specularFactor"); float terrainShininess = queryFloat(terrainElement, "shininess"); - struct stat buf; std::string terrainTexturePath = "../" + globalTexturePath + terrainTexture; if(stat(terrainTexturePath.c_str(), &buf) != 0){ std::cout << "The texture file " << terrainTexturePath << " does not exist." << std::endl; diff --git a/game/loader.hh b/game/loader.hh index 8e97c89..f7689e5 100644 --- a/game/loader.hh +++ b/game/loader.hh @@ -10,7 +10,9 @@ class Loader { public: Loader(); void loadConfig(Application* application); - void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath, std::string geometryPath, std::string texturePath); + void load(std::string filePath, Level* level, std::string compositionsPath, + std::string scriptPath, std::string geometryPath, std::string texturePath, + std::string heightmapPath); glm::vec3 reloadPlayerPosition(std::string filePath, Level* level); private: float queryFloat(XMLElement* element, const char* attribute);