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

📄 applicationbase.cpp

📁 RGA: Biowaste Game Example This C++ application demonstrates how to create a 2D mobile game for S60
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        iKeyboard->SetOrientation( aAngle );
        }
	}

void CApplicationBase::RedrawWindow( const IWindow& /*aWindow*/ ) NO_THROW
	{

	}


void CApplicationBase::InputKeyPressed( IInputDevice& /*aDevice*/,
                                        uint64 /*aTimeStamp*/,
                                        uint32 aKeyCode ) NO_THROW
	{
	iKeyboardMask |= aKeyCode;
	}


void CApplicationBase::InputKeyReleased( IInputDevice& /*aDevice*/,
                                         uint64 /*aTimeStamp*/,
                                         uint32 aKeyCode ) NO_THROW
	{
	iKeyboardMask &= (~aKeyCode);
	}

// ------------------------------------------------------------------------
// InputAxisMoved()
// Pointing device moved.
// ------------------------------------------------------------------------
//
void CApplicationBase::InputAxisMoved( IInputDevice& /*aDevice*/,
                                       uint64 /*aTimeStamp*/,
                                       uint32 /*aAxisNumber*/,
                                       int32 /*aNewAxisValue*/ ) NO_THROW
	{
	}

// ------------------------------------------------------------------------
// DeviceDisconnected()
// Device has been disconnected.
// ------------------------------------------------------------------------
//
void CApplicationBase::DeviceDisconnected( IInputDevice& ) NO_THROW
{
}

// ------------------------------------------------------------------------
// DeviceConnected()
// Device has been connected.
// ------------------------------------------------------------------------
//
void CApplicationBase::DeviceConnected( IInputDevice& ) NO_THROW
{
}

void CApplicationBase::EnableScreenUpdate(const TBool aEnable)
	{
	iUpdateScreen = aEnable;
	}


void CApplicationBase::HandleTimer( uint32 aTimerID ) NO_THROW
	{
	if (aTimerID == iFPSTimer->GetTimerID())
		{
		iFPS = iFrameCounter;
		iFrameCounter = 0;
		}
	}

TUint32 CApplicationBase::Fps() const
	{
	return iFPS;
	}


TInt CApplicationBase::LoadImage(IBitmap& aBitmap, const TDesC& aFilename, TBool aBitmapFormat)
	{
	ReturnCode ret = OK;
	
	TFileName fn = aFilename;
	fn.ZeroTerminate();

	if (aBitmapFormat)
		{
		ret = iConverter->Load(aBitmap, (const char16*)fn.Ptr(), TRUE);
		}
	else
		{
		ret = iConverter->Load(aBitmap, (const char16*)fn.Ptr());
		}
	
    return RGAErrorToSymbianError(ret);
	}


TInt CApplicationBase::CreateBitmapAndContext(	const TSize& aSize,
												IBitmap*& aBitmap,
												IGraphicsContext*& aContext)
	{
	// create bitmap
	ReturnCode ret = CRuntime::CreateInstance( aBitmap );
	if (ret != OK)
		{
		return RGAErrorToSymbianError(ret);
		}

	// set size and format
	CSize buffersize(aSize.iWidth, aSize.iHeight);
	ret = aBitmap->Reconfigure(buffersize, iBackBuffer->GetColorFormat());
	if (ret != OK)
		{
		return RGAErrorToSymbianError(ret);
		}
	
	// create context
	ret = CRuntime::CreateInstance( aContext );
	if (ret != OK)
		{
		return RGAErrorToSymbianError(ret);
		}
	// set context to bitmap
	aContext->SetGraphicsDevice(*aBitmap);
	
	return KErrNone;
	}


void CApplicationBase::ReleaseBitmapAndContext(IBitmap*& aBitmap, IGraphicsContext*& aContext)
	{
	if (aContext)
		{
		aContext->Release();
		aContext = NULL;
		}
	if (aBitmap)
		{
		aBitmap->Release();
		aBitmap = NULL;
		}
	}



TBool CApplicationBase::IsKeyDown(TUint32 aKeyCode) const
	{
	if ( (iKeyboardMask & aKeyCode) )
		{
		return ETrue;
		}
	else
		{
		return EFalse;
		}
	}


CGraphicsContext* CApplicationBase::SystemGc(IGraphicsDevice& aDevice)
	{
	if (iSymbianBitmap)
		{
		return NULL;
		}

	CSize srcsize = aDevice.GetSize();
	TDisplayMode mode = RGAFormatToDisplayMode(aDevice.GetColorFormat());
	
	if (srcsize.mX == 0 || srcsize.mY == 0 || mode == ENone)
		{
		return NULL;
		}
	
	
	// create symbian bitmap that points to given bitmap
	iSymbianBitmap = new (ELeave)CFbsBitmap;
	if (iSymbianBitmap->Create(TSize(srcsize.mX, srcsize.mY), mode) != KErrNone)
		{
		delete iSymbianBitmap;
		return NULL;
		}
	
	iRGADevice = &aDevice;
	
	// copy contents of given device to symbian bitmap
	TBool islocked = iRGADevice->IsLocked();
	iSymbianBitmap->LockHeap();
	
	if (!islocked)
		{
		iRGADevice->Lock();
		}
	TUint8* destdata = (TUint8*)iSymbianBitmap->DataAddress();
	TUint8* srcdata = (TUint8*)iRGADevice->GetAddress();
	
	const TInt srcstride = iRGADevice->GetStride();
	const TInt deststride = BitmapStride(*iSymbianBitmap);
	
	TInt i, j;
	for (i=0; i<srcsize.mY; i++)
		{
		for (j=0; j<deststride; j++)
			{
			destdata[j] = srcdata[j];
			}
		
		destdata += deststride;
		srcdata += srcstride;
		}
	
	iSymbianBitmap->UnlockHeap();
	
	if (!islocked)
		{
		iRGADevice->Unlock();
		}
	
	// create graphics context for the new bitmap
	iSymbianBitmapDevice = CFbsBitmapDevice::NewL(iSymbianBitmap);
	iSymbianBitmapDevice->CreateContext(iSymbianGc);
	
	return iSymbianGc;
	}

void CApplicationBase::ApplySystemGc()
	{
	if (!iSymbianBitmap || !iRGADevice)
		{
		return;
		}
	
	// copy contents of symbian bitmap to device
	TBool islocked = iRGADevice->IsLocked();

	iSymbianBitmap->LockHeap();
	
	if (!islocked)
		{
		iRGADevice->Lock();
		}
	TUint8* srcdata = (TUint8*)iSymbianBitmap->DataAddress();
	TUint8* destdata = (TUint8*)iRGADevice->GetAddress();
	
	const CSize srcsize = iRGADevice->GetSize();
	const TInt deststride = iRGADevice->GetStride();
	const TInt srcstride = BitmapStride(*iSymbianBitmap);
	
	TInt i, j;
	for (i=0; i<srcsize.mY; i++)
		{
		for (j=0; j<srcstride; j++)
			{
			destdata[j] = srcdata[j];
			}
		
		destdata += deststride;
		srcdata += srcstride;
		}
	
	iSymbianBitmap->UnlockHeap();
	
	if (!islocked)
		{
		iRGADevice->Unlock();
		}

	delete iSymbianGc;
	iSymbianGc = NULL;
	delete iSymbianBitmapDevice;
	iSymbianBitmapDevice = NULL;
	delete iSymbianBitmap;
	iSymbianBitmap = NULL;
	iRGADevice = NULL;
	}


IBackBuffer& CApplicationBase::BackBuffer() const
	{
	return *iBackBuffer;
	}

TSize CApplicationBase::BackBufferSize() const
	{
	CSize bbsize = iBackBuffer->GetSize();
	return TSize(bbsize.mX, bbsize.mY);
	}


IInputDevice* CApplicationBase::Keyboard()
	{
	return iKeyboard;
	}

IWindow* CApplicationBase::Window()
	{
	return iWindow;
	}




TInt CApplicationBase::RandInt(const TInt aLow, const TInt aHigh)
	{
	return (Math::Rand(iRandomSeed) % ((aHigh + 1) - aLow) + aLow);
	}



TReal CApplicationBase::RandFloat(const TReal aLow, const TReal aHigh)
	{
	return ( (aHigh - aLow) * (float)(Math::Rand(iRandomSeed) / (float)KMaxTInt)) + aLow;
	}


void CApplicationBase::BuildFilepath(const TDesC& aFilename, TDes& aFullPath)
	{
	RFs fs;
	TFileName fn;
	TChar drive;
	
	fs.Connect();
	
	// get drive letter from session path
	fs.SessionPath(fn);
	drive = fn[0];
	
	// emulator builds, use always 'z' drive
	#ifdef __WINS__
	drive = 'z';
	#endif
	
	// get application private folder
	fs.PrivatePath(fn);
	fs.Close();
	
	// add drive letter
	aFullPath.Append(drive);
	aFullPath.Append(':');
	
	// add private path
	aFullPath.Append(fn);
	
	// add filename
	aFullPath.Append(aFilename);
	}


TInt CApplicationBase::RGAErrorToSymbianError(ReturnCode aError)
	{
	switch (aError)
	{
	case OK:
		return KErrNone;
		
    case ERROR_ILLEGAL_ARGUMENT:
    	return KErrArgument;

    case ERROR_BUFFER_TOO_SHORT:
    	return KErrUnderflow;

    case ERROR_OUT_OF_MEMORY:
    	return KErrNoMemory;

    case ERROR_NOT_READY:
		return KErrNotReady;

    case ERROR_NOT_SUPPORTED:
    	return KErrNotSupported;

    case ERROR_NOT_INITIALIZED:
    	return KErrGeneral;

    case ERROR_TIMEOUT:
    	return KErrTimedOut;

    case ERROR_UNEXPECTED:
    	return KErrGeneral;

    case ERROR_NOT_FOUND:
    	return KErrNotFound;

    case ERROR_ALREADY_EXISTS:
    	return KErrAlreadyExists;

    case ERROR_ACCESS_DENIED:
    	return KErrAccessDenied;

    case ERROR_ALREADY_IN_USE:
    	return KErrGeneral;

    case ERROR_NOT_STARTED:
    	return KErrGeneral;

    case ERROR_ALREADY_STARTED:
    	return KErrGeneral;

    case ERROR_PENDING:
    	return KErrGeneral;

    case ERROR_NOT_EMPTY:
    	return KErrGeneral;

    case ERROR_ABORTED:
    	return KErrAbort;

    case ERROR_FILE_READ_ONLY:
    	return KErrGeneral;

    case ERROR_END_OF_FILE:
    	return KErrEof;

    case ERROR_DISK_FULL:
    	return KErrDiskFull;

    case ERROR_PERMISSION_DENIED:
    	return KErrPermissionDenied;

    case ERROR_OBSERVER_NOT_SET:
    	return KErrGeneral;

    case ERROR_NGI_INTERNAL_BASE:
    	return KErrGeneral;

    case ERROR_API_SPECIFIC_BASE:
    	return KErrGeneral;

    case ERROR_USER_BASE:
    	return KErrGeneral;
	}
	
	return KErrUnknown;
	}


TDisplayMode CApplicationBase::RGAFormatToDisplayMode(TUint32 aFormat)
	{
	switch (aFormat)
		{
		case GRAPHICS_FORMAT_MONO:
			return EGray2;
		case GRAPHICS_FORMAT_GRAY4:
			return EGray4;
		case GRAPHICS_FORMAT_GRAY16:
			return EGray16;
		case GRAPHICS_FORMAT_GRAY256:
			return EGray256;
		case GRAPHICS_FORMAT_COLOR16:
			return EColor16;
		case GRAPHICS_FORMAT_COLOR256:
			return EColor256;
		case GRAPHICS_FORMAT_XRGB4444:
			return EColor4K;
//		case GRAPHICS_FORMAT_XRGB1555:
//			return;
		case GRAPHICS_FORMAT_RGB565:
			return EColor64K;
		case GRAPHICS_FORMAT_RGB888:
			return EColor16M;
		case GRAPHICS_FORMAT_XRGB8888:
			return EColor16MU;
			
		default:
			return ENone;
		}
	}


TInt CApplicationBase::BitmapStride(CFbsBitmap& aBitmap)
	{
	const TInt width = aBitmap.SizeInPixels().iWidth;
	TDisplayMode mode = aBitmap.DisplayMode();
	TInt stride = 0;
	
	switch (mode)
		{
		case EGray2:
		case EGray4:
		case EGray16:
		case EGray256:
		case EColor16:
		case EColor256:
			stride = width;
			break;
			
		case EColor4K:
		case EColor64K:
			stride = (width << 1);
			break;
			
		case EColor16M:
			stride = (width * 3);
			break;
			
		case EColor16MU:
			stride = (width << 2);
			break;
			
		default:
			stride = 0;
			break;
		}
	
	return stride;
	}



// functions from IAlarmObserver
void CApplicationBase::AlarmOccurence( AlarmType /*aType*/ ) NO_THROW
	{

	}


// functions from ITelephonyObserver
void CApplicationBase::IncomingCall() NO_THROW
	{

	}

void CApplicationBase::FinishedCall() NO_THROW
	{
	iWindow->BringToForeground();
	}

void CApplicationBase::IncomingMessage( MessageType /*aMessage*/ ) NO_THROW
	{

	}


⌨️ 快捷键说明

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