📄 rotatingsprite.cpp
字号:
#include "RotatingSprite.h"
#include "ExampleApplication.h"
CRotatingSprite::CRotatingSprite(CApplicationBase& aApp)
: CSpriteBase(aApp)
{
CExampleApplication* app = (CExampleApplication*)&aApp;
iSettings = app->Settings();
}
CRotatingSprite::~CRotatingSprite()
{
TInt i;
for (i=0; i<2; i++)
{
if (iRotateBufferContext[i])
{
iRotateBufferContext[i]->Release();
iRotateBufferContext[i] = NULL;
}
if (iRotateBuffer[i])
{
iRotateBuffer[i]->Release();
iRotateBuffer[i] = NULL;
}
}
}
TInt CRotatingSprite::Init( IBitmap* aBitmap,
IBitmap* aMask)
{
iBitmap = aBitmap;
iMask = aMask;
// create temporary buffers for rotation
IBitmap* from[2] = {iBitmap, iMask };
TInt i;
for (i=0; i<2; i++)
{
iRotateBuffer[i] = NULL;
iRotateBufferContext[i] = NULL;
if (!from[i])
{
break;
}
// create buffer for rotation
ReturnCode ret;
ret = CRuntime::CreateInstance( iRotateBuffer[i] );
if (ret != OK )
{
return KErrNoMemory;
}
ret = iRotateBuffer[i]->Reconfigure(from[i]->GetSize(), from[i]->GetColorFormat());
if (ret != OK)
{
return KErrNoMemory;
}
ret = CRuntime::CreateInstance( iRotateBufferContext[i] );
if (ret != OK)
{
return KErrNoMemory;
}
iRotateBufferContext[i]->SetGraphicsDevice( *iRotateBuffer[i] );
ret = iRotateBuffer[i]->Lock();
iRotateBufferContext[i]->Clear(0);
ret = iRotateBuffer[i]->Unlock();
TSize size;
size.iWidth = iRotateBuffer[i]->GetSize().mX;
size.iHeight = iRotateBuffer[i]->GetSize().mY;
SetSize(size);
}
RotateBuffers();
return KErrNone;
}
void CRotatingSprite::Update(const TReal64 aFrametime)
{
CSpriteBase::Update(aFrametime);
ComputeRotationAngle(iDir);
}
void CRotatingSprite::ComputeRotationAngle(const TVector2& aDirection)
{
// get the squared length of the direction vector
const TReal64 lengthsq = aDirection.iX * aDirection.iX + aDirection.iY * aDirection.iY;
// if direction has some length, update the
// sprite rotation angle;
const TReal64 lengthlimitsq = 2.0 * 2.0;
if (lengthsq > lengthlimitsq)
{
TVector2 tmp = aDirection;
if (tmp.iY == 0.0)
{
tmp.iY = 0.000000001;
}
TReal64 angle;
Math::ATan(angle, tmp.iX / tmp.iY);
if (tmp.iY > 0.0)
{
angle -= KPi;
}
// convert radians to degrees
iAngle = angle * KRadToDeg;
}
}
void CRotatingSprite::RotateBuffers()
{
if (iBitmap)
{
// get the center of the rotated bitmap
CPoint center(iSize.iWidth >> 1, iSize.iHeight >> 1);
ReturnCode ret;
TInt i;
IBitmap* from[2] = { iBitmap, iMask };
// rotate temporary buffers
for (i=0; i<2; i++)
{
if (from[i])
{
ret = from[i]->Lock();
if (ret == OK)
{
ret = iRotateBuffer[i]->Lock();
ret = iRotateBufferContext[i]->Rotate(center, *from[i], (TInt)iAngle, center);
iRotateBuffer[i]->Unlock();
from[i]->Unlock();
}
}
}
}
}
void CRotatingSprite::Draw(IGraphicsContext& aContext, const TVector2& aCamera)
{
if (iBitmap)
{
CPoint position( (TInt)(iPos.iX - aCamera.iX),
(TInt)(iPos.iY - aCamera.iY) );
position.mX -= (iSize.iWidth >> 1);
position.mY -= (iSize.iHeight >> 1);
ReturnCode ret;
if (iSettings->iGraphicsDetail > 1)
{
RotateBuffers();
}
// draw sprite
ret = iRotateBuffer[0]->Lock();
if (ret == OK)
{
if (iRotateBuffer[1])
{
ret = iRotateBuffer[1]->Lock();
}
if (iRotateBuffer[1])
{
ret = aContext.BitBlitAlphaMask( position,
*iRotateBuffer[0],
*iRotateBuffer[1]);
}
else
{
ret = aContext.BitBlitTransparent(position, *iRotateBuffer[0], 0);
}
if (iRotateBuffer[1])
{
iRotateBuffer[1]->Unlock();
}
iRotateBuffer[0]->Unlock();
}
}
}
TBool CRotatingSprite::IsVisible(const TVector2& aCamera) const
{
// get sprite center coordinate
TPoint centerposition( (TInt)(iPos.iX - aCamera.iX),
(TInt)(iPos.iY - aCamera.iY) );
// check if sprite is visible at all...
// since sprite 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 + -