|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : gamecontroller.cpp 00004 * Part of : OpenC / OpenCOpenglEx 00005 * Interface : 00006 * Description : Contains the implementations of CGameController 00007 * & CWsEventReceiver. 00008 * Version : 00009 * 00010 * Copyright (c) 2007 Nokia Corporation. 00011 * This material, including documentation and any related 00012 * computer programs, is protected by copyright controlled by 00013 * Nokia Corporation. 00014 * ============================================================================== 00015 */ 00016 #include "game.h" 00017 #include "gamecontroller.h" 00018 #include <coemain.h> // for CCoeEnv 00019 00020 // ----------------------------------------------------------------------------- 00021 // CGameController::NewLC 00022 // The Factory Function for a CGameController object. 00023 // ----------------------------------------------------------------------------- 00024 // 00025 CGameController* CGameController::NewLC( EGLDisplay aEglDisplay ) 00026 { 00027 CGameController* self = new (ELeave) CGameController( aEglDisplay ); 00028 00029 CleanupStack::PushL( self ); 00030 00031 self->ConstructL(); 00032 00033 return self; 00034 } 00035 00036 // ----------------------------------------------------------------------------- 00037 // CGameController::CGameController 00038 // Constructor. 00039 // ----------------------------------------------------------------------------- 00040 // 00041 CGameController::CGameController( EGLDisplay aEglDisplay ) 00042 :iEglDisplay( aEglDisplay ), 00043 iEglSurface( NULL ), 00044 iEglContext( NULL ), 00045 iWindow( NULL ), 00046 iWsEventReceiver( NULL ), 00047 iGame( NULL ), 00048 iWsScreenDevice( NULL ), 00049 iIsAppInFocus( EFalse ), 00050 iIsVisible( ETrue ) 00051 { 00052 } 00053 00054 // ----------------------------------------------------------------------------- 00055 // CGameController::~CGameController 00056 // Destructor. 00057 // ----------------------------------------------------------------------------- 00058 // 00059 CGameController::~CGameController() 00060 { 00061 // EGL related cleanup. 00062 eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, 00063 EGL_NO_SURFACE, EGL_NO_CONTEXT 00064 ); 00065 eglDestroySurface( iEglDisplay, iEglSurface ); 00066 eglDestroyContext( iEglDisplay, iEglContext ); 00067 00068 // Destroy the RWindow. 00069 if( iWindow != NULL ) 00070 { 00071 iWindow->SetOrdinalPosition( KOrdinalPositionSwitchToOwningWindow ); 00072 iWindow->Close(); 00073 delete iWindow; 00074 iWindow = NULL; 00075 } 00076 00077 delete iWsEventReceiver; 00078 } 00079 00080 // ----------------------------------------------------------------------------- 00081 // CGameController::ConstructL 00082 // Second phase constructor. 00083 // Initializes EGL. 00084 // ----------------------------------------------------------------------------- 00085 // 00086 void CGameController::ConstructL() 00087 { 00088 CCoeEnv* env = CCoeEnv::Static(); 00089 if( !env ) 00090 { 00091 User::Leave( KErrCoeEnvNotInitialized ); 00092 } 00093 00094 // Get the Windows Server Session, screen device and window group. 00095 iWsSession = env->WsSession(); 00096 iWsScreenDevice = env->ScreenDevice(); 00097 iWindowGroup = env->RootWin(); 00098 if( !iWsScreenDevice ) 00099 { 00100 User::Leave( KErrCoeEnvNotInitialized ); 00101 } 00102 00103 // Intializes iWindow to a full screen window. 00104 CreateNativeWindowL(); 00105 00106 // Choose the buffer size based on the Window's display mode. 00107 TDisplayMode displayMode = iWindow->DisplayMode(); 00108 TInt bufferSize = 0; 00109 00110 switch( displayMode ) 00111 { 00112 case(EColor4K): 00113 bufferSize = 12; 00114 break; 00115 case(EColor64K): 00116 bufferSize = 16; 00117 break; 00118 case(EColor16M): 00119 bufferSize = 24; 00120 break; 00121 case(EColor16MU): 00122 case(EColor16MA): 00123 bufferSize = 32; 00124 break; 00125 default: 00126 break; 00127 } 00128 00129 // Set the desired properties for the EGLSurface 00130 const EGLint attributeList[] = { 00131 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, 00132 EGL_BUFFER_SIZE, bufferSize, 00133 EGL_NONE 00134 }; 00135 EGLConfig bestConfig; 00136 EGLint numConfigs = 0; 00137 00138 // Choose the best EGLConfig that matches the desired properties. 00139 if( eglChooseConfig( iEglDisplay, attributeList, &bestConfig, 00140 1, &numConfigs 00141 ) == EGL_FALSE 00142 ) 00143 { 00144 User::Leave( KErrConfigFailed ); 00145 } 00146 00147 if( numConfigs == 0 ) 00148 { 00149 User::Leave( KErrConfigFailed ); 00150 } 00151 00152 // Create a window surface where the graphics are blitted. 00153 // Pass the Native Window handle to egl. 00154 iEglSurface = eglCreateWindowSurface( iEglDisplay, bestConfig, 00155 (void*)iWindow, NULL 00156 ); 00157 if( iEglSurface == NULL ) 00158 { 00159 User::Leave( KErrCreateWindowSurfaceFailed ); 00160 } 00161 00162 // Create a rendering context 00163 iEglContext = eglCreateContext( iEglDisplay, bestConfig, 00164 EGL_NO_CONTEXT, NULL 00165 ); 00166 if( iEglContext == NULL ) 00167 { 00168 User::Leave( KErrCreateContextFailed ); 00169 } 00170 00171 // Make it the current context. 00172 if( eglMakeCurrent( iEglDisplay, iEglSurface, 00173 iEglSurface, iEglContext 00174 ) == EGL_FALSE 00175 ) 00176 { 00177 User::Leave( KErrSetContextFailed ); 00178 } 00179 00180 // Creates the Active Object that waits for Windows Server events. 00181 iWsEventReceiver = CWsEventReceiver::NewL( *this ); 00182 } 00183 00184 // ----------------------------------------------------------------------------- 00185 // CGameController::StartGameL 00186 // Intializes the Game and Starts the game loop. 00187 // ----------------------------------------------------------------------------- 00188 // 00189 void CGameController::StartGameL( CGame& aGame ) 00190 { 00191 iGame = &aGame; 00192 00193 // Allow the game to initialize itself. 00194 // The opengl es state intialization is done here. 00195 aGame.Initialize( iWindow->Size().iWidth, iWindow->Size().iHeight ); 00196 00197 TTime currentTime; 00198 TTime lastTimeVisited; 00199 lastTimeVisited.HomeTime(); 00200 00201 while( 1 ) // Loop until the Game wants to exit. 00202 { 00203 // Process any pending tasks. 00204 // This runs any Active objects that are waiting for 00205 // some processing. 00206 // The CWsEventReceiver Active Object gets a chance to 00207 // run here (on a key event for example). 00208 ProcessBackgroundTasks( EFalse ); 00209 00210 // If the application is not in focus or is not visible. 00211 // Block until it regains focus. 00212 while( EFalse == iIsAppInFocus || EFalse == iIsVisible ) 00213 ProcessBackgroundTasks( ETrue ); 00214 00215 // Get the current time. 00216 currentTime.HomeTime(); 00217 TTimeIntervalMicroSeconds dur 00218 = currentTime.MicroSecondsFrom( lastTimeVisited ); 00219 00220 // The game renders itself using opengl es apis. 00221 // A return value of EFalse signifies an exit from the game. 00222 // Pass in the time (in micro secs) elapsed since last call. 00223 if( EFalse == aGame.RenderFrame( dur.Int64() ) ) 00224 { 00225 break; 00226 } 00227 00228 // Call eglSwapBuffers, which blits the graphics to the window. 00229 eglSwapBuffers( iEglDisplay, iEglSurface ); 00230 00231 // To keep the background light on. 00232 if( !( ( iGame->GetCurrentFrame() )%100 ) ) 00233 { 00234 User::ResetInactivityTime(); 00235 } 00236 00237 // Store the last time the Game was rendered. 00238 lastTimeVisited = currentTime; 00239 } 00240 00241 // Cleanup. 00242 aGame.Cleanup(); 00243 } 00244 00245 // ----------------------------------------------------------------------------- 00246 // CGameController::ProcessBackgroundTasks 00247 // Calls the RunL on all Active Objects which have 00248 // pending requests. 00249 // If aIsBlocking is ETrue, this API will block till an event occurs. 00250 // ----------------------------------------------------------------------------- 00251 // 00252 void CGameController::ProcessBackgroundTasks( TBool aIsBlocking ) 00253 { 00254 RThread thread; 00255 TInt error = KErrNone; 00256 00257 if ( aIsBlocking && !thread.RequestCount() ) 00258 { 00259 // Block for a event. 00260 User::WaitForAnyRequest(); 00261 00262 // Run the Active Object corresponding to the event 00263 CActiveScheduler::RunIfReady( error, CActive::EPriorityIdle ); 00264 } 00265 00266 // Processes all outstanding requests 00267 while( thread.RequestCount() ) 00268 { 00269 // Processes an event. 00270 CActiveScheduler::RunIfReady( error, CActive::EPriorityIdle ); 00271 00272 // Decreases thread.RequestCount 00273 User::WaitForAnyRequest(); 00274 } 00275 } 00276 00277 // ----------------------------------------------------------------------------- 00278 // CGameController::CreateNativeWindowL 00279 // Create a full screen RWindow and enables screen size 00280 // change events. 00281 // ----------------------------------------------------------------------------- 00282 // 00283 void CGameController::CreateNativeWindowL() 00284 { 00285 iWindow = new (ELeave) RWindow( iWsSession ); 00286 00287 // Construct the window. 00288 TInt err = iWindow->Construct( iWindowGroup, 00289 reinterpret_cast<TUint32>( this ) 00290 ); 00291 User::LeaveIfError( err ); 00292 00293 // Enable the EEventScreenDeviceChanged event. 00294 iWindowGroup.EnableScreenChangeEvents(); 00295 00296 TPixelsTwipsAndRotation pixnrot; 00297 iWsScreenDevice->GetScreenModeSizeAndRotation( 00298 iWsScreenDevice->CurrentScreenMode(), 00299 pixnrot ); 00300 00301 // Set size of the window (cover the entire screen) 00302 iWindow->SetExtent( TPoint( 0, 0 ), 00303 pixnrot.iPixelSize 00304 ); 00305 00306 iWindow->SetRequiredDisplayMode( iWsScreenDevice->DisplayMode() ); 00307 00308 // Activate window and bring it to the foreground 00309 iWindow->Activate(); 00310 iWindow->SetVisible( ETrue ); 00311 iWindow->SetNonFading( ETrue ); 00312 iWindow->SetShadowDisabled( ETrue ); 00313 iWindow->EnableRedrawStore( EFalse ); 00314 iWindow->EnableVisibilityChangeEvents(); 00315 iWindow->SetNonTransparent(); 00316 iWindow->SetBackgroundColor(); 00317 iWindow->SetOrdinalPosition( 0 ); 00318 } 00319 00320 // ----------------------------------------------------------------------------- 00321 // CGameController::HandleWsEvent 00322 // This function is called by the CWsEventReceiver 00323 // Active Object, whenever it receives a Windows Server 00324 // event. 00325 // ----------------------------------------------------------------------------- 00326 // 00327 void CGameController::HandleWsEvent( const TWsEvent& aWsEvent ) 00328 { 00329 TInt eventType = aWsEvent.Type(); 00330 00331 // Handle Key and Screen Size change Events. 00332 switch( eventType ) 00333 { 00334 case EEventKeyDown: 00335 { 00336 if( iGame ) 00337 iGame->HandleKeyEvent( aWsEvent.Key()->iScanCode, EFalse ); 00338 00339 break; 00340 } 00341 case EEventKeyUp: 00342 { 00343 if( iGame ) 00344 iGame->HandleKeyEvent( aWsEvent.Key()->iScanCode, ETrue ); 00345 00346 break; 00347 } 00348 case( EEventScreenDeviceChanged ): // The screen size has changed. 00349 { 00350 TPixelsTwipsAndRotation pixnrot; 00351 iWsScreenDevice->GetScreenModeSizeAndRotation( 00352 iWsScreenDevice->CurrentScreenMode(), 00353 pixnrot 00354 ); 00355 if( pixnrot.iPixelSize != iWindow->Size() ) 00356 { 00357 00358 // Update the window. 00359 iWindow->SetExtent( TPoint( 0, 0 ), 00360 pixnrot.iPixelSize 00361 ); 00362 00363 // If a game is running, notify it about the change. 00364 if( iGame ) 00365 { 00366 iGame->SetScreenSize( pixnrot.iPixelSize.iWidth, 00367 pixnrot.iPixelSize.iHeight 00368 ); 00369 00370 // Call eglSwapBuffers after the window size has changed. 00371 // This updates the window size used by egl. 00372 eglSwapBuffers( iEglDisplay, iEglSurface ); 00373 } 00374 00375 } 00376 break; 00377 } 00378 case EEventFocusLost: 00379 { 00380 iIsAppInFocus = EFalse; 00381 break; 00382 } 00383 case EEventFocusGained: 00384 { 00385 iIsAppInFocus = ETrue; 00386 break; 00387 } 00388 case EEventWindowVisibilityChanged: 00389 { 00390 // Check if the event is for the iWindow 00391 if( aWsEvent.Handle() == 00392 reinterpret_cast<TUint32>( this ) 00393 00394 ) 00395 { 00396 if( aWsEvent.VisibilityChanged()->iFlags & 00397 TWsVisibilityChangedEvent::ECanBeSeen 00398 ) 00399 { 00400 iIsVisible = ETrue; 00401 } 00402 else 00403 iIsVisible = EFalse; 00404 00405 } 00406 break; 00407 } 00408 case EEventNull: 00409 case EEventKey: 00410 case EEventUser: 00411 case EEventWindowGroupListChanged: 00412 case EEventModifiersChanged: 00413 case EEventSwitchOn: 00414 case EEventPassword: 00415 case EEventWindowGroupsChanged: 00416 case EEventErrorMessage: 00417 case EEventPointer: 00418 case EEventPointerEnter: 00419 case EEventPointerExit: 00420 case EEventPointerBufferReady: 00421 case EEventDragDrop: 00422 break; 00423 default: 00424 break; 00425 } 00426 } 00427 00428 //////////////////////////////////////////////////////////////////////////////// 00429 // Windows Server Event Receiver Implementation // 00430 //////////////////////////////////////////////////////////////////////////////// 00431 00432 // ----------------------------------------------------------------------------- 00433 // CWsEventReceiver::CWsEventReceiver 00434 // Constructor. 00435 // ----------------------------------------------------------------------------- 00436 // 00437 CWsEventReceiver::CWsEventReceiver() 00438 :CActive( CActive::EPriorityStandard ), 00439 iParent( NULL ) 00440 { 00441 } 00442 00443 // ----------------------------------------------------------------------------- 00444 // CWsEventReceiver::~CWsEventReceiver 00445 // Destructor. 00446 // ----------------------------------------------------------------------------- 00447 // 00448 CWsEventReceiver::~CWsEventReceiver() 00449 { 00450 Cancel(); 00451 } 00452 00453 // ----------------------------------------------------------------------------- 00454 // CWsEventReceiver::NewL 00455 // Factory Function. 00456 // ----------------------------------------------------------------------------- 00457 // 00458 CWsEventReceiver* CWsEventReceiver::NewL( CGameController& aParent ) 00459 { 00460 CWsEventReceiver* self = new (ELeave) CWsEventReceiver; 00461 00462 CleanupStack::PushL( self ); 00463 00464 self->ConstructL( aParent ); 00465 00466 CleanupStack::Pop( self ); 00467 00468 return self; 00469 } 00470 00471 // ----------------------------------------------------------------------------- 00472 // CWsEventReceiver::ConstructL 00473 // Second phase constructor. 00474 // Add itself to the Active Scheduler. 00475 // ----------------------------------------------------------------------------- 00476 // 00477 void CWsEventReceiver::ConstructL( CGameController& aParent ) 00478 { 00479 if( !CCoeEnv::Static() ) 00480 User::Leave( KErrCoeEnvNotCreated ); 00481 00482 iParent = &aParent; 00483 00484 // Register with the Windows Server for events. 00485 iWsSession = CCoeEnv::Static()->WsSession(); 00486 00487 iWsSession.EventReady( &iStatus ); 00488 00489 CActiveScheduler::Add( this ); 00490 00491 SetActive(); 00492 } 00493 00494 // ----------------------------------------------------------------------------- 00495 // CWsEventReceiver::RunL 00496 // Is called by the Active Scheduler when a Windows 00497 // Server event is pending. 00498 // NOTE: This API does not leave, so RunError is not 00499 // implemented. 00500 // ----------------------------------------------------------------------------- 00501 // 00502 void CWsEventReceiver::RunL() 00503 { 00504 TWsEvent wsEvent; 00505 iWsSession.GetEvent( wsEvent ); 00506 00507 // Pass the event on to the Game Controller. 00508 iParent->HandleWsEvent( wsEvent ); 00509 00510 // Request for more events. 00511 iWsSession.EventReady( &iStatus ); 00512 00513 SetActive(); 00514 } 00515 00516 // ----------------------------------------------------------------------------- 00517 // CWsEventReceiver::DoCancel 00518 // Cancels any pending Windows Server event requests. 00519 // ----------------------------------------------------------------------------- 00520 // 00521 void CWsEventReceiver::DoCancel() 00522 { 00523 iWsSession.EventReadyCancel(); 00524 } 00525 00526 // eof
|
© Nokia 2009 |