Nokia N97 SDK
Example Applications Guide

game.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : game.cpp
00004 *  Part of     : OpenC / OpenCOpenglEx
00005 *  Interface   : 
00006 *  Description : Contains the implementation of the CGame class.
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2007 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 #include "game.h"
00016 #include <e32base.h>
00017 #include <gles/gl.h> // for opengl es functions.
00018 #include <unistd.h>  // for usleep.
00019 
00020 // -----------------------------------------------------------------------------
00021 // CGame::NewLC
00022 // The factory function.
00023 // -----------------------------------------------------------------------------
00024 //
00025 CGame* CGame::NewLC()
00026 {
00027     CGame* self = new (ELeave) CGame;
00028     
00029     CleanupStack::PushL( self );
00030     
00031     self->ConstructL();
00032 
00033     return self;    
00034 }
00035 
00036 // -----------------------------------------------------------------------------
00037 // CGame::CGame
00038 // Constructor.
00039 // -----------------------------------------------------------------------------
00040 //
00041 CGame::CGame()
00042       :iScreenWidth( 0 ),
00043        iScreenHeight( 0 ),
00044        iGameOver( EFalse ),
00045        iZPos( -100 ),
00046        iIsUpPressed( EFalse ),
00047        iIsDownPressed( EFalse ),
00048        iFrame( 0 )
00049 {
00050 
00051 }
00052 
00053 // -----------------------------------------------------------------------------
00054 // CGame::~CGame
00055 // Destructor.
00056 // -----------------------------------------------------------------------------
00057 //
00058 CGame::~CGame()
00059 {
00060     Cleanup();
00061 }
00062 
00063 // -----------------------------------------------------------------------------
00064 // CGame::Cleanup
00065 // Perform any cleanup here.
00066 // (does nothing)
00067 // -----------------------------------------------------------------------------
00068 //
00069 void CGame::Cleanup()
00070 {
00071 
00072 }
00073 
00074 // -----------------------------------------------------------------------------
00075 // CGame::ConstructL
00076 // Second phase contructor.
00077 // (Does nothing)
00078 // -----------------------------------------------------------------------------
00079 //
00080 void CGame::ConstructL()
00081 {
00082 
00083 }
00084 
00085 // -----------------------------------------------------------------------------
00086 // CGame::Initialize
00087 // Initializes the opengl es state, based on the screen height and width.
00088 // -----------------------------------------------------------------------------
00089 //
00090 void CGame::Initialize( TUint aScreenWidth, TUint aScreenHeight )
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 }
00132 
00133 // -----------------------------------------------------------------------------
00134 // CGame::RenderFrame
00135 // Renders a game frame using opengl es apis.
00136 // It returns EFalse, if the game has ended.
00137 // -----------------------------------------------------------------------------
00138 //
00139 TBool CGame::RenderFrame( TInt64 aMicrosSinceLastVisit )
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 }
00181 
00182 // -----------------------------------------------------------------------------
00183 // CGame::HandleKeyEvent
00184 // Handle a Key Event. This called by the Game Controller, when a key up/down
00185 // event occurs.
00186 // -----------------------------------------------------------------------------
00187 //
00188 void CGame::HandleKeyEvent( TUint32 aKey, TBool aIsKeyUp )
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 }
00239 
00240 // -----------------------------------------------------------------------------
00241 // CGame::SetScreenSize
00242 // Initialize the opengl viewport based on the screen width and height.
00243 // This is called by the Game Controller when the screen size changes.
00244 // -----------------------------------------------------------------------------
00245 //
00246 void CGame::SetScreenSize( TUint aScreenWidth, TUint aScreenHeight )
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 }
00264 
00265 // eof
00266 

© Nokia 2009

Back to top