00001 /* 00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 00003 00004 Redistribution and use in source and binary forms, with or without 00005 modification, are permitted provided that the following conditions are met: 00006 00007 * Redistributions of source code must retain the above copyright notice, this 00008 list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright notice, 00010 this list of conditions and the following disclaimer in the documentation 00011 and/or other materials provided with the distribution. 00012 * Neither the name of Nokia Corporation nor the names of its contributors 00013 may be used to endorse or promote products derived from this software 00014 without specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00027 Description: 00028 */ 00029 00030 #include "rpsgameengine.h" 00031 #include "common.hrh" 00032 00033 const TInt KPeriod = KOneSecondInMicroSeconds/30; 00034 const TTimeIntervalSeconds KInactivityTimeout = 30; // 30 seconds user inactivity timeout 00035 00036 class CInactivityTimer : public CTimer 00037 { 00038 public: 00039 static CInactivityTimer* NewL(CRpsGameEngine& aGameEngine); 00040 void StartInactivityMonitor(TTimeIntervalSeconds aSeconds); 00041 protected: 00042 CInactivityTimer(CRpsGameEngine& aRpsGameEngine); 00043 virtual void RunL(); 00044 private: 00045 CRpsGameEngine& iGameEngine; 00046 TTimeIntervalSeconds iTimeout; 00047 }; 00048 00049 CInactivityTimer::CInactivityTimer(CRpsGameEngine& aRpsGameEngine) 00050 : CTimer(EPriorityLow), iGameEngine(aRpsGameEngine) 00051 { 00052 CActiveScheduler::Add(this); 00053 } 00054 00055 CInactivityTimer* CInactivityTimer::NewL(CRpsGameEngine& aRpsGameEngine) 00056 { 00057 CInactivityTimer* me = new (ELeave) CInactivityTimer(aRpsGameEngine); 00058 CleanupStack::PushL(me); 00059 me->ConstructL(); 00060 CleanupStack::Pop(me); 00061 return (me); 00062 } 00063 00064 void CInactivityTimer::StartInactivityMonitor(TTimeIntervalSeconds aSeconds) 00065 { 00066 if (!IsActive()) 00067 { 00068 iTimeout = aSeconds; 00069 Inactivity(iTimeout); 00070 } 00071 } 00072 00073 void CInactivityTimer::RunL() 00074 {// Game has been inactive - no user input for iTimeout seconds 00075 iGameEngine.StopHeartbeat(ETrue); // Stops the game loop 00076 } 00077 00078 00079 // ============================================================================ 00080 // CRpsGameEngine 00081 // ============================================================================ 00082 CRpsGameEngine* CRpsGameEngine::NewL(MEngineObserver& aObs) 00083 { 00084 CRpsGameEngine* me = new (ELeave) CRpsGameEngine(aObs); 00085 CleanupStack::PushL(me); 00086 me->ConstructL(); 00087 CleanupStack::Pop(me); 00088 return (me); 00089 } 00090 00091 /* 00092 ============================================================================ 00093 CRpsGameEngine's second phase constructor 00094 ============================================================================ 00095 */ 00096 void CRpsGameEngine::ConstructL() 00097 { 00098 // Game screen manager 00099 iGameScreenMgr = CGameScreenManager::NewL(*this); 00100 // Create timer for game loop. 00101 iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard); 00102 // Create the inactivity timer 00103 iInactivity = CInactivityTimer::NewL(*this); 00104 } 00105 00106 /* 00107 ============================================================================ 00108 CRpsGameEngine's constructor 00109 ============================================================================ 00110 */ 00111 CRpsGameEngine::CRpsGameEngine(MEngineObserver& aObs) 00112 : iObs(aObs) 00113 {} 00114 00115 /* 00116 ============================================================================ 00117 CRpsGameEngine's destructor 00118 ============================================================================ 00119 */ 00120 CRpsGameEngine::~CRpsGameEngine() 00121 { 00122 StopHeartbeat(); 00123 delete iPeriodicTimer; 00124 00125 if (iInactivity) 00126 { 00127 iInactivity->Cancel(); 00128 delete iInactivity; 00129 } 00130 00131 delete iGameScreenMgr; 00132 } 00133 00134 /* 00135 ============================================================================ 00136 Redraw the view every KPeriod and start monitoring the player inactivity. 00137 ============================================================================ 00138 */ 00139 void CRpsGameEngine::StartHeartbeat() 00140 { 00141 if (!iPeriodicTimer->IsActive()) 00142 iPeriodicTimer->Start(KPeriod, KPeriod, TCallBack(CRpsGameEngine::Tick, this)); 00143 00144 iInactivity->StartInactivityMonitor(KInactivityTimeout); // Notify if inactive for KInactivityTimeout 00145 } 00146 00147 /* 00148 ============================================================================ 00149 Called when the game loses focus, closes or inactivity timeout occurs 00150 Called by the CRpsDestructor, so iPeriodicTimer pointer must be verified before use 00151 ============================================================================ 00152 */ 00153 void CRpsGameEngine::StopHeartbeat(TBool aTimedOut /*=EFalse*/) 00154 { 00155 if (aTimedOut) // Only pause the game if a timeout 00156 { 00157 PauseGame(); // Pause the game 00158 } 00159 00160 if (iPeriodicTimer) 00161 { 00162 if (iPeriodicTimer->IsActive()) 00163 { 00164 iPeriodicTimer->Cancel(); 00165 } 00166 } 00167 } 00168 00169 /* 00170 ============================================================================ 00171 Called by framework when application focus changes 00172 ============================================================================ 00173 */ 00174 void CRpsGameEngine::FocusChanged(TBool aFocus) 00175 { 00176 if (aFocus) 00177 {// Focus gained 00178 StartHeartbeat(); 00179 } 00180 else 00181 {// Focus lost 00182 StopHeartbeat(); 00183 } 00184 } 00185 00186 /* 00187 ============================================================================ 00188 Handles key events 00189 ============================================================================ 00190 */ 00191 void CRpsGameEngine::KeyEvent(TUint& aKeyState) 00192 { 00193 CGameScreen* gameScreen = iGameScreenMgr->GameScreen(); 00194 ASSERT(gameScreen); 00195 gameScreen->ProcessInput(aKeyState); 00196 } 00197 00198 /* 00199 ============================================================================ 00200 Kick off the drawing of the current screen 00201 ============================================================================ 00202 */ 00203 void CRpsGameEngine::DrawGameScreen() 00204 { 00205 CGameScreen* gameScreen = iGameScreenMgr->GameScreen(); 00206 ASSERT(gameScreen); 00207 gameScreen->DrawGameScreen(); 00208 } 00209 00210 /* 00211 ============================================================================ 00212 Pause the game. The pause screen is displayed 00213 ============================================================================ 00214 */ 00215 void CRpsGameEngine::PauseGame() 00216 { 00217 iGameScreenMgr->SetGameState(CGameScreenManager::EPausedScreen); // Switch on the pause screen 00218 iGameScreenMgr->GameData().iRpsError = KErrTimedOut; 00219 iObs.UpdateScreen(); 00220 } 00221 00222 /* 00223 ============================================================================ 00224 Restart the game after a pause 00225 ============================================================================ 00226 */ 00227 void CRpsGameEngine::UnpauseGame() 00228 {// Start the game ticking again 00229 StartHeartbeat(); 00230 } 00231 00232 // -------------------------------------------------------------------------- 00233 // Tick(TAny *aPtr) 00234 // -------------------------------------------------------------------------- 00235 TInt CRpsGameEngine::Tick(TAny* aCallback) 00236 { 00237 ASSERT(aCallback); 00238 static_cast<CRpsGameEngine*>(aCallback)->GameLoop(); 00239 00240 // Returning a value of ETrue indicates the callback should be done again. 00241 return ETrue; 00242 } 00243 00244 // -------------------------------------------------------------------------- 00245 // GameLoop() 00246 // -------------------------------------------------------------------------- 00247 void CRpsGameEngine::GameLoop() 00248 { 00249 iObs.UpdateScreen(); 00250 }