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

📄 appstateintro.cpp

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

#include "AppStateIntro.h"
#include "ExampleApplication.h"

#include <bautils.h>


// intro state constants
const TInt KIntroStateLoading = 0;
const TInt KIntroStateForumPresents = 1;
const TInt KIntroStateGameBy = 2;
const TInt KIntroStateRunVideo = 3;
const TInt KIntroStateGameTitle = 4;


CAppStateIntro::CAppStateIntro(CExampleApplication& aApp)
	: CAppState(aApp),
	  iNextState(EAppUpdateStateContinue)
	{

	}


CAppStateIntro::~CAppStateIntro()
	{
	ReleaseVideoPlayer();
	
	// release temporary buffers
	if (iAlphaBufferContext)
		{
		iAlphaBufferContext->Release();
		iAlphaBufferContext = NULL;
		}
	if (iAlphaBuffer)
		{
		iAlphaBuffer->Release();
		iAlphaBuffer = NULL;
		}
	if (iTextBufferContext)
		{
		iTextBufferContext->Release();
		iTextBufferContext = NULL;
		}
	if (iTextBuffer)
		{
		iTextBuffer->Release();
		iTextBuffer = NULL;
		}
	}


void CAppStateIntro::ReleaseVideoPlayer()
	{
	// release video player
	if (iVideoPlayer)
		{
		iVideoPlayer->SetWindow(NULL);
		iVideoPlayer->SetObserver(NULL);
		iVideoPlayer->Release();
		iVideoPlayer = NULL;
		}
	}


TInt CAppStateIntro::Init()
	{
	// create temporary buffers for text display and
	// alpha blending
	TSize bbsize = App().BackBufferSize();
	TSize fadebuffersize(bbsize.iWidth, bbsize.iHeight);
	fadebuffersize.iHeight /= 4;
	
	TInt error = App().CreateBitmapAndContext(	fadebuffersize,
			iTextBuffer,
			iTextBufferContext);
	if (error != KErrNone)
		{
		return error;
		}
	
	
	
	ReturnCode ret = CRuntime::CreateInstance( iAlphaBuffer );
	if (ret != OK)
		{
		return App().RGAErrorToSymbianError(ret);
		}
	
	ret = iAlphaBuffer->Reconfigure(	CSize(fadebuffersize.iWidth, fadebuffersize.iHeight),
										GRAPHICS_FORMAT_GRAY256);
	if (ret != OK)
		{
		return App().RGAErrorToSymbianError(ret);
		}
	
	ret = CRuntime::CreateInstance( iAlphaBufferContext );
	if (ret != OK)
		{
		return App().RGAErrorToSymbianError(ret);
		}
	iAlphaBufferContext->SetGraphicsDevice(*iAlphaBuffer);

	// create videoplayer
	InitVideoplayer();
	
	UpdateIntroState();
	
	return KErrNone;
	}


TInt CAppStateIntro::InitVideoplayer()
	{
	// check if video file exists
	#ifdef __WINS__
	App().BuildFilepath(KPathIntroVideoWins, iVideoFilename);
	#else
	App().BuildFilepath(KPathIntroVideo, iVideoFilename);
	#endif

	RFs fs;
	fs.Connect();
	TBool fileexists = BaflUtils::FileExists(fs, iVideoFilename);
	fs.Close();
	if (!fileexists)
		{
		// video file does not exist
		return KErrNotFound;
		}
	iVideoFilename.ZeroTerminate();
	
	// create videoplayer object
	ReturnCode ret = CRuntime::CreateInstance( iVideoPlayer );
	if (ret != OK)
		{
		// failed to create video player.
		iVideoPlayer = NULL;
		return App().RGAErrorToSymbianError(ret);
		}
	else
		{
    	iVideoPlayer->SetObserver(this);
    	iVideoPlayer->SetWindow( App().Window() );

    	CSize playbacksize;
		playbacksize.mX = App().BackBufferSize().iWidth;
		playbacksize.mY = App().BackBufferSize().iHeight;
		iVideoPlayer->SetPlaybackAreaSize(playbacksize);
		iVideoPlayer->SetPlaybackAreaPosition(CPoint(0, 0));
    	}
 
	return KErrNone;
	}


EAppUpdateState CAppStateIntro::Update(const TReal64& aFrametime)
	{
	// update fade timer if video is not running
	if (iIntroState != KIntroStateRunVideo)
		{
		if (iIntroState == KIntroStateGameTitle)
			{
			// update intro a little bit slower
			// on last State
			iIntroTimer += aFrametime * 0.5;
			}
		else if (iIntroState == KIntroStateLoading)
			{
			// update intro faster with loading state
			iIntroTimer += aFrametime * 6.0;
			}
		else
			{
			iIntroTimer += aFrametime;
			}
		}
	
	// after 3 seconds, move to next intro State
	if (iIntroTimer > 3.0)
		{
		if (iIntroState == KIntroStateLoading)
			{
			// load application resources
			TInt error = App().InitResources();
			if (error != KErrNone)
				{
				return EAppUpdateStateExit;
				}
			}
		
		iIntroState++;
		iIntroTimer = 0.0;
		
		if (iIntroState > KIntroStateGameTitle)
			{
			// last state complete, exit the intro
			iNextState = EAppUpdateStateInitMenu;
			}
		else
			{
			UpdateIntroState();
			}
		}
	
	// keep backlight on
	User::ResetInactivityTime();
	
	return iNextState;
	}


void CAppStateIntro::Draw(IGraphicsContext& aContext)
	{
	const TInt ypos = (App().BackBufferSize().iHeight >> 1) - (iTextBuffer->GetSize().mY >> 1);
	ReturnCode ret;

	if (iIntroState == KIntroStateRunVideo)
		{
		// video is running
		}
	else if (iIntroState == KIntroStateLoading)
		{
		// draw loading text to screen
		aContext.Clear(0);
		ret = iTextBuffer->Lock();
		if (ret == OK)
			{
			ret = aContext.BitBlit(	CPoint(0, ypos),
								*iTextBuffer);
	
			ret = iTextBuffer->Unlock();
			}
		}
	else
		{
		aContext.Clear(0);
		
		// update the fade buffer alpha channel
		TUint8 alpha = 255;
		if (iIntroTimer < 1.0)
			{
			// fading in
			alpha = (TUint8)(iIntroTimer * 255.0);
			}
		else if (iIntroTimer > 2.0)
			{
			// fading out
			alpha = (TUint8)((3.0 - iIntroTimer) * 255.0);
			}
		
		ret = iAlphaBuffer->Lock();
		
		if ( ret == OK )
			{
			iAlphaBufferContext->Clear(alpha);
			
			ret = iTextBuffer->Lock();
			ret = aContext.BitBlitAlphaMask(	CPoint(0, ypos),
												*iTextBuffer,
												*iAlphaBuffer);
		
			ret = iTextBuffer->Unlock();
			ret = iAlphaBuffer->Unlock();
			}
		}
	}


void CAppStateIntro::UpdateIntroState()
	{
	switch (iIntroState)
		{
		case KIntroStateLoading:
			App().DrawTextToBitmap(	iTextBuffer,
					iTextBufferContext,
					KTextIntroLoading,
					App().NormalFont(),
					KRgbWhite);
			break;
			
		case KIntroStateForumPresents:
			App().DrawTextToBitmap(	iTextBuffer,
									iTextBufferContext,
									KTextIntroPresents,
									App().NormalFont(),
									KRgbWhite);
			break;
		
		case KIntroStateGameBy:
			App().DrawTextToBitmap(	iTextBuffer,
									iTextBufferContext,
									KTextIntroGameBy,
									App().NormalFont(),
									KRgbWhite);
			break;
		
		case KIntroStateRunVideo:
			if (iVideoPlayer)
				{
				ReturnCode ret;
				ret = iVideoPlayer->OpenVideoFile(
						(const char16*)iVideoFilename.Ptr() );
				if (ret != OK)
					{
					// failed to open video file
					// skip the video state
					ReleaseVideoPlayer();
					iIntroTimer = 99.0;
					}
				}
			else
				{
				// skip the video state
				iIntroTimer = 99.0;
				}
			break;
		
		case KIntroStateGameTitle:
			App().DrawTextToBitmap(	iTextBuffer,
									iTextBufferContext,
									KTextIntroGameTitle,
									App().BigFont(),
									KRgbGreen);
			break;
		}
	}



void CAppStateIntro::KeyDown(TUint32 /*aKeyCode*/)
	{
	if (iIntroState != KIntroStateLoading)
		{
		// exit intro with any keypress
		PlayCompleted( OK );
		iNextState = EAppUpdateStateInitMenu;
		}
	}



// functions from IVideoPlaybackObserver

void CAppStateIntro::OpenCompleted( ReturnCode aError ) NO_THROW
	{
	if (aError != OK)
		{
		// failed to open video file, skip the video state
		PlayCompleted(OK);
		}
	else
		{
		// start playback
		App().EnableScreenUpdate(EFalse);
		iVideoPlayer->Play();
		}
	}

void CAppStateIntro::PlayCompleted( ReturnCode /*aError*/ ) NO_THROW
	{
	if (iVideoPlayer)
		{
		// move to next state
		iIntroTimer = 99.0;
		iVideoPlayer->CloseVideoFile();
		iVideoPlayer->Release();
		iVideoPlayer = NULL;
		}
	App().EnableScreenUpdate(ETrue);
	}

⌨️ 快捷键说明

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