GL - Updated LevelLoader. Added lights.

Added lights.

Changed the return type to vector<SmartPointer<ObjectTypeHeader>>

Hid the LevelParser from the outside.
This commit is contained in:
Pontus Fransson 2014-02-03 14:50:15 +01:00
parent 8f234611d2
commit e7fee8b2e7
9 changed files with 156 additions and 51 deletions

Binary file not shown.

BIN
Bin/map

Binary file not shown.

Binary file not shown.

View File

@ -3,16 +3,30 @@
////////////////////////////////// //////////////////////////////////
#include "LevelLoader.h" #include "LevelLoader.h"
#include "LevelParser.h"
using namespace GameLogic; using namespace GameLogic;
using namespace GameLogic::LevelFileLoader; using namespace GameLogic::LevelFileLoader;
struct LevelLoader::PrivData
std::vector<ObjectTypeHeader> LevelLoader::LoadLevel(std::string fileName)
{ {
return parser.Parse(fileName); LevelParser parser;
};
LevelLoader::LevelLoader()
: pData(new PrivData)
{
}
LevelLoader::~LevelLoader()
{
}
std::vector<Utility::DynamicMemory::SmartPointer<ObjectTypeHeader>> LevelLoader::LoadLevel(std::string fileName)
{
return pData->parser.Parse(fileName);
} }
LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName) LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName)
{ {
return parser.ParseHeader(fileName); return pData->parser.ParseHeader(fileName);
} }

View File

@ -7,36 +7,36 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <Vector.h> #include "../Misc/Utilities.h"
#include "ObjectDefines.h" #include "ObjectDefines.h"
#include "LevelParser.h"
namespace GameLogic namespace GameLogic
{ {
class LevelLoader class LevelLoader
{ {
public: public:
LevelLoader(){this->parser = GameLogic::LevelFileLoader::LevelParser(); } LevelLoader();
~LevelLoader(){} ~LevelLoader();
/******************************************************** /********************************************************
* Loads the level and objects from file. * Loads the level and objects from file.
* @param fileName: Path to the level-file that you want to load. * @param fileName: Path to the level-file that you want to load.
* @return: Returns all structs with objects and information about the level. * @return: Returns all structs with objects and information about the level.
********************************************************/ ********************************************************/
std::vector<ObjectTypeHeader> LoadLevel(std::string fileName); std::vector<Utility::DynamicMemory::SmartPointer<ObjectTypeHeader>> LoadLevel(std::string fileName);
/******************************************************** /********************************************************
* Just for fast access for the meta information about the level. * Just for fast access for the meta information about the level.
* @param fileName: Path to the level-file that you want to load. * @param fileName: Path to the level-file that you want to load.
* @return: Returns the meta information about the level. * @return: Returns the meta information about the level.
********************************************************/ ********************************************************/
LevelMetaData LoadLevelHeader(std::string fileName); //. LevelMetaData LoadLevelHeader(std::string fileName); //.
private: private:
GameLogic::LevelFileLoader::LevelParser parser; struct PrivData;
}; Utility::DynamicMemory::SmartPointer<PrivData> pData;
};
} }
#endif #endif

View File

@ -5,6 +5,7 @@
using namespace GameLogic; using namespace GameLogic;
using namespace ::LevelFileLoader; using namespace ::LevelFileLoader;
using namespace Utility::DynamicMemory;
LevelParser::LevelParser() LevelParser::LevelParser()
{ {
@ -16,12 +17,12 @@ LevelParser::~LevelParser()
{ {
} }
std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename) std::vector<SmartPointer<ObjectTypeHeader>> LevelParser::Parse(std::string filename)
{ {
int bufferSize = 0; int bufferSize = 0;
int counter = 0; int counter = 0;
std::vector<ObjectTypeHeader> objects; std::vector<SmartPointer<ObjectTypeHeader>> objects;
//Read entire level file. //Read entire level file.
Loader loader; Loader loader;
@ -42,26 +43,66 @@ std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename)
ParseObject(&buffer[counter], &typeID, sizeof(typeID)); ParseObject(&buffer[counter], &typeID, sizeof(typeID));
switch((int)typeID.typeID) switch((int)typeID.typeID)
{ {
case ObjectType_LevelMetaData: case ObjectType_LevelMetaData:
{ {
LevelMetaData header; LevelMetaData* header = new LevelMetaData;
ParseLevelMetaData(&buffer[counter], header, counter); ParseLevelMetaData(&buffer[counter], *header, counter);
objects.push_back(header); objects.push_back(header);
break; break;
} }
case ObjectType_Dynamic: //This is by design, static and dynamic is using the same converter. Do not add anything inbetween them.
{ case ObjectType_Static: case ObjectType_Dynamic:
ObjectHeader header; {
ParseObject(&buffer[counter], &header, sizeof(header)); ObjectHeader* header = new ObjectHeader;
objects.push_back(header); ParseObject(&buffer[counter], header, sizeof(*header));
counter += sizeof(header); objects.push_back(header);
break; counter += sizeof(*header);
} break;
}
default: case ObjectType_Light:
//Couldn't find typeID. FAIL!!!!!! {
break; LightType lightType;
//Get Light type
ParseObject(&buffer[counter+4], &lightType, sizeof(lightType));
switch(lightType)
{
case LightType_PointLight:
{
PointLight* header = new PointLight;
ParseObject(&buffer[counter], header, sizeof(*header));
counter += sizeof(*header);
objects.push_back(header);
break;
}
case LightType_DirectionalLight:
{
DirectionalLight* header = new DirectionalLight;
ParseObject(&buffer[counter], header, sizeof(*header));
counter += sizeof(*header);
objects.push_back(header);
break;
}
case LightType_SpotLight:
{
SpotLight* header = new SpotLight;
ParseObject(&buffer[counter], header, sizeof(*header));
counter += sizeof(*header);
objects.push_back(header);
break;
}
default:
//Undefined LightType.
break;
}
break;
}
default:
//Couldn't find typeID. FAIL!!!!!!
break;
} }
} }

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "ObjectDefines.h" #include "ObjectDefines.h"
#include "../Misc/Utilities.h"
namespace GameLogic namespace GameLogic
{ {
@ -16,7 +17,7 @@ namespace GameLogic
~LevelParser(); ~LevelParser();
// //
std::vector<ObjectTypeHeader> Parse(std::string filename); std::vector<Utility::DynamicMemory::SmartPointer<ObjectTypeHeader>> Parse(std::string filename);
// //
LevelMetaData ParseHeader(std::string filename); LevelMetaData ParseHeader(std::string filename);

View File

@ -15,11 +15,12 @@ namespace GameLogic
ObjectType_LevelMetaData, ObjectType_LevelMetaData,
ObjectType_Static, ObjectType_Static,
ObjectType_Dynamic, ObjectType_Dynamic,
ObjectType_Light,
//Etc //Etc
ObjectType_NUM_OF_TYPES, ObjectType_NUM_OF_TYPES,
ObjectType_Unknown = -1, ObjectType_Unknown = -1
}; };
enum UsePhysics enum UsePhysics
@ -29,7 +30,17 @@ namespace GameLogic
UsePhysics_IgnorePhysics, UsePhysics_IgnorePhysics,
UsePhysics_Count, UsePhysics_Count,
UsePhysics_Unknown = -1, UsePhysics_Unknown = -1
};
enum LightType
{
LightType_PointLight,
LightType_DirectionalLight,
LightType_SpotLight,
LightType_Count,
LightType_Unknown = -1
}; };
//Should this be moved somewhere else? //Should this be moved somewhere else?
@ -40,7 +51,7 @@ namespace GameLogic
//Etc //Etc
GameMode_Count, GameMode_Count,
GameMode_Unknown = -1, GameMode_Unknown = -1
}; };
@ -104,6 +115,36 @@ namespace GameLogic
//Scale //Scale
float scale[3]; float scale[3];
}; };
/************************************
Lights
*************************************/
struct BasicLight : public ObjectTypeHeader
{
LightType lightType;
float ambientColor[3];
float diffuseColor[3];
float specularColor[3];
};
struct PointLight : public BasicLight
{
float position[3];
};
struct DirectionalLight : public BasicLight
{
float direction[3];
};
struct SpotLight : public BasicLight
{
float direction[3];
float range;
float attenuation[3];
};
} }
#endif #endif

View File

@ -10,6 +10,14 @@ namespace GameLogic
{ {
namespace LevelFileLoader namespace LevelFileLoader
{ {
/*
These functions will copy data from where the buffer pointer points.
header is the destination where the data will be copied.
size is either the size of the data to be copied (if it is NOT sent by reference).
Or the current index that is being used to parse the entire file (if it is sent by reference) this means you have to increase size with the appropiate size after you have copied.
*/
void ParseObject(char* buffer, void *header, int size); void ParseObject(char* buffer, void *header, int size);
void ParseLevelMetaData(char* buffer, LevelMetaData &header, int &size); void ParseLevelMetaData(char* buffer, LevelMetaData &header, int &size);
} }