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

📄 exampleapplication.cpp

📁 RGA: Biowaste Game Example This C++ application demonstrates how to create a 2D mobile game for S60
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        	iAudioSound[i]->Release();
        	iAudioSound[i] = NULL;
        	}
		}
	
	
	// release audio mixer
	if (iAudioMixer)
		{
		iAudioMixer->Stop();
		iAudioMixer->Release();
		iAudioMixer = NULL;
		}
	
	ReleaseBitmapAndContext(iInfoAreaBuffer, iInfoAreaBufferContext);
	
	
	// release graphics
	for (i=0; i<iBitmaps.Count(); i++)
		{
		IBitmap* bitmap = iBitmaps[i];
		if (bitmap)
			{
			bitmap->Release();
			}
		}
	iBitmaps.Reset();
	iBitmaps.Close();

	for (i=0; i<iMasks.Count(); i++)
		{
		IBitmap* mask = iMasks[i];
		if (mask)
			{
			mask->Release();
			}
		}
	iMasks.Reset();
	iMasks.Close();
	
	// release fonts
	iWsScreen->ReleaseFont(iSmallFont);
	iWsScreen->ReleaseFont(iBigFont);
	iWsScreen->ReleaseFont(iNormalFont);
	}


void CExampleApplication::OnDraw(IGraphicsContext& aContext)
	{
	const TReal64 frametime = FrameTimer().ElapsedSeconds();
	
	if (iInfoAreaPriorityMsgTimer > 0.0)
		{
		iInfoAreaPriorityMsgTimer -= frametime;
		if (iInfoAreaPriorityMsgTimer <= 0.0)
			{
			SetInfoAreaText(_L(""), ETrue);
			}
		}
	
	if (iState)
		{
		EAppUpdateState state = iState->Update(frametime);
		if (state != EAppUpdateStateContinue)
			{
			TInt error = ChangeAppStateL(state);
			if (error != KErrNone)
				{
				Close(error);
				return;
				}
			}
		else
			{
			iState->Draw(aContext);
			DrawInfoArea(aContext);
			}
		}
	}


void CExampleApplication::DrawInfoArea(IGraphicsContext& aContext)
	{
	TRect area(0, BackBufferSize().iHeight - 32, BackBufferSize().iWidth, BackBufferSize().iHeight);
	BlendRect(area);
	
	// draw text buffer
	TBool draw = ETrue;
	ReturnCode ret;
	
	if (	iInfoAreaPriorityMsgTimer > 0.0 &&
			((TInt)iInfoAreaPriorityMsgTimer & 1) )
		{
		draw = EFalse;
		}
	
	if (draw)
		{
		CPoint pos;
		pos.mX = area.iTl.iX;
		pos.mY = area.iTl.iY;
		
		ret = iInfoAreaBuffer->Lock();
		if ( ret == OK )
			{
			ret = aContext.BitBlitTransparent(	pos,
												*iInfoAreaBuffer,
												0);
			
			ret = iInfoAreaBuffer->Unlock();
			}
		}
	}


void CExampleApplication::SetInfoAreaText(const TDesC& aText, TBool aPriorityMsg)
	{
	if (!aPriorityMsg && iInfoAreaPriorityMsgTimer > 0.0)
		{
		return;
		}
	
	DrawTextToBitmap(	iInfoAreaBuffer,
						iInfoAreaBufferContext,
						aText,
						SmallFont(),
						KRgbWhite);
	
	if (aText.Length() && aPriorityMsg)
		{
		iInfoAreaPriorityMsgTimer = 10.0;
		}
	}


TInt CExampleApplication::DrawTextToBitmap(	IBitmap* aBitmap,
											IGraphicsContext* aContext,
											const TDesC& aText,
											const CFont* aFont,
											TRgb aColor)
	{
	if (!aBitmap)
		{
		return KErrNotReady;
		}
	
	ReturnCode ret;
	ret = aBitmap->Lock();
	
	if ( ret == OK )
		{
		aContext->Clear(0);
		
		if (aText.Length())
			{
			CGraphicsContext* context = SystemGc(*aBitmap);
			
			context->UseFont(aFont);
			context->SetPenColor(aColor);
			
			CSize buffersize = aBitmap->GetSize();
			context->DrawText(	aText,
						TRect(0, 0, buffersize.mX, buffersize.mY),
						buffersize.mY / 2,
						CGraphicsContext::ECenter);
			
			context->DiscardFont();
			ApplySystemGc();
			}
		
		ret = aBitmap->Unlock();
		}
	
	return KErrNone;
	}


void CExampleApplication::BlendRect(const TRect& aRect)
	{
	IBackBuffer& backbuffer = BackBuffer();

	// NOTE!: assuming GRAPHICS_FORMAT_XRGB8888 backbuffer and graphics
	if (	backbuffer.GetColorFormat() != GRAPHICS_FORMAT_XRGB8888)
		{
		// invalid format
		return;
		}
	
	// back buffer is already locked during the main loop
	TUint32* bbpointer = (TUint32*)backbuffer.GetAddress();
	TUint32 stride = backbuffer.GetStride() >> 2;
	
	// move destination pointer to right location
	bbpointer += aRect.iTl.iY * stride + aRect.iTl.iX;

	const TInt areawidth = aRect.Width();
	const TInt areaheight = aRect.Height();
	
	TInt x, y;
	for (y=0; y<areaheight; y++)
		{
		for (x=0; x<areawidth; x++)
			{
			// use 50% alpha
			bbpointer[x] &= 0xFEFEFEFE;
			bbpointer[x] >>= 1;
			}
		
		bbpointer += stride;
		}
	}



TInt CExampleApplication::ChangeAppStateL(EAppUpdateState aState)
	{
	if (iState)
		{
		delete iState;
		iState = NULL;
		}
	
	SetInfoAreaText(_L(""), ETrue);
	
	TInt ret = KErrNone;
	switch (aState)
		{
		case EAppUpdateStateExit:
			Close(KErrNone);
			break;

		case EAppUpdateStateInitIntro:
			iState = new (ELeave) CAppStateIntro(*this);
			ret = iState->Init();
			break;
			
			
		case EAppUpdateStateInitMenu:
		case EAppUpdateStateInitMenuHighScores:
			iState = new (ELeave) CAppStateMenu(*this);
			ret = iState->Init();
			if (aState == EAppUpdateStateInitMenuHighScores)
				{
				CAppStateMenu* menu = (CAppStateMenu*)iState;
				menu->OpenHighScoresTable(iHighScores->LatestAddedItemIndex());
				}
			break;
			
			
		case EAppUpdateStateInitGame:
			iState = new (ELeave) CAppStateGame(*this);
			ret = iState->Init();
			break;
			
			
		case EAppUpdateStateInitNewHighScore:
			iState = new (ELeave) CAppStateNewHighScore(*this);
			ret = iState->Init();
			break;
			
		case EAppUpdateStateContinue:
			break;
		}
	
	return ret;
	}


void CExampleApplication::FocusGained() NO_THROW
	{
	CApplicationBase::FocusGained();
	
	// check if audio is active
	if (iRepository)
		{
		TInt value = 1;
		
		// in documentation KProEngActiveWarningTones is described
		// as 'Warning and game tones'
		iRepository->Get( KProEngActiveWarningTones, value );
		if (value)
			{
			iAudioEnabled = ETrue;
			}
		}
	}


void CExampleApplication::InputKeyPressed(	IInputDevice& aDevice,
											uint64 aTimeStamp,
											uint32 aKeyCode ) NO_THROW
	{
	CApplicationBase::InputKeyPressed(aDevice, aTimeStamp, aKeyCode);
	
	if (iState)
		{
		iState->KeyDown(aKeyCode);
		}
	}


void CExampleApplication::DisplayOrientationChanged(
        uint32 aDisplayIndex,
        GraphicsOrientationType aType,
        GraphicsOrientationAngle aAngle ) NO_THROW
    {
    
    CApplicationBase::DisplayOrientationChanged(	aDisplayIndex,
    												aType,
    												aAngle);
    if (iState)
    	{
    	// pass message to application states
    	iState->DisplayOrientationChanged();
    	}
    }


void CExampleApplication::AlarmOccurence( AlarmType aType ) NO_THROW
	{
	if (aType == ALARM_TYPE_CALENDER)
		{
		SetInfoAreaText(KTextAlarmCalendar, ETrue);
		}
	else if (aType == ALARM_TYPE_CLOCK)
		{
		SetInfoAreaText(KTextAlarmClock, ETrue);
		}
	
	if (iState)
		{
		iState->Pause();
		}
	}


void CExampleApplication::IncomingCall() NO_THROW
	{
	SetInfoAreaText(KTextNotificationCall, ETrue);
	if (iState)
		{
		iState->Pause();
		}
	}


void CExampleApplication::IncomingMessage( MessageType aMessage ) NO_THROW
	{
	if (aMessage == MESSAGE_TYPE_SMS)
		{
		SetInfoAreaText(KTextNotificationSMS, ETrue);
		}
	else if (aMessage == MESSAGE_TYPE_MMS)
		{
		SetInfoAreaText(KTextNotificationMMS, ETrue);
		}
	else if (aMessage == MESSAGE_TYPE_EMAIL)
		{
		SetInfoAreaText(KTextNotificationEmail, ETrue);
		}
	
	if (iState)
		{
		iState->Pause();
		}
	}



IBitmap* CExampleApplication::Bitmap(TUint aBitmapId)
	{
	if (aBitmapId < (TUint)iBitmaps.Count())
		{
		return iBitmaps[aBitmapId];
		}
	else
		{
		return NULL;
		}
	}


IBitmap* CExampleApplication::Mask(TUint aBitmapId)
	{
	if (aBitmapId < (TUint)iMasks.Count())
		{
		return iMasks[aBitmapId];
		}
	else
		{
		return NULL;
		}
	}


const CFont* CExampleApplication::NormalFont() const
	{
	return iNormalFont;
	}


const CFont* CExampleApplication::BigFont() const
	{
	return iBigFont;
	}

const CFont* CExampleApplication::SmallFont() const
	{
	return iSmallFont;
	}

CHighScores* CExampleApplication::HighScores()
	{
	return iHighScores;
	}

CSettings* CExampleApplication::Settings()
	{
	return iSettings;
	}


void CExampleApplication::SetLastGameScore(const TReal64 aScore)
	{
	iLastGameScore = aScore;
	}

TReal64 CExampleApplication::LastGameScore()
	{
	return iLastGameScore;
	}



⌨️ 快捷键说明

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