|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : graphicsappview.cpp 00004 * Part of : Graphics 00005 * Interface : 00006 * Description : 00007 * Version : 00008 * 00009 * Copyright (c) 2004 - 2006 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================== 00014 */ 00015 00016 // INCLUDE FILES 00017 #include <GraphicsAppImages.mbg> 00018 #include <aknview.h> 00019 #include <AknUtils.h> 00020 #include <akndef.h> 00021 #include <eikapp.h> 00022 #include <eikappui.h> 00023 #include "IconFileProvider.h" 00024 #include "GraphicsAppView.h" 00025 #include "bitmapmethods.h" 00026 #include "sprite.h" 00027 00028 00029 // ============================ MEMBER FUNCTIONS =============================== 00030 00031 // ----------------------------------------------------------------------------- 00032 // CGraphicsAppView::CGraphicsAppView() 00033 // C++ default constructor can NOT contain any code, that might leave. 00034 // ----------------------------------------------------------------------------- 00035 // 00036 CGraphicsAppView::CGraphicsAppView() 00037 : iOffScreenBitmap(NULL) 00038 , iOffScreenBitmapDevice(NULL) 00039 , iOffScreenBitmapGc(NULL) 00040 { 00041 // No implementation required 00042 } 00043 00044 // ----------------------------------------------------------------------------- 00045 // CGraphicsAppView::NewL() 00046 // Two-phased constructor. 00047 // ----------------------------------------------------------------------------- 00048 // 00049 CGraphicsAppView* CGraphicsAppView::NewL( const TRect& aRect ) 00050 { 00051 CGraphicsAppView* self = NewLC( aRect ); 00052 CleanupStack::Pop( self ); 00053 return self; 00054 } 00055 00056 // ----------------------------------------------------------------------------- 00057 // CGraphicsAppView::NewLC() 00058 // Two-phased constructor. 00059 // ----------------------------------------------------------------------------- 00060 // 00061 CGraphicsAppView* CGraphicsAppView::NewLC( const TRect& aRect ) 00062 { 00063 CGraphicsAppView* self = new ( ELeave ) CGraphicsAppView; 00064 CleanupStack::PushL( self ); 00065 self->ConstructL( aRect ); 00066 return self; 00067 } 00068 00069 // ----------------------------------------------------------------------------- 00070 // CGraphicsAppView::ConstructL() 00071 // Symbian 2nd phase constructor can leave. 00072 // ----------------------------------------------------------------------------- 00073 // 00074 void CGraphicsAppView::ConstructL( const TRect& aRect ) 00075 { 00076 // Create a window for this application view 00077 CreateWindowL(); 00078 00079 // Set the window's size and position 00080 SetRect( aRect ); 00081 00082 // Load in the bitmap images from the multi bitmap file 00083 00084 // create and open file server session 00085 User::LeaveIfError(iFsSession.Connect()); 00086 iFsSession.ShareProtected(); 00087 TFileName filePath; 00088 00089 // set path of the bitmap file 00090 User::LeaveIfError(iFsSession.PrivatePath(filePath)); 00091 00092 // append the MBM file name to the private path 00093 filePath.Append(KMultiBitmapFilename); 00094 00095 // insert the drive to the private path 00096 TParsePtrC parse((CEikonEnv::Static()->EikAppUi()->Application())->AppFullName()); 00097 filePath.Insert(0, parse.Drive()); 00098 00099 // with the help of icon-provider AknIconUtils can get get the file 00100 // from this application's private folder 00101 iIconProvider = CIconFileProvider::NewL( iFsSession, filePath ); 00102 00103 iBackgroundImage = AknIconUtils::CreateIconL(*iIconProvider, 00104 EMbmGraphicsappimagesImage1); 00105 // Load sprite bitmap and mask with a single call 00106 AknIconUtils::CreateIconL(iSpriteImage, 00107 iSpriteMask, 00108 *iIconProvider, 00109 EMbmGraphicsappimagesImage2, 00110 EMbmGraphicsappimagesImage2_mask); 00111 00112 // Load svg.-image and mask with a single call 00113 AknIconUtils::CreateIconL(iSpriteImage2, 00114 iSpriteMask2, 00115 *iIconProvider, 00116 EMbmGraphicsappimagesStar, 00117 EMbmGraphicsappimagesStar_mask); 00118 // This call preserves SVG-T vector data in memory so that the data is not needed to 00119 // load from the resource when the icon is subsequently scaled. 00120 AknIconUtils::PreserveIconData(iSpriteImage2); 00121 00122 ScaleImages(); 00123 00124 // Create a periodic timer but don't start it yet 00125 iPeriodicTimer = CPeriodic::NewL( CActive::EPriorityStandard ); 00126 00127 // Create the array of sprite pointers 00128 iSprites = new ( ELeave ) CArrayPtrFlat<CSprite> ( 1 ); 00129 00130 SetUpSpritesL(); 00131 00132 // Activate the window, which makes it ready to be drawn 00133 ActivateL(); 00134 } 00135 00136 // ----------------------------------------------------------------------------- 00137 // CGraphicsAppView::~CGraphicsAppView() 00138 // Destructor. 00139 // ----------------------------------------------------------------------------- 00140 // 00141 CGraphicsAppView::~CGraphicsAppView() 00142 { 00143 if ( iPeriodicTimer ) 00144 { 00145 // Stop the periodic timer 00146 iPeriodicTimer->Cancel(); 00147 } 00148 delete iPeriodicTimer; 00149 iPeriodicTimer = NULL; 00150 00151 delete iOffScreenBitmapGc; 00152 iOffScreenBitmapGc = NULL; 00153 00154 delete iOffScreenBitmapDevice; 00155 iOffScreenBitmapDevice = NULL; 00156 00157 delete iOffScreenBitmap; 00158 iOffScreenBitmap = NULL; 00159 00160 delete iBackgroundImage; 00161 iBackgroundImage = NULL; 00162 00163 if ( iSprites ) 00164 { 00165 iSprites->ResetAndDestroy(); 00166 delete iSprites; 00167 iSprites = NULL; 00168 } 00169 00170 delete iSpriteImage; 00171 iSpriteImage = NULL; 00172 00173 delete iSpriteMask; 00174 iSpriteMask = NULL; 00175 00176 delete iSpriteImage2; 00177 iSpriteImage2 = NULL; 00178 00179 delete iSpriteMask2; 00180 iSpriteMask2 = NULL; 00181 00182 iFsSession.Close(); 00183 delete iIconProvider; 00184 } 00185 00186 // ----------------------------------------------------------------------------- 00187 // CGraphicsAppView::SetUpSpritesL() 00188 // Sets up sprites. 00189 // ----------------------------------------------------------------------------- 00190 // 00191 void CGraphicsAppView::SetUpSpritesL() 00192 { 00193 ASSERT( iSpriteImage ); 00194 ASSERT( iSpriteImage2 ); 00195 ASSERT( iSprites ); 00196 00197 // Create the sprites 00198 CSprite* sprite = NULL; 00199 00200 sprite = CSprite::NewLC( KInitialXSpeed, 00201 KInitialYSpeed+1, 00202 Rect().iTl, 00203 iSpriteImage, 00204 iSpriteMask ); 00205 iSprites->AppendL( sprite ); 00206 CleanupStack::Pop( sprite ); 00207 00208 sprite = CSprite::NewLC( -KInitialXSpeed, 00209 -KInitialYSpeed, 00210 Rect().iBr - iSpriteImage->SizeInPixels(), 00211 iSpriteImage, 00212 iSpriteMask ); 00213 iSprites->AppendL( sprite ); 00214 CleanupStack::Pop( sprite ); 00215 00216 sprite = CSprite::NewLC( KInitialXSpeed, 00217 -KInitialYSpeed+1, 00218 Rect().iTl + iSpriteImage2->SizeInPixels(), 00219 iSpriteImage2, 00220 iSpriteMask2 ); 00221 iSprites->AppendL( sprite ); 00222 CleanupStack::Pop( sprite ); 00223 } 00224 00225 // ----------------------------------------------------------------------------- 00226 // CGraphicsAppView::Draw() 00227 // Draws the display. 00228 // ----------------------------------------------------------------------------- 00229 // 00230 void CGraphicsAppView::Draw( const TRect& /*aRect*/ ) const 00231 { 00232 // The system GC will already be activated when this function is called 00233 // by the framework 00234 UpdateDisplay(); 00235 } 00236 00237 // ----------------------------------------------------------------------------- 00238 // Handle resource change event, here it is the event that occurs 00239 // when screen mode changes. 00240 // ----------------------------------------------------------------------------- 00241 void CGraphicsAppView::HandleResourceChange(TInt aType, const TRect& aRect) 00242 { 00243 CCoeControl::HandleResourceChange(aType); 00244 00245 if( aType == KEikDynamicLayoutVariantSwitch) 00246 { 00247 SetRect(aRect); 00248 ScaleImages(); 00249 00250 // adjust sprite positions if needed (new resolution may have caused 00251 // current position to be out of bounds 00252 for ( TInt count = 0; count < iSprites->Count(); ++count ) 00253 { 00254 CSprite *sprite = iSprites->At(count); 00255 sprite->Adjust( sprite->Image()->SizeInPixels(), Rect() ); 00256 } 00257 } 00258 } 00259 00260 00261 // ----------------------------------------------------------------------------- 00262 // Scales background and sprite images according to current layout. 00263 // ----------------------------------------------------------------------------- 00264 void CGraphicsAppView::ScaleImages() 00265 { 00266 TInt x = Size().iWidth; 00267 TInt y = Size().iHeight; 00268 00269 AknIconUtils::SetSize(iBackgroundImage, 00270 Size(), 00271 EAspectRatioNotPreserved); 00272 00273 // target size for the sprites is ~40x40 in 352x416 resolution. 00274 // orientation does not affect on scaling. 00275 AknIconUtils::SetSize(iSpriteImage, TSize((x+y)/19, (x+y)/19)); 00276 AknIconUtils::SetSize(iSpriteImage2, TSize((x+y)/19, (x+y)/19)); 00277 00278 delete iOffScreenBitmapGc; 00279 iOffScreenBitmapGc = NULL; 00280 delete iOffScreenBitmapDevice; 00281 iOffScreenBitmapDevice = NULL; 00282 delete iOffScreenBitmap; 00283 iOffScreenBitmap = NULL; 00284 00285 // Create the off screen bitmap and device / gc 00286 iOffScreenBitmap = NBitmapMethods::CreateBitmapL( Size(), KColourDepth ); 00287 iOffScreenBitmapDevice = NBitmapMethods::CreateBitmapDeviceL( *iOffScreenBitmap ); 00288 iOffScreenBitmapGc = NBitmapMethods::CreateGraphicsContextL( *iOffScreenBitmapDevice ); 00289 } 00290 00291 // ----------------------------------------------------------------------------- 00292 // CGraphicsAppView::UpdateDisplay() 00293 // Updates the display. 00294 // ----------------------------------------------------------------------------- 00295 // 00296 void CGraphicsAppView::UpdateDisplay() const 00297 { 00298 CWindowGc& gc = SystemGc(); 00299 00300 if ( !iUsingOffScreenBitmap ) 00301 { 00302 // Blit the background image onto the screen at the top left position 00303 gc.BitBlt( Rect().iTl,iBackgroundImage ); 00304 00305 // Blit the sprites on top of it using their mask to retain 00306 // the background where necessary 00307 for ( TInt count = 0; count<iSprites->Count(); ++count ) 00308 { 00309 CSprite *sprite = iSprites->At(count); 00310 TRect sourceRect( TPoint( 0,0 ), sprite->Image()->SizeInPixels() ); 00311 gc.BitBltMasked( sprite->Position(), 00312 sprite->Image(), 00313 sourceRect, 00314 sprite->Mask(), 00315 ETrue ); 00316 } 00317 } 00318 else 00319 { 00320 // Blit the background image onto the off screen bitmap 00321 // at the top left position 00322 iOffScreenBitmapGc->BitBlt( TPoint( 0,0 ), iBackgroundImage ); 00323 00324 // Blit the sprites on top of it using its mask to retain 00325 // the background where necessary 00326 for ( TInt count = 0; count<iSprites->Count(); ++count ) 00327 { 00328 CSprite *sprite = iSprites->At(count); 00329 NBitmapMethods::BitBltMaskedEntireBitmap( *iOffScreenBitmapGc, 00330 sprite->Position(), 00331 *(sprite->Image()), 00332 *(sprite->Mask())); 00333 } 00334 00335 // Blit the offscreen image onto the screen at the top left position 00336 gc.BitBlt( Rect().iTl,iOffScreenBitmap ); 00337 } 00338 } 00339 00340 // ----------------------------------------------------------------------------- 00341 // CGraphicsAppView::StopDemo() 00342 // Stops the balls. 00343 // ----------------------------------------------------------------------------- 00344 // 00345 void CGraphicsAppView::StopDemo() 00346 { 00347 // Stop the timer if it is active 00348 if ( iPeriodicTimer->IsActive() ) 00349 { 00350 iPeriodicTimer->Cancel(); 00351 } 00352 } 00353 00354 // ----------------------------------------------------------------------------- 00355 // CGraphicsAppView::StartOffScreenDemo() 00356 // Start Off Screen Demo. 00357 // ----------------------------------------------------------------------------- 00358 // 00359 void CGraphicsAppView::StartOffScreenDemo() 00360 { 00361 iUsingOffScreenBitmap = ETrue; 00362 StartTimer(); 00363 } 00364 00365 // ----------------------------------------------------------------------------- 00366 // CGraphicsAppView::StartNoOffScreenDemo() 00367 // Start No Off Screen Demo. 00368 // ----------------------------------------------------------------------------- 00369 // 00370 void CGraphicsAppView::StartNoOffScreenDemo() 00371 { 00372 iUsingOffScreenBitmap = EFalse; 00373 StartTimer(); 00374 } 00375 00376 // ----------------------------------------------------------------------------- 00377 // CGraphicsAppView::StartTimer() 00378 // Start Timer. 00379 // ----------------------------------------------------------------------------- 00380 // 00381 void CGraphicsAppView::StartTimer() 00382 { 00383 // If the timer is not already running, start it 00384 if ( !iPeriodicTimer->IsActive() ) 00385 { 00386 iPeriodicTimer->Start(1, 1, TCallBack( CGraphicsAppView::Period, this)); 00387 } 00388 00389 } 00390 00391 // ----------------------------------------------------------------------------- 00392 // CGraphicsAppView::DoPeriodTask() 00393 // Move the sprites. 00394 // ----------------------------------------------------------------------------- 00395 // 00396 void CGraphicsAppView::DoPeriodTask() 00397 { 00398 for ( TInt count = 0; count < iSprites->Count(); ++count ) 00399 { 00400 CSprite *sprite = iSprites->At(count); 00401 sprite->Move( sprite->Image()->SizeInPixels(), Rect() ); 00402 } 00403 00404 // Update the screen 00405 CWindowGc& gc = SystemGc(); 00406 gc.Activate( *DrawableWindow() ); 00407 UpdateDisplay(); 00408 gc.Deactivate(); 00409 } 00410 00411 // ----------------------------------------------------------------------------- 00412 // CGraphicsAppView::Period() 00413 // This function is called by the periodic timer. 00414 // ----------------------------------------------------------------------------- 00415 // 00416 TInt CGraphicsAppView::Period( TAny* aPtr ) 00417 { 00418 ( static_cast<CGraphicsAppView*>( aPtr ) )->DoPeriodTask(); 00419 // Returning a value of TRUE indicates the callback should be done again 00420 return ETrue; 00421 } 00422 00423 // End of File
|
© Nokia 2009 |