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

📄 graphicsappview.cpp

📁 symbian os c++ 碰撞小球 代码
💻 CPP
字号:
/*
 ============================================================================
 Name		: GraphicsAppView.cpp
 Author	  : 
 Copyright   : Your copyright notice
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>

#include <aknview.h>
#include <images.mbg>
#include <eikdef.h>
#include <eikenv.h>
#include "BitmapMethods.h"
#include "GraphicsAppView.h"


#include "Sprite.h"



_LIT (KMultiBitmapFilename,"\\system\\apps\\graphics\\images.mbm");

static const TInt KInitialXSpeed = 4;
static const TInt KInitialYSpeed = 4;




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

// -----------------------------------------------------------------------------
// CGraphicsAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CGraphicsAppView* CGraphicsAppView::NewL(const TRect& aRect)
	{
	CGraphicsAppView* self = CGraphicsAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CGraphicsAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CGraphicsAppView* CGraphicsAppView::NewLC(const TRect& aRect)
	{
	CGraphicsAppView* self = new (ELeave) CGraphicsAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

// -----------------------------------------------------------------------------
// CGraphicsAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::ConstructL(const TRect& aRect)
	{
	// Create a window for this application view
	CreateWindowL();

	// Set the windows size
	SetRect(aRect);
	
	
	iBackgroundImage = iEikonEnv->CreateBitmapL(KMultiBitmapFilename,EMbmImagesImage1);
	iSpriteImage     = iEikonEnv->CreateBitmapL(KMultiBitmapFilename,EMbmImagesImage2);
	iSpriteMask      = iEikonEnv->CreateBitmapL(KMultiBitmapFilename,EMbmImagesImage2_mask);

	
	 iOffScreenBitmap       = NBitmapMethods::CreateBitmapL(Rect().Size(),KColourDepth);
	 iOffScreenBitmapDevice = NBitmapMethods::CreateBitmapDeviceL(*iOffScreenBitmap);
	 iOffScreenBitmapGc     = NBitmapMethods::CreateGraphicsContextL(*iOffScreenBitmapDevice);	 
	 
	 /*
	     如果在一个文件中写太多的代码会使得一点也不清晰,故采用了一个独立的namespace 来完成

	  和当前view 关系不太紧密的创建工作。

	  NOTE:先创建Bitmap,再创建Device,最后创建BitmapGc

	  */	 
	 
	 
	 iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
	
	 iSprites = new (ELeave) CArrayPtrFlat<CSprite> (1);

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

// -----------------------------------------------------------------------------
// CGraphicsAppView::CGraphicsAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CGraphicsAppView::CGraphicsAppView()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CGraphicsAppView::~CGraphicsAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CGraphicsAppView::~CGraphicsAppView()
	{
	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 iBackgroundImage;
	 iBackgroundImage = NULL;

	 if (iSprites)
	  {
	  /*
	  void ResetAndDestroy();
	        Description
	        Empties the array and deletes the referenced objects.

	        It frees all memory allocated to the array and resets the internal state 
	  
	  so that it is ready to be reused. The function also deletes all of the objects 
	  
	  whose pointers are contained by the array.

	       This array object can be allowed to go out of scope after a call to this function.

	  */
	  iSprites->ResetAndDestroy();
	  //删除一个CArrayPtrFlat 这样就可以了
	  delete iSprites;
	  iSprites = NULL;
	  }

	 delete iSpriteImage;
	 iSpriteImage = NULL;

	 delete iSpriteMask;
	 iSpriteMask = NULL;
	    }



void CGraphicsAppView::SetUpSpritesL()
 {
    ASSERT(iSpriteImage);
 ASSERT(iSprites);

 // Create the sprites 初始化为 NULL, 好习惯
 CSprite* sprite = NULL;
    // 创建了3个小精灵,就是3个来回撞的小红球,加入到数组iSprites当中
 //3个参数意义:x方向速度,y方向速度,初始位置
 sprite = CSprite::NewLC(KInitialXSpeed,KInitialYSpeed,Rect().iTl);
 iSprites->AppendL(sprite);
 CleanupStack::Pop(sprite);
 sprite = CSprite::NewLC(-KInitialXSpeed,-KInitialYSpeed,Rect().iBr - iSpriteImage->SizeInPixels());
  iSprites->AppendL(sprite);
  CleanupStack::Pop(sprite);

  sprite = CSprite::NewLC(-KInitialXSpeed,KInitialYSpeed,Rect().iTl + iSpriteImage->SizeInPixels());
  iSprites->AppendL(sprite);
  CleanupStack::Pop(sprite);
  }


	
	
	
	

// -----------------------------------------------------------------------------
// CGraphicsAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::Draw(const TRect& /*aRect*/) const
	{
	UpdateDisplay();


	}


void CGraphicsAppView::UpdateDisplay() const
    {
    CWindowGc& gc = SystemGc();

    if (!iUsingOffScreenBitmap)
        {
        // Blit the background image onto the screen at the top left position
        gc.BitBlt(Rect().iTl,iBackgroundImage);

        // Blit the sprites on top of it using their mask to retain the background where necessary
        TRect sourceRect(TPoint(0,0),iSpriteImage->SizeInPixels());
        for (TInt count = 0;count<iSprites->Count();count++)
            {
            gc.BitBltMasked(iSprites->At(count)->Position(),iSpriteImage,sourceRect,iSpriteMask,ETrue);
            }
        }
    else
        {
        // Blit the background image onto the off screen bitmap at the top left position
        iOffScreenBitmapGc->BitBlt(TPoint(0,0),iBackgroundImage);

        // Blit the sprites on top of it using its mask to retain the background where necessary
        for (TInt count = 0;count<iSprites->Count();count++)
            {  NBitmapMethods::BitBltMaskedEntireBitmap(*iOffScreenBitmapGc,iSprites->At(count)->Position(),*iSpriteImage,*iSpriteMask);
            }

        // Blit the offscreen image onto the screen at the top left position
        gc.BitBlt(Rect().iTl,iOffScreenBitmap);
        }
    }


void CGraphicsAppView::StopDemo()
 {
 // Stop the timer if it is active
 if (iPeriodicTimer->IsActive())
  {
  iPeriodicTimer->Cancel();
  }
 }

void CGraphicsAppView::StartOffScreenDemo()
 {
 iUsingOffScreenBitmap = ETrue;
 StartTimer();
 }
 

void CGraphicsAppView::StartNoOffScreenDemo()
 {
 iUsingOffScreenBitmap = EFalse;
 StartTimer();
 }

void CGraphicsAppView::StartTimer()
 {
 // If the timer is not already running, start it
 if (!iPeriodicTimer->IsActive())
  {
  iPeriodicTimer->Start(1,1,TCallBack(CGraphicsAppView::Period,this));
  }

 }


TInt CGraphicsAppView::Period( TAny* aPtr )//周期启动函数,注意,这是个静态函数,但static只在头文件中才做了申明。

    {

    ( static_cast<CGraphicsAppView*>( aPtr ) )->DoPeriodTask();

    return ETrue;

    }



void CGraphicsAppView::DoPeriodTask()
 {
 // Move the sprites
 for (TInt count = 0;count<iSprites->Count();count++)
  {
  iSprites->At(count)->Move(iSpriteImage->SizeInPixels(),Rect());
  }
 // Update the screen
  CWindowGc& gc = SystemGc();
  gc.Activate(*DrawableWindow());
  UpdateDisplay();
  gc.Deactivate();
  }











// -----------------------------------------------------------------------------
// CGraphicsAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::SizeChanged()
	{
	DrawNow();
	}

// -----------------------------------------------------------------------------
// CGraphicsAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs, 
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{

	// Call base class HandlePointerEventL()
	CCoeControl::HandlePointerEventL(aPointerEvent);
	}

// End of File

⌨️ 快捷键说明

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