jewelview.cpp

来自「symbian下的泡泡龙游戏源码,手机游戏开放新手学习的好资料」· C++ 代码 · 共 327 行

CPP
327
字号
/*
* ============================================================================
*  Name     : CJewelView from JewelView.h
*  Part of  : Jewel
*  Created  : 02.10.2005 by Thosing
*  Implementation notes:
*     Initial content was generated by Series 60 Application Wizard.
*  Version  : 0.1
*  Copyright: Another
* ============================================================================
*/

// INCLUDE FILES
#include "JewelView.h"
#include <fbs.h>
#include <aknutils.h>
#include <Jewel.mbg>
#include <e32math.h>
#include "JewelEngine.h"
#include <eikenv.h>


const TRect CJewelView::iJewelSmallRect = TRect(0, 0, 19, 19);
const TRect CJewelView::iJewelLargeRect = TRect(0, 0, 29, 29);
const TRect CJewelView::iBoardRect = TRect(2, 2, 174, 174);
const TPoint CJewelView::iGridStart = TPoint (4, 4);
const TSize CJewelView::iGridSize = TSize(21, 21);
const TInt CJewelView::iNumGridsX = 8;
const TInt CJewelView::iNumGridsY = 8;
const TInt CJewelView::iLifeTotal = 100;

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

// ---------------------------------------------------------
// CJewelView::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CJewelView::ConstructL(const TRect& aRect) {
  CreateWindowL();

  SetRect(aRect);
  //ActivateL();

  //Load game elements
  iGameBoard=LoadBitmapL(EMbmJewelBoard);
  iLifeIndicator=LoadBitmapL(EMbmJewelLife);
  iLifeIndicatorMask=LoadBitmapL(EMbmJewelLife_mask);
  iProgressBar=LoadBitmapL(EMbmJewelProgress);
  iGameOverBitmap=LoadBitmapL(EMbmJewelGameover);

  for(int i=0; i<NUM_OF_JEWELS; i++) {
    iJewelsSmall[i]=LoadBitmapL(EMbmJewelBlue_s+i*4);
    iJewelsSmallMask[i]=LoadBitmapL(EMbmJewelBlue_s+i*4+1);
    iJewelsLarge[i]=LoadBitmapL(EMbmJewelBlue_s+i*4+2);
    iJewelsLargeMask[i]=LoadBitmapL(EMbmJewelBlue_s+i*4+3);
  }

  //Create back buffer
  TDisplayMode displayMode = CEikonEnv::Static()->ScreenDevice()->DisplayMode();
  iBackBuffer = new(ELeave) CFbsBitmap();

  CleanupStack::PushL(iBackBuffer);

  User::LeaveIfError(iBackBuffer->Create(aRect.Size(), displayMode));


  // Create bitmap device for the bitmap
  iBackDevice = CFbsBitmapDevice::NewL(iBackBuffer);

  // Create graphics context for the bitmap
  User::LeaveIfError(iBackDevice->CreateContext(iBackGc));

  CleanupStack::Pop();

  iGameEngine=new(ELeave) CJewelEngine();
  CleanupStack::PushL(iGameEngine);
  iGameEngine->ConstructL(this);
  CleanupStack::Pop();

  iGameEngine->FlushGameboard();
}

// Destructor
CJewelView::~CJewelView() {
  if(iGameBoard)            delete iGameBoard;
  if(iGameOverBitmap)       delete iGameOverBitmap;
  if(iLifeIndicator)        delete iLifeIndicator;
  if(iLifeIndicatorMask)    delete iLifeIndicatorMask;
  if(iProgressBar)					delete iProgressBar;
  for(int i=0; i<NUM_OF_JEWELS; i++) {
    if(iJewelsSmall[i])     delete iJewelsSmall[i];
    if(iJewelsSmallMask[i]) delete iJewelsSmallMask[i];
    if(iJewelsLarge[i])     delete iJewelsLarge[i];
    if(iJewelsLargeMask[i]) delete iJewelsLargeMask[i];
  }

  if(iBackGc)               delete iBackGc;
  if(iBackDevice)           delete iBackDevice;
  if(iBackBuffer)           delete iBackBuffer;

  if(iGameEngine)           delete iGameEngine;


}

// key event
TKeyResponse CJewelView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType) {
  static TInt hx, hy;
  switch(aType) {
    case EEventKey:
      iGameEngine->KeyPressed(aKeyEvent.iScanCode);
      break;
  }
  return EKeyWasNotConsumed;
}


// ---------------------------------------------------------
// CJewelView::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CJewelView::Draw(const TRect& /*aRect*/) const {
  CWindowGc& gc = SystemGc();
  if(!iGameEngine->IsGameOver()) {
    gc.BitBlt(TPoint(0, 0), iBackBuffer);
  } else {
    gc.BitBlt(TPoint(0, 0), iGameOverBitmap); // draw game over
    const CFont* ft=iEikonEnv->TitleFont();
    gc.UseFont(ft);
    gc.SetPenStyle(CFbsBitGc::ESolidPen);
    gc.SetPenSize(TSize(1, 1));
    gc.SetPenColor(KRgbWhite);
    
    TBuf<32> yourScore(_L("Your Score: "));  //score
    TBuf<32> highScore(_L("High Score: "));
    TInt yourScoreInt=iGameEngine->Marks();
    TInt highScoreInt=iGameEngine->LoadHighScore();
    yourScore.AppendNum(yourScoreInt);
    highScore.AppendNum(highScoreInt);
    
    gc.DrawText(yourScore, TPoint(50, 100));
    gc.DrawText(highScore, TPoint(50, 120));
    if(yourScoreInt>=highScoreInt) {		//high score
      gc.DrawText(_L("Congratulations,"), TPoint(45, 150));
      gc.DrawText(_L("you break the record!"), TPoint(30, 170));
    }
  }
}

CFbsBitmap* CJewelView::LoadBitmapL(TInt aID) {
  // set the name of the multi-bitmap file containing the bitmaps

  _LIT(KMBMFileName,"Jewel.mbm");
  TFileName mbmFileName(KMBMFileName);
  CompleteWithAppPath(mbmFileName);

  // load bitmap from the mbm file
  CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
  CleanupStack::PushL(bitmap);

  User::LeaveIfError(bitmap->Load(mbmFileName, aID));
  CleanupStack::Pop(); //bitmap

  return bitmap;
}

void CJewelView::DrawBoard(TInt *aJewels, TInt aHighLight, TBool aShowIndicator) {
  //aJewels MUST contain at least iNumGridsY*iNumGridsX elements!!

  iBackGc->Clear();

  //Draw game board
  iBackGc->BitBlt(iBoardRect.iTl, iGameBoard);

  //Draw general jewels
  TInt pointer=0;
  for(int y=0; y<iNumGridsY; y++) {
    for(int x=0; x<iNumGridsX; x++) {
      TInt jewelID=aJewels[pointer];
      if(!iGameEngine->IsJewelMasked(pointer))
        DrawJewel(TPoint(iGridStart.iX+x*iGridSize.iWidth+1, iGridStart.iY+y*iGridSize.iHeight+1),jewelID);
      pointer++;
    }
  }

  //Draw highlighted Jewel.

  if(aHighLight>=0) {
    TInt hx=aHighLight%iNumGridsX, hy=aHighLight/iNumGridsY;
    TInt hID=aJewels[aHighLight];

    if(!iGameEngine->IsJewelMasked(aHighLight))
      DrawJewel(TPoint(iGridStart.iX+hx*iGridSize.iWidth-4, iGridStart.iY+hy*iGridSize.iHeight-4), hID, EFalse);

    if(aShowIndicator)
      DrawSwapIndicator(hx, hy);
  }
  //Draw Life indicator
  iBackGc->BitBltMasked(TPoint(2, 175), iLifeIndicator, TRect(0, 0, 32, 32), iLifeIndicatorMask, ETrue);
  iBackGc->UseBrushPattern(iProgressBar);
  iBackGc->SetBrushStyle(CFbsBitGc::EPatternedBrush);
  iBackGc->SetPenStyle(CFbsBitGc::ENullPen);
  iBackGc->DrawRect(TRect(40, 177, iGameEngine->Life()+40, 185));
  iBackGc->SetBrushStyle(CFbsBitGc::ENullBrush);
  iBackGc->SetBrushColor(KRgbWhite);

  //Draw Marks
  const CFont* ab=iEikonEnv->TitleFont();
  iBackGc->UseFont(ab);
  iBackGc->SetPenStyle(CFbsBitGc::ESolidPen);
  iBackGc->SetPenSize(TSize(1, 1));
  TBuf<12> strMark;
  strMark.Num(iGameEngine->Marks());
  iBackGc->DrawText(strMark, TPoint(50, 200));
}

//draw single jewel
void CJewelView::DrawJewel(TPoint aPoint, TInt aID, TBool aDrawSmall) {
  CFbsBitmap* src;
  CFbsBitmap* mask;
  TRect drawRect;

  if(aID<0) return;

  if(aDrawSmall) {		//small
    src=iJewelsSmall[aID];
    mask=iJewelsSmallMask[aID];
    drawRect=iJewelSmallRect;
  } else {				//large
    src=iJewelsLarge[aID];
    mask=iJewelsLargeMask[aID];
    drawRect=iJewelLargeRect;
  }
  iBackGc->BitBltMasked(aPoint, src, drawRect, mask, ETrue);
}

//draw swap indicator
void CJewelView::DrawSwapIndicator(TInt aX, TInt aY) {
  CFbsBitGc::TPenStyle penStyle=CFbsBitGc::EDashedPen;

  iBackGc->SetPenStyle(CFbsBitGc::ENullPen);
  iBackGc->SetPenColor(KRgbRed);
  iBackGc->SetPenSize(TSize(2, 2));
  iBackGc->MoveTo(TPoint(iGridStart.iX+aX*iGridSize.iWidth, iGridStart.iY+aY*iGridSize.iHeight));

  if(aY>0) {
    iBackGc->SetPenStyle(penStyle);
  } else {
    iBackGc->SetPenStyle(CFbsBitGc::ENullPen);
  }
  iBackGc->DrawLineBy(TPoint(0,                 -iGridSize.iHeight));
  iBackGc->DrawLineBy(TPoint(iGridSize.iWidth,  0));
  iBackGc->DrawLineBy(TPoint(0,                 iGridSize.iHeight));

  if(aX<7) {
    iBackGc->SetPenStyle(penStyle);
  } else {
    iBackGc->SetPenStyle(CFbsBitGc::ENullPen);
  }
  iBackGc->DrawLineBy(TPoint(iGridSize.iWidth,  0));
  iBackGc->DrawLineBy(TPoint(0,                 iGridSize.iHeight));
  iBackGc->DrawLineBy(TPoint(-iGridSize.iWidth, 0));

  if(aY<7) {
    iBackGc->SetPenStyle(penStyle);
  } else {
    iBackGc->SetPenStyle(CFbsBitGc::ENullPen);
  }
  iBackGc->DrawLineBy(TPoint(0,                 iGridSize.iHeight));
  iBackGc->DrawLineBy(TPoint(-iGridSize.iWidth, 0));
  iBackGc->DrawLineBy(TPoint(0,                 -iGridSize.iHeight));

  if(aX>0) {
    iBackGc->SetPenStyle(penStyle);
  } else {
    iBackGc->SetPenStyle(CFbsBitGc::ENullPen);
  }
  iBackGc->DrawLineBy(TPoint(-iGridSize.iWidth, 0));
  iBackGc->DrawLineBy(TPoint(0,                 -iGridSize.iHeight));
  iBackGc->DrawLineBy(TPoint(iGridSize.iWidth,  0));

}

//draw swap effect
void CJewelView::SawpEffect(TInt aJewelA, TInt aJewelB) {
  TInt idA=iGameEngine->BoardJewel(aJewelA);
  TInt idB=iGameEngine->BoardJewel(aJewelB);

  //Set A and B invisable
  iGameEngine->SetJewelMasked(aJewelA);
  iGameEngine->SetJewelMasked(aJewelB);

  TPoint startA=TPoint(iGridStart.iX+(aJewelA%iNumGridsX)*iGridSize.iWidth, 
                       iGridStart.iY+(aJewelA/iNumGridsY)*iGridSize.iHeight);
  TPoint startB=TPoint(iGridStart.iX+(aJewelB%iNumGridsX)*iGridSize.iWidth, 
                       iGridStart.iY+(aJewelB/iNumGridsY)*iGridSize.iHeight);

  TPoint moveA, moveB;
  TInt steps;
  if(startA.iX!=startB.iX) {
    moveA=TPoint((startB.iX-startA.iX)/iGridSize.iWidth*4, 0);
    moveB=TPoint((startA.iX-startB.iX)/iGridSize.iWidth*4, 0);
    steps=iGridSize.iWidth/4;
  } else {
    moveA=TPoint(0, (startB.iY-startA.iY)/iGridSize.iHeight*4);
    moveB=TPoint(0, (startA.iY-startB.iY)/iGridSize.iHeight*4);
    steps=iGridSize.iHeight/4;
  }

  for(TInt i=0; i<steps; i++) {
    startA+=moveA;
    startB+=moveB;
    iGameEngine->FlushGameboard();
    DrawJewel(startA, idA);
    DrawJewel(startB, idB);
    iGameEngine->Delay(10000, ETrue);
  }

  iGameEngine->MaskJewelsAll(EFalse);
}

void CJewelView::FullLife() {
  iGameEngine->FullLife();
}
// End of File  

⌨️ 快捷键说明

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