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

📄 applicationbase.cpp

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


#include "ApplicationBase.h"


const TUint32 KDisplayIndex = 0;


CApplicationBase::CApplicationBase()
	{
	iWs.Connect();
	CRuntime::Initialize();
	
	TTime time;
	time.UniversalTime();
	iRandomSeed = time.Int64();
	
	iExitValue = KErrNone;
	iUpdateScreen = ETrue;
	}
	
	
CApplicationBase::~CApplicationBase()
	{
	if (iExitMessage)
		{
		delete iExitMessage;
		iExitMessage = NULL;
		}
	
	CRuntime::Close();
	iWs.Close();
	}
	
	
TInt CApplicationBase::Init(TUint32 aColorFormat)
	{
	// create Symbian window server screen device
	// this allows to use Symbian graphics, create fonts
	// aso.
	iWsScreen = new (ELeave) CWsScreenDevice(iWs);
	iWsScreen->Construct();

	ReturnCode ret;

	ret = CRuntime::CreateInstance( iIdle );
	if (ret != OK)
		{
		return RGAErrorToSymbianError(ret);
		}
	
	ret = InitDisplay();
	if (ret != OK)
		{
		Release();
		return RGAErrorToSymbianError(ret);
		}
	
	ret = InitGraphics(aColorFormat);
	if (ret != OK)
		{
		Release();
		return RGAErrorToSymbianError(ret);
		}
	
	ret = InitInput();
	if (ret != OK)
		{
		Release();
		return RGAErrorToSymbianError(ret);
		}
	
	
	ret = CRuntime::CreateInstance( iApplicationState );
	if (ret != OK)
		{
		Release();
		return RGAErrorToSymbianError(ret);
		}
	iApplicationState->SetObserver(this);

	// device status observers, alarms, messages, phone-calls
	ret = InitDeviceStatusObservers();
	if (ret != OK)
		{
		Release();
		return RGAErrorToSymbianError(ret);
		}

	
    // get the display's orientation
    GraphicsOrientationType type;
    GraphicsOrientationAngle angle;
    iDisplay->GetOrientation( type, angle );
    iBackBuffer->Transpose( type, angle );
    iKeyboard->SetOrientation(angle);

	// create the FPS timer
	ret = CRuntime::CreateInstance( iFPSTimer );
	if (ret != OK)
		{
		Release();
		return RGAErrorToSymbianError(ret);
		}
	
	iFPSTimer->SetObserver(this);
	iFPSTimer->SetPeriod(1000000);
    
	
	// init frametimer
	iFrameTimer = new CTickTimer;
	if (iFrameTimer == NULL)
		{
		return KErrNoMemory;
		}
	iFrameTimer->BeginTimer();
	
	
	
	TInt initret = OnInitialize();
	if (initret != KErrNone)
		{
		Release();
		return initret;
		}
	
	// start the FPS timer
	iFPSTimer->Start();

	return KErrNone;
	}


ReturnCode CApplicationBase::InitDisplay()
	{
	ReturnCode ret = OK;
	IDisplayManager* dispman = NULL;

	ret = CRuntime::CreateInstance( dispman );
	if (ret != OK)
		{
		return ret;
		}
	
	ret = dispman->CreateDisplay( KDisplayIndex, iDisplay );
	dispman->Release();
	if (ret != OK)
		{
		return ret;
		}
	
	iDisplay->SetObserver(this);
	
	const uint32 CONFIG_INDEX = 0;          // index of the configuration
	IDisplayConfiguration* dispconf = NULL;
	ret = iDisplay->GetDisplayConfiguration( CONFIG_INDEX, dispconf );
	if (ret != OK)
		{
		return ret;
		}
	
	ret = iDisplay->CreateWindow( *dispconf, iWindow );
	dispconf->Release();
	return ret;
	}


ReturnCode CApplicationBase::InitGraphics(TUint32 aColorFormat)
	{
	ReturnCode ret = OK;
	IBackBufferFactory* bbfactory = NULL;
	ret = CRuntime::CreateInstance( bbfactory );
	if (ret != OK)
		{
		return ret;
		}
	
	// back buffer configuration to use
	const uint32 BACKBUFFER_CONFIG = 0;
	// possible back buffer resolutions
	const uint32 BACKBUFFER_RESOLUTION_MASK = SolveResolution();
	// usable back buffer color depth
	const uint32 BACKBUFFER_COLOR_MASK = aColorFormat;
	CBackBufferResolution BBResolution;
	uint32 ColorDepthMask;
	// retrieve the back buffer configuration matching the masks
	bbfactory->GetBackBufferConfiguration( BACKBUFFER_CONFIG,
                                           BACKBUFFER_RESOLUTION_MASK,
                                           BACKBUFFER_COLOR_MASK,
                                           BBResolution,
                                           ColorDepthMask );
	
	// create the backbuffer object
	ret = bbfactory->CreateBackBuffer( iWindow,
	                                   BBResolution,
	                                   ColorDepthMask,
	                                   BACKBUFFER_FLAG_ANTI_TEARING_DISABLED |
	                                   BACKBUFFER_FLAG_CONSISTENT,
	                                   iBackBuffer );
	bbfactory->Release();
	if (ret != OK)
		{
		return ret;
		}
	
    ret = CRuntime::CreateInstance( iBackBufferContext );
	if (ret != OK)
		{
		return ret;
		}
	
    // create image converter
    ret = CRuntime::CreateInstance( iConverter );
    if (ret != OK)
    	{
    	return ret;
    	}
	

	iBackBufferContext->SetGraphicsDevice( *iBackBuffer );
	return OK;
	}


ReturnCode CApplicationBase::InitInput()
	{
	ReturnCode ret = OK;
    IInput* input = NULL;

    ret = CRuntime::CreateInstance( input );
    if (ret != OK)
    	{
    	return ret;
    	}

    int32 i;
    int32 number = input->GetDeviceCount();
    for( i = 0; i < number && !iKeyboard; i++ )
    {
        // GetDeviceInfo produce a panic within low memory
        // conditions: ESTLIB-INIT
        CInputDeviceInfo deviceInfo;
        ret = input->GetDeviceInfo( i, deviceInfo );
        if (ret != OK)
        {
            input->Release();
            return ret;
        }
        if( ( deviceInfo.mDeviceType == INPUT_DEVICE_TYPE_KEYPAD ) &&
            ( deviceInfo.mInterfaceType == INPUT_INTERFACE_TYPE_INTERNAL ) )
        {
        	ret = input->CreateDevice( i, iKeyboard );
            if (ret != OK)
            {
                input->Release();
                return ret;
            }
        }
    }

    input->Release();

    if( !iKeyboard )
    {
        // No internal keypad device found
        return ERROR_NOT_FOUND;
    }

    ret = iKeyboard->Start();
    return ret;
	}


ReturnCode CApplicationBase::InitDeviceStatusObservers()
	{
	ReturnCode ret = CRuntime::CreateInstance( iAlarmStatus );
	if ( ret == OK )
		{
		iAlarmStatus->SetObserver(this);
		}
	
	ret = CRuntime::CreateInstance( iTelephonyStatus );
	if ( ret == OK )
		{
		iTelephonyStatus->SetObserver(this);
		}
	
	
	return OK;
	}


void CApplicationBase::Release()
	{
	if (iFPSTimer)
		{
		iFPSTimer->Stop();
		iFPSTimer->SetObserver(NULL);
		iFPSTimer->Release();
		iFPSTimer = NULL;
		}
	if (iApplicationState)
		{
		iApplicationState->SetObserver(NULL);
		iApplicationState->Release();
		iApplicationState = NULL;
		}
	if (iKeyboard)
		{
		iKeyboard->SetObserver(NULL);
		iKeyboard->Release();
		iKeyboard = NULL;
		}
	if (iFrameTimer)
		{
		delete iFrameTimer;
		iFrameTimer = NULL;
		}
	if (iConverter)
		{
		iConverter->Release();
		iConverter = NULL;
		}
	if (iBackBufferContext)
		{
		iBackBufferContext->Release();
		iBackBufferContext = NULL;
		}
	if (iBackBuffer)
		{
		iBackBuffer->Release();
		iBackBuffer =  NULL;
		}
	if (iWindow)
		{
		iWindow->Release();
		iWindow = NULL;
		}
	if (iDisplay)
		{
		iDisplay->SetObserver(NULL);
		iDisplay->Release();
		iDisplay = NULL;
		}
	if (iIdle)
		{
		iIdle->Release();
		iIdle = NULL;
		}
	
	if (iAlarmStatus)
		{
		iAlarmStatus->Release();
		iAlarmStatus = NULL;
		}
	if (iTelephonyStatus)
		{
		iTelephonyStatus->Release();
		iTelephonyStatus = NULL;
		}
	
	delete iWsScreen;
	}


uint32 CApplicationBase::SolveResolution()
	{
	const TSize screensize = iWsScreen->SizeInPixels();
	
	if (screensize.iWidth == 240 || screensize.iHeight == 240)
		{
		return BACKBUFFER_RESOLUTION_240x320 | BACKBUFFER_RESOLUTION_320x240;
		}
	else if (screensize.iWidth == 176 || screensize.iHeight == 176)
		{
		return BACKBUFFER_RESOLUTION_176x208 | BACKBUFFER_RESOLUTION_208x176;
		}
	else if (screensize.iWidth == 352 || screensize.iHeight == 352)
		{
		return BACKBUFFER_RESOLUTION_352x416 | BACKBUFFER_RESOLUTION_416x352;
		}
	else if (	(screensize.iWidth == 320 && screensize.iHeight == 480) ||
				(screensize.iWidth == 480 && screensize.iHeight == 320))
		{		
		return BACKBUFFER_RESOLUTION_320x480 | BACKBUFFER_RESOLUTION_480x320;
		}
	else if (	(screensize.iWidth == 480 && screensize.iHeight == 640) ||
				(screensize.iWidth == 640 && screensize.iHeight == 480))
		{		
		return BACKBUFFER_RESOLUTION_480x640 | BACKBUFFER_RESOLUTION_640x480;
		}
/*    
const uint32 BACKBUFFER_RESOLUTION_160x240 = 0x0400; ///< HHVGA Portrait
const uint32 BACKBUFFER_RESOLUTION_240x160 = 0x0800; ///< HHVGA Landscape
const uint32 BACKBUFFER_RESOLUTION_480x848 = 0x1000; ///< WVGA Portrait
const uint32 BACKBUFFER_RESOLUTION_848x480 = 0x2000; ///< WVGA Landscape
const uint32 BACKBUFFER_RESOLUTION_120x160 = 0x4000; ///< QQVGA Portrait
const uint32 BACKBUFFER_RESOLUTION_160x120 = 0x8000; ///< QQVGA Landscape
const uint32 BACKBUFFER_RESOLUTION_360x640 = 0x10000; ///< QHD Portrait
const uint32 BACKBUFFER_RESOLUTION_640x360 = 0x20000; ///< QHD Landscape
const uint32 BACKBUFFER_RESOLUTION_352x800 = 0x40000; ///< 352x800 Portrait
const uint32 BACKBUFFER_RESOLUTION_800x352 = 0x80000; ///< 800x352 Landscape
	*/
	else
		{
		// no suitable resolution found
		return BACKBUFFER_RESOLUTION_NONE;
		}
	}


CTickTimer& CApplicationBase::FrameTimer() const
	{
	return *iFrameTimer;
	}



TInt CApplicationBase::Run()
	{
    // main loop
	iRunning = ETrue;
    iKeyboard->SetObserver( this );
	
    while( iRunning )
    	{
        if( iFocused )
        	{
        	if (iUpdateScreen)
        		{
	            // copy the content of the graphics context to the ngi backbuffer
	            if( iBackBuffer)
	            	{
	            	if ( iBackBuffer->Lock() == OK )
	        	    	{
	        	    	iFrameTimer->EndTimer();
	        	    	iFrameTimer->BeginTimer();
	        	    	
	        	    	OnDraw(*iBackBufferContext);
	        	    	
	        	        iBackBuffer->Unlock();
	        	        iBackBuffer->Swap( TRUE );
	        	    	}
	            	}
	            // use non-blocking mode while in foreground
	            iIdle->Process( 0, FALSE );
	            iFrameCounter++;
        		}
        	else
            	{
            	// use blocking mode while not updating screen
                iIdle->Process( 0, TRUE );
            	}
        	}
        else
        	{
        	// use blocking mode while in background
            iIdle->Process( 0, TRUE );
        	}
    	}
    
    OnClose();
    Release();
    return iExitValue;
	}


void CApplicationBase::Close(TInt aError, const TDesC& aDescription)
	{
	iKeyboard->SetObserver(NULL);
	iRunning = EFalse;
	
	iExitValue = aError;
	
	if (iExitMessage)
		{
		delete iExitMessage;
		iExitMessage = NULL;
		}
	
	const TInt textlength = aDescription.Length();
	if (textlength)
		{
		iExitMessage = HBufC::New(textlength);
		if (iExitMessage)
			{
			*iExitMessage = aDescription;
			}
		}
	}


HBufC* CApplicationBase::ExitMessage()
	{
	return iExitMessage;
	}



TBool CApplicationBase::IsFocused() const
	{
	return iFocused;
	}

void CApplicationBase::FocusGained() NO_THROW
	{
	iKeyboardMask = 0;
	iFrameTimer->BeginTimer();
	iFocused = ETrue;
	}

void CApplicationBase::FocusLost() NO_THROW
	{
	iKeyboardMask = 0;
	iFocused = EFalse;
	}

void CApplicationBase::ExitRequested() NO_THROW
	{
	Close(KErrNone);
	}

void CApplicationBase::DisplayOrientationChanged(
        uint32 aDisplayIndex,
        GraphicsOrientationType aType,
        GraphicsOrientationAngle aAngle ) NO_THROW
	{
    if ( aDisplayIndex == KDisplayIndex )
        {
    	iKeyboardMask = 0;
        iBackBuffer->Transpose( aType, aAngle );

⌨️ 快捷键说明

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