GL - LevelLoader = done and done

This commit is contained in:
Sam Mario Svensson 2014-01-28 16:15:10 +01:00
parent 2b63563a9b
commit f968adebeb
11 changed files with 78 additions and 38 deletions

BIN
Bin/Level.txt Normal file

Binary file not shown.

BIN
Bin/map Normal file

Binary file not shown.

Binary file not shown.

View File

@ -9,10 +9,10 @@ using namespace GameLogic::LevelFileLoader;
std::vector<ObjectTypeHeader> LevelLoader::LoadLevel(std::string fileName)
{
return parser->Parse(fileName);
return parser.Parse(fileName);
}
LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName)
{
return parser->ParseHeader(fileName);
return parser.ParseHeader(fileName);
}

View File

@ -17,7 +17,7 @@ namespace GameLogic
{
public:
LevelLoader(){this->parser = new GameLogic::LevelFileLoader::LevelParser(); }
LevelLoader(){this->parser = GameLogic::LevelFileLoader::LevelParser(); }
~LevelLoader(){}
/********************************************************
@ -35,7 +35,7 @@ namespace GameLogic
LevelMetaData LoadLevelHeader(std::string fileName); //.
private:
GameLogic::LevelFileLoader::LevelParser *parser;
GameLogic::LevelFileLoader::LevelParser parser;
};
}

View File

@ -19,17 +19,17 @@ LevelParser::~LevelParser()
std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename)
{
int bufferSize = 0;
unsigned int counter = 0;
int counter = 0;
std::vector<ObjectTypeHeader> objects;
//Read entire level file.
Loader loader;
unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize);
char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize);
//Read format version
FormatVersion levelFormatVersion;
ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion));
//ParseObject(&buffer[counter], &levelFormatVersion, sizeof(formatVersion));
if(this->formatVersion != levelFormatVersion)
{
//Do something if it's not the same version
@ -39,22 +39,26 @@ std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename)
{
//Get typeID
ObjectTypeHeader typeID;
ParseObject(&buffer[counter], typeID, sizeof(typeID));
ParseObject(&buffer[counter], &typeID, sizeof(typeID));
switch((int)typeID.typeID)
{
case ObjectType_LevelMetaData:
{
LevelMetaData header;
//ParseObject(&buffer[counter], header, sizeof(header));
ParseLevelMetaData(&buffer[counter], header, counter);
objects.push_back(header);
break;
}
case ObjectType_Dynamic:
{
ObjectHeader header;
ParseObject(&buffer[counter], &header, sizeof(header));
objects.push_back(header);
counter += sizeof(header);
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;
@ -68,18 +72,18 @@ std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename)
LevelMetaData LevelParser::ParseHeader(std::string filename)
{
int bufferSize = 0;
unsigned int counter = 0;
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(), bufferSize);
char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize);
//Read format version
FormatVersion levelFormatVersion;
ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion));
//ParseObject(&buffer[counter], &levelFormatVersion, sizeof(formatVersion));
if(this->formatVersion != levelFormatVersion)
{
//Do something if it's not the same version
@ -89,14 +93,13 @@ LevelMetaData LevelParser::ParseHeader(std::string filename)
while(counter < bufferSize)
{
ObjectTypeHeader typeID;
ParseObject(&buffer[counter], typeID, sizeof(typeID));
ParseObject(&buffer[counter], &typeID, sizeof(typeID));
switch(typeID.typeID)
{
case ObjectType_LevelMetaData:
//ParseObject(&buffer[counter], levelHeader, sizeof(levelHeader));
ParseLevelMetaData(&buffer[counter], levelHeader, counter);
return levelHeader;
counter += sizeof(LevelMetaData);
break;
case ObjectType_Dynamic:
//Do not call parse this object, since we are only interested in the LevelMetaData

View File

@ -3,19 +3,31 @@
//////////////////////////////////
#include "Loader.h"
#include <fstream>
using namespace GameLogic::LevelFileLoader;
using namespace Oyster::Resource;
using namespace std;
unsigned char* Loader::LoadFile(std::string fileName, int &size)
char* Loader::LoadFile(std::string fileName, int &size)
{
//convert from string to wstring
std::wstring temp(fileName.begin(), fileName.end());
//convert from wstring to wchar then loads the file
unsigned char* buffer = (unsigned char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false);
char* buffer = (char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false);
//std::ifstream f;
//f.open(fileName, std::ios::binary);
//if (!f.is_open())
// return 0;
//f.seekg(0, std::ifstream::end);
//size = f.tellg();
//f.seekg(0);
//char* buffer = new char[size];
//f.read(buffer, size);
//
//f.close();
//gets the size of the char buffer.
size = OysterResource::GetResourceSize(buffer);
return buffer;

View File

@ -17,7 +17,7 @@ namespace GameLogic
public:
Loader (){};
~Loader(){};
unsigned char* LoadFile(std::string fileName, int &size);
char* LoadFile(std::string fileName, int &size);
//TODO:
//Add functionality to load physicsObjects (hitboxes)

View File

@ -91,7 +91,7 @@ namespace GameLogic
std::vector<GameMode> gameModesSupported;
};
struct ObjectHeader : public PhysicsObject, public ObjectTypeHeader
struct ObjectHeader : public ObjectTypeHeader
{
//Model,
int ModelID;

View File

@ -15,41 +15,66 @@ namespace GameLogic
{
namespace LevelFileLoader
{
void ParseObject(unsigned char* buffer, void* header, int size)
void ParseObject(char* buffer, void *header, int size)
{
memcpy(header, buffer, size);
}
void ParseLevelMetaData(unsigned char* buffer, struct LevelMetaData &header)
void ParseLevelMetaData(char* buffer, LevelMetaData &header, int &size)
{
int start = 0;
int tempSize;
char tempName[100];
memcpy(&header.typeID, &buffer[start], 4);
start += 4;
memcpy(&tempSize , &buffer[start], 4);
start += 4;
memcpy(&header.levelName, &buffer[start], tempSize);
memcpy(&tempName, &buffer[start], tempSize);
header.levelName.assign(&tempName[0], &tempName[tempSize]);
start += tempSize;
memcpy(&header.levelVersion, &buffer[start], 8)
memcpy(&header.levelVersion, &buffer[start], 8);
start += 8;
memcpy(&tempSize, &buffer[start], 4);
start +=4;
memcpy(&header.description, &buffer[start], tempSize);
memcpy(&tempName, &buffer[start], tempSize);
header.levelDescription.assign(&tempName[0], &tempName[tempSize]);
start += tempSize;
memcpy(&tempSize, &buffer[start], 4);
start += 4;
memcpy(&header.author, &buffer[start], tempSize);
memcpy(&tempName, &buffer[start], tempSize);
header.levelAuthor.assign(&tempName[0], &tempName[tempSize]);
start += tempSize;
memcpy(&header.nrOfPlayers, &buffer[start], 4);
memcpy(&header.maxNumberOfPlayer, &buffer[start], 4);
start += 4;
memcpy(&header.worldSize, &buffer[start], 4);
start += 4;
memcpy(&header.map, &buffer[start], 4);
memcpy(&header.overviewPictureID, &buffer[start], 4);
start += 4;
memcpy(&tempSize, &buffer[start], 4);
start += 4;
memcpy(&header.gameModes, &buffer[start], 4 * tempSize);
start += tempSize;
int temp;
for(int i = 0; i < tempSize; i++)
{
memcpy(&temp, &buffer[start], 4);
start += 4;
header.gameModesSupported.push_back((GameMode)temp);
}
size += start;
}
}
}

View File

@ -10,8 +10,8 @@ namespace GameLogic
{
namespace LevelFileLoader
{
void ParseObject(unsigned char* buffer, void* header, int size);
void ParseLevelMetaData(unsigned char* buffer, struct ObjectTypeHeader &header);
void ParseObject(char* buffer, void *header, int size);
void ParseLevelMetaData(char* buffer, LevelMetaData &header, int &size);
}
}