Nokia N97 SDK
Example Applications Guide

CGameController Class Reference

#include <gamecontroller.h>

Inheritance diagram for CGameController:

List of all members.

Detailed Description

The Game Controller class. Controls the intialization of the Game and mediates the Key Event delivery to the game.

Definition at line 46 of file gamecontroller.h.


Public Member Functions

void  StartGameL (CGame &aGame)
void  HandleWsEvent (const TWsEvent &aEvent)
  ~CGameController ()

Static Public Member Functions

static CGameController *  NewLC (EGLDisplay aEglDisplay)

Private Member Functions

  CGameController (EGLDisplay aEglDisplay)
void  ConstructL ()
void  ProcessBackgroundTasks (TBool aIsBlocking)
void  CreateNativeWindowL ()

Private Attributes

EGLDisplay  iEglDisplay
EGLSurface  iEglSurface
EGLContext  iEglContext
RWindow *  iWindow
CWsEventReceiver *  iWsEventReceiver
CGame *  iGame
RWsSession  iWsSession
RWindowGroup  iWindowGroup
CWsScreenDevice *  iWsScreenDevice
TBool  iIsAppInFocus
TBool  iIsVisible


Constructor & Destructor Documentation

CGameController::~CGameController (   ) 
 

Destructor.

Definition at line 59 of file gamecontroller.cpp.

References iEglContext, iEglDisplay, iEglSurface, and iWindow.

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 }

CGameController::CGameController (  EGLDisplay  aEglDisplay  )  [private]
 

Constructor.

Parameters:
aEglDisplay  A handle to the egl display.

Definition at line 41 of file gamecontroller.cpp.

Referenced by NewLC().

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 }


Member Function Documentation

CGameController * CGameController::NewLC (  EGLDisplay  aEglDisplay  )  [static]
 

Two phase constructor.

Parameters:
aEglDisplay  the egl display to drawn on.

Definition at line 25 of file gamecontroller.cpp.

References CGameController().

00026 {
00027     CGameController* self = new (ELeave) CGameController( aEglDisplay );
00028     
00029     CleanupStack::PushL( self );
00030     
00031     self->ConstructL();
00032 
00033     return self;    
00034 }

void CGameController::StartGameL (  CGame &  aGame  ) 
 

Starts the Game Loop.

Parameters:
aGame  A reference to the game object.

Definition at line 189 of file gamecontroller.cpp.

References CGame::GetCurrentFrame(), iEglDisplay, iEglSurface, iGame, iIsAppInFocus, iIsVisible, CGame::Initialize(), iWindow, ProcessBackgroundTasks(), and CGame::RenderFrame().

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 }

void CGameController::HandleWsEvent (  const TWsEvent &  aEvent  ) 
 

Handles a Windows Server event. This is called by the CWsEventReciever whenever it has a pending Windows Server Event.

Parameters:
aEvent  A reference to the Windows server event.

Definition at line 327 of file gamecontroller.cpp.

References iEglDisplay, iEglSurface, iGame, iWindow, and iWsScreenDevice.

Referenced by CWsEventReceiver::RunL().

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 }

void CGameController::ConstructL (   )  [private]
 

Two phase constructor. Intializes the EGL Context and surface.

Definition at line 86 of file gamecontroller.cpp.

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 }

void CGameController::ProcessBackgroundTasks (  TBool  aIsBlocking  )  [private]
 

Runs the Active Scheduler so that the any pending Active Objects get a chance to run.

Parameters:
aIsBlocking  If ETrue, the API blocks till a asynchronous event occurs.

Definition at line 252 of file gamecontroller.cpp.

Referenced by StartGameL().

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 }

void CGameController::CreateNativeWindowL (   )  [private]
 

Creates a RWindow and intializes it to cover the full screen.

Definition at line 283 of file gamecontroller.cpp.

References iWindow, iWindowGroup, iWsScreenDevice, and iWsSession.

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 }


Member Data Documentation

EGLDisplay CGameController::iEglDisplay [private]
 

Handle to the EGL Display

Definition at line 106 of file gamecontroller.h.

Referenced by HandleWsEvent(), StartGameL(), and ~CGameController().

EGLSurface CGameController::iEglSurface [private]
 

Handle to the EGL Surface

Definition at line 109 of file gamecontroller.h.

Referenced by HandleWsEvent(), StartGameL(), and ~CGameController().

EGLContext CGameController::iEglContext [private]
 

Handle to the EGL Context

Definition at line 112 of file gamecontroller.h.

Referenced by ~CGameController().

RWindow* CGameController::iWindow [private]
 

Handle to Symbian's Window

Definition at line 115 of file gamecontroller.h.

Referenced by CreateNativeWindowL(), HandleWsEvent(), StartGameL(), and ~CGameController().

CWsEventReceiver* CGameController::iWsEventReceiver [private]
 

The Active Object that waits for Windows Server Events

Definition at line 118 of file gamecontroller.h.

CGame* CGameController::iGame [private]
 

The Game

Definition at line 121 of file gamecontroller.h.

Referenced by HandleWsEvent(), and StartGameL().

RWsSession CGameController::iWsSession [private]
 

Handle to the Windows Server session

Definition at line 124 of file gamecontroller.h.

Referenced by CreateNativeWindowL().

RWindowGroup CGameController::iWindowGroup [private]
 

Handle to the Window group

Definition at line 127 of file gamecontroller.h.

Referenced by CreateNativeWindowL().

CWsScreenDevice* CGameController::iWsScreenDevice [private]
 

The Screen device

Definition at line 130 of file gamecontroller.h.

Referenced by CreateNativeWindowL(), and HandleWsEvent().

TBool CGameController::iIsAppInFocus [private]
 

Flag for the applications focus state

Definition at line 133 of file gamecontroller.h.

Referenced by StartGameL().

TBool CGameController::iIsVisible [private]
 

Flag to check if the Window is visible

Definition at line 136 of file gamecontroller.h.

Referenced by StartGameL().


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top