Nokia N97 SDK
Example Applications Guide

CSimpleLightContainer Class Reference

#include <SimpleLightContainer.h>

Inheritance diagram for CSimpleLightContainer:

List of all members.

Detailed Description

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

Definition at line 29 of file SimpleLightContainer.h.


Public Member Functions

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

Static Public Member Functions

static int  DrawCallBack (TAny *aInstance)

Public Attributes

TInt  iFrame
CSimpleLight *  iSimpleLight

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
CAknAppUi *  iAppUi


Constructor & Destructor Documentation

CSimpleLightContainer::~CSimpleLightContainer (   )  [virtual]
 

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

Definition at line 204 of file SimpleLightContainer.cpp.

References CSimpleLight::AppExit(), iEglContext, iEglDisplay, iEglSurface, iPeriodic, and iSimpleLight.

00205     {
00206     delete iPeriodic;
00207 
00208     if ( iSimpleLight )
00209         {
00210         iSimpleLight->AppExit();
00211         delete iSimpleLight;
00212         }
00213 
00214     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00215     eglDestroySurface( iEglDisplay, iEglSurface );
00216     eglDestroyContext( iEglDisplay, iEglContext );
00217     eglTerminate( iEglDisplay );                   // Release resources associated
00218                                                    // with EGL and OpenGL ES
00219     }


Member Function Documentation

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

EPOC default constructor. Initializes the OpenGL ES and creates the rendering context.

Parameters:
aRect  Screen rectangle for container.

Definition at line 24 of file SimpleLightContainer.cpp.

References CSimpleLight::AppInit(), DrawCallBack(), iAppUi, iEglContext, iEglDisplay, iEglSurface, iFrame, iOpenGlInitialized, iPeriodic, iSimpleLight, and CSimpleLight::NewL().

Referenced by CSimpleLightAppUi::ConstructL().

00025     {
00026     iAppUi = aAppUi;
00027     CreateWindowL();
00028     iOpenGlInitialized = EFalse;
00029 
00030    // Do not go to full-screen in touch devices.    
00031     if (AknLayoutUtils::PenEnabled())
00032     {
00033         SetRect(aRect);
00034     }
00035     else
00036     {
00037         SetExtentToWholeScreen();                // Take the whole screen into use
00038     }
00039     ActivateL();
00040 
00041     iFrame = 0;                              // Frame counter
00042 
00043     EGLConfig Config;                        // Describes the format, type and
00044                                              // size of the color buffers and
00045                                              // ancillary buffers for EGLSurface
00046 
00047     // Get the display for drawing graphics
00048     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00049     if ( iEglDisplay == NULL )
00050         {
00051         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00052         User::Panic( KGetDisplayFailed, 0 );
00053         }
00054 
00055     // Initialize display
00056     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00057         {
00058         _LIT(KInitializeFailed, "eglInitialize failed");
00059         User::Panic( KInitializeFailed, 0 );
00060         }
00061 
00062     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00063     EGLint numOfConfigs = 0;
00064     EGLint configSize   = 0;
00065 
00066     // Get the number of possible EGLConfigs
00067     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs )
00068         == EGL_FALSE )
00069         {
00070         _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00071         User::Panic( KGetConfigsFailed, 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(KConfigAllocFailed, "config alloc failed");
00081         User::Panic( KConfigAllocFailed, 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     if ( iEglSurface == NULL )
00166         {
00167         _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00168         User::Panic( KCreateWindowSurfaceFailed, 0 );
00169         }
00170 
00171     // Create a rendering context
00172     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00173     if ( iEglContext == NULL )
00174         {
00175         _LIT(KCreateContextFailed, "eglCreateContext failed");
00176         User::Panic( KCreateContextFailed, 0 );
00177         }
00178 
00179     /* Make the context current. Binds context to the current rendering thread
00180        and surface.*/
00181     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext )
00182         == EGL_FALSE )
00183         {
00184         _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00185         User::Panic( KMakeCurrentFailed, 0 );
00186         }
00187 
00188 
00189     TSize size;
00190     size = this->Size();
00191 
00192     iSimpleLight = CSimpleLight::NewL( size.iWidth, size.iHeight ); // Create an instance of SimpleLight
00193     iSimpleLight->AppInit();                                       // Initialize OpenGL ES
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,
00200                       TCallBack( CSimpleLightContainer::DrawCallBack, this ) );
00201     }

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

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

Parameters:
aInstance  Pointer to this instance of CSimpleLightContainer.

Definition at line 295 of file SimpleLightContainer.cpp.

References CSimpleLight::AppCycle(), iEglDisplay, iEglSurface, iFrame, and iSimpleLight.

Referenced by ConstructL().

00296     {
00297     CSimpleLightContainer* instance = (CSimpleLightContainer*) aInstance;
00298     instance->iFrame++;
00299 
00300     // Call the main OpenGL ES Symbian rendering 'loop'
00301     instance->iSimpleLight->AppCycle( instance->iFrame );
00302 
00303     // Call eglSwapBuffers, which blit the graphics to the window
00304     eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00305 
00306     // To keep the background light on
00307     if ( !(instance->iFrame%100) )
00308         {
00309         User::ResetInactivityTime();
00310         }
00311 
00312     /* Suspend the current thread for a short while. Give some time
00313        to other threads and AOs, avoids the ViewSrv error in ARMI and
00314        THUMB release builds. One may try to decrease the callback
00315        function instead of this. */
00316     if ( !(instance->iFrame%50) )
00317          {
00318          User::After(0);
00319          }
00320 
00321     return 0;
00322     }

void CSimpleLightContainer::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 226 of file SimpleLightContainer.cpp.

References iOpenGlInitialized, iSimpleLight, and CSimpleLight::SetScreenSize().

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

void CSimpleLightContainer::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 245 of file SimpleLightContainer.cpp.

References iAppUi.

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

TInt CSimpleLightContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 267 of file SimpleLightContainer.cpp.

00268     {
00269     return 0; // return nbr of controls inside this container
00270     }

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

Method from CCoeControl. Does nothing in this implementation.

Definition at line 276 of file SimpleLightContainer.cpp.

00277     {
00278     return NULL;
00279     }

void CSimpleLightContainer::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 285 of file SimpleLightContainer.cpp.

00286     {
00287     // No need to implement anything here!
00288     }

void CSimpleLightContainer::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 329 of file SimpleLightContainer.cpp.

00331     {
00332     }


Member Data Documentation

EGLDisplay CSimpleLightContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 99 of file SimpleLightContainer.h.

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

EGLSurface CSimpleLightContainer::iEglSurface [private]
 

Window surface that is the target of OpenGL ES graphics rendering.

Definition at line 102 of file SimpleLightContainer.h.

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

EGLContext CSimpleLightContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 105 of file SimpleLightContainer.h.

Referenced by ConstructL(), and ~CSimpleLightContainer().

CPeriodic* CSimpleLightContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 108 of file SimpleLightContainer.h.

Referenced by ConstructL(), and ~CSimpleLightContainer().

TBool CSimpleLightContainer::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 114 of file SimpleLightContainer.h.

Referenced by ConstructL(), and SizeChanged().

CAknAppUi* CSimpleLightContainer::iAppUi [private]
 

Application UI class for querying the client rectangle size.

Definition at line 117 of file SimpleLightContainer.h.

Referenced by ConstructL(), and HandleResourceChange().

TInt CSimpleLightContainer::iFrame
 

Frame counter variable, used in the animation.

Definition at line 122 of file SimpleLightContainer.h.

Referenced by ConstructL(), and DrawCallBack().

CSimpleLight* CSimpleLightContainer::iSimpleLight
 

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

Definition at line 125 of file SimpleLightContainer.h.

Referenced by ConstructL(), DrawCallBack(), CSimpleLightAppUi::DynInitMenuPaneL(), CSimpleLightAppUi::HandleCommandL(), SizeChanged(), and ~CSimpleLightContainer().


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

© Nokia 2009

Back to top