diff --git a/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp b/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp index 3827251a..a6f8bb98 100644 --- a/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp +++ b/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp @@ -12,7 +12,7 @@ std::vector LevelLoader::LoadLevel(std::string fileName) return parser->Parse(fileName); } -ObjectTypeHeader LevelLoader::LoadLevelHeader(std::string fileName) +LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName) { return parser->ParseHeader(fileName); } \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelLoader/LevelLoader.h b/Code/Game/GameLogic/LevelLoader/LevelLoader.h index ed7aab4d..8f87d141 100644 --- a/Code/Game/GameLogic/LevelLoader/LevelLoader.h +++ b/Code/Game/GameLogic/LevelLoader/LevelLoader.h @@ -19,8 +19,20 @@ namespace GameLogic public: LevelLoader(){this->parser = new GameLogic::LevelFileLoader::LevelParser(); } ~LevelLoader(){} - std::vector LoadLevel(std::string fileName); //loads the level and objects from file - ObjectTypeHeader LoadLevelHeader(std::string fileName); //just for fast access for the meta information about the level. + + /******************************************************** + * Loads the level and objects from file. + * @param fileName: Path to the level-file that you want to load. + * @return: Returns all structs with objects and information about the level. + ********************************************************/ + std::vector LoadLevel(std::string fileName); + + /******************************************************** + * Just for fast access for the meta information about the level. + * @param fileName: Path to the level-file that you want to load. + * @return: Returns the meta information about the level. + ********************************************************/ + LevelMetaData LoadLevelHeader(std::string fileName); //. private: GameLogic::LevelFileLoader::LevelParser *parser; diff --git a/Code/Game/GameLogic/LevelLoader/LevelParser.cpp b/Code/Game/GameLogic/LevelLoader/LevelParser.cpp index 08c4da7e..72d68d97 100644 --- a/Code/Game/GameLogic/LevelLoader/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelLoader/LevelParser.cpp @@ -6,9 +6,10 @@ using namespace GameLogic; using namespace ::LevelFileLoader; - LevelParser::LevelParser() { + formatVersion.formatVersionMajor = 1; + formatVersion.formatVersionMinor = 0; } LevelParser::~LevelParser() @@ -17,35 +18,43 @@ LevelParser::~LevelParser() std::vector LevelParser::Parse(std::string filename) { - int stringSize = 0; - //Read entire level file. - Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize); + int bufferSize = 0; + unsigned int counter = 0; std::vector objects; - unsigned int counter = 0; - - while(counter < stringSize) + //Read entire level file. + Loader loader; + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize); + + //Read format version + FormatVersion levelFormatVersion; + ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion)); + if(this->formatVersion != levelFormatVersion) + { + //Do something if it's not the same version + } + + while(counter < bufferSize) { //Get typeID ObjectTypeHeader typeID; - typeID = ParseObjectTypeHeader(&buffer[counter]); - //counter += 4; - //Unpack ID + ParseObject(&buffer[counter], typeID, sizeof(typeID)); switch((int)typeID.typeID) { - case TypeID_LevelHeader: - //Call function - counter += LevelHeaderSize; + case ObjectType_LevelMetaData: + LevelMetaData header; + //ParseObject(&buffer[counter], header, sizeof(header)); + objects.push_back(header); + counter += sizeof(header); break; - case TypeID_Object: - //Call function - objects.push_back(ParseObjectHeader(&buffer[counter])); - counter += sizeof(ObjectHeader); - break; + case ObjectType_Dynamic: + ObjectHeader header; + ParseObject(&buffer[counter], header, sizeof(header)); + objects.push_back(header); + counter += sizeof(header); default: //Couldn't find typeID. FAIL!!!!!! break; @@ -55,36 +64,44 @@ std::vector LevelParser::Parse(std::string filename) return objects; } -//för meta information om leveln. Måste göra så den returnerar rätt strukt så fort vi -//vi definierat en! -ObjectTypeHeader LevelParser::ParseHeader(std::string filename) +//för meta information om leveln. +LevelMetaData LevelParser::ParseHeader(std::string filename) { - int stringSize = 0; + int bufferSize = 0; + unsigned int counter = 0; + + LevelMetaData levelHeader; + levelHeader.typeID = ObjectType::ObjectType_Unknown; + //Read entire level file. Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize); + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize); + + //Read format version + FormatVersion levelFormatVersion; + ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion)); + if(this->formatVersion != levelFormatVersion) + { + //Do something if it's not the same version + } //Find the header in the returned string. - unsigned int counter = 0; - - - ObjectTypeHeader header; - header.typeID = ObjectType_Level; - - while(counter < stringSize) + while(counter < bufferSize) { ObjectTypeHeader typeID; - typeID = ParseObjectTypeHeader(buffer); + ParseObject(&buffer[counter], typeID, sizeof(typeID)); + switch(typeID.typeID) { - case TypeID_LevelHeader: - //Call function - - counter += LevelHeaderSize; + case ObjectType_LevelMetaData: + //ParseObject(&buffer[counter], levelHeader, sizeof(levelHeader)); + return levelHeader; + counter += sizeof(LevelMetaData); break; - case TypeID_Object: - //Call function - counter += ObjectHeaderSize; + case ObjectType_Dynamic: + //Do not call parse this object, since we are only interested in the LevelMetaData + //Only increase the counter size + counter += sizeof(ObjectHeader); break; default: @@ -93,5 +110,5 @@ ObjectTypeHeader LevelParser::ParseHeader(std::string filename) } } - return header; + return levelHeader; } \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelLoader/LevelParser.h b/Code/Game/GameLogic/LevelLoader/LevelParser.h index bcb9cb31..6dce25f8 100644 --- a/Code/Game/GameLogic/LevelLoader/LevelParser.h +++ b/Code/Game/GameLogic/LevelLoader/LevelParser.h @@ -19,31 +19,10 @@ namespace GameLogic std::vector Parse(std::string filename); // - ObjectTypeHeader ParseHeader(std::string filename); + LevelMetaData ParseHeader(std::string filename); private: - static const int TypeHeaderSize = 4; //Including the TypeID - static const int ObjectHeaderSize = 10; //Including modelID, textureID, position, rotation, scale. - static const int LevelHeaderSize = 10; //Including Format version, Name, Map version etc. - - static const int FormatVersionMajor = 1; - static const int FormatVersionMinor = 0; - - /************************************* - Question - *************************************/ - //Could we use the IDs in "ObjectDefines.h" or do we have to use our own??? - enum TypeID - { - TypeID_LevelHeader, - TypeID_Player, - TypeID_Object, - - - TypeID_NUM_OF_TYPES, - - ObjectType_Unknown = -1, - }; + FormatVersion formatVersion; }; } diff --git a/Code/Game/GameLogic/LevelLoader/ObjectDefines.h b/Code/Game/GameLogic/LevelLoader/ObjectDefines.h index 7f2307d1..2d3e09f0 100644 --- a/Code/Game/GameLogic/LevelLoader/ObjectDefines.h +++ b/Code/Game/GameLogic/LevelLoader/ObjectDefines.h @@ -1,27 +1,97 @@ #ifndef OBJECT_DEFINES_H #define OBJECT_DEFINES_H +#include +#include + namespace GameLogic { + /************************************ + Enums + *************************************/ + enum ObjectType { - ObjectType_Level, + ObjectType_LevelMetaData, ObjectType_Static, ObjectType_Dynamic, - + //Etc ObjectType_NUM_OF_TYPES, ObjectType_Unknown = -1, }; + enum UsePhysics + { + UsePhysics_UseFullPhysics, + UsePhysics_IgnoreGravity, + UsePhysics_IgnorePhysics, + + UsePhysics_Count, + UsePhysics_Unknown = -1, + }; + + //Should this be moved somewhere else? + enum GameMode + { + GameMode_FreeForAll, + GameMode_TeamDeathMatch, + //Etc + + GameMode_Count, + GameMode_Unknown = -1, + }; + + + /************************************ + Structs + *************************************/ + + struct FormatVersion + { + int formatVersionMajor; + int formatVersionMinor; + + bool operator ==(const FormatVersion& obj) + { + return (this->formatVersionMajor != obj.formatVersionMajor && this->formatVersionMinor != obj.formatVersionMinor); + } + + bool operator !=(const FormatVersion& obj) + { + return !(*this == obj); + } + }; + struct ObjectTypeHeader { ObjectType typeID; - }; - struct ObjectHeader : public ObjectTypeHeader + struct PhysicsObject + { + float mass; + float elasticity; + float frictionCoeffStatic; + float frictionCoeffDynamic; + float inertiaTensor[16]; + UsePhysics usePhysics; + }; + + struct LevelMetaData : ObjectTypeHeader + { + std::string levelName; + FormatVersion levelVersion; + std::string levelDescription; + std::string levelAuthor; + int maxNumberOfPlayer; + float worldSize; + int overviewPictureID; + std::vector gameModesSupported; + }; + + struct ObjectHeader : public PhysicsObject, public ObjectTypeHeader { //Model, int ModelID;