?? ddbouncingball_engine.cpp
字號:
// DDBouncingBall_Engine.cpp
// ---------------------------
//
// Copyright (c) 2002 Symbian Ltd. All rights reserved.
//
////////////////////////////////////////////////////////////////////
//
// Engine takes ownership of bitmaps passed in during construction
// and therefore must be responsible for deletion.
//
////////////////////////////////////////////////////////////////////
#include "DDBouncingBall_Engine.h"
const TInt KGenerationInterval = 10;
const TInt KCBAOffset = 45;
_LIT(KDDBouncingBallApp, "DDBouncingBall");
CDDBouncingBallEngine::CDDBouncingBallEngine(RWsSession& aClient, CWsScreenDevice& aScreenDevice, RWindow& aWindow,
CFbsBitmap* aBitmap, CFbsBitmap* aBitmapMask, TInt aXVelocity, TInt aYVelocity)
: CTimer(CActive::EPriorityStandard),
iClient(aClient),
iScreenDevice(aScreenDevice),
iWindow(aWindow),
iBallImage(aBitmap), // Takes ownership of bitmap
iBallImageMask(aBitmapMask), // Takes ownership of bitmap
iXVelocity(aXVelocity),
iYVelocity(aYVelocity)
{
}
CDDBouncingBallEngine::~CDDBouncingBallEngine()
{
Cancel();
delete iDirectScreenAccess;
delete iBallImage;
delete iBallImageMask;
}
void CDDBouncingBallEngine::ConstructL()
{
iBouncing = EFalse;
CTimer::ConstructL();
// Create the DSA object
iDirectScreenAccess = CDirectScreenAccess::NewL(iClient, iScreenDevice, iWindow, *this);
CActiveScheduler::Add(this);
}
// Start Bouncing
void CDDBouncingBallEngine::StartBouncingL()
{
if (iBouncing)
User::Panic(KDDBouncingBallApp, EDDBouncingAlreadyStarted);
iBouncing = ETrue;
// 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);
After(TTimeIntervalMicroSeconds32(KGenerationInterval));
}
// Stop Bouncing
void CDDBouncingBallEngine::StopBouncing()
{
if (!iBouncing)
User::Panic(KDDBouncingBallApp, EDDBouncingAlreadyStopped);
// Cancel timer and display
Cancel();
iBouncing = EFalse;
}
// Implement MDirectScreenAccess
void CDDBouncingBallEngine::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
StartBouncingL();
}
void CDDBouncingBallEngine::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
{
// Cancel timer and display
Cancel();
iBouncing = EFalse;
}
void CDDBouncingBallEngine::RunL()
{
// Force screen update: this required for WINS, but may
// not be for all hardware
iDirectScreenAccess->ScreenDevice()->Update();
TRect sourceRect(iPosition,iBallImage->SizeInPixels());
iGc->Clear(sourceRect);
// Update the balls position
MoveBall();
// and draw ball
iGc->BitBlt(iPosition, iBallImage);
iClient.Flush();
// Renew request
After(TTimeIntervalMicroSeconds32(KGenerationInterval));
}
void CDDBouncingBallEngine::DoCancel()
{
// Cancel timer
CTimer::DoCancel();
// Cancel DSA
iDirectScreenAccess->Cancel();
}
void CDDBouncingBallEngine::MoveBall()
{
// Move Ball
iPosition += TPoint(iXVelocity, iYVelocity);
// Check for Boundary collision
TRect bounds = iRegion->BoundingRect();
// Has it gone outside the left right edge
if ((iPosition.iX + iBallImage->SizeInPixels().iWidth > bounds.iBr.iX) || (iPosition.iX < bounds.iTl.iX))
iXVelocity = -iXVelocity;
// or top and bottom edge ?
if ((iPosition.iY + iBallImage->SizeInPixels().iHeight > (bounds.iBr.iY - KCBAOffset)) || (iPosition.iY < 0))
iYVelocity = -iYVelocity;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -