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

📄 s60btlabappview.cpp

📁 S60培训教材代码(连载)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* ============================================================================
*  Name     : CS60BTLabAppView from CS60BTLabAppView.h
*  Part of  : S60BTLab
*  Created  : 10/14/2002 by 
*  Implementation notes:
*     Initial content was generated by Series 60 AppWizard.
*  Version  :
*  Copyright: 
* ============================================================================
*/

// INCLUDE FILES
#include <aknnotewrappers.h> 
#include <aknwaitdialog.h>

#include "S60BTLabAppView.h"
#include "bitmapmethods.h"
#include "sprite.h"
#include "BTConnectionHost.h"
#include "BTConnectionClient.h"
#include <graphicslab.mbg>
#include <s60btlab.rsg>

_LIT (KMultiBitmapFilename,"c:\\System\\Apps\\Graphicslab\\graphicslab.mbm"); // multi bitmap file
_LIT (KBounceCounter,"Number of bounces : "); // Text string for display
_LIT8 (KCRLF, "\r\n");
_LIT (KLogFile, "C:\\System\\Apps\\S60BTLab\\Debug.log");

static const TInt KMaxDisplayString = 256; // buffer size for string
static const TInt KMaxTIntLen = 11;

// 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 = 70000;

// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// CS60BTLabAppView::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//

// Standard Epoc construction sequence
CS60BTLabAppView* CS60BTLabAppView::NewL(const TRect& aRect)
    {
    CS60BTLabAppView* self = CS60BTLabAppView::NewLC(aRect);
    CleanupStack::Pop();
    return self;
    }

CS60BTLabAppView* CS60BTLabAppView::NewLC(const TRect& aRect)
    {
    CS60BTLabAppView* self = new (ELeave) CS60BTLabAppView;
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    return self;
    }


void CS60BTLabAppView::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);

	// 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));

    User::LeaveIfError(iDebugLogFs.Connect());
    iDebugLogFs.Delete(KLogFile); // delete any previous instance of the log file
    User::LeaveIfError(iDebugLogFile.Create(iDebugLogFs, KLogFile, EFileWrite | EFileShareExclusive | EFileStreamText));

	
    // Activate the window, which makes it ready to be drawn
    ActivateL();
    }

CS60BTLabAppView::CS60BTLabAppView() : iKeyPressed(ENoKey)
                                       
    {
    }

// Destructor
CS60BTLabAppView::~CS60BTLabAppView()
    {
    TRAPD(
        error, 
        LogL(_L8("View Destructor - function entry"));

        if (iWaitNote)
            {
            iWaitNote->ProcessFinishedL();
            }

        EndGameL();

	    delete iOffScreenBitmapGc;
	    iOffScreenBitmapGc = NULL;

	    delete iOffScreenBitmapDevice;
	    iOffScreenBitmapDevice = NULL;

	    delete iOffScreenBitmap;
	    iOffScreenBitmap = NULL;

	    delete iBackground;
	    iBackground = NULL;

	    delete iMySprite;
	    iMySprite = NULL;

        delete iOtherSprite;
	    iOtherSprite = NULL;

	    delete iBallImage;
	    iBallImage = NULL;

	    delete iBallMask;
	    iBallMask = NULL; 

	    delete iOtherBallImage;
	    iOtherBallImage = NULL;

	    delete iOtherBallMask;
	    iOtherBallMask = NULL; 
    
        LogL(_L8("View Destructor - closing log file"));
        iDebugLogFile.Close();
        iDebugLogFs.Close();
        )
	
    }

void CS60BTLabAppView::SetUpSpritesL()
    {
	// 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
	iMySprite = CSprite::NewL(KInitialSpeed,KInitialSpeed,Rect().iTl + TPoint(ballXPos, ballYPos));

	// and the other players'ball sprites
	iOtherSprite = CSprite::NewL(KInitialSpeed,-KInitialSpeed,Rect().iBr - iBallImage->SizeInPixels());

    }

// ---------------------------------------------------------
// CS60BTLabAppView::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CS60BTLabAppView::Draw(const TRect& /* aRect */) const
    {
    // The system GC will already be activated when this function is called by the framework
	UpdateDisplay();
    }

void CS60BTLabAppView::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 player's ball sprite on top of it
    NBitmapMethods::BitBltMaskedEntireBitmap(*iOffScreenBitmapGc,
											 iOtherSprite->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
		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 CS60BTLabAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
    {
	if((aType != EEventKeyDown) && (aType != EEventKey) && (aType != EEventKeyUp))
	    {
		// ignore other events 
		return EKeyWasNotConsumed;
	    }

	if(aKeyEvent.iCode == EKeyLeftArrow)
	    {
        if (iServer)
            {
		    if(iMySprite->GetXVelocity() > 0)
		        {
			    // change X velocity so ball is moving left
			    iMySprite->SetXVelocity(-(iMySprite->GetXVelocity()));
                }
		    else
		        {
			    return EKeyWasNotConsumed;
		        }
            }
        else 
            {
            // store the key press for transmission to the server
            iKeyPressed = ELeftArrow;
		    }
	    }
	else if(aKeyEvent.iCode == EKeyRightArrow)
	    {
        if (iServer)
            {
		    if(iMySprite->GetXVelocity() < 0)
		        {
			    // change X velocity so ball is moving right
			    iMySprite->SetXVelocity(-(iMySprite->GetXVelocity()));
                }
		    else
		        {
			    return EKeyWasNotConsumed;
		        }
            }
        else 
            {
            // store the key press for transmission to the server
            iKeyPressed = ERightArrow;
		    }
	    }
	else if(aKeyEvent.iCode == EKeyUpArrow) 
	    {
        if (iServer)
            {
            if(iMySprite->GetYVelocity() > 0)
		        {
			    // change Y velocity so ball is moving up
			    iMySprite->SetYVelocity(-(iMySprite->GetYVelocity()));
                }
		    else
		        {
			    return EKeyWasNotConsumed;
		        }
            }
        else 
            {
            // store the key press for transmission to the server
            iKeyPressed = EUpArrow;
		    }
	    }
	else if(aKeyEvent.iCode == EKeyDownArrow)
	    {
        if (iServer)
            {
            if(iMySprite->GetYVelocity() < 0)
		        {
			    // change Y velocity so ball is moving down
			    iMySprite->SetYVelocity(-(iMySprite->GetYVelocity()));
                }
		    else
		        {
			    return EKeyWasNotConsumed;
		        }
            }
        else 
            {
            // store the key press for transmission to the server
            iKeyPressed = EDownArrow;
		    }

⌨️ 快捷键说明

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