view LightClone/Source/Loader.cpp @ 37:58a16d529d95

Refactoring code and adding events for drag and drop
author koryspansel <koryspansel@bendbroadband.com>
date Wed, 21 Sep 2011 20:30:29 -0700
parents 7e3a0ae9c016
children efd2b1ca5b77
line wrap: on
line source

/*
 * Loader
 */

#include "Loader.h"
#include "Util.h"

/*
 * Loader
 */
Loader::Loader()
{
	kSize.X			= 0;
	kSize.Y			= 0;
	pType			= 0;
	pHeight			= 0;
	kPosition.X		= 0;
	kPosition.Y		= 0;
	kDirection		= Direction_North;
}

/*
 * ~Loader
 */
Loader::~Loader()
{
	if(pType)
	{
		delete[] pType;
		pType = 0;
	}

	if(pHeight)
	{
		delete[] pHeight;
		pHeight = 0;
	}
}

/*
 * Load
 */
ErrorCode Loader::Load(const char* pName)
{
	if(pType)
	{
		delete[] pType;
		pType = 0;
	}

	if(pHeight)
	{
		delete[] pHeight;
		pHeight = 0;
	}

	Buffer kBuffer = LoadFile(pName);

	uint32 nTemp;

	if(kBuffer.Read(&kSize.X) != Error_Success)
		return Error_Fail;

	if(kBuffer.Read(&kSize.Y) != Error_Success)
		return Error_Fail;

	if(kBuffer.Read(&kPosition.X) != Error_Success)
		return Error_Fail;

	if(kBuffer.Read(&kPosition.Y) != Error_Success)
		return Error_Fail;

	if(kBuffer.Read(&kDirection) != Error_Success)
		return Error_Fail;

	if(kBuffer.Read(&nTemp) != Error_Success)
		return Error_Fail;

	if(kSize.X == 0 || kSize.Y == 0)
		return Error_Fail;

	pType			= new uint32[kSize.X * kSize.Y];
	pHeight			= new uint32[kSize.X * kSize.Y];

	for(uint32 i = 0; i < nTemp; ++i)
	{
		uint32 nTemp2 = 0;
		if(kBuffer.Read(&nTemp2) != Error_Success)
			return Error_Fail;
	}

	for(uint32 nY = 0; nY < kSize.Y; ++nY)
	{
		for(uint32 nX = 0; nX < kSize.X; ++nX)
		{
			const uint32 nIndex = nY * kSize.X + nX;

			if(kBuffer.Read(pType + nIndex) != Error_Success)
				return Error_Fail;

			if(kBuffer.Read(pHeight + nIndex) != Error_Success)
				return Error_Fail;
		}
	}

	return Error_Success;
}

/*
 * GetSize
 */
const Size& Loader::GetSize() const
{
	return kSize;
}

/*
 * GetTowerType
 */
uint32 Loader::GetTowerType(uint32 nX, uint32 nY)
{
	return pType[nY * kSize.X + nX];
}

/*
 * GetTowerHeight
 */
uint32 Loader::GetTowerHeight(uint32 nX, uint32 nY)
{
	return pHeight[nY * kSize.X + nX];
}

/*
 * GetInitialPosition
 */
const Position& Loader::GetInitialPosition() const
{
	return kPosition;
}

/*
 * GetInitialDirection
 */
const Direction& Loader::GetInitialDirection() const
{
	return kDirection;
}