Added loading from config.xml, then debuged it for more than three hours...

This commit is contained in:
Steffen Fündgens 2015-02-06 18:00:14 +01:00
parent a14d3420d5
commit d2cf4f0003
12 changed files with 71 additions and 76 deletions

View File

@ -5,7 +5,7 @@ using namespace tinyxml2;
Application::Application() { Application::Application() {
//load the config.xml //load the config.xml
loadConfig(); loadConfig();
graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 150.0f); graphics = Graphics(glm::uvec2(windowWidth, windowHeight), 0.1f, farPlane);
} }
void Application::init() void Application::init()
@ -13,7 +13,9 @@ void Application::init()
// init random generator // init random generator
std::srand(std::time(NULL)); std::srand(std::time(NULL));
// choose Level TODO: Choose this in a menu // choose Level TODO: Choose this in a menu
this->level = Level("1"); std::string heightmapFilePath = heightmapPath + "heightmapLvl1.png";
this->level = Level(heightmapFilePath);
level.getPhysics()->init(geometryPath);
// Don't change this! // Don't change this!
ignoredMouseUpdates = 0; ignoredMouseUpdates = 0;
cameraLock = true; cameraLock = true;
@ -22,15 +24,15 @@ void Application::init()
// define where shaders and textures can be found: // define where shaders and textures can be found:
ACGL::Base::Settings::the()->setResourcePath("../"); ACGL::Base::Settings::the()->setResourcePath("../");
ACGL::Base::Settings::the()->setShaderPath("Shader/"); ACGL::Base::Settings::the()->setShaderPath(shaderPath);
ACGL::Base::Settings::the()->setTexturePath("Levels/Textures/"); ACGL::Base::Settings::the()->setTexturePath(texturePath);
ACGL::Base::Settings::the()->setGeometryPath("Levels/Geometry/"); ACGL::Base::Settings::the()->setGeometryPath(geometryPath);
// load Level // load Level
level.load(); level.load();
Loader loader = Loader(); Loader loader = Loader();
loader.load("Level1.xml", &level); std::string levelXmlFilePath = levelXmlPath + "Level1.xml";
loader.load(levelXmlFilePath, &level, compositionsPath, scriptPath);
graphics.init(&level); graphics.init(&level);
// just in case: check for errors // just in case: check for errors

View File

@ -5,7 +5,7 @@
<farPlane>150.0</farPlane> <farPlane>150.0</farPlane>
<compositionsPath>Levels/ObjectSetups/</compositionsPath> <compositionsPath>../Levels/ObjectSetups/Compositions.xml</compositionsPath>
<shaderPath>Shader/</shaderPath> <shaderPath>Shader/</shaderPath>
@ -13,8 +13,8 @@
<texturePath>Levels/Textures/</texturePath> <texturePath>Levels/Textures/</texturePath>
<scriptPath>Levels/scripts/</scriptPath> <scriptPath>../Levels/scripts/</scriptPath>
<heightmapPath>Levels/</heightmapPath> <heightmapPath>../Levels/</heightmapPath>
<levelXmlPath>Levels/ObjectSetups/</levelXmlPath> <levelXmlPath>../Levels/ObjectSetups/</levelXmlPath>

View File

@ -1,11 +1,11 @@
#include "level.hh" #include "level.hh"
#include <string> #include <string>
Level::Level(std::string levelNum){ Level::Level(std::string heightmapFilePath){
this->levelNum = levelNum; this->terrain = Terrain(heightmapFilePath);
this->terrain = Terrain(levelNum);
// default value // default value
skydomeSize = 50.0f; skydomeSize = 50.0f;
physics = Physics();
} }
Level::Level() { Level::Level() {
@ -41,10 +41,6 @@ void Level::load() {
luabridge::push(luaState, this); luabridge::push(luaState, this);
lua_setglobal(luaState, "level"); lua_setglobal(luaState, "level");
this->physics = Physics();
this->physics.init();
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f); this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
} }

View File

@ -20,7 +20,7 @@ extern "C" {
class Level { class Level {
public: public:
Level(std::string levelNum); Level(std::string heightmapFilePath);
Level(); Level();
~Level(); ~Level();
void load(); void load();
@ -57,7 +57,6 @@ class Level {
Terrain* getTerrain(); Terrain* getTerrain();
private: private:
lua_State* luaState=nullptr; lua_State* luaState=nullptr;
std::string levelNum;
std::vector<Object*> objects; std::vector<Object*> objects;
std::vector<Object*> physicsObjects; std::vector<Object*> physicsObjects;
std::vector<Light> lights; std::vector<Light> lights;

View File

@ -4,16 +4,15 @@ using namespace tinyxml2;
Loader::Loader() { Loader::Loader() {
} }
void Loader::load(std::string filePath, Level* level) { void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath) {
//Loading from xml: //Loading from xml:
XMLDocument* doc = new XMLDocument(); XMLDocument* doc = new XMLDocument();
const char* xmlFile = ("../Levels/ObjectSetups/" + filePath).c_str(); const char* xmlFile = filePath.c_str();
doc->LoadFile(xmlFile); doc->LoadFile(xmlFile);
if (doc->ErrorID()!=0){ if (doc->ErrorID()!=0){
printf("Could not open ObjectSetupXml!\n"); printf("Could not open ObjectSetupXml!\n");
exit(-1); exit(-1);
} }
//load global physic parameter //load global physic parameter
float friction, strength; float friction, strength;
XMLElement* physicsElement = doc->FirstChildElement("physics"); XMLElement* physicsElement = doc->FirstChildElement("physics");
@ -87,7 +86,7 @@ void Loader::load(std::string filePath, Level* level) {
std::vector<std::vector<int>> objectIdentifiers = std::vector<std::vector<int>>(); //The first entry is the index in objects, the second one the index in physicObjects, the others are idGreen, idBlue and objectNum. std::vector<std::vector<int>> objectIdentifiers = std::vector<std::vector<int>>(); //The first entry is the index in objects, the second one the index in physicObjects, the others are idGreen, idBlue and objectNum.
XMLDocument* compositions = new XMLDocument(); XMLDocument* compositions = new XMLDocument();
//TODO move path to config.xml //TODO move path to config.xml
const char* compositionsFile = "../Levels/ObjectSetups/Compositions.xml"; const char* compositionsFile = compositionsPath.c_str();
compositions->LoadFile(compositionsFile); compositions->LoadFile(compositionsFile);
if (compositions->ErrorID()!=0){ if (compositions->ErrorID()!=0){
printf("Could not open Compositions!\n"); printf("Could not open Compositions!\n");
@ -233,7 +232,6 @@ void Loader::load(std::string filePath, Level* level) {
exit(-1); exit(-1);
} }
if(compositionType == 20){ if(compositionType == 20){
level->setCameraCenter(object); level->setCameraCenter(object);
} }
@ -363,7 +361,7 @@ void Loader::load(std::string filePath, Level* level) {
} }
} }
if (object != 0) { if (object != 0) {
Trigger trigger = Trigger(position, distance, isBigger, object, luaScript, level->getLuaState(), objectToChange); Trigger trigger = Trigger(position, distance, isBigger, object, luaScript, level->getLuaState(), objectToChange, scriptPath);
level->addTrigger(trigger); level->addTrigger(trigger);
} }
else { else {

View File

@ -6,7 +6,7 @@
class Loader { class Loader {
public: public:
Loader(); Loader();
void load(std::string filePath, Level* level); void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath);
private: private:
void errorCheck(tinyxml2::XMLError error); void errorCheck(tinyxml2::XMLError error);
}; };

View File

@ -8,7 +8,7 @@ Physics::Physics() {
Physics::~Physics() { Physics::~Physics() {
} }
void Physics::init() //prepares bullet by creating all initial classes void Physics::init(std::string geometryPath) //prepares bullet by creating all initial classes
{ {
colConfig = new btDefaultCollisionConfiguration(); colConfig = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(colConfig); dispatcher = new btCollisionDispatcher(colConfig);
@ -16,6 +16,10 @@ void Physics::init() //prepares bullet by creating all initial classes
solver = new btSequentialImpulseConstraintSolver(); solver = new btSequentialImpulseConstraintSolver();
world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig); world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig);
world->setGravity(btVector3(0,-10,-0)); world->setGravity(btVector3(0,-10,-0));
if (world == NULL) {
printf("No World after init\n");
}
this->geometryPath = geometryPath;
} }
void Physics::takeUpdateStep(float timeDiff) void Physics::takeUpdateStep(float timeDiff)
@ -110,9 +114,8 @@ void Physics::addPlayer(float friction, float rad, Entity entity, float mass, fl
addCamera(); //now that the player exists add a camera for the player addCamera(); //now that the player exists add a camera for the player
} }
void Physics::addTerrain(int width, int length, float** heightData) //The terrian adding function void Physics::addTerrain(int width, int length, float** heightData) //The terrain adding function
{ {
float* heightfield = new float[width * length];//bullet only accepts data in a one dimensional array, so parse data into appropriate format float* heightfield = new float[width * length];//bullet only accepts data in a one dimensional array, so parse data into appropriate format
int highest = -999999, j = 0, i = 0; int highest = -999999, j = 0, i = 0;
for (i = 0; i < width; i++) for (i = 0; i < width; i++)
@ -124,20 +127,20 @@ void Physics::addTerrain(int width, int length, float** heightData) //The terria
highest = heightData[j][i]; //bullet needs to know the highest point of the heightmap highest = heightData[j][i]; //bullet needs to know the highest point of the heightmap
} }
} }
highest++; highest++;
btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false); btHeightfieldTerrainShape* terrainShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false);
btRigidBody::btRigidBodyConstructionInfo info(0,new btDefaultMotionState(),terrianShape,btVector3(0,0,0)); //next we process all data for the rigid body into info btRigidBody::btRigidBodyConstructionInfo info(0,new btDefaultMotionState(),terrainShape,btVector3(0,0,0)); //next we process all data for the rigid body into info
info.m_restitution = 0; info.m_restitution = 0;
btRigidBody* tBody = new btRigidBody(info); btRigidBody* tBody = new btRigidBody(info);
tBody->getWorldTransform().setOrigin(btVector3(0,((float)highest)/2,0)); //we have to move the origin of our rigid body down, because bullet sets the origin (0,0,0) at (width/2, height/2, length/2) in the map the x and z are correct in our level, but y needs to be addapted tBody->getWorldTransform().setOrigin(btVector3(0,((float)highest)/2,0)); //we have to move the origin of our rigid body down, because bullet sets the origin (0,0,0) at (width/2, height/2, length/2) in the map the x and z are correct in our level, but y needs to be addapted
terrainBody = tBody; terrainBody = tBody;
if (world == NULL) {
printf("No World while adding terrain.\n");
}
world->addRigidBody(terrainBody, COL_TERRAIN, COL_TERRAIN | COL_OBJECTS); //COL_XXXX are collision masks, allowing us to ignore collisions between certain object groups (required for buttons) world->addRigidBody(terrainBody, COL_TERRAIN, COL_TERRAIN | COL_OBJECTS); //COL_XXXX are collision masks, allowing us to ignore collisions between certain object groups (required for buttons)
} }
@ -149,13 +152,11 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f
std::vector< unsigned int > vertexIndices; //temp lists for data sets std::vector< unsigned int > vertexIndices; //temp lists for data sets
std::vector< btVector3 > temp_vertices; std::vector< btVector3 > temp_vertices;
path = "../" + geometryPath + path;
path = "../Levels/Geometry/" + path;
FILE * file = fopen(path.c_str(), "r"); FILE * file = fopen(path.c_str(), "r");
if( file == NULL ){ if( file == NULL ){
throw std::invalid_argument( "Impossible to open the file" ); //create correct filepath and report error if cannot open throw std::invalid_argument( "Impossible to open the file" ); //create correct filepath and report error if cannot open
} }
while( 1 ){ while( 1 ){
char lineHeader[128]; char lineHeader[128];
// read the first word of the line // read the first word of the line
@ -437,6 +438,9 @@ void Physics::addStaticGroundPlane()
void Physics::kill()//delete dynamically allocated memory void Physics::kill()//delete dynamically allocated memory
{ {
if (world == NULL) {
return;
}
//btDynamimcWorld* //btDynamimcWorld*
for(unsigned i = 0; i < bodies.size();i++) for(unsigned i = 0; i < bodies.size();i++)
{ {
@ -467,5 +471,3 @@ void Physics::kill()//delete dynamically allocated memory
//feel like a good little programmer because everything is clean //feel like a good little programmer because everything is clean
} }

View File

@ -45,7 +45,7 @@ class Physics {
public: public:
Physics(); Physics();
~Physics(); ~Physics();
void init(); void init(std::string geometryPath);
void takeUpdateStep(float timeDiff); //must be used in level.update to proagate the physics void takeUpdateStep(float timeDiff); //must be used in level.update to proagate the physics
void rollForward(glm::vec3 camPos, float strength); //self explainitory void rollForward(glm::vec3 camPos, float strength); //self explainitory
void rollLeft(glm::vec3 camPos, float strength); void rollLeft(glm::vec3 camPos, float strength);
@ -80,7 +80,7 @@ class Physics {
std::vector<positionConstraint> allPositionConstraints; std::vector<positionConstraint> allPositionConstraints;
void addCamera(); //Do NOT impliment before Player has been created; void addCamera(); //Do NOT impliment before Player has been created;
btDynamicsWorld* world; //contains physical attributes of the world. btDynamicsWorld* world = NULL; //contains physical attributes of the world.
btDispatcher* dispatcher; // btDispatcher* dispatcher; //
btCollisionConfiguration* colConfig; //defines the type of collision detection. btCollisionConfiguration* colConfig; //defines the type of collision detection.
btBroadphaseInterface* broadphase; //defines how objects are culled from collision detection. btBroadphaseInterface* broadphase; //defines how objects are culled from collision detection.
@ -89,7 +89,7 @@ class Physics {
int specialPhysicsCollision = 2 | 4; int specialPhysicsCollision = 2 | 4;
int terrainPhysicsCollision = 2; int terrainPhysicsCollision = 2;
int counter = 0; int counter = 0;
std::string geometryPath;
}; };
enum collisionTypes{ enum collisionTypes{

View File

@ -1,8 +1,8 @@
#include "terrain.hh" #include "terrain.hh"
#include "lodepng.h" #include "lodepng.h"
Terrain::Terrain(std::string levelNum){ Terrain::Terrain(std::string heightmapFilePath){
this->levelNum = levelNum; this->heightmapFilePath = heightmapFilePath;
} }
Terrain::Terrain(){ Terrain::Terrain(){
@ -14,9 +14,9 @@ Terrain::~Terrain() {
void Terrain::load() { void Terrain::load() {
std::vector<unsigned char> image; //the raw pixels std::vector<unsigned char> image; //the raw pixels
unsigned error = lodepng::decode(image, heightmapWidth, heightmapHeight, "../Levels/heightmapLvl" + levelNum + ".png"); unsigned error = lodepng::decode(image, heightmapWidth, heightmapHeight, heightmapFilePath);
if (error) { if (error) {
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl; std::cout << "Decoder error " << error << " from Terrain::load: " << lodepng_error_text(error) << std::endl;
} }
this->heightmap = new float*[this->heightmapHeight]; //initialize the heightmap this->heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
for(unsigned int rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap for(unsigned int rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
@ -25,7 +25,6 @@ void Terrain::load() {
this->heightmap[rowNum][columnNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 6; //<--heightmap is scaled here this->heightmap[rowNum][columnNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 6; //<--heightmap is scaled here
} }
} }
this->makeTriangleMesh(); this->makeTriangleMesh();
heightmapChanged = false; //no need to make a TriangleMesh again before rendering heightmapChanged = false; //no need to make a TriangleMesh again before rendering

View File

@ -19,12 +19,11 @@ class Terrain {
private: private:
Material material; Material material;
std::string levelNum; std::string heightmapFilePath;
unsigned int heightmapHeight, heightmapWidth; unsigned int heightmapHeight, heightmapWidth;
float** heightmap; //can be accessed like 'float[][]' float** heightmap; //can be accessed like 'float[][]'
bool heightmapChanged; bool heightmapChanged;
ACGL::OpenGL::SharedVertexArrayObject triangleMesh; ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
void makeTriangleMesh(); void makeTriangleMesh();
void set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum); void set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum);
}; };

View File

@ -1,11 +1,11 @@
#include "trigger.hh" #include "trigger.hh"
Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange) { Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, std::string scriptPath) {
this->position=position; this->position=position;
this->distance=distance; this->distance=distance;
this->isBigger=isBigger; this->isBigger=isBigger;
this->object=object; this->object=object;
this->luaScript="../Levels/scripts/" + luaScript + ".lua"; this->luaScript= scriptPath + luaScript + ".lua";
this->L = L; this->L = L;
if(L == nullptr){ if(L == nullptr){
printf("L is NULL in trigger!\n"); printf("L is NULL in trigger!\n");

View File

@ -14,7 +14,7 @@ class Level;
class Trigger { class Trigger {
public: public:
Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange); Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, std::string scriptPath);
Trigger(); Trigger();
~Trigger(); ~Trigger();
void triggerUpdate(); void triggerUpdate();