#include "../engine/engine.h"
#include ".\cbuild.h"
#include ".\cground.h"

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \return 返回值的描述
 */
cBuild::cBuild(void)
{
	m_ulCount = 1;
	m_pTexture = NULL;
	m_nPicLib = 0;
	m_nPic = 0;
}

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \return 返回值的描述
 */
cBuild::~cBuild(void)
{
	FinalRelease();
}

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \return 返回值的描述
 */
void cBuild::FinalRelease()
{
	m_pTexture = NULL;
};

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \param ptMapOffset : 描述
 * \return 返回值的描述
 */
//在地图偏移为ptMapOffset的地方绘制
bool cBuild::Draw(POINT ptMapOffset)
{
	if(!m_pTexture)
		RestoreDeviceObjects();
	if (!m_pTexture)
		return false;
	
	int w,h;
	w = m_pTexture->GetWidth();
	h = m_pTexture->GetHeight();

	int m_iScreenOffsetX = m_dwX * GRID_WIDTH;
	int m_iScreenOffsetY = m_dwY * GRID_HEIGHT - h + GRID_HEIGHT;

	POINT pt;
	pt.x	=	m_iScreenOffsetX - ptMapOffset.x;
	pt.y	=	m_iScreenOffsetY - ptMapOffset.y;
	m_pTexture->Render(pt.x,pt.y);
	return true;
}

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \param ptScreen : 描述
 * \return 返回值的描述
 */
//在屏幕某处直接绘制
bool cBuild::DrawAtScreen(POINT ptScreen)
{
	return false;
}

#define OBJ_GROUP 128
#define GetTileGroupFrame(tile,group,frame) group = (tile)/OBJ_GROUP;frame = (tile)%OBJ_GROUP;

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \param rcBuild : 描述
 * \param pRes : 描述
 * \return 返回值的描述
 */
bool cBuild::GetBuildRect(stRectI& rcBuild,IBitmapList* pRes)
{
	ATLASSERT(pRes);
	if (pRes == NULL)
		return false;
	if (m_nPicLib==0 || m_nPic==0)
		return false;
	int w,h;
	int nGroup,nFrame;

	GetTileGroupFrame(m_nPic,nGroup,nFrame);

	IBitmap* p = pRes->GetBitmap(nGroup,nFrame,0);
	if(!p) return false;

	w = p->GetWidth();
	h = p->GetHeight();

	rcBuild.left = m_dwX * GRID_WIDTH;
	rcBuild.right = rcBuild.left + w;
	rcBuild.top = m_dwY * GRID_HEIGHT - h + GRID_HEIGHT;
	rcBuild.bottom = rcBuild.top + h;
	return true;
}

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \return 返回值的描述
 */
HRESULT cBuild::RestoreDeviceObjects() 
{
	if (m_nPicLib && m_nPic)
	{
		
		IBitmapList* pRes = GetDevice()->FindResource(cGround::GetObjName(m_nPicLib),c_nMagicResourceFlags,Usage_Building);
		if(pRes)
		{
			int nGroup,nFrame;
			GetTileGroupFrame(m_nPic,nGroup,nFrame);
			m_pTexture = pRes->GetBitmap(nGroup,nFrame);
		}
	}
	return S_OK;
}

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \param dwObject : 描述
 * \param xGrid : 描述
 * \param yGrid : 描述
 * \return 返回值的描述
 */
void cBuild::SetPic(DWORD dwObject,int xGrid,int yGrid)
{
	//DWORD dwObject = pTile->uObject;
	m_nPicLib = (dwObject & 0xffff0000)>>16;
	m_nPic = (dwObject & 0xffff);
	m_dwX = xGrid;
	m_dwY = yGrid;
}

/**
 * \brief 简短描述
 * 
 * 详细描述
 * 
 * \param p : 描述
 * \param nPic : 描述
 * \return 返回值的描述
 */
void cBuild::PreLoad(IBitmapList* p, int nPic)
{
}
