Merge branch 'GameLogic-LevelFormat' of https://github.com/dean11/Danbias into GameLogic-LevelFormat

This commit is contained in:
Sam Mario Svensson 2014-01-28 11:30:59 +01:00
commit 2b63563a9b
5 changed files with 148 additions and 70 deletions

View File

@ -12,7 +12,7 @@ std::vector<ObjectTypeHeader> LevelLoader::LoadLevel(std::string fileName)
return parser->Parse(fileName); return parser->Parse(fileName);
} }
ObjectTypeHeader LevelLoader::LoadLevelHeader(std::string fileName) LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName)
{ {
return parser->ParseHeader(fileName); return parser->ParseHeader(fileName);
} }

View File

@ -19,8 +19,20 @@ namespace GameLogic
public: public:
LevelLoader(){this->parser = new GameLogic::LevelFileLoader::LevelParser(); } LevelLoader(){this->parser = new GameLogic::LevelFileLoader::LevelParser(); }
~LevelLoader(){} ~LevelLoader(){}
std::vector<ObjectTypeHeader> 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<ObjectTypeHeader> 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: private:
GameLogic::LevelFileLoader::LevelParser *parser; GameLogic::LevelFileLoader::LevelParser *parser;

View File

@ -6,9 +6,10 @@
using namespace GameLogic; using namespace GameLogic;
using namespace ::LevelFileLoader; using namespace ::LevelFileLoader;
LevelParser::LevelParser() LevelParser::LevelParser()
{ {
formatVersion.formatVersionMajor = 1;
formatVersion.formatVersionMinor = 0;
} }
LevelParser::~LevelParser() LevelParser::~LevelParser()
@ -17,35 +18,43 @@ LevelParser::~LevelParser()
std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename) std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename)
{ {
int stringSize = 0; int bufferSize = 0;
//Read entire level file. unsigned int counter = 0;
Loader loader;
unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize);
std::vector<ObjectTypeHeader> objects; std::vector<ObjectTypeHeader> objects;
unsigned int counter = 0; //Read entire level file.
Loader loader;
unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize);
while(counter < stringSize) //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 //Get typeID
ObjectTypeHeader typeID; ObjectTypeHeader typeID;
typeID = ParseObjectTypeHeader(&buffer[counter]); ParseObject(&buffer[counter], typeID, sizeof(typeID));
//counter += 4;
//Unpack ID
switch((int)typeID.typeID) switch((int)typeID.typeID)
{ {
case TypeID_LevelHeader: case ObjectType_LevelMetaData:
//Call function LevelMetaData header;
counter += LevelHeaderSize; //ParseObject(&buffer[counter], header, sizeof(header));
objects.push_back(header);
counter += sizeof(header);
break; break;
case TypeID_Object: case ObjectType_Dynamic:
//Call function ObjectHeader header;
objects.push_back(ParseObjectHeader(&buffer[counter])); ParseObject(&buffer[counter], header, sizeof(header));
counter += sizeof(ObjectHeader); objects.push_back(header);
break; counter += sizeof(header);
default: default:
//Couldn't find typeID. FAIL!!!!!! //Couldn't find typeID. FAIL!!!!!!
break; break;
@ -55,36 +64,44 @@ std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename)
return objects; return objects;
} }
//för meta information om leveln. Måste göra så den returnerar rätt strukt så fort vi //för meta information om leveln.
//vi definierat en! LevelMetaData LevelParser::ParseHeader(std::string filename)
ObjectTypeHeader LevelParser::ParseHeader(std::string filename)
{ {
int stringSize = 0; int bufferSize = 0;
//Read entire level file.
Loader loader;
unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize);
//Find the header in the returned string.
unsigned int counter = 0; unsigned int counter = 0;
LevelMetaData levelHeader;
levelHeader.typeID = ObjectType::ObjectType_Unknown;
ObjectTypeHeader header; //Read entire level file.
header.typeID = ObjectType_Level; Loader loader;
unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize);
while(counter < stringSize) //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.
while(counter < bufferSize)
{ {
ObjectTypeHeader typeID; ObjectTypeHeader typeID;
typeID = ParseObjectTypeHeader(buffer); ParseObject(&buffer[counter], typeID, sizeof(typeID));
switch(typeID.typeID) switch(typeID.typeID)
{ {
case TypeID_LevelHeader: case ObjectType_LevelMetaData:
//Call function //ParseObject(&buffer[counter], levelHeader, sizeof(levelHeader));
return levelHeader;
counter += LevelHeaderSize; counter += sizeof(LevelMetaData);
break; break;
case TypeID_Object: case ObjectType_Dynamic:
//Call function //Do not call parse this object, since we are only interested in the LevelMetaData
counter += ObjectHeaderSize; //Only increase the counter size
counter += sizeof(ObjectHeader);
break; break;
default: default:
@ -93,5 +110,5 @@ ObjectTypeHeader LevelParser::ParseHeader(std::string filename)
} }
} }
return header; return levelHeader;
} }

View File

@ -19,31 +19,10 @@ namespace GameLogic
std::vector<ObjectTypeHeader> Parse(std::string filename); std::vector<ObjectTypeHeader> Parse(std::string filename);
// //
ObjectTypeHeader ParseHeader(std::string filename); LevelMetaData ParseHeader(std::string filename);
private: private:
static const int TypeHeaderSize = 4; //Including the TypeID FormatVersion formatVersion;
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,
};
}; };
} }

View File

@ -1,27 +1,97 @@
#ifndef OBJECT_DEFINES_H #ifndef OBJECT_DEFINES_H
#define OBJECT_DEFINES_H #define OBJECT_DEFINES_H
#include <string>
#include <vector>
namespace GameLogic namespace GameLogic
{ {
/************************************
Enums
*************************************/
enum ObjectType enum ObjectType
{ {
ObjectType_Level, ObjectType_LevelMetaData,
ObjectType_Static, ObjectType_Static,
ObjectType_Dynamic, ObjectType_Dynamic,
//Etc
ObjectType_NUM_OF_TYPES, ObjectType_NUM_OF_TYPES,
ObjectType_Unknown = -1, 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 struct ObjectTypeHeader
{ {
ObjectType typeID; 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<GameMode> gameModesSupported;
};
struct ObjectHeader : public PhysicsObject, public ObjectTypeHeader
{ {
//Model, //Model,
int ModelID; int ModelID;