📄 tree.cpp
字号:
#include "Tree.h"
#include "ApplicationBase.h"
CTree::CTree(CApplicationBase& aApp)
: CSpriteBase(aApp)
{
}
CTree::~CTree()
{
iLayers.Reset();
iLayers.Close();
}
void CTree::AddLayer(TTreeLayer* aLayer)
{
// add layer to internal array
iLayers.Append(aLayer);
// store the size of the biggest layer added
if (aLayer->iBitmap)
{
CSize bmsize = aLayer->iBitmap->GetSize();
if (bmsize.mX > iSize.iWidth)
{
iSize.iWidth = bmsize.mX;
}
if (bmsize.mY > iSize.iHeight)
{
iSize.iHeight = bmsize.mY;
}
}
}
void CTree::Update(const TReal64 /*aFrametime*/)
{
}
void CTree::Draw(IGraphicsContext& aContext, const TVector2& aCamera)
{
// draw tree...
CPoint centerposition( (TInt)(iPos.iX - aCamera.iX),
(TInt)(iPos.iY - aCamera.iY) );
TSize bbsize = iApp.BackBufferSize();
// go thru tree layers
TInt i;
for (i=0; i<iLayers.Count(); i++)
{
TTreeLayer* layer = iLayers[i];
if (layer->iBitmap)
{
CPoint position = centerposition;
// compute offset based on height
TReal64 offsetx = (TReal64)((bbsize.iWidth >> 1) - centerposition.mX);
TReal64 offsety = (TReal64)((bbsize.iHeight >> 1) - centerposition.mY);
offsetx *= layer->iHeight * 0.010f;
offsety *= layer->iHeight * 0.010f;
position.mX -= (TInt)offsetx;
position.mY -= (TInt)offsety;
// add half of the layer size to center the
// tree to its coordinate
position.mX -= (layer->iBitmap->GetSize().mX >> 1);
position.mY -= (layer->iBitmap->GetSize().mY >> 1);
// draw tree layer
ReturnCode ret;
ret = layer->iBitmap->Lock();
if (ret == OK)
{
if (layer->iMask)
{
ret = layer->iMask->Lock();
if ( ret == OK)
{
ret = aContext.BitBlitAlphaMask( position,
*layer->iBitmap,
*layer->iMask);
layer->iMask->Unlock();
}
}
else
{
ret = aContext.BitBlitTransparent( position,
*layer->iBitmap,
KTransparentColor);
}
ret = layer->iBitmap->Unlock();
}
}
}
}
TBool CTree::IsVisible(const TVector2& aCamera) const
{
// get tree center coordinate
TPoint centerposition( (TInt)(iPos.iX - aCamera.iX),
(TInt)(iPos.iY - aCamera.iY) );
// check if tree is visible at all...
// since tree is centered to its coordinate,
// take half of the maximum layer size
TSize tmp(iSize);
tmp.iWidth >>= 1;
tmp.iHeight >>= 1;
TSize bbsize = iApp.BackBufferSize();
if (centerposition.iX - tmp.iWidth > bbsize.iWidth)
{
// out from the right
return EFalse;
}
if (centerposition.iX < -tmp.iWidth)
{
// out from the left
return EFalse;
}
if (centerposition.iY - tmp.iHeight > bbsize.iHeight)
{
// out from the bottom
return EFalse;
}
if (centerposition.iY < -tmp.iHeight)
{
// out from the top
return EFalse;
}
return ETrue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -