Nokia N97 SDK
Example Applications Guide

CGame Class Reference

#include <game.h>

Inheritance diagram for CGame:

List of all members.

Detailed Description

Class that does the actual OpenGL ES rendering.

Definition at line 101 of file game.h.


Public Member Functions

  ~CGame ()
void  Initialize (TUint aScreenWidth, TUint aScreenHeight)
TBool  RenderFrame (TInt64 aDurationSinceLastVisit)
void  Cleanup ()
void  HandleKeyEvent (TUint32 aKey, TBool aIsKeyUp)
void  SetScreenSize (TUint aScreenWidth, TUint aScreenHeight)
TUint  GetCurrentFrame () const

Static Public Member Functions

static CGame *  NewLC ()

Protected Member Functions

  CGame ()
void  ConstructL ()

Private Attributes

TInt  iScreenWidth
TInt  iScreenHeight
TBool  iGameOver
TInt  iZPos
TBool  iIsUpPressed
TBool  iIsDownPressed
TUint  iFrame


Constructor & Destructor Documentation

CGame::~CGame (   ) 
 

Destructor.

Definition at line 58 of file game.cpp.

References Cleanup().

00059 {
00060     Cleanup();
00061 }

CGame::CGame (   )  [protected]
 

Standard constructor that must never Leave.

Definition at line 41 of file game.cpp.

00042       :iScreenWidth( 0 ),
00043        iScreenHeight( 0 ),
00044        iGameOver( EFalse ),
00045        iZPos( -100 ),
00046        iIsUpPressed( EFalse ),
00047        iIsDownPressed( EFalse ),
00048        iFrame( 0 )
00049 {
00050 
00051 }


Member Function Documentation

CGame * CGame::NewLC (   )  [static]
 

Factory method for creating a new CGame object.

Definition at line 25 of file game.cpp.

References ConstructL().

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

void CGame::ConstructL (   )  [protected]
 

Second phase contructor.

Definition at line 80 of file game.cpp.

Referenced by NewLC().

00081 {
00082 
00083 }

void CGame::Initialize (  TUint  aScreenWidth,
TUint  aScreenHeight
) 
 

Intializes the Game.

Parameters:
aScreenWidth  Width of the screen.
aScreenHeight  Height of the screen.

Definition at line 90 of file game.cpp.

References iScreenHeight, and iScreenWidth.

Referenced by CGameController::StartGameL().

00091 {
00092     iScreenWidth = aScreenWidth;
00093     iScreenHeight = aScreenHeight;
00094 
00095     // Set the screen background color. 
00096     glClearColor( 0.f, 0.f, 0.f, 1.f );
00097 
00098     // Enable back face culling. 
00099     glEnable( GL_CULL_FACE  );
00100 
00101     // Initialize viewport and projection. 
00102     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00103     glMatrixMode( GL_PROJECTION );
00104 
00105     // Calculate the view frustrum
00106     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00107     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00108                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00109                 FRUSTUM_NEAR, FRUSTUM_FAR );
00110 
00111     glMatrixMode( GL_MODELVIEW );
00112 
00113     // Enable vertex arrays. 
00114     glEnableClientState( GL_VERTEX_ARRAY );
00115 
00116     // Set array pointers. 
00117     glVertexPointer( 3, GL_BYTE, 0, vertices );
00118 
00119     // Enable color arrays. 
00120     glEnableClientState( GL_COLOR_ARRAY );
00121 
00122     // Set color pointers. 
00123     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
00124 
00125     // Set the initial shading mode 
00126     glShadeModel( GL_FLAT ); 
00127 
00128     // Do not use perspective correction 
00129     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00130 
00131 }

TBool CGame::RenderFrame (  TInt64  aDurationSinceLastVisit  ) 
 

Renders the next frame using opengl es calls.

Parameters:
aDurationSinceLastVisit  The time (in microseconds) since the function was last visited.

Definition at line 139 of file game.cpp.

References iFrame, iGameOver, iIsDownPressed, iIsUpPressed, and iZPos.

Referenced by CGameController::StartGameL().

00140 {      
00141     if( iGameOver ) // If the game has ended, return EFalse to exit.
00142         return EFalse;
00143     
00144     // Ensure that the desired fps is maintained by sleeping 
00145     // for the rest of the time.   
00146     if( aMicrosSinceLastVisit < timeForOneFrame )
00147     {
00148         usleep( timeForOneFrame - aMicrosSinceLastVisit );    
00149     }
00150     
00151     // Clear the buffer.
00152     glClear( GL_COLOR_BUFFER_BIT );
00153         
00154     glLoadIdentity();
00155     
00156     // Move the Cube.
00157     glTranslatex( 0 , 0 , iZPos << 16 );
00158     
00159     // Rotate the cube.
00160     glRotatex( iFrame << 16, 1 << 16,    0   ,    0    );
00161     glRotatex( iFrame << 15,    0   , 1 << 16,    0    );
00162     glRotatex( iFrame << 14,    0   ,    0   , 1 << 16 );
00163 
00164     glScalef( 15.0f, 15.0f, 15.0f );
00165     
00166     glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00167     
00168     ++iFrame;
00169     
00170     // Move the cube if the up/down keys are pressed, but not released.
00171     // If the up/down keys are not released, then continuously move
00172     // cube.
00173     if( iIsUpPressed )
00174         --iZPos;
00175     
00176     if( iIsDownPressed )
00177         ++iZPos;
00178     
00179     return ETrue;
00180 }

void CGame::Cleanup (   ) 
 

Cleanups any resources.

Definition at line 69 of file game.cpp.

Referenced by ~CGame().

00070 {
00071 
00072 }

void CGame::HandleKeyEvent (  TUint32  aKey,
TBool  aIsKeyUp
) 
 

Handles a Key up / down event.

Parameters:
aKey  The TStdScanCode of the key.
aIsKeyUp  ETrue if the Key is released, else EFalse.

Definition at line 188 of file game.cpp.

References iGameOver, iIsDownPressed, iIsUpPressed, and iZPos.

00189 {
00190    // Handle key events
00191    if( EFalse == aIsKeyUp )
00192    {
00193         switch( aKey )
00194         {
00195             case( EStdKeyUpArrow ):
00196             {
00197                 // Move the cube farther from the screen.
00198                 --iZPos;
00199                 iIsUpPressed = ETrue;
00200                 break;
00201             }
00202             case( EStdKeyDownArrow ): 
00203             {
00204                 // Move the cube closer.
00205                 ++iZPos;
00206                 iIsDownPressed = ETrue;
00207                 break;
00208             }
00209             case( EStdKeyDevice1 ): 
00210             {
00211                 // Exit on Right Soft Key press.
00212                 iGameOver = ETrue;
00213                 break;
00214             }
00215             default:                
00216                 break;
00217         }
00218    }
00219    else
00220    {
00221         // On a key release, reset the flags.
00222         switch( aKey )
00223         {
00224             case( EStdKeyUpArrow ):
00225             {                
00226                 iIsUpPressed = EFalse;
00227                 break;
00228             }
00229             case( EStdKeyDownArrow ):
00230             {
00231                 iIsDownPressed = EFalse;
00232                 break;
00233             }
00234             default:
00235                 break;
00236         }       
00237    }
00238 }

void CGame::SetScreenSize (  TUint  aScreenWidth,
TUint  aScreenHeight
) 
 

Handle screen size changes.

Parameters:
aScreenWidth  Width of the screen.
aScreenHeight  Height of the screen.

Definition at line 246 of file game.cpp.

References iScreenHeight, and iScreenWidth.

00247 {    
00248     iScreenWidth = aScreenWidth;
00249     iScreenHeight = aScreenHeight;
00250     
00251     // Initialize the viewport and projection. 
00252     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00253     glMatrixMode( GL_PROJECTION );
00254     glLoadIdentity();
00255     
00256     // Calculate the view frustrum
00257     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00258     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00259                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00260                 FRUSTUM_NEAR, FRUSTUM_FAR );
00261 
00262     glMatrixMode( GL_MODELVIEW );    
00263 }

TUint CGame::GetCurrentFrame (   )  const [inline]
 

Get the current frame number.

Definition at line 165 of file game.h.

References iFrame.

Referenced by CGameController::StartGameL().

00166     {
00167         return iFrame;
00168     }


Member Data Documentation

TInt CGame::iScreenWidth [private]
 

Width of the screen

Definition at line 172 of file game.h.

Referenced by Initialize(), and SetScreenSize().

TInt CGame::iScreenHeight [private]
 

Height of the screen

Definition at line 175 of file game.h.

Referenced by Initialize(), and SetScreenSize().

TBool CGame::iGameOver [private]
 

Flags if the game needs to exit

Definition at line 178 of file game.h.

Referenced by HandleKeyEvent(), and RenderFrame().

TInt CGame::iZPos [private]
 

The z co-ordinate of the cube

Definition at line 181 of file game.h.

Referenced by HandleKeyEvent(), and RenderFrame().

TBool CGame::iIsUpPressed [private]
 

Flags if the up key is pressed

Definition at line 184 of file game.h.

Referenced by HandleKeyEvent(), and RenderFrame().

TBool CGame::iIsDownPressed [private]
 

Flags if the down key is pressed

Definition at line 187 of file game.h.

Referenced by HandleKeyEvent(), and RenderFrame().

TUint CGame::iFrame [private]
 

The current frame being rendered

Definition at line 190 of file game.h.

Referenced by GetCurrentFrame(), and RenderFrame().


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

© Nokia 2009

Back to top