📄 graphicslabappview.cpp
字号:
/*
* ============================================================================
* Name : CGraphicsLabAppView from CGraphicsLabAppView.h
* Part of : GraphicsLab
* Created : 10/14/2002 by
* Implementation notes:
* Initial content was generated by Series 60 AppWizard.
* Version :
* Copyright:
* ============================================================================
*/
// INCLUDE FILES
#include "GraphicsLabAppView.h"
#include "bitmapmethods.h"
#include "sprite.h"
#include <graphicslab.mbg>
_LIT (KMultiBitmapFilename,"c:\\System\\Apps\\Graphicslab\\graphicslab.mbm"); // multi bitmap file
_LIT (KBounceCounter,"Number of bounces : "); // Text string for display
static const TInt KMaxDisplayString = 256; // buffer size for string
// these values can easily be changed if required
// note that animation appears much faster on the handset than in the emulator
static const TInt KInitialSpeed = 4; // slow ball speed
static const TInt KFastSpeed = 8; // fast ball speed
static const TInt KMaxNumBounces = 99; // max number of bounces before animation stops
static const TInt KTimerIntervalMicroSeconds = 50000;
// ================= MEMBER FUNCTIONS =======================
// ---------------------------------------------------------
// CGraphicsLabAppView::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
// Standard Epoc construction sequence
CGraphicsLabAppView* CGraphicsLabAppView::NewL(const TRect& aRect)
{
CGraphicsLabAppView* self = CGraphicsLabAppView::NewLC(aRect);
CleanupStack::Pop();
return self;
}
CGraphicsLabAppView* CGraphicsLabAppView::NewLC(const TRect& aRect)
{
CGraphicsLabAppView* self = new (ELeave) CGraphicsLabAppView;
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
void CGraphicsLabAppView::ConstructL(const TRect& aRect)
{
// Create a window for this application view
CreateWindowL();
// Set the window's size and position
SetRect(aRect);
// Load in the bitmap images from the multi bitmap file
iBackground = NBitmapMethods::CreateBitmapL(KMultiBitmapFilename,EMbmGraphicslabBackground);
iBallImage = NBitmapMethods::CreateBitmapL(KMultiBitmapFilename,EMbmGraphicslabBall);
iBallMask = NBitmapMethods::CreateBitmapL(KMultiBitmapFilename,EMbmGraphicslabBall_mask);
iOtherBallImage = NBitmapMethods::CreateBitmapL(KMultiBitmapFilename,EMbmGraphicslabOther_ball);
iOtherBallMask = NBitmapMethods::CreateBitmapL(KMultiBitmapFilename,EMbmGraphicslabOther_ball_mask);
// Create the off screen bitmap and device / gc
iOffScreenBitmap = NBitmapMethods::CreateBitmapL(Rect().Size(),KColourDepth);
iOffScreenBitmapDevice = NBitmapMethods::CreateBitmapDeviceL(*iOffScreenBitmap);
iOffScreenBitmapGc = NBitmapMethods::CreateGraphicsContextL(*iOffScreenBitmapDevice);
// Create a periodic timer but don't start it yet
iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
//Create an array of sprite pointers for other ball sprites
iOtherSprites = new (ELeave) CArrayPtrFlat<CSprite> (2);
// set up sprites
SetUpSpritesL();
// set position on screen for text (bounce count) to appear
iTextDisplay = Rect().iTl + TPoint(1,12);
// Create font
TTypefaceSupport myTypefaceSupport;
iOffScreenBitmapDevice->TypefaceSupport(myTypefaceSupport,1);
TFontSpec fontspec1(myTypefaceSupport.iTypeface.iName.Des(), 100);
User::LeaveIfError(iOffScreenBitmapDevice->GetNearestFontInTwips(iFont, fontspec1));
// Activate the window, which makes it ready to be drawn
ActivateL();
}
CGraphicsLabAppView::CGraphicsLabAppView()
{
}
// Destructor
CGraphicsLabAppView::~CGraphicsLabAppView()
{
if (iPeriodicTimer)
{
// Stop the periodic timer
iPeriodicTimer->Cancel();
}
delete iPeriodicTimer;
iPeriodicTimer = NULL;
delete iOffScreenBitmapGc;
iOffScreenBitmapGc = NULL;
delete iOffScreenBitmapDevice;
iOffScreenBitmapDevice = NULL;
delete iOffScreenBitmap;
iOffScreenBitmap = NULL;
delete iBackground;
iBackground = NULL;
delete iMySprite;
iMySprite = NULL;
if (iOtherSprites)
{
iOtherSprites->ResetAndDestroy();
delete iOtherSprites;
iOtherSprites = NULL;
}
delete iBallImage;
iBallImage = NULL;
delete iBallMask;
iBallMask = NULL;
delete iOtherBallImage;
iOtherBallImage = NULL;
delete iOtherBallMask;
iOtherBallMask = NULL;
}
void CGraphicsLabAppView::SetUpSpritesL()
{
CSprite* sprite = NULL;
// start the player's ball right in the middle of the screen
TInt ballXPos = iBackground->SizeInPixels().iWidth/2 -
iBallImage->SizeInPixels().iWidth/2;
TInt ballYPos = iBackground->SizeInPixels().iHeight/2 -
iBallImage->SizeInPixels().iHeight/2;
//Create the player's ball sprite
sprite = CSprite::NewLC(KInitialSpeed,KInitialSpeed,Rect().iTl + TPoint(ballXPos, ballYPos));
iMySprite = sprite;
CleanupStack::Pop(sprite);
// and the other ball sprites.....
sprite = CSprite::NewLC(KInitialSpeed,-KInitialSpeed,Rect().iBr - iBallImage->SizeInPixels());
iOtherSprites->AppendL(sprite);
CleanupStack::Pop(sprite);
sprite = CSprite::NewLC(-KInitialSpeed,KInitialSpeed,Rect().iTl);
iOtherSprites->AppendL(sprite);
CleanupStack::Pop(sprite);
// ....could add as many as you like
}
// ---------------------------------------------------------
// CGraphicsLabAppView::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CGraphicsLabAppView::Draw(const TRect& /* aRect */) const
{
// The system GC will already be activated when this function is called by the framework
UpdateDisplay();
}
void CGraphicsLabAppView::UpdateDisplay() const
{
CWindowGc& gc = SystemGc();
// Blit the background image onto the off screen bitmap at the top left position
iOffScreenBitmapGc->BitBlt(TPoint(0,0),iBackground);
// Blit the other ball sprites on top of it
for (TInt count = 0; count < iOtherSprites->Count(); count++)
{
NBitmapMethods::BitBltMaskedEntireBitmap(*iOffScreenBitmapGc,
iOtherSprites->At(count)->Position(),
*iOtherBallImage,*iOtherBallMask);
}
// Blit my ball sprite on last
NBitmapMethods::BitBltMaskedEntireBitmap(*iOffScreenBitmapGc, iMySprite->Position(),
*iBallImage, *iBallMask);
// Blit the offscreen image onto the screen at the top left position
gc.BitBlt(Rect().iTl,iOffScreenBitmap);
// check if the ball has reached max allowed bounces
if(iMySprite->GetBounceCount() > KMaxNumBounces)
{
// stop animation and reset counter to 0
StopAnimation();
iMySprite->SetBounceCount(0);
}
// create a string to display the current number of bounces
_LIT (KFormatString,"%02d");
TBuf<KMaxDisplayString> DisplayString;
DisplayString.Format(KFormatString, iMySprite->GetBounceCount());
iOffScreenBitmapGc->SetPenColor(TRgb(0xFFFFFF));
// select font
iOffScreenBitmapGc->UseFont(iFont);
// write text to screen
iOffScreenBitmapGc->DrawText(KBounceCounter, TPoint(iTextDisplay));
// get width of text in pixels
TInt Width = iFont->TextWidthInPixels(KBounceCounter);
// reset pen colour for numbers
iOffScreenBitmapGc->SetPenColor(TRgb(0xFFFF00));
// draw figures next to the text
iOffScreenBitmapGc->DrawText(DisplayString, TPoint(iTextDisplay.iX + Width, iTextDisplay.iY));
// discard the font
iOffScreenBitmapGc->DiscardFont();
}
TKeyResponse CGraphicsLabAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
if((aType != EEventKeyDown) && (aType != EEventKey) && (aType != EEventKeyUp))
{
// ignore other events
return EKeyWasNotConsumed;
}
if(aKeyEvent.iCode == EKeyOK)
{
// change ball speed in X direction
if(iMySprite->GetXVelocity() == KInitialSpeed)
{
iMySprite->SetXVelocity(KFastSpeed);
}
else if (iMySprite->GetXVelocity() == -KInitialSpeed)
{
iMySprite->SetXVelocity(-KFastSpeed);
}
else if (iMySprite->GetXVelocity() == KFastSpeed)
{
iMySprite->SetXVelocity(KInitialSpeed);
}
else
{
iMySprite->SetXVelocity(-KInitialSpeed);
}
// and in Y direction
if(iMySprite->GetYVelocity() == KInitialSpeed)
{
iMySprite->SetYVelocity(KFastSpeed);
}
else if (iMySprite->GetYVelocity() == -KInitialSpeed)
{
iMySprite->SetYVelocity(-KFastSpeed);
}
else if (iMySprite->GetYVelocity() == KFastSpeed)
{
iMySprite->SetYVelocity(KInitialSpeed);
}
else
{
iMySprite->SetYVelocity(-KInitialSpeed);
}
}
else if(aKeyEvent.iCode == EKeyLeftArrow)
{
if(iMySprite->GetXVelocity() > 0)
{
// change X velocity so ball is moving left
iMySprite->SetXVelocity(-(iMySprite->GetXVelocity()));
}
else
{
return EKeyWasNotConsumed;
}
}
else if(aKeyEvent.iCode == EKeyRightArrow)
{
if(iMySprite->GetXVelocity() < 0)
{
// change X velocity so ball is moving right
iMySprite->SetXVelocity(-(iMySprite->GetXVelocity()));
}
else
{
return EKeyWasNotConsumed;
}
}
else if(aKeyEvent.iCode == EKeyUpArrow)
{
if(iMySprite->GetYVelocity() > 0)
{
// change Y velocity so ball is moving up
iMySprite->SetYVelocity(-(iMySprite->GetYVelocity()));
}
else
{
return EKeyWasNotConsumed;
}
}
else if(aKeyEvent.iCode == EKeyDownArrow)
{
if(iMySprite->GetYVelocity() < 0)
{
// change Y velocity so ball is moving down
iMySprite->SetYVelocity(-(iMySprite->GetYVelocity()));
}
else
{
return EKeyWasNotConsumed;
}
}
else
{
return EKeyWasNotConsumed;
}
return EKeyWasConsumed;
}
void CGraphicsLabAppView::StopAnimation() const
{
//Stop the timer if it is active
if (iPeriodicTimer->IsActive())
{
iPeriodicTimer->Cancel();
}
}
void CGraphicsLabAppView::StartAnimation()
{
StartTimer();
}
void CGraphicsLabAppView::ResetCounter() const
{
// reset the counter to 0
iMySprite->SetBounceCount(0);
}
void CGraphicsLabAppView::StartTimer()
{
//If the timer is not already running, start it
if (!iPeriodicTimer->IsActive())
{
iPeriodicTimer->Start(1, KTimerIntervalMicroSeconds, TCallBack(CGraphicsLabAppView::Period,this));
}
}
void CGraphicsLabAppView::DoPeriodTask()
{
// Move the player's ball sprite
iMySprite->Move(iBallImage->SizeInPixels(),Rect());
// and the other balls
for (TInt count = 0; count < iOtherSprites->Count(); count++)
{
iOtherSprites->At(count)->Move(iOtherBallImage->SizeInPixels(),Rect());
}
// Update the screen
CWindowGc& gc = SystemGc();
gc.Activate(*DrawableWindow());
UpdateDisplay();
gc.Deactivate();
}
// This function is called by the periodic timer
TInt CGraphicsLabAppView::Period(TAny * aPtr)
{
((CGraphicsLabAppView*)aPtr)->DoPeriodTask();
//returning CGraphicsLabAppView value of TRUE indicates the callback should be done again
return TRUE;
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -