📄 scalablescreendrawingengine.cpp
字号:
// ScalableScreenDrawingEngine.cpp
// --------------------
//
////////////////////////////////////////////////////////////////////////
//
// Source file for the implementation of the
// application engine class - CScalableScreenDrawingEngine
//
////////////////////////////////////////////////////////////////////////
#include <ScalableScreenDrawingIcons.mbg>
#include <eikapp.h>
#include <eikappui.h>
#include <eikenv.h>
#include <e32math.h>
#include <AknUtils.h>
#include <AknIconUtils.h>
#include <e32svr.h>
#include <aknappui.h>
#include <eikbtgpc.h>
#include "ScalableScreenDrawingEngine.h"
// Constructor
CScalableScreenDrawingEngine::CScalableScreenDrawingEngine(RWsSession& aClient, CAknViewAppUi* aAppUi) : iClient(aClient)
{
iAppUi = aAppUi;
iGc = NULL;
iRegion = NULL;
iIxSelection = 0;
iFpsCounter = 0;
iScrollOffset = 0;
iScrollOffsetStars = 0;
// Default values for menu options
iUseDoubleBuffer = ETrue; // Double buffer is enabled by default
iUseBitmapIcons = EFalse;
iUseAnchors = EFalse;
iUseVectorBackground = EFalse;
iUseScrolling = EFalse;
// Default values for keypad options
iHideMenu = EFalse;
iHideSnow = EFalse;
iHideStars = EFalse;
iHideCBA = EFalse;
}
// Second-phase constructor
void CScalableScreenDrawingEngine::ConstructL(RWindow& aWindow, CWsScreenDevice* aScreenDevice)
{
iScreenDevice = aScreenDevice;
// Create Direct Screen Access
iDirectScreenAccess = CDirectScreenAccess::NewL(iClient, *aScreenDevice, aWindow, *this);
// Create back buffer
iVirtualScreen = new (ELeave) CFbsBitmap;
// We always use 16-bit back buffer so that we don't need to
// worry about changing color-depth when we use custom drawing routines.
// Instead, we use BitBlt to copy the back buffer onto the display
// because it handles the color-depth conversion for us, even
// if BitBlt is a bit slow.
User::LeaveIfError(iVirtualScreen->Create(aScreenDevice->SizeInPixels(), OFFSCREEN_DISPLAYMODE));
RDebug::Print(_L("DisplayMode: %d, off-screen mode: %d"), aScreenDevice->DisplayMode(), OFFSCREEN_DISPLAYMODE);
// Create Bitmap Context for off-screen bitmap drawing
iBitmapDevice = CFbsBitmapDevice::NewL(iVirtualScreen);
User::LeaveIfError(iBitmapDevice->CreateBitmapContext(iBC));
// create and open file server session
User::LeaveIfError(iFsSession.Connect());
#ifdef EKA2
iFsSession.ShareProtected();
#endif //EKA2
// set path of the icon files
#if defined ( EKA2 )
User::LeaveIfError(iFsSession.PrivatePath(completefilename));
#else
_LIT( KAppPath, "\\private\\10009AD7\\" );
completefilename.Append( KAppPath );
#endif
// append the MBM file name to the private path
_LIT(KMBMFile,"ScalableScreenDrawingIcons.mbm");
completefilename.Append( KMBMFile );
// insert the drive to the private path
TParsePtrC parse((CEikonEnv::Static()->EikAppUi()->Application())->AppFullName());
completefilename.Insert(0, parse.Drive());
// Load bitmaps from the resource. Bitmaps loaded with AknIconUtils::CreateIconL
// are not ready for use before they are initialized by a call to AknIconUtils::SetSize,
// which occurs in the ScaleImage method.
#ifdef EKA2
iIconProvider = CIconFileProvider::NewL(iFsSession, completefilename);
// Load backgound bitmap
iBackground = AknIconUtils::CreateIconL(*iIconProvider, EMbmScalablescreendrawingiconsBackground);
// Load vector background
// The vector icon is stored in a .mif file, but this call knows how to load it
// even when we give the .mbm file as the param.
iBackground2 = AknIconUtils::CreateIconL(*iIconProvider, EMbmScalablescreendrawingiconsBackground2);
// Load small snow flakes bitmap and mask with a single call
AknIconUtils::CreateIconL(iSnowflakeSmall, iSnowflakeSmall_mask, *iIconProvider,
EMbmScalablescreendrawingiconsFlakesmall, EMbmScalablescreendrawingiconsFlakesmall_mask);
// Load big snow flakes
AknIconUtils::CreateIconL(iSnowflakeBig, iSnowflakeBig_mask, *iIconProvider,
EMbmScalablescreendrawingiconsFlakebig, EMbmScalablescreendrawingiconsFlakebig_mask);
// Load menu selector bitmap
AknIconUtils::CreateIconL(iIconSelector, iIconSelector_mask, *iIconProvider,
EMbmScalablescreendrawingiconsSelector, EMbmScalablescreendrawingiconsSelector_mask);
// Load vector menu selector icon
AknIconUtils::CreateIconL(iIconSelector2, iIconSelector2_mask, *iIconProvider,
EMbmScalablescreendrawingiconsStar, EMbmScalablescreendrawingiconsStar_mask);
// This call preserves SVG-T vector data in memory so that the data is not needed to
// load from the resource when the icon is subsequently scaled.
AknIconUtils::PreserveIconData(iIconSelector2);
// Load soft button bitmaps
TSize size;
iIconExit = AknIconUtils::CreateIconL(*iIconProvider, EMbmScalablescreendrawingiconsExit);
AknIconUtils::GetContentDimensions(iIconExit, size);
AknIconUtils::SetSize(iIconExit, size);
iIconMenu = AknIconUtils::CreateIconL(*iIconProvider, EMbmScalablescreendrawingiconsMenu);
AknIconUtils::GetContentDimensions(iIconMenu, size);
AknIconUtils::SetSize(iIconMenu, size);
#else
iBackground = AknIconUtils::CreateIconL(completefilename, EMbmScalablescreendrawingiconsBackground);
iBackground2 = AknIconUtils::CreateIconL(completefilename, EMbmScalablescreendrawingiconsBackground2);
AknIconUtils::CreateIconL(iSnowflakeSmall, iSnowflakeSmall_mask, completefilename,
EMbmScalablescreendrawingiconsFlakesmall, EMbmScalablescreendrawingiconsFlakesmall_mask);
AknIconUtils::CreateIconL(iSnowflakeBig, iSnowflakeBig_mask, completefilename,
EMbmScalablescreendrawingiconsFlakebig, EMbmScalablescreendrawingiconsFlakebig_mask);
AknIconUtils::CreateIconL(iIconSelector, iIconSelector_mask, completefilename,
EMbmScalablescreendrawingiconsSelector, EMbmScalablescreendrawingiconsSelector_mask);
AknIconUtils::CreateIconL(iIconSelector2, iIconSelector2_mask, completefilename,
EMbmScalablescreendrawingiconsStar, EMbmScalablescreendrawingiconsStar_mask);
AknIconUtils::PreserveIconData(iIconSelector2);
TSize size;
iIconExit = AknIconUtils::CreateIconL(completefilename, EMbmScalablescreendrawingiconsExit);
AknIconUtils::GetContentDimensions(iIconExit, size);
AknIconUtils::SetSize(iIconExit, size);
iIconMenu = AknIconUtils::CreateIconL(completefilename, EMbmScalablescreendrawingiconsMenu);
AknIconUtils::GetContentDimensions(iIconMenu, size);
AknIconUtils::SetSize(iIconMenu, size);
// close the file server session
iFsSession.Close();
#endif //EKA2
ScaleImage();
// Change bitmap color depths (should also check that it does not fail)
iBackground->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
iBackground2->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
iSnowflakeSmall->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
iSnowflakeBig->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
iIconSelector->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
iIconSelector2->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
iIconExit->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
iIconMenu->SetDisplayMode(OFFSCREEN_DISPLAYMODE);
SnowGenerator();
// Create Animation timer, not started yet
iPeriodic = CPeriodic::NewL(CActive::EPriorityLow);
// Create periodic timer for calculating FPS
iPeriodicFps = CPeriodic::NewL(CActive::EPriorityHigh);
// Start FPS counter (actually needed only when animation timer is running)
iPeriodicFps->Start(PERIOD_FPS, PERIOD_FPS, TCallBack(CScalableScreenDrawingEngine::Fps, this));
// Start direct screen access
iDirectScreenAccess->StartL();
}
CScalableScreenDrawingEngine::~CScalableScreenDrawingEngine()
{
if(iPeriodic)
{
iPeriodic->Cancel();
delete iPeriodic;
}
if(iPeriodicFps)
{
iPeriodicFps->Cancel();
delete iPeriodicFps;
}
if(iDirectScreenAccess)
{
iDirectScreenAccess->Cancel();
delete iDirectScreenAccess;
}
delete iBackground;
delete iBackground2;
delete iSnowflakeSmall;
delete iSnowflakeSmall_mask;
delete iSnowflakeBig;
delete iSnowflakeBig_mask;
delete iIconSelector;
delete iIconSelector_mask;
delete iIconSelector2;
delete iIconSelector2_mask;
delete iIconExit;
delete iIconMenu;
#ifdef EKA2
iFsSession.Close();
delete iIconProvider;
#endif
delete iVirtualScreen;
delete iBitmapDevice;
delete iBC;
}
TInt CScalableScreenDrawingEngine::Animate(TAny* aObj)
{
static_cast<CScalableScreenDrawingEngine*>(aObj)->DoFrame();
return 1;
}
TInt CScalableScreenDrawingEngine::Fps(TAny* aObj)
{
CScalableScreenDrawingEngine* me = static_cast<CScalableScreenDrawingEngine*>(aObj);
me->iFpsText.Format(_L("%d"), me->iFpsCounter);
me->iFpsCounter = 0;
return 1;
}
void CScalableScreenDrawingEngine::DoFrame()
{
MoveFlakes();
// Scroll background
iScrollOffset++;
if(iScrollOffset >= iPixTwipsRot.iPixelSize.iWidth)
{
iScrollOffset = 0;
}
// Scroll stars
iScrollOffsetStars++;
TSize size = iIconSelector2->SizeInPixels();
if(iScrollOffsetStars >= size.iWidth)
{
iScrollOffsetStars = 0;
}
if(iUseDoubleBuffer)
{
DrawScreenDoubleBuffer();
}
else
{
DrawScreen(iGc);
}
iDirectScreenAccess->ScreenDevice()->Update();
iFpsCounter++;
}
void CScalableScreenDrawingEngine::SnowGenerator()
{
TInt64 randomseed = User::TickCount() + 0x1f47a311;
for (TInt i=0;i<NUMBER_OF_FLAKES;i++)
{
TInt xscaler = KMaxTInt / iPixTwipsRot.iPixelSize.iWidth-50;
TInt yscaler = KMaxTInt / (iPixTwipsRot.iPixelSize.iHeight-50 / 2) - 100; //make them on top half
TInt rndx = Math::Rand(randomseed) / xscaler;
TInt rndy = Math::Rand(randomseed) / yscaler;
TInt rndsize = Math::Rand(randomseed) / (KMaxTInt / 2); //0-1
TInt rndspeed = Math::Rand(randomseed) / (KMaxTInt / 4); //0-3
iSnowflakes[i].iSpeed = rndspeed;
iSnowflakes[i].iSize = rndsize;
iSnowflakes[i].iPos.iX = rndx;
iSnowflakes[i].iPos.iY = rndy;
}
}
void CScalableScreenDrawingEngine::MoveFlakes()
{
for (TInt i=0;i<NUMBER_OF_FLAKES;i++)
{
TInt deltax = WINDSPEED;
TInt deltay = GRAVITY + iSnowflakes[i].iSpeed;
iSnowflakes[i].iPos.iX += deltax;
iSnowflakes[i].iPos.iY += deltay;
if (iSnowflakes[i].iPos.iY > iPixTwipsRot.iPixelSize.iHeight) iSnowflakes[i].iPos.iY = -70;
if (iSnowflakes[i].iPos.iX > iPixTwipsRot.iPixelSize.iWidth) iSnowflakes[i].iPos.iX = -70;
}
}
void CScalableScreenDrawingEngine::Restart(RDirectScreenAccess::TTerminationReasons /*aReason*/)
{
// Initialise DSA
TRAPD(dsaErr, iDirectScreenAccess->StartL());
if(dsaErr == KErrNone)
{
iGc = iDirectScreenAccess->Gc();
iRegion = iDirectScreenAccess->DrawingRegion();
iGc->SetClippingRegion(iRegion);
// Start animation timer
if (!iPeriodic->IsActive())
iPeriodic->Start(DELAY, DELAY, TCallBack(CScalableScreenDrawingEngine::Animate, this));
}
}
void CScalableScreenDrawingEngine::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
{
if (iPeriodic)
{
iPeriodic->Cancel();
}
}
void CScalableScreenDrawingEngine::DrawScreen(CBitmapContext* aBc) const
{
// Background
CFbsBitmap* bitmapBackground;
if(iUseVectorBackground)
{
bitmapBackground = iBackground2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -