Nokia N97 SDK
Example Applications Guide

CBumpMapContainer Class Reference

#include <BumpMapcontainer.h>

Inheritance diagram for CBumpMapContainer:

List of all members.

Detailed Description

Container control class that handles the OpenGL ES initialization and deinitializations. Also uses the CBumpMap class to do the actual OpenGL ES rendering.

Definition at line 29 of file BumpMapcontainer.h.


Public Member Functions

void  ConstructL (const TRect &aRect, CAknAppUi *aAppUi)
virtual  ~CBumpMapContainer ()

Static Public Member Functions

static TInt  DrawCallBack (TAny *aInstance)
static TTime  GetTimeTick ()

Public Attributes

CBumpMap *  iBumpMap

Private Member Functions

void  SizeChanged ()
void  HandleResourceChange (TInt aType)
TInt  CountComponentControls () const
CCoeControl *  ComponentControl (TInt aIndex) const
void  Draw (const TRect &aRect) const
void  HandleControlEventL (CCoeControl *aControl, TCoeEvent aEventType)

Private Attributes

EGLDisplay  iEglDisplay
EGLSurface  iEglSurface
EGLContext  iEglContext
CPeriodic *  iPeriodic
TBool  iOpenGlInitialized
TUint  iStartTime
GLfloat  iLastFrameTime
TUint  iFrame
CAknAppUi *  iAppUi


Constructor & Destructor Documentation

CBumpMapContainer::~CBumpMapContainer (   )  [virtual]
 

Destructor. Destroys the CPeriodic, CBumpMap and uninitializes OpenGL ES.

Definition at line 208 of file BumpMapcontainer.cpp.

References CBumpMap::AppExit(), iBumpMap, iEglContext, iEglDisplay, iEglSurface, and iPeriodic.

00209     {
00210     delete iPeriodic;
00211 
00212     /* AppExit call is made to release any allocations made in AppInit.
00213      This call has to be made here before we destroy the rendering context. */
00214     if ( iBumpMap )
00215         {
00216         iBumpMap->AppExit();
00217         delete iBumpMap;
00218         }
00219 
00220     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00221     eglDestroySurface( iEglDisplay, iEglSurface );
00222     eglDestroyContext( iEglDisplay, iEglContext );
00223     eglTerminate( iEglDisplay );
00224     }


Member Function Documentation

void CBumpMapContainer::ConstructL (  const TRect &  aRect,
CAknAppUi *  aAppUi
) 
 

Second phase constructor that can call methods that may leave. Initializes the OpenGL ES for rendering to the window surface.

Parameters:
aRect  Screen rectangle for container.

Definition at line 24 of file BumpMapcontainer.cpp.

References iAppUi, iEglDisplay, and iOpenGlInitialized.

Referenced by CBumpMapAppUi::ConstructL().

00025     {
00026     iAppUi = aAppUi;
00027     // Open GL hasn't been initialized
00028     iOpenGlInitialized = EFalse;
00029 
00030     // Create the native window
00031     CreateWindowL();
00032 
00033     // Do not go to full-screen in touch devices.    
00034     if (AknLayoutUtils::PenEnabled())
00035     {
00036         SetRect(aRect);
00037     }
00038     else
00039     {
00040         SetExtentToWholeScreen();                // Take the whole screen into use
00041     }
00042     ActivateL();
00043 
00044     // Describes the format, type and size of the color buffers and
00045     // ancillary buffers for EGLSurface
00046     EGLConfig config;
00047 
00048     // Get the display for drawing graphics
00049     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00050     if ( iEglDisplay == NULL )
00051         {
00052         _LIT( KGetDisplayFailed, "eglGetDisplay failed" );
00053         User::Panic( KGetDisplayFailed, 0 );
00054         }
00055 
00056     // Initialize display
00057     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00058         {
00059         _LIT( KInitializeFailed, "eglInitialize failed" );
00060         User::Panic( KInitializeFailed, 0 );
00061         }
00062 
00063     // Pointer for EGLConfigs
00064     EGLConfig *configList = NULL;
00065     EGLint numOfConfigs   = 0;
00066     EGLint configSize     = 0;
00067 
00068     // Get the number of possible EGLConfigs
00069     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00070         {
00071         _LIT( KGetConfigsFailed, "eglGetConfigs failed" );
00072         User::Panic( KGetConfigsFailed, 0 );
00073         }
00074 
00075     configSize = numOfConfigs;
00076 
00077     // Allocate memory for the configList
00078     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00079     if ( configList == NULL )
00080         {
00081         _LIT( KConfigAllocFailed, "config alloc failed" );
00082         User::Panic( KConfigAllocFailed, 0 );
00083         }
00084 
00085     // Define properties for the wanted EGLSurface
00086     TDisplayMode DMode = Window().DisplayMode();
00087     TInt BufferSize = 0;
00088 
00089     switch(DMode)
00090         {
00091         case(EColor4K):
00092             BufferSize = 12;
00093             break;
00094         case(EColor64K):
00095             BufferSize = 16;
00096             break;
00097         case(EColor16M):
00098             BufferSize = 24;
00099             break;
00100         case(EColor16MU):
00101         case(EColor16MA):
00102             BufferSize = 32;
00103             break;
00104         default:
00105             _LIT(KDModeError, "unsupported displaymode");
00106             User::Panic( KDModeError, 0 );
00107             break;
00108         }
00109 
00110     // Define properties for requesting a full-screen antialiased window surface
00111     const EGLint attrib_list_fsaa[] =
00112         {
00113                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00114         EGL_BUFFER_SIZE,  BufferSize,
00115         EGL_DEPTH_SIZE,   16,
00116                                 EGL_SAMPLE_BUFFERS, 1,
00117                                 EGL_SAMPLES,        4,
00118         EGL_NONE
00119         };
00120 
00121     // Define properties for requesting a non-antialiased window surface
00122     const EGLint attrib_list[] =
00123         {
00124                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00125         EGL_BUFFER_SIZE,  BufferSize,
00126         EGL_DEPTH_SIZE,   16,
00127         EGL_NONE
00128         };
00129 
00130     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00131     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00132         {
00133         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00134         User::Panic( KChooseConfigFailed, 0 );
00135         }
00136 
00137     // Check if no configurations were found
00138     if ( numOfConfigs == 0 )
00139         {
00140         // No configs with antialising were found. Try to get the non-antialiased config
00141                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00142                               {
00143                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00144                   User::Panic( KChooseConfigFailed, 0 );
00145                               }
00146 
00147                     if ( numOfConfigs == 0 )
00148                               {
00149                               // No configs found without antialiasing
00150                   _LIT( KNoConfig, "Can't find the requested config." );
00151                   User::Panic( KNoConfig, 0 );
00152                   }
00153               }
00154 
00155     // Choose the best EGLConfig. EGLConfigs returned by eglChooseConfig are
00156     // sorted so that the best matching EGLConfig is first in the list.
00157     config = configList[0];
00158 
00159     // Free configList, as it's not used anymore.
00160     User::Free( configList );
00161 
00162     // Create a window where the graphics are blitted
00163     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00164     if ( iEglSurface == NULL )
00165         {
00166         _LIT( KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed" );
00167         User::Panic( KCreateWindowSurfaceFailed, 0 );
00168         }
00169 
00170     // Create a rendering context
00171     iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00172     if ( iEglContext == NULL )
00173         {
00174         _LIT( KCreateContextFailed, "eglCreateContext failed" );
00175         User::Panic( KCreateContextFailed, 0 );
00176         }
00177 
00178     // Make the context current. Binds context to the current rendering thread
00179     // and surface.
00180     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00181         {
00182         _LIT( KMakeCurrentFailed, "eglMakeCurrent failed" );
00183         User::Panic( KMakeCurrentFailed, 0 );
00184         }
00185 
00186 
00187     TSize size = this->Size();
00188     iBumpMap = CBumpMap::NewL( size.iWidth, size.iHeight );
00189     iBumpMap->AppInitL();
00190 
00191     iOpenGlInitialized = ETrue;
00192 
00193     // Initialize timers and counters
00194     iFrame             = 0;
00195     iStartTime         = User::NTickCount();
00196     iLastFrameTime     = iStartTime;
00197 
00198     // Create an active object for animating the scene
00199     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );
00200     iPeriodic->Start( 100, 100, TCallBack( CBumpMapContainer::DrawCallBack, this ) );
00201     }

TInt CBumpMapContainer::DrawCallBack (  TAny *  aInstance  )  [static]
 

Callback function for the CPeriodic. Calculates the current frame, keeps the background light from turning off and orders the CBumpMap to do the rendering for each frame.

Parameters:
aInstance  Pointer to this instance of CBumpMapContainer.

Definition at line 332 of file BumpMapcontainer.cpp.

References CBumpMap::AppCycle(), iBumpMap, iEglDisplay, iEglSurface, iFrame, iLastFrameTime, and iStartTime.

00333     {
00334     // Cast parameter to container instance
00335     CBumpMapContainer* instance = (CBumpMapContainer*) aInstance;
00336 
00337     // Update the frame counts
00338     instance->iFrame++;
00339 
00340     // Compute the elapsed time in seconds since the startup of the example
00341 #ifdef __WINS__
00342 
00343     // In the emulator the tickcount runs at 200Hz
00344     GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTime ) ) / 200.f;
00345 
00346 #else
00347 
00348     // In the device the tickcount runs at 1000hz (as intended)
00349     GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTime ) ) / 1000.f;
00350 
00351 #endif
00352 
00353     // Compute the elapsed time since last frame was drawn. Note that due to the
00354     // resolution of the system timer this value can be 0.0 even though new frames are being rendered.
00355     // This applies especially when running the emulator on a PC.
00356     GLfloat deltaTimeSecs = timeSecs - instance->iLastFrameTime;
00357     if ( instance->iBumpMap->GetState() == CBumpMap::ERunning )
00358         {
00359         // Call the main OpenGL ES Symbian rendering 'loop'
00360         instance->iBumpMap->AppCycle( instance->iFrame, timeSecs, deltaTimeSecs );
00361 
00362         // Call eglSwapBuffers, which blit the graphics to the window
00363         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00364 
00365         if ( !(instance->iFrame % 50 ) )
00366             {
00367             // Reset inactivity timer to keep the background light on
00368             User::ResetInactivityTime();
00369 
00370             // Suspend the current thread for a short while. Give some time
00371             // to other threads and AOs, avoids the ViewSrv error in ARMI and
00372             // THUMB release builds. One may try to decrease the callback
00373             // function instead of this.
00374             User::After( 0 );
00375             }
00376         }
00377 
00378     // Set the current time to be the last frame time for the upcoming frame
00379     instance->iLastFrameTime = timeSecs;
00380 
00381     return 0;
00382     }

TTime CBumpMapContainer::GetTimeTick (   )  [static]
 

Returns the current system time.

Returns:
System time.

Definition at line 319 of file BumpMapcontainer.cpp.

00320     {
00321     TTime time;
00322     time.UniversalTime();
00323     return time;
00324     }

void CBumpMapContainer::SizeChanged (   )  [private]
 

Method from CoeControl that gets called when the display size changes. If OpenGL has been initialized, notifies the renderer class that the screen size has changed.

Definition at line 231 of file BumpMapcontainer.cpp.

References iBumpMap, iOpenGlInitialized, and CBumpMap::SetScreenSize().

00232     {
00233     if( iOpenGlInitialized && iBumpMap )
00234         {
00235         // If example is running and OpenGL ES is initialized
00236         // notify it of the screen size change
00237         TSize size = this->Size();
00238         iBumpMap->SetScreenSize( size.iWidth, size.iHeight );
00239         }
00240     }

void CBumpMapContainer::HandleResourceChange (  TInt  aType  )  [private]
 

Handles a change to the control's resources. This method reacts to the KEikDynamicLayoutVariantSwitch event (that notifies of screen size change) by calling the SetExtentToWholeScreen() again so that this control fills the new screen size. This will then trigger a call to the SizeChanged() method.

Parameters:
aType  Message UID value, only KEikDynamicLayoutVariantSwitch is handled by this method.

Definition at line 250 of file BumpMapcontainer.cpp.

References iAppUi.

00251     {
00252     switch( aType )
00253        {
00254             case KEikDynamicLayoutVariantSwitch:
00255                 // Do not go full-screen in touch devices.    
00256             if (AknLayoutUtils::PenEnabled())
00257             {
00258                 SetRect(iAppUi->ClientRect());
00259             }
00260             else
00261             {
00262                 SetExtentToWholeScreen();                // Take the whole screen into use
00263             }
00264                 break;
00265             }
00266     }

TInt CBumpMapContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 273 of file BumpMapcontainer.cpp.

00274     {
00275     return 0;
00276     }

CCoeControl * CBumpMapContainer::ComponentControl (  TInt  aIndex  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation.

Definition at line 282 of file BumpMapcontainer.cpp.

00283     {
00284     return NULL;
00285     }

void CBumpMapContainer::Draw (  const TRect &  aRect  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation. All rendering is done in the DrawCallBack() method.

Definition at line 291 of file BumpMapcontainer.cpp.

References iBumpMap.

00292     {
00293     CWindowGc& gc = SystemGc();
00294     gc.Clear( aRect );
00295 
00296     if ( iBumpMap->GetState() == CBumpMap::ELoadingTextures )
00297         {
00298         _LIT(KLoading, "Loading...");
00299         utilOutputText(SystemGc(), KLoading, TPoint(70, 105), KRgbBlack );
00300         }
00301     }

void CBumpMapContainer::HandleControlEventL (  CCoeControl *  aControl,
TCoeEvent  aEventType
)  [private]
 

Method from MCoeControlObserver that handles an event from the observed control. Does nothing in this implementation.

Parameters:
aControl  Control changing its state.
aEventType  Type of the control event.

Definition at line 308 of file BumpMapcontainer.cpp.

00310     {
00311     // TODO: Add any control event handler code here
00312     }


Member Data Documentation

EGLDisplay CBumpMapContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 106 of file BumpMapcontainer.h.

Referenced by ConstructL(), DrawCallBack(), and ~CBumpMapContainer().

EGLSurface CBumpMapContainer::iEglSurface [private]
 

Window surface where the OpenGL ES rendering is blitted to.

Definition at line 108 of file BumpMapcontainer.h.

Referenced by DrawCallBack(), and ~CBumpMapContainer().

EGLContext CBumpMapContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 110 of file BumpMapcontainer.h.

Referenced by ~CBumpMapContainer().

CPeriodic* CBumpMapContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 112 of file BumpMapcontainer.h.

Referenced by ~CBumpMapContainer().

TBool CBumpMapContainer::iOpenGlInitialized [private]
 

Flag that indicates if OpenGL ES has been initialized or not. Used to check if SizeChanged() can react to incoming notifications.

Definition at line 117 of file BumpMapcontainer.h.

Referenced by ConstructL(), and SizeChanged().

TUint CBumpMapContainer::iStartTime [private]
 

Time when the example started running (rendering frames).

Definition at line 120 of file BumpMapcontainer.h.

Referenced by DrawCallBack().

GLfloat CBumpMapContainer::iLastFrameTime [private]
 

Time when the previous frame was rendered.

Definition at line 122 of file BumpMapcontainer.h.

Referenced by DrawCallBack().

TUint CBumpMapContainer::iFrame [private]
 

Counts the number of frames rendered.

Definition at line 124 of file BumpMapcontainer.h.

Referenced by DrawCallBack().

CAknAppUi* CBumpMapContainer::iAppUi [private]
 

Application UI class for querying the client rectangle size.

Definition at line 127 of file BumpMapcontainer.h.

Referenced by ConstructL(), and HandleResourceChange().

CBumpMap* CBumpMapContainer::iBumpMap
 

Used in DrawCallBack() method to do the actual OpenGL ES rendering.

Definition at line 133 of file BumpMapcontainer.h.

Referenced by Draw(), DrawCallBack(), SizeChanged(), and ~CBumpMapContainer().


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

© Nokia 2009

Back to top