GL - More Object structs, LevelLoader api comments.

Changed the LevelParser to use the new functions from ParserFunctions.
Might need to cast something to (void*).
This commit is contained in:
Pontus Fransson 2014-01-28 11:27:29 +01:00
parent dd2b214122
commit 4de1c1aafd
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);
}
ObjectTypeHeader LevelLoader::LoadLevelHeader(std::string fileName)
LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName)
{
return parser->ParseHeader(fileName);
}

View File

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

View File

@ -6,9 +6,10 @@
using namespace GameLogic;
using namespace ::LevelFileLoader;
LevelParser::LevelParser()
{
formatVersion.formatVersionMajor = 1;
formatVersion.formatVersionMinor = 0;
}
LevelParser::~LevelParser()
@ -18,35 +19,43 @@ LevelParser::~LevelParser()
//
std::vector<ObjectTypeHeader> 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<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
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;
@ -56,36 +65,44 @@ std::vector<ObjectTypeHeader> 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;
//Read entire level file.
Loader loader;
unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize);
//Find the header in the returned string.
int bufferSize = 0;
unsigned int counter = 0;
LevelMetaData levelHeader;
levelHeader.typeID = ObjectType::ObjectType_Unknown;
ObjectTypeHeader header;
header.typeID = ObjectType_Level;
//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
}
//Find the header in the returned string.
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:
@ -94,5 +111,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);
//
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;
};
}

View File

@ -1,27 +1,97 @@
#ifndef OBJECT_DEFINES_H
#define OBJECT_DEFINES_H
#include <string>
#include <vector>
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<GameMode> gameModesSupported;
};
struct ObjectHeader : public PhysicsObject, public ObjectTypeHeader
{
//Model,
int ModelID;