GL - Updated LevelLoader 2.0. Bounding volume 1.0

The ObjectSpecialType enum is not correct. It has to be updated.
This commit is contained in:
Pontus Fransson 2014-02-07 14:12:54 +01:00
parent 81d43b59c0
commit 9bdf258c5d
5 changed files with 256 additions and 44 deletions

View File

@ -9,7 +9,7 @@ using namespace Utility::DynamicMemory;
LevelParser::LevelParser()
{
formatVersion.formatVersionMajor = 1;
formatVersion.formatVersionMajor = 2;
formatVersion.formatVersionMinor = 0;
}
@ -29,12 +29,15 @@ std::vector<SmartPointer<ObjectTypeHeader>> LevelParser::Parse(std::string filen
char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize);
//Read format version
FormatVersion levelFormatVersion;
LevelLoaderInternal::FormatVersion levelFormatVersion;
ParseObject(&buffer[counter], &levelFormatVersion, sizeof(levelFormatVersion));
counter += sizeof(levelFormatVersion);
if(this->formatVersion != levelFormatVersion)
{
//Do something if it's not the same version
//Returns an empty vector, because it will most likely fail to read the level format.
return objects;
}
while(counter < bufferSize)
@ -53,11 +56,73 @@ std::vector<SmartPointer<ObjectTypeHeader>> LevelParser::Parse(std::string filen
}
//This is by design, static and dynamic is using the same converter. Do not add anything inbetween them.
//Unless they are changed to not be the same.
case ObjectType_Static: case ObjectType_Dynamic:
{
//Get specialType.
ObjectSpecialType specialType;
ParseObject(&buffer[counter+4], &specialType, sizeof(typeID));
switch(specialType)
{
//These three does not have any specail variables at this time.
//There for they are using the same 'parser'.
case ObjectSpecialType_World:
case ObjectSpecialType_Building:
case ObjectSpecialType_Damaging:
case ObjectSpecialType_Explosive:
{
ObjectHeader* header = new ObjectHeader;
ParseObject(&buffer[counter], *header, counter);
objects.push_back(header);
break;
}
case ObjectSpecialType_JumpPad:
{
JumpPadAttributes* header = new JumpPadAttributes;
ParseObject(&buffer[counter], *header, counter);
//Read the spec
ParseObject(&buffer[counter], header->direction, 16);
objects.push_back(header);
break;
}
case ObjectSpecialType_BoostPad:
{
JumpPadAttributes* header = new JumpPadAttributes;
ParseObject(&buffer[counter], *header, counter);
ParseObject(&buffer[counter], header->direction, 16);
objects.push_back(header);
break;
}
case ObjectSpecialType_Portal:
{
PortalAttributes* header = new PortalAttributes;
ParseObject(&buffer[counter], *header, counter);
ParseObject(&buffer[counter], header->destination, 12);
objects.push_back(header);
break;
}
case ObjectSpecialType_SpawnPoint:
{
SpawnPointAttributes* header = new SpawnPointAttributes;
ParseObject(&buffer[counter], *header, counter);
ParseObject(&buffer[counter], header->spawnPosition, 12);
objects.push_back(header);
break;
}
default:
//Couldn't find specialType
break;
}
break;
}
@ -68,7 +133,12 @@ std::vector<SmartPointer<ObjectTypeHeader>> LevelParser::Parse(std::string filen
//Get Light type
ParseObject(&buffer[counter+4], &lightType, sizeof(lightType));
switch(lightType)
//We only support PointLight for now.
BasicLight* header = new BasicLight;
ParseObject(&buffer[counter], header, sizeof(*header));
counter += sizeof(*header);
objects.push_back(header);
/*switch(lightType)
{
case LightType_PointLight:
{
@ -98,7 +168,7 @@ std::vector<SmartPointer<ObjectTypeHeader>> LevelParser::Parse(std::string filen
//Undefined LightType.
break;
}
break;
break;*/
}
default:
//Couldn't find typeID. FAIL!!!!!!
@ -123,12 +193,16 @@ LevelMetaData LevelParser::ParseHeader(std::string filename)
char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize);
//Read format version
FormatVersion levelFormatVersion;
LevelLoaderInternal::FormatVersion levelFormatVersion;
ParseObject(&buffer[counter], &levelFormatVersion, sizeof(formatVersion));
counter += sizeof(levelFormatVersion);
if(this->formatVersion != levelFormatVersion)
{
//Do something if it's not the same version
//Returns an empty levelHeader with ObjectType_Unknown.
//Because it will not be able to read another version of the level format.
return levelHeader;
}
//Find the header in the returned string.
@ -157,6 +231,9 @@ LevelMetaData LevelParser::ParseHeader(std::string filename)
LightType lightType;
ParseObject(&buffer[counter+4], &lightType, sizeof(lightType));
//We only support pointlight for now.
counter += sizeof(BasicLight);
/*
switch(lightType)
{
case LightType_PointLight:
@ -177,7 +254,7 @@ LevelMetaData LevelParser::ParseHeader(std::string filename)
default:
//Undefined LightType.
break;
}
}*/
}
default:

View File

@ -23,7 +23,7 @@ namespace GameLogic
LevelMetaData ParseHeader(std::string filename);
private:
FormatVersion formatVersion;
LevelLoaderInternal::FormatVersion formatVersion;
};
}

View File

@ -23,6 +23,21 @@ namespace GameLogic
ObjectType_Unknown = -1
};
enum ObjectSpecialType
{
ObjectSpecialType_World,
ObjectSpecialType_Building,
ObjectSpecialType_Damaging,
ObjectSpecialType_Explosive,
ObjectSpecialType_JumpPad,
ObjectSpecialType_BoostPad,
ObjectSpecialType_Portal,
ObjectSpecialType_SpawnPoint,
ObjectSpecialType_Count,
ObjectSpecialType_Unknown = -1
};
enum UsePhysics
{
UsePhysics_UseFullPhysics,
@ -43,11 +58,12 @@ namespace GameLogic
CollisionGeometryType_Unknown = -1
};
//Only supports Pointlight right now.
enum LightType
{
LightType_PointLight,
LightType_DirectionalLight,
LightType_SpotLight,
//LightType_DirectionalLight,
//LightType_SpotLight,
LightType_Count,
LightType_Unknown = -1
@ -80,15 +96,24 @@ namespace GameLogic
/************************************
Structs
*************************************/
namespace LevelLoaderInternal
{
struct FormatVersion
{
unsigned int formatVersionMajor;
unsigned int formatVersionMinor;
FormatVersion()
: formatVersionMajor(0), formatVersionMinor(0)
{}
FormatVersion(unsigned int major, unsigned int minor)
: formatVersionMajor(major), formatVersionMinor(minor)
{}
bool operator ==(const FormatVersion& obj)
{
return (this->formatVersionMajor != obj.formatVersionMajor && this->formatVersionMinor != obj.formatVersionMinor);
return (this->formatVersionMajor == obj.formatVersionMajor && this->formatVersionMinor == obj.formatVersionMinor);
}
bool operator !=(const FormatVersion& obj)
@ -96,12 +121,44 @@ namespace GameLogic
return !(*this == obj);
}
};
}
struct ObjectTypeHeader
{
ObjectType typeID;
};
namespace LevelLoaderInternal
{
const FormatVersion boundingVolumeVersion(1, 0);
struct BoundingVolumeBase
{
float position[3];
};
struct BoundingVolumeBox : public BoundingVolumeBase
{
float size[3];
float angularAxis[3];
float angle;
};
struct BoundingVolumeSphere : public BoundingVolumeBase
{
float radius;
};
struct BoundingVolume
{
CollisionGeometryType geoType;
union
{
LevelLoaderInternal::BoundingVolumeBox box;
LevelLoaderInternal::BoundingVolumeSphere sphere;
};
};
struct PhysicsObject
{
UsePhysics usePhysics;
@ -110,8 +167,10 @@ namespace GameLogic
float inertiaRotation[3];
float frictionCoeffStatic;
float frictionCoeffDynamic;
CollisionGeometryType geometryType;
float restitutionCoeff;
BoundingVolume boundingVolume;
};
}
struct LevelMetaData : public ObjectTypeHeader
{
@ -125,8 +184,10 @@ namespace GameLogic
std::vector<GameMode> gameModesSupported;
};
struct ObjectHeader : public ObjectTypeHeader, public PhysicsObject
struct ObjectHeader : public ObjectTypeHeader, public LevelLoaderInternal::PhysicsObject
{
//Special type id for special objects: portal, jumppad, exploding objects, etc.
ObjectSpecialType specialTypeID;
//Model,
std::string ModelFile;
//Position
@ -138,6 +199,25 @@ namespace GameLogic
float scale[3];
};
/************************************
Special objects
*************************************/
struct JumpPadAttributes : public ObjectHeader
{
float direction[3];
float power;
};
struct PortalAttributes : public ObjectHeader
{
float destination[3];
};
struct SpawnPointAttributes : public ObjectHeader
{
float spawnPosition[3];
};
/************************************
Lights
@ -145,12 +225,13 @@ namespace GameLogic
struct BasicLight : public ObjectTypeHeader
{
LightType lightType;
float ambientColor[3];
float diffuseColor[3];
float specularColor[3];
LightType lightType; //Is not used right now
float color[3];
float position[3];
float raduis;
float intensity;
};
/* We only support pointlight right now.
struct PointLight : public BasicLight
{
float position[3];
@ -166,7 +247,7 @@ namespace GameLogic
float direction[3];
float range;
float attenuation[3];
};
};*/
}
#endif

View File

@ -4,6 +4,7 @@
#include "ParseFunctions.h"
#include "../../../Misc/Packing/Packing.h"
#include "Loader.h"
#include <string>
using namespace Oyster::Packing;
@ -29,6 +30,9 @@ namespace GameLogic
memcpy(&header.typeID, &buffer[start], 4);
start += 4;
memcpy(&header.specialTypeID, &buffer[start], 4);
start += 4;
memcpy(&tempSize, &buffer[start], 4);
start += 4;
@ -36,14 +40,19 @@ namespace GameLogic
header.ModelFile.assign(&tempName[0], &tempName[tempSize]);
start += tempSize;
//The reset of the object struct
//3 float[3], 1 float
memcpy(&header.position, &buffer[start], 40);
start += 40;
//2 float[3], 3 float, 2 uint
//Physics struct
//2 float[3], 4 float, 1 uint
memcpy(&header.usePhysics, &buffer[start], 44);
start += 44;
//Read path for bounding volume
ParseBoundingVolume(&buffer[start], header.boundingVolume, start);
size += start;
}
@ -107,5 +116,49 @@ namespace GameLogic
size += start;
}
void ParseBoundingVolume(char* buffer, LevelLoaderInternal::BoundingVolume& volume, int &size)
{
int start = 0;
int tempSize = 0;
char tempName[128];
memcpy(&tempSize, &buffer[start], 4);
start += 4;
memcpy(&tempName, &buffer[start], tempSize);
string fileName;
fileName.assign(&tempName[0], &tempName[tempSize]);
start += tempSize;
//Läs in filen.
int fileLength = 0;
Loader loader;
char* buf = loader.LoadFile("E:\\Dropbox\\Programming\\Github\\Danbias\\Bin\\Content\\Worlds\\cgf\\"+ fileName, fileLength);
LevelLoaderInternal::FormatVersion version;
memcpy(&version, &buffer[0], sizeof(version));
memcpy(&volume.geoType, &buf[8], sizeof(volume.geoType));
//start += sizeof(volume.geoType);
switch(volume.geoType)
{
case CollisionGeometryType_Box:
memcpy(&volume.box, &buf[12], sizeof(volume.box));
//start += sizeof(volume.box);
break;
case CollisionGeometryType_Sphere:
memcpy(&volume.sphere, &buf[12], sizeof(volume.sphere));
int a = sizeof(volume.sphere);
a = 5;
//start += sizeof(volume.sphere);
break;
}
size += start;
}
}
}

View File

@ -21,6 +21,7 @@ namespace GameLogic
void ParseObject(char* buffer, void *header, int size);
void ParseObject(char* buffer, ObjectHeader& header, int& size);
void ParseLevelMetaData(char* buffer, LevelMetaData &header, int &size);
void ParseBoundingVolume(char* buffer, LevelLoaderInternal::BoundingVolume& volume, int &size);
}
}