Nokia N97 SDK
Example Applications Guide

CEnvMappingContainer Class Reference

#include <EnvMappingContainer.h>

Inheritance diagram for CEnvMappingContainer:

List of all members.

Detailed Description

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

Definition at line 30 of file EnvMappingContainer.h.


Public Member Functions

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

Static Public Member Functions

static int  DrawCallBack (TAny *aInstance)

Public Attributes

CEnvMapping *  iEnvMapping

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
TInt  iFrame
CPeriodic *  iPeriodic
TBool  iOpenGlInitialized
CAknAppUi *  iAppUi


Constructor & Destructor Documentation

CEnvMappingContainer::~CEnvMappingContainer (   )  [virtual]
 

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

Definition at line 203 of file EnvMappingContainer.cpp.

References CEnvMapping::AppExit(), iEglContext, iEglDisplay, iEglSurface, iEnvMapping, and iPeriodic.

00204 {
00205     delete iPeriodic;
00206 
00207     /* AppExit call is made to release any allocations made in AppInit.
00208      This call has to be made here before we destroy the rendering context. */
00209     if ( !(iEnvMapping == NULL) )
00210     {
00211         iEnvMapping->AppExit();
00212         delete iEnvMapping;
00213     }
00214 
00215     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00216     eglDestroySurface( iEglDisplay, iEglSurface );
00217     eglDestroyContext( iEglDisplay, iEglContext );
00218     eglTerminate( iEglDisplay );                   // Release resources associated
00219                                                    // with EGL and OpenGL ES
00220 
00221 }


Member Function Documentation

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

EPOC default constructor. Initializes the OpenGL ES for rendering.

Parameters:
aRect  Screen rectangle for container.

Definition at line 25 of file EnvMappingContainer.cpp.

References iAppUi, iEglDisplay, iFrame, and iOpenGlInitialized.

Referenced by CEnvMappingAppUi::ConstructL().

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

int CEnvMappingContainer::DrawCallBack (  TAny *  aInstance  )  [static]
 

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

Parameters:
aInstance  Pointer to this instance of CEnvMappingContainer.

Definition at line 271 of file EnvMappingContainer.cpp.

References CEnvMapping::AppCycle(), iEglDisplay, iEglSurface, iEnvMapping, and iFrame.

00272 {
00273     CEnvMappingContainer* instance = (CEnvMappingContainer*) aInstance;
00274 
00275     if ( instance->iEnvMapping->GetState() == CEnvMapping::ERunning )
00276     {
00277         instance->iFrame++;
00278 
00279         /* Call the main OpenGL ES Symbian rendering 'loop' */
00280         instance->iEnvMapping->AppCycle( instance->iFrame );
00281 
00282         /* Call eglSwapBuffers, which blit the graphics to the window */
00283         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00284 
00285         /* To keep the background light on */
00286         if(!(instance->iFrame%100))
00287         {
00288             User::ResetInactivityTime();
00289         }
00290 
00291         /* Suspend the current thread for a short while. Give some time
00292            to other threads and AOs, avoids the ViewSrv error in ARMI and
00293            THUMB release builds. One may try to decrease the callback
00294            function instead of this. */
00295         if(!(instance->iFrame%50))
00296         {
00297             User::After(0);
00298         }
00299     }
00300 
00301     return 0;
00302 }

void CEnvMappingContainer::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 228 of file EnvMappingContainer.cpp.

References iEnvMapping, iOpenGlInitialized, and CEnvMapping::SetScreenSize().

00229     {
00230     if( iOpenGlInitialized && iEnvMapping )
00231         {
00232         TSize size;
00233         size = this->Size();
00234 
00235         iEnvMapping->SetScreenSize( size.iWidth, size.iHeight );
00236         }
00237     }

void CEnvMappingContainer::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 321 of file EnvMappingContainer.cpp.

References iAppUi.

00322     {
00323         switch( aType )
00324         {
00325             case KEikDynamicLayoutVariantSwitch:
00326                 // Do not go full-screen in touch devices.    
00327             if (AknLayoutUtils::PenEnabled())
00328             {
00329                 SetRect(iAppUi->ClientRect());
00330             }
00331             else
00332             {
00333                 SetExtentToWholeScreen();                // Take the whole screen into use
00334             }
00335                 break;
00336             }
00337     }

TInt CEnvMappingContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 243 of file EnvMappingContainer.cpp.

00244 {
00245     return 0;
00246 }

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

Method from CCoeControl. Does nothing in this implementation.

Definition at line 252 of file EnvMappingContainer.cpp.

00253 {
00254     return NULL;
00255 }

void CEnvMappingContainer::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 261 of file EnvMappingContainer.cpp.

00262 {
00263     // No need to implement anything here!
00264 }

void CEnvMappingContainer::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 309 of file EnvMappingContainer.cpp.

00311 {
00312 }


Member Data Documentation

EGLDisplay CEnvMappingContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 100 of file EnvMappingContainer.h.

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

EGLSurface CEnvMappingContainer::iEglSurface [private]
 

Window surface where the OpenGL ES rendering is blitted to.

Definition at line 103 of file EnvMappingContainer.h.

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

EGLContext CEnvMappingContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 106 of file EnvMappingContainer.h.

Referenced by ~CEnvMappingContainer().

TInt CEnvMappingContainer::iFrame [private]
 

Frame counter variable, used in the animation.

Definition at line 109 of file EnvMappingContainer.h.

Referenced by ConstructL(), and DrawCallBack().

CPeriodic* CEnvMappingContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 112 of file EnvMappingContainer.h.

Referenced by ~CEnvMappingContainer().

TBool CEnvMappingContainer::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 118 of file EnvMappingContainer.h.

Referenced by ConstructL(), and SizeChanged().

CAknAppUi* CEnvMappingContainer::iAppUi [private]
 

Application UI class for querying the client rectangle size.

Definition at line 121 of file EnvMappingContainer.h.

Referenced by ConstructL(), and HandleResourceChange().

CEnvMapping* CEnvMappingContainer::iEnvMapping
 

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

Definition at line 127 of file EnvMappingContainer.h.

Referenced by DrawCallBack(), CEnvMappingAppUi::HandleCommandL(), CEnvMappingAppUi::HandleKeyEventL(), SizeChanged(), and ~CEnvMappingContainer().


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

© Nokia 2009

Back to top