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

📄 polytubeengine.cpp

📁 手机 GAME c++ 版
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////
//
// PolyTubeEngine.cpp
//
// Copyright (c) 2003 Nokia Mobile Phones Ltd.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////

#include "PolyTubeEngine.h"

//System includes
#include <e32math.h>
#include <eikenv.h>

//User includes
#include "KeyHandler.h"
#include "DoublebufferedArea.h"
#include "GameTimer.h"

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

CPolyTubeEngine* CPolyTubeEngine::NewLC(RWsSession& aClient, CWsScreenDevice& aScreenDevice, RWindow& aWindow,TKeyHandler& aKeyHandler, const TSize& aSize)
	{
	CPolyTubeEngine* self = new (ELeave) CPolyTubeEngine(aClient, aScreenDevice, aWindow, aKeyHandler, aSize);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

////////////////////////////////////////////////////////////////////////

CPolyTubeEngine* CPolyTubeEngine::NewL(RWsSession& aClient, CWsScreenDevice& aScreenDevice, RWindow& aWindow,TKeyHandler& aKeyHandler, const TSize& aSize)
	{
	CPolyTubeEngine* self = CPolyTubeEngine::NewLC(aClient, aScreenDevice, aWindow, aKeyHandler, aSize);
	CleanupStack::Pop();
	return self;
	}

////////////////////////////////////////////////////////////////////////

CPolyTubeEngine::CPolyTubeEngine(RWsSession& aClient, CWsScreenDevice& aScreenDevice, RWindow& aWindow,TKeyHandler& aKeyHandler, const TSize& aSize) :
	iClient(aClient),
	iScreenDevice(aScreenDevice),
	iWindow(aWindow),
	iKeyHandler(aKeyHandler),
	iSize(aSize),
	iDropRenderFrames(ETrue), // Set to "EFalse" to demonstrate ViewSrv 11 problem
	iFrameCounter(0),
	iScanConverter(aSize),
	iCube( 128 ),
	iTorusSector( 2048, 256, iMath )
	{
	}

////////////////////////////////////////////////////////////////////////
	
void CPolyTubeEngine::ConstructL()
	{
	iPlaying = EFalse;

	iGameTimer = CGameTimer::NewL(*this);
	// Direct screen access
	iDirectScreenAccess = CDirectScreenAccess::NewL(iClient, iScreenDevice, iWindow, *this);

	iDoubleBufferedArea = CDoubleBufferedArea::NewL(iSize, EColor4K);
	iOffscreenContext = &(iDoubleBufferedArea->GetDoubleBufferedAreaContext());

	iRedraw.Set(TRawEvent::ERedraw);

	iCube.SetPosition( TVector3( 0, 0, 1024 ) );
	iTorusSector.SetPosition( TVector3( -2048, 0, 256 ) );
	}

////////////////////////////////////////////////////////////////////////

CPolyTubeEngine::~CPolyTubeEngine()
	{
	delete iGameTimer;
	iGameTimer = NULL;
	
	delete iDirectScreenAccess;
	iDirectScreenAccess = NULL;
	
	delete iDoubleBufferedArea;
	iDoubleBufferedArea = NULL;
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::Simulate()
	{
	iTorusSector.Simulate();
	iCube.Simulate();
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::Render() 
	{
	//
	// NB: we must lock the offscreen bitmap with TBitmapUtil::Begin() to
	// prevent it being moved during the rendering process.
	//
	CFbsBitmap* bitmap = &(iDoubleBufferedArea->GetDoubleBufferedAreaBitmap());
	TBitmapUtil dummyUtil(bitmap);

	dummyUtil.Begin(TPoint(0,0));

	// Note assumption of screen format as 16 bpp:
	TUint16 * buffer = (TUint16 *)bitmap->DataAddress();
	iScanConverter.SetScreenPtr(buffer);

	// Clear screen to magenta. Note that -
	//
	// 1. this assumes pixels are 444 RGB; and
	// 2. screen clearance is not strictly necessary, since
	//    rendering covers the entire game window area.
	iScanConverter.ClearScreen( 0x0f0f );

	iTorusSector.Render( iScanConverter, iMath );
	iCube.Render( iScanConverter, iMath );

	dummyUtil.End();
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::SetupDirectScreenAccessL()
	{
	// Initialise DSA
	iDirectScreenAccess->StartL();

	// Get graphics context for it
	iGc = iDirectScreenAccess->Gc();

	// Get region that DSA can draw in
	iRegion = iDirectScreenAccess->DrawingRegion();

	// Set the display to clip to this region
	iGc->SetClippingRegion(iRegion);
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::StartFirstGameL()
	{
	StartGameL();
	// We only want to play the game if we have the whole screen available so 
	// on first start we record the region available for comparision later (in restart)
	iGameDawingArea = iRegion->BoundingRect();
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::StartGameL()
	{
	iPaused = EFalse;
	if(!iPlaying)
		{
		SetupDirectScreenAccessL();
		iGameTimer->Restart();
		}
	iPlaying = ETrue;
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::StopGame()
	{
	iPlaying = EFalse;
	iPaused = ETrue;
	iGameTimer->CancelTimer();
	iDirectScreenAccess->Cancel();
	}

////////////////////////////////////////////////////////////////////////

TInt CPolyTubeEngine::DoGameFrame()
	{
	if(iPaused)
		{
		PauseFrame();
		}
	else
		{
		// Force backlight to remain on:
		UserSvr::AddEvent(iRedraw);

		// Force screen update:
		iDirectScreenAccess->ScreenDevice()->Update();

		Simulate();

		// Drop 1 render frame in every 128, to avoid ViewSrv 11 problems:
		if (iDropRenderFrames & ((iFrameCounter & 0x007f) != 0x007f))
			{
			Render();
			}

		iFrameCounter++;

		// Draw from offscreen bitmap
		iGc->BitBlt(TPoint(0,0), &(iDoubleBufferedArea->GetDoubleBufferedAreaBitmap()));
		}
	return CGameTimer::TickAgain;
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::Restart(RDirectScreenAccess::TTerminationReasons /*aReason*/)
	{
	// Restart display
	// Note that this will result in the clipping region being updated
	// so that menus, overlaying dialogs, etc. will not be drawn over

	if(iPaused)
		{
		SetupDirectScreenAccessL();
		if(iGameDawingArea == iRegion->BoundingRect())
			{
			iPaused = EFalse;	
			if(!iGameTimer->IsActive())
				{
				iGameTimer->Restart();
				}
			}
		else
			{
			PauseFrame();
			}
		}
	else
		{
		if(!iGameTimer->IsActive())
			{
			iGameTimer->Restart();
			}
		}
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
	{
	// Cancel timer and display
	iDirectScreenAccess->Cancel();
	iPaused = ETrue;
	}

////////////////////////////////////////////////////////////////////////

void CPolyTubeEngine::PauseFrame()
	{
	// Force backlight to remain on:
	UserSvr::AddEvent(iRedraw);

	// Force screen update: this is required for WINS, but may
	// not be for all hardware:
	iDirectScreenAccess->ScreenDevice()->Update();
	}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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