#include "stdafx.h"
#include "War3TypesAndConstants.h"
#include "ShadowMap.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CShadowMap::CShadowMap()
{
	m_pByteMap = NULL;
}

CShadowMap::~CShadowMap()
{
	if (m_pByteMap)
	{delete []m_pByteMap;}
}

void CShadowMap::SetSize(int width, int height)
{
	byte* pOldByteMap;
	int oldwidth, oldheight, x, y, maxx, maxy;

	if ((width > 0) && (height > 0) && (width <= SHAD_MAX_WIDTH) && (height <= SHAD_MAX_HEIGHT))
	{
		pOldByteMap = m_pByteMap;
		oldwidth = m_Width;
		oldheight = m_Height;
		m_Height = height;
		m_Width = width;
		m_pByteMap = new byte[width*height];
		memset( m_pByteMap, 0, width*height);
		if (pOldByteMap)
		{
			if (m_Width < oldwidth)
			{
				maxx = m_Width;
			}
			else
			{
				maxx = oldwidth;
			}
			if (m_Height < oldheight)
			{
				maxy = m_Height;
			}
			else
			{
				maxy = oldheight;
			}
			for (y=0; y < maxy; y++)
			for (x=0; x < maxx; x++)
			{
				m_pByteMap[y*m_Width + x] = pOldByteMap[y*oldwidth + x];
			}
			delete []pOldByteMap;
		}

	}
}

void CShadowMap::SetShadowAt(int x, int y)
{
	if ((x >= 0) && (x < m_Width) && (y >= 0) && (y < m_Height))
	{m_pByteMap[y*m_Width+x] = 0x0000;}
}

void CShadowMap::SetShadowAt(CRect rect)
{
	//AfxMessageBox("Not implemented yet!", MB_OK | MB_APPLMODAL | MB_ICONSTOP);
}

void CShadowMap::RemoveShadowAt(int x, int y)
{
	if ((x >= 0) && (x < m_Width) && (y >= 0) && (y < m_Height))
	{m_pByteMap[y*m_Width+x] = 0x0000;}
}

void CShadowMap::RemoveShadowAt(CRect rect)
{
	//AfxMessageBox("Not implemented yet!", MB_OK | MB_APPLMODAL | MB_ICONSTOP);
}

int CShadowMap::GetWidth()
{
	return m_Width;
}

int CShadowMap::GetHeight()
{
	return m_Height;
}

byte* CShadowMap::GetBytemapPtr()
{
	return m_pByteMap;
}
