⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 player.cpp

📁 RGA: Biowaste Game Example This C++ application demonstrates how to create a 2D mobile game for S60
💻 CPP
字号:


#include "Player.h"


CPlayer::CPlayer(CApplicationBase& aApp, CLevel& aLevel)
	:	CRotatingSprite(aApp),
		iLevel(aLevel)
	{
	}

CPlayer::~CPlayer()
	{
	if (iScrollBufferContext)
		{
		iScrollBufferContext->Release();
		iScrollBufferContext = NULL;
		}
	if (iScrollBuffer)
		{
		iScrollBuffer->Release();
		iScrollBuffer = NULL;
		}
	}
		
TInt CPlayer::Init(	IBitmap* aBitmap, IBitmap* aMask)
	{
	TInt error;
	
	// create buffer for scrolling
	ReturnCode ret;
	ret = CRuntime::CreateInstance( iScrollBuffer );
	if (ret != OK )
		{
		return KErrNoMemory;
		}
	
	ret = iScrollBuffer->Reconfigure(aBitmap->GetSize(), aBitmap->GetColorFormat());
	if (ret != OK)
		{
		return KErrNoMemory;
		}
	
	ret = CRuntime::CreateInstance( iScrollBufferContext );
	if (ret != OK)
		{
		return KErrNoMemory;
		}
	iScrollBufferContext->SetGraphicsDevice( *iScrollBuffer );
	
	ret = iScrollBuffer->Lock();
	iScrollBufferContext->Clear(0);
	ret = iScrollBuffer->Unlock();
	
	iOriginalBitmap = aBitmap;

	error = CRotatingSprite::Init(iScrollBuffer, aMask);
	if (error != KErrNone)
		{
		return error;
		}
	
	return error;
	}


void CPlayer::Update(const TReal64 aFrametime)
	{
	// update controls
	TBool movementkeydown = EFalse;
	const TReal64 speed = 160.0f;
	TVector2 accel(0, 0);

	if (iApp.IsKeyDown(INPUT_KEY_DIGITAL_UP))
		{
		accel.iY = -speed;
		movementkeydown = ETrue;
		}
	if (iApp.IsKeyDown(INPUT_KEY_DIGITAL_DOWN))
		{
		accel.iY = speed;
		movementkeydown = ETrue;
		}
	if (iApp.IsKeyDown(INPUT_KEY_DIGITAL_LEFT))
		{
		accel.iX = -speed;
		movementkeydown = ETrue;
		}
	if (iApp.IsKeyDown(INPUT_KEY_DIGITAL_RIGHT))
		{
		accel.iX = speed;
		movementkeydown = ETrue;
		}
	
	SetAcceleration(accel);

	iMovementSpeed = iDir.Length();
	
	// clamp dir to max speed
	if (iMovementSpeed > iMaxSpeed)
		{
		TReal64 tmp = iMaxSpeed / iMovementSpeed;
		iDir.iX *= tmp;
		iDir.iY *= tmp;
		iMovementSpeed = iMaxSpeed;
		}
	
	// update ball scrolling animation phase
	iBallAnimationPhase -= iMovementSpeed * iScrollSpeed * aFrametime;
	if (iBallAnimationPhase < 0.0)
		{
		iBallAnimationPhase += iScrollBuffer->GetSize().mY;
		}
	
	CRotatingSprite::Update(aFrametime);
	
	
	// apply some friction
	if (!movementkeydown && iMovementSpeed > 1.0)
		{
		const TReal64 friction = 160.0 * aFrametime;
		
		iMovementSpeed -= friction;
		if (iMovementSpeed < 1.0)
			{
			// stop the movement
			iDir.iX = 0.0;
			iDir.iY = 0.0;
			}
		else
			{
			iDir.SetLength(iMovementSpeed);
			}
		}
	
	
	
	
	// check level boundaries, bounce from off limits
	TSize levelsize = iLevel.SizeInPixels();

	TSize tmp(iSize);
	tmp.iWidth >>= 1;
	tmp.iHeight >>= 1;

	if (iPos.iX < tmp.iWidth)
		{
		iPos.iX = tmp.iWidth;
		iDir.iX = -iDir.iX * 0.5;
		}
	if (iPos.iX > levelsize.iWidth - tmp.iWidth)
		{
		iPos.iX = levelsize.iWidth - tmp.iWidth;
		iDir.iX = -iDir.iX * 0.5;
		}
	if (iPos.iY < tmp.iHeight)
		{
		iPos.iY = tmp.iHeight;
		iDir.iY = -iDir.iY * 0.5;
		}
	if (iPos.iY > levelsize.iHeight - tmp.iHeight)
		{
		iPos.iY = levelsize.iHeight - tmp.iHeight;
		iDir.iY = -iDir.iY * 0.5;
		}
	}
		

void CPlayer::Draw(IGraphicsContext& aContext, const TVector2& aCamera)
	{
	// scroll the ball...
	if (iSettings->iGraphicsDetail > 1)
		{
		iOriginalBitmap->Lock();
		iScrollBuffer->Lock();
		TInt scrolloffset = (TInt)iBallAnimationPhase;
		CPoint scrollpos(0, scrolloffset);
		iScrollBufferContext->BitBlit(scrollpos, *iOriginalBitmap);
		
		scrollpos.mY -= iOriginalBitmap->GetSize().mY;
		iScrollBufferContext->BitBlit(scrollpos, *iOriginalBitmap);
		iScrollBuffer->Unlock();
		iOriginalBitmap->Unlock();

		RotateBuffers();
		}

	// custom player draw function
	CPoint position(	(TInt)(iPos.iX - aCamera.iX),
			(TInt)(iPos.iY - aCamera.iY) );


	position.mX -= (iSize.iWidth >> 1);
	position.mY -= (iSize.iHeight >> 1);
	
	if (iSettings->iGraphicsDetail == 1)
		{
		ReturnCode ret = iOriginalBitmap->Lock();
		if (ret == OK)
			{
			aContext.BitBlit(position, *iOriginalBitmap);
			iOriginalBitmap->Unlock();
			}
		}
	else
		{
		IBackBuffer& backbuffer = App().BackBuffer();
	
		// NOTE!: assuming GRAPHICS_FORMAT_XRGB8888 backbuffer and graphics
		// NOTE!: assuming GRAPHICS_FORMAT_GRAY256 alpha mask
		if (	backbuffer.GetColorFormat() != GRAPHICS_FORMAT_XRGB8888 ||
				iRotateBuffer[0]->GetColorFormat() != GRAPHICS_FORMAT_XRGB8888 ||
				iMask->GetColorFormat() != GRAPHICS_FORMAT_GRAY256)
			{
			// invalid format
			return;
			}
		
		// back buffer is already locked during the main loop
		//backbuffer->Lock();
		iRotateBuffer[0]->Lock();
		iMask->Lock();
	
		TUint8* dest = (TUint8*)backbuffer.GetAddress();
		TUint8* src = (TUint8*)iRotateBuffer[0]->GetAddress();
		TUint8* srcmask = (TUint8*)iMask->GetAddress();
		
		// move destination pointer to right location
		dest += position.mY * backbuffer.GetStride() + (position.mX << 2);
	
		TInt i, j, k, l;
		TInt alpha;
		TUint32 tmp;
		for (j=0; j<iSize.iHeight; j++)
			{
			k = 0;
			l = 0;
			for (i=0; i<iSize.iWidth; i++)
				{
				// read alpha value
				alpha = 255 - srcmask[i];
				
				if (alpha != 255)
					{
					// subtract alpha value from color components
					// clamping is necessary to avoid overflows
					tmp = src[l++] - alpha;
					if (tmp > 255) tmp = 0;
					dest[k++] = tmp;
					
					tmp = src[l++] - alpha;
					if (tmp > 255) tmp = 0;
					dest[k++] = tmp;
		
					tmp = src[l++] - alpha;
					if (tmp > 255) tmp = 0;
					dest[k++] = tmp;
					
					// skip the extra byte of the 32bit color value
					k++;
					l++;
					}
				else
					{
					// skip the pixel
					k += 4;
					l += 4;
					}
				}
			
			dest += backbuffer.GetStride();
			src += iRotateBuffer[0]->GetStride();
			srcmask += iMask->GetStride();
			}
		
		iMask->Unlock();
		iRotateBuffer[0]->Unlock();
		//backbuffer->Unlock();
		}
	}


void CPlayer::SetMaxSpeed(TReal64 aMaxSpeed)
	{
	iMaxSpeed = aMaxSpeed;
	}


void CPlayer::SetScrollSpeed(TReal64 aScrollSpeed)
	{
	iScrollSpeed = aScrollSpeed;
	}

TReal64 CPlayer::MovementSpeed() const
	{
	return iMovementSpeed;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -