examples/SFExamples/SkeletonS60/src/SkeletonAppView.cpp

00001 // 
00002 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of the License "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 // 
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 // 
00012 // Contributors:
00013 // 
00014 // Description:  Application view implementation
00015 // 
00016 // 
00017 
00018 
00019 // INCLUDE FILES
00020 #include <coemain.h>
00021 #include "SkeletonAppView.h"
00022 
00023 const TInt KOneSecondInMicroSeconds = 1000000;
00024 const TInt KFramesPerSecond = KOneSecondInMicroSeconds/30; // 30 fps
00025 const TTimeIntervalSeconds KInactivityTimeout = 10; // 10 seconds user inactivity timeout
00026 
00027 // Key identifiers
00028 const TUint KControllerUp       = 0x00000001;
00029 const TUint KControllerDown     = 0x00000002;
00030 const TUint KControllerRight    = 0x00000004;
00031 const TUint KControllerLeft     = 0x00000008;
00032 const TUint KControllerCentre   = 0x00000010;
00033 const TUint KKey0                       = 0x00000020;
00034 const TUint KKey1                       = 0x00000040;
00035 const TUint KKey2                       = 0x00000080;
00036 const TUint KKey3                       = 0x00000100;
00037 const TUint KKey4                       = 0x00000200;
00038 const TUint KKey5                       = 0x00000400;
00039 const TUint KKey6                       = 0x00000800;
00040 const TUint KKey7                       = 0x00001000;
00041 const TUint KKey8                       = 0x00002000;
00042 const TUint KKey9                       = 0x00004000;
00043 
00044 _LIT(K1, "1");
00045 _LIT(K2, "2");
00046 _LIT(K3, "3");
00047 _LIT(K4, "4");
00048 _LIT(K5, "5");
00049 _LIT(K6, "6");
00050 _LIT(K7, "7");
00051 _LIT(K8, "8");
00052 _LIT(K9, "9");
00053 _LIT(K0, "0");
00054 
00055 _LIT(KUp, "U");
00056 _LIT(KDown, "D");
00057 _LIT(KLeft, "L");
00058 _LIT(KRight, "R");
00059 _LIT(KCentre, "*");
00060 _LIT(KFired, "Fired!");
00061 
00062 class CInactivityTimer : public CTimer
00063         {
00064 public:
00065         static CInactivityTimer* NewL(CSkeletonAppView& aAppView);
00066         void StartInactivityMonitor(TTimeIntervalSeconds aSeconds);
00067 protected:
00068         CInactivityTimer(CSkeletonAppView& aAppView);
00069         virtual void RunL();
00070 private:
00071         CSkeletonAppView& iAppView;
00072         TTimeIntervalSeconds iTimeout;
00073         };
00074         
00075 CInactivityTimer::CInactivityTimer(CSkeletonAppView& aAppView)
00076 : CTimer(EPriorityLow), iAppView(aAppView)
00077         {
00078         CActiveScheduler::Add(this);
00079         }
00080 
00081 CInactivityTimer* CInactivityTimer::NewL(CSkeletonAppView& aAppView)
00082         {
00083         CInactivityTimer* me = new (ELeave) CInactivityTimer(aAppView);
00084         CleanupStack::PushL(me);
00085         me->ConstructL();
00086         CleanupStack::Pop(me);
00087         return (me);
00088         }
00089 
00090 void CInactivityTimer::StartInactivityMonitor(TTimeIntervalSeconds aSeconds)
00091         {
00092         if (!IsActive())
00093                 {
00094                 iTimeout = aSeconds;
00095                 Inactivity(iTimeout);
00096                 }
00097         }
00098 
00099 void CInactivityTimer::RunL()
00100         {// Game has been inactive - no user input for iTimeout seconds
00101         iAppView.SetPausedDisplay(ETrue); // Displays the pause screen
00102         iAppView.StopHeartbeat(); // Stops the game loop
00103         }
00104 
00105 // -----------------------------------------------------------------------------
00106 // CSkeletonAppView::NewL()
00107 // Two-phased constructor.
00108 // -----------------------------------------------------------------------------
00109 //
00110 CSkeletonAppView* CSkeletonAppView::NewL( const TRect& aRect )
00111         {
00112         CSkeletonAppView* self = CSkeletonAppView::NewLC( aRect );
00113         CleanupStack::Pop( self );
00114         return self;
00115         }
00116 
00117 // -----------------------------------------------------------------------------
00118 // CSkeletonAppView::NewLC()
00119 // Two-phased constructor.
00120 // -----------------------------------------------------------------------------
00121 //
00122 CSkeletonAppView* CSkeletonAppView::NewLC( const TRect& aRect )
00123         {
00124         CSkeletonAppView* self = new ( ELeave ) CSkeletonAppView;
00125         CleanupStack::PushL( self );
00126         self->ConstructL( aRect );
00127         return self;
00128         }
00129 
00130 // -----------------------------------------------------------------------------
00131 // CSkeletonAppView::ConstructL()
00132 // Symbian 2nd phase constructor can leave.
00133 // -----------------------------------------------------------------------------
00134 //
00135 void CSkeletonAppView::ConstructL( const TRect& aRect )
00136         {
00137         // Create a window for this application view
00138         CreateWindowL();
00139 
00140         // Set the windows size
00141         SetRect( aRect );
00142         
00143         // Creates timer for game loop.
00144         iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
00145         iInactivity = CInactivityTimer::NewL(*this);
00146 
00147         // Activate the window, which makes it ready to be drawn and starts the heartbeat
00148         ActivateL();
00149         
00150         iStart.HomeTime();
00151         }
00152 
00153 void CSkeletonAppView::StartHeartbeat()
00154         {
00155         SetPausedDisplay(EFalse);
00156         
00157         if (!iPeriodicTimer->IsActive())
00158                 iPeriodicTimer->Start(KFramesPerSecond, KFramesPerSecond, TCallBack(CSkeletonAppView::Tick, this));             
00159                 
00160         iInactivity->StartInactivityMonitor(KInactivityTimeout); // Notify if inactive for 30 seconds   
00161         }
00162 
00163 void CSkeletonAppView::StopHeartbeat()
00164         {
00165         iPeriodicTimer->Cancel();
00166         }
00167 
00168 
00169 void CSkeletonAppView::SetPausedDisplay(TBool aPaused) 
00170         {
00171         iPauseDisplay = aPaused;
00172         DrawNow();
00173         }
00174 // -----------------------------------------------------------------------------
00175 // CSkeletonAppView::CSkeletonAppView()
00176 // C++ default constructor can NOT contain any code, that might leave.
00177 // -----------------------------------------------------------------------------
00178 //
00179 CSkeletonAppView::CSkeletonAppView()
00180         {
00181         // No implementation required
00182         }
00183 
00184 
00185 // -----------------------------------------------------------------------------
00186 // CSkeletonAppView::~CSkeletonAppView()
00187 // Destructor.
00188 // -----------------------------------------------------------------------------
00189 CSkeletonAppView::~CSkeletonAppView()
00190         {
00191         StopHeartbeat();
00192         delete iPeriodicTimer;
00193         
00194         iInactivity->Cancel();
00195         delete iInactivity;
00196         }
00197 
00198 // --------------------------------------------------------------------------
00199 // FocusChanged
00200 // --------------------------------------------------------------------------
00201 void CSkeletonAppView::FocusChanged(TDrawNow aDrawNow)
00202         {
00203         if (IsFocused())
00204                 {// Focus gained
00205                 StartHeartbeat();
00206                 }
00207         else
00208                 {// Focus lost
00209                 if (iPeriodicTimer->IsActive())
00210                         {
00211                         StopHeartbeat();
00212                         }
00213                 //      Save game data here if necessary
00214                 }
00215         
00216         if(aDrawNow)
00217                 DrawNow();
00218         }
00219 
00220 // --------------------------------------------------------------------------
00221 // Tick(TAny *aPtr)
00222 // --------------------------------------------------------------------------
00223 TInt CSkeletonAppView::Tick(TAny* aCallback)
00224         {
00225         ASSERT(aCallback);
00226         static_cast<CSkeletonAppView*>(aCallback)->GameLoop();
00227 
00228         // Returning a value of ETrue indicates the callback should be done again.
00229         return ETrue;
00230         }
00231 
00232 // --------------------------------------------------------------------------
00233 // GameLoop()
00234 // --------------------------------------------------------------------------
00235 void CSkeletonAppView::GameLoop()
00236         {
00237         // Update frame measurement.
00238         iStop.HomeTime();
00239         iDuration = iStop.Int64() - iStart.Int64();
00240         ++iFrames;
00241 
00242         if(iDuration >= KOneSecondInMicroSeconds) // if Duration exceeds one second
00243                 { 
00244                 _LIT(KFramesPerSecond, "%d fps");
00245                 iFpsMessage.Format(KFramesPerSecond, iFrames);
00246                 iFrames = 0;
00247                 iStart = iStop;
00248                 }
00249         
00250                 DrawNow();
00251                 
00252         if (iFired)
00253                 {// Centre key was pressed, or is held down and firing events are occurring repeatedly
00254                 //  Peform action on firing, e.g. play a sound, flash the screen, here, print to debug for illustration
00255                 RDebug::Print(KFired);
00256                 iFired = EFalse;
00257                 }
00258         }
00259 
00260 TKeyResponse CSkeletonAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
00261         {
00262         TKeyResponse response = EKeyWasConsumed;
00263         TUint input = 0x00000000;
00264 
00265         switch (aKeyEvent.iScanCode)
00266                 {
00267                 case EStdKeyUpArrow:    
00268                         input = KControllerUp;
00269                         break;
00270                 case EStdKeyDownArrow: 
00271                         input = KControllerDown;
00272                         break;
00273                 case EStdKeyLeftArrow: 
00274                         input = KControllerLeft;
00275                         break;
00276                 case EStdKeyRightArrow: 
00277                         input = KControllerRight;
00278                         break;
00279                 case EStdKeyDevice3:  // This is the "firing" key
00280                         input = KControllerCentre;
00281                         if (EEventKey == aType)
00282                                 {
00283                                 iFired = ETrue;
00284                                 }
00285                         break;
00286                 case '0': 
00287                         input = KKey0;
00288                         break;
00289                 case '1': 
00290                         input = KKey1;
00291                         break;
00292                 case '2': 
00293                         input = KKey2;
00294                         break;
00295                 case '3': 
00296                         input = KKey3;
00297                         break;
00298                 case '4': 
00299                         input = KKey4;
00300                         break;
00301                 case '5': 
00302                         input = KKey5;
00303                         break;
00304                 case '6': 
00305                         input = KKey6;
00306                         break;
00307                 case '7': 
00308                         input = KKey7;
00309                         break;
00310                 case '8': 
00311                         input = KKey8;
00312                         break;
00313                 case '9': 
00314                         input = KKey9;
00315                         break;
00316 
00317                 default:
00318                         response = EKeyWasNotConsumed;// Keypress was not handled
00319                         break;
00320                 }
00321         
00322         if (EEventKeyDown == aType)
00323                 {
00324                 iKeyState |= input; // set bitmask
00325                 }
00326         else if (EEventKeyUp == aType)
00327                 {
00328                 iKeyState &= ~input; // clear bitmask
00329                 }
00330         
00331     return (response);
00332         }
00333 
00334 // -----------------------------------------------------------------------------
00335 // CSkeletonAppView::Draw()
00336 // Draws the display.
00337 // -----------------------------------------------------------------------------
00338 //
00339 void CSkeletonAppView::Draw( const TRect& /*aRect*/ ) const
00340         {
00341         // Get the standard graphics context
00342         CWindowGc& gc = SystemGc();
00343 
00344         gc.SetPenStyle(CGraphicsContext::ENullPen);     
00345         gc.UseFont(iCoeEnv->NormalFont());
00346         gc.SetPenColor(KRgbGray);
00347         
00348         // Gets the control's extent
00349         TRect drawRect( Rect());        
00350         TInt width = drawRect.Width();
00351         TInt height = drawRect.Height();
00352         
00353         gc.Clear( drawRect );
00354         
00355         if (!iPauseDisplay)
00356                 {               
00357                 gc.DrawText(iFpsMessage, TPoint(0, 20));
00358                 
00359                 gc.DrawText(KUp, TPoint(width/2, height/8));
00360                 gc.DrawText(KLeft, TPoint(width/4, height/4));
00361                 gc.DrawText(KCentre, TPoint(width/2, height/4));
00362                 gc.DrawText(KRight, TPoint(3*width/4, height/4));
00363                 gc.DrawText(KDown, TPoint(width/2, 3*height/8));
00364                 
00365                 gc.DrawText(K1, TPoint(width/4, height/2));
00366                 gc.DrawText(K2, TPoint(width/2, height/2));
00367                 gc.DrawText(K3, TPoint(3*width/4, height/2));
00368                 gc.DrawText(K4, TPoint(width/4, 5*height/8));
00369                 gc.DrawText(K5, TPoint(width/2, 5*height/8));
00370                 gc.DrawText(K6, TPoint(3*width/4, 5*height/8));
00371                 gc.DrawText(K7, TPoint(width/4, 3*height/4));
00372                 gc.DrawText(K8, TPoint(width/2, 3*height/4));
00373                 gc.DrawText(K9, TPoint(3*width/4, 3*height/4));
00374                 gc.DrawText(K0, TPoint(width/2, 7*height/8));
00375                 
00376                 
00377                 gc.SetPenColor(KRgbRed);
00378                 
00379                 if (iKeyState & KControllerUp)
00380                         gc.DrawText(KUp, TPoint(width/2, height/8));
00381                 
00382                 if (iKeyState & KControllerLeft)
00383                         gc.DrawText(KLeft, TPoint(width/4, height/4));
00384                 
00385                 if (iKeyState & KControllerCentre)
00386                                 gc.DrawText(KCentre, TPoint(width/2, height/4));
00387                 
00388                 if (iKeyState & KControllerRight)
00389                         gc.DrawText(KRight, TPoint(3*width/4, height/4));
00390                 
00391                 if (iKeyState & KControllerDown)
00392                         gc.DrawText(KDown, TPoint(width/2, 3*height/8));
00393                 
00394                 if (iKeyState & KKey1)
00395                         gc.DrawText(K1, TPoint(width/4, height/2));
00396                 
00397                 if (iKeyState & KKey2)
00398                         gc.DrawText(K2, TPoint(width/2, height/2));
00399                 
00400                 if (iKeyState & KKey3)
00401                         gc.DrawText(K3, TPoint(3*width/4, height/2));
00402                 
00403                 if (iKeyState & KKey4)
00404                         gc.DrawText(K4, TPoint(width/4, 5*height/8));
00405                 
00406                 if (iKeyState & KKey5)
00407                         gc.DrawText(K5, TPoint(width/2, 5*height/8));
00408         
00409                 if (iKeyState & KKey6)
00410                         gc.DrawText(K6, TPoint(3*width/4, 5*height/8));
00411                 
00412                 if (iKeyState & KKey7)
00413                         gc.DrawText(K7, TPoint(width/4, 3*height/4));
00414                 
00415                 if (iKeyState & KKey8)
00416                         gc.DrawText(K8, TPoint(width/2, 3*height/4));
00417                 
00418                 if (iKeyState & KKey9)
00419                         gc.DrawText(K9, TPoint(3*width/4, 3*height/4));
00420                 
00421                 if (iKeyState & KKey0)
00422                         gc.DrawText(K0, TPoint(width/2, 7*height/8));
00423                 }
00424         else
00425                 {//Game is paused
00426                 _LIT(KPaused, "G A M E  P A U S E D");
00427                 gc.DrawText(KPaused, TPoint(10, height/2));
00428                 
00429                 }
00430         }
00431 
00432 // -----------------------------------------------------------------------------
00433 // CSkeletonAppView::SizeChanged()
00434 // Called by framework when the view size is changed.
00435 // -----------------------------------------------------------------------------
00436 //
00437 void CSkeletonAppView::SizeChanged()
00438         {  
00439         DrawNow();
00440         }
00441 // End of File

Generated by  doxygen 1.6.2