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

📄 graphicsappview.cpp

📁 Symbian -画图程序 graphics-vc.rar
💻 CPP
字号:
/* Copyright (c) 2004, Nokia. All rights reserved */


// INCLUDE FILES
#include <images.mbg>
#include <aknview.h>
#include "GraphicsAppView.h"
#include "bitmapmethods.h"
#include "sprite.h"


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

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

// -----------------------------------------------------------------------------
// CGraphicsAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CGraphicsAppView* CGraphicsAppView::NewL( const TRect& aRect )
    {
    CGraphicsAppView* self = 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 window's size and position
    SetRect( aRect );

    // Load in the bitmap images from the multi bitmap file
    iBackgroundImage = iEikonEnv->CreateBitmapL
								( KMultiBitmapFilename,EMbmImagesImage1 );
    iSpriteImage     = iEikonEnv->CreateBitmapL
								( KMultiBitmapFilename,EMbmImagesImage2 );
    iSpriteMask      = iEikonEnv->CreateBitmapL
								( KMultiBitmapFilename,EMbmImagesImage2_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 the array of sprite pointers
    iSprites = new ( ELeave ) CArrayPtrFlat<CSprite> ( 1 );

    SetUpSpritesL();

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

// -----------------------------------------------------------------------------
// 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 )
		{
		iSprites->ResetAndDestroy();
		delete iSprites;
		iSprites = NULL;
		}

	delete iSpriteImage;
	iSpriteImage = NULL;

	delete iSpriteMask;
	iSpriteMask = NULL;
    }

// -----------------------------------------------------------------------------
// CGraphicsAppView::SetUpSpritesL()
// Sets up sprites.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::SetUpSpritesL()
	{
    ASSERT( iSpriteImage );
	ASSERT( iSprites );

	// Create the sprites
	CSprite* sprite = NULL;

	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
    {
	// The system GC will already be activated when this function is called
	// by the framework
	UpdateDisplay();
    }

// -----------------------------------------------------------------------------
// CGraphicsAppView::UpdateDisplay()
// Updates the display.
// -----------------------------------------------------------------------------
//
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 );
        }
    }

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

// -----------------------------------------------------------------------------
// CGraphicsAppView::StartOffScreenDemo()
// Start Off Screen Demo.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::StartOffScreenDemo()
	{
	iUsingOffScreenBitmap = ETrue;
	StartTimer();
	}
	
// -----------------------------------------------------------------------------
// CGraphicsAppView::StartNoOffScreenDemo()
// Start No Off Screen Demo.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::StartNoOffScreenDemo()
	{
	iUsingOffScreenBitmap = EFalse;
	StartTimer();
	}

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

	}

// -----------------------------------------------------------------------------
// CGraphicsAppView::DoPeriodTask()
// Move the sprites.
// -----------------------------------------------------------------------------
//
void CGraphicsAppView::DoPeriodTask()
	{
	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::Period()
// This function is called by the periodic timer.
// -----------------------------------------------------------------------------
//
TInt CGraphicsAppView::Period( TAny* aPtr )
	{
    ( static_cast<CGraphicsAppView*>( aPtr ) )->DoPeriodTask();
    // Returning a value of TRUE indicates the callback should be done again
	return ETrue;
	}

// End of File

⌨️ 快捷键说明

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