00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <coemain.h>
00021 #include "SkeletonAppView.h"
00022
00023 const TInt KOneSecondInMicroSeconds = 1000000;
00024 const TInt KFramesPerSecond = KOneSecondInMicroSeconds/30;
00025 const TTimeIntervalSeconds KInactivityTimeout = 10;
00026
00027
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 {
00101 iAppView.SetPausedDisplay(ETrue);
00102 iAppView.StopHeartbeat();
00103 }
00104
00105
00106
00107
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
00119
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
00132
00133
00134
00135 void CSkeletonAppView::ConstructL( const TRect& aRect )
00136 {
00137
00138 CreateWindowL();
00139
00140
00141 SetRect( aRect );
00142
00143
00144 iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
00145 iInactivity = CInactivityTimer::NewL(*this);
00146
00147
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);
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
00176
00177
00178
00179 CSkeletonAppView::CSkeletonAppView()
00180 {
00181
00182 }
00183
00184
00185
00186
00187
00188
00189 CSkeletonAppView::~CSkeletonAppView()
00190 {
00191 StopHeartbeat();
00192 delete iPeriodicTimer;
00193
00194 iInactivity->Cancel();
00195 delete iInactivity;
00196 }
00197
00198
00199
00200
00201 void CSkeletonAppView::FocusChanged(TDrawNow aDrawNow)
00202 {
00203 if (IsFocused())
00204 {
00205 StartHeartbeat();
00206 }
00207 else
00208 {
00209 if (iPeriodicTimer->IsActive())
00210 {
00211 StopHeartbeat();
00212 }
00213
00214 }
00215
00216 if(aDrawNow)
00217 DrawNow();
00218 }
00219
00220
00221
00222
00223 TInt CSkeletonAppView::Tick(TAny* aCallback)
00224 {
00225 ASSERT(aCallback);
00226 static_cast<CSkeletonAppView*>(aCallback)->GameLoop();
00227
00228
00229 return ETrue;
00230 }
00231
00232
00233
00234
00235 void CSkeletonAppView::GameLoop()
00236 {
00237
00238 iStop.HomeTime();
00239 iDuration = iStop.Int64() - iStart.Int64();
00240 ++iFrames;
00241
00242 if(iDuration >= KOneSecondInMicroSeconds)
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 {
00254
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:
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;
00319 break;
00320 }
00321
00322 if (EEventKeyDown == aType)
00323 {
00324 iKeyState |= input;
00325 }
00326 else if (EEventKeyUp == aType)
00327 {
00328 iKeyState &= ~input;
00329 }
00330
00331 return (response);
00332 }
00333
00334
00335
00336
00337
00338
00339 void CSkeletonAppView::Draw( const TRect& ) const
00340 {
00341
00342 CWindowGc& gc = SystemGc();
00343
00344 gc.SetPenStyle(CGraphicsContext::ENullPen);
00345 gc.UseFont(iCoeEnv->NormalFont());
00346 gc.SetPenColor(KRgbGray);
00347
00348
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 {
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
00434
00435
00436
00437 void CSkeletonAppView::SizeChanged()
00438 {
00439 DrawNow();
00440 }
00441