S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CHierarchyContainer Class Reference

#include <HierarchyContainer.h>

List of all members.

Public Member Functions

void ConstructL (const TRect &aRect)
virtual ~CHierarchyContainer ()

Static Public Member Functions

static int DrawCallBack (TAny *aInstance)

Public Attributes

CHierarchy * iHierarchy

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
TInt iFrame


Detailed Description

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

Definition at line 31 of file HierarchyContainer.h.


Constructor & Destructor Documentation

CHierarchyContainer::~CHierarchyContainer (   )  [virtual]
 

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

Definition at line 164 of file HierarchyContainer.cpp.

References CHierarchy::AppExit(), iEglContext, iEglDisplay, iEglSurface, iHierarchy, and iPeriodic.

00165     {
00166     delete iPeriodic;
00167 
00168     /* AppExit call is made to release 
00169        any allocations made in AppInit. */
00170     if ( iHierarchy )
00171         {
00172         iHierarchy->AppExit();
00173         delete iHierarchy;
00174         }
00175 
00176     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00177     eglDestroySurface( iEglDisplay, iEglSurface ); 
00178     eglDestroyContext( iEglDisplay, iEglContext );
00179     eglTerminate( iEglDisplay );                   // Release resources associated
00180                                                    // with EGL and OpenGL ES
00181     }


Member Function Documentation

void CHierarchyContainer::ConstructL (  const TRect &  aRect  ) 
 

EPOC default constructor. Initializes the OpenGL ES for rendering to the window surface.

Parameters:
aRect Screen rectangle for container.

Definition at line 26 of file HierarchyContainer.cpp.

References CHierarchy::AppInit(), DrawCallBack(), iEglContext, iEglDisplay, iEglSurface, iFrame, iHierarchy, iOpenGlInitialized, iPeriodic, and CHierarchy::NewL().

00027     {
00028     iOpenGlInitialized = EFalse;
00029 
00030     CreateWindowL();
00031 
00032     SetExtentToWholeScreen();                // Take the whole screen into use
00033     ActivateL();
00034     
00035     iFrame = 0;                              // Frame counter
00036     
00037     EGLConfig Config;                        // Describes the format, type and 
00038                                              // size of the color buffers and
00039                                              // ancillary buffers for EGLSurface
00040 
00041     // Get the display for drawing graphics
00042     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00043     if ( iEglDisplay == NULL )
00044         {
00045         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00046         User::Panic( KGetDisplayFailed, 0 );
00047         }
00048     
00049     // Initialize display 
00050     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00051         {
00052         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00053         User::Panic( KGetDisplayFailed, 0 );
00054         }
00055     
00056     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00057     EGLint numOfConfigs = 0; 
00058     EGLint configSize   = 0;
00059 
00060     // Get the number of possible EGLConfigs 
00061     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) 
00062         == EGL_FALSE )
00063         {
00064         _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00065         User::Panic( KGetConfigsFailed, 0 );
00066         }
00067     
00068     configSize = numOfConfigs;
00069 
00070     // Allocate memory for the configList 
00071     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00072     if ( configList == NULL )
00073         {
00074         _LIT(KConfigAllocFailed, "config alloc failed");
00075         User::Panic( KConfigAllocFailed, 0 );
00076         }
00077     
00078     /* Define properties for the wanted EGLSurface.
00079        To get the best possible performance, choose
00080        an EGLConfig with a buffersize matching
00081        the current window's display mode*/
00082     TDisplayMode DMode = Window().DisplayMode();
00083     TInt BufferSize = 0;
00084 
00085     switch ( DMode )
00086          {
00087          case(EColor4K):
00088              BufferSize = 12;
00089              break;
00090          case(EColor64K):
00091              BufferSize = 16;
00092              break;
00093          case(EColor16M):
00094              BufferSize = 24;
00095              break;
00096          case(EColor16MU):
00097              BufferSize = 32;
00098              break;
00099          default:
00100              _LIT(KDModeError,       "unsupported displaymode");
00101              User::Panic( KDModeError, 0 );
00102              break;
00103          }
00104 
00105     // Define properties for the wanted EGLSurface 
00106     const EGLint attrib_list[] = { EGL_BUFFER_SIZE,BufferSize,   
00107                                    EGL_DEPTH_SIZE,16,    // 16-bit depthbuffer
00108                                    EGL_NONE };
00109 
00110     // Choose an EGLConfig that best matches to the properties in attrib_list 
00111     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, 
00112                          &numOfConfigs ) == EGL_FALSE )
00113         {
00114         _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00115         User::Panic( KChooseConfigFailed, 0 );
00116         }
00117     
00118     Config = configList[0];  // Choose the best EGLConfig. EGLConfigs 
00119                               // returned by eglChooseConfig are sorted so 
00120                               // that the best matching EGLConfig is first in
00121                               // the list.
00122     User::Free( configList ); // Free configList, not used anymore.
00123 
00124     // Create a window where the graphics are blitted 
00125     iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL );
00126     if ( iEglSurface == NULL )
00127         {
00128         _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00129         User::Panic( KCreateWindowSurfaceFailed, 0 );
00130         }
00131     
00132     // Create a rendering context 
00133     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00134     if ( iEglContext == NULL )
00135         {
00136         _LIT(KCreateContextFailed, "eglCreateContext failed");
00137         User::Panic( KCreateContextFailed, 0 );
00138         }
00139     
00140     /* Make the context current. Binds context to the current rendering thread
00141        and surface.*/
00142     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) 
00143         == EGL_FALSE )
00144         {
00145         _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00146         User::Panic( KMakeCurrentFailed, 0 );
00147         }
00148     
00149     TSize size;
00150     size = this->Size();
00151 
00152     iHierarchy = CHierarchy::NewL( size.iWidth, size.iHeight ); 
00153     iHierarchy->AppInit();
00154     
00155     iOpenGlInitialized = ETrue;
00156 
00157     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle ); // Create an active object for
00158                                                            // animating the scene
00159     iPeriodic->Start( 100, 100, 
00160                       TCallBack( CHierarchyContainer::DrawCallBack, this ) );
00161     }

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

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

Parameters:
aInstance Pointer to this instance of CHierarchyContainer.

Definition at line 251 of file HierarchyContainer.cpp.

References CHierarchy::AppCycle(), iEglDisplay, iEglSurface, iFrame, and iHierarchy.

00252     {
00253     CHierarchyContainer* instance = (CHierarchyContainer*) aInstance;
00254     instance->iFrame++;
00255     
00256     // Call the main OpengGL ES Symbian rendering 'loop' 
00257     instance->iHierarchy->AppCycle( instance->iFrame );
00258     
00259     // Call eglSwapBuffers, which blit the graphics to the window 
00260     eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00261     
00262     // To keep the background light on 
00263     if ( !(instance->iFrame%100) )
00264         {
00265         User::ResetInactivityTime();
00266         }
00267     
00268     /* Suspend the current thread for a short while. Give some time 
00269        to other threads and AOs, avoids the ViewSrv error in ARMI and
00270        THUMB release builds. One may try to decrease the callback 
00271        function instead of this. */ 
00272     if ( !(instance->iFrame%50) )
00273         {
00274         User::After(0);
00275         }
00276     
00277     return 0;
00278     }

void CHierarchyContainer::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 188 of file HierarchyContainer.cpp.

References iHierarchy, iOpenGlInitialized, and CHierarchy::SetScreenSize().

00189     {
00190     if( iOpenGlInitialized && iHierarchy )
00191         {
00192         TSize size;
00193         size = this->Size();
00194         
00195         iHierarchy->SetScreenSize( size.iWidth, size.iHeight );
00196         }
00197     }

void CHierarchyContainer::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 207 of file HierarchyContainer.cpp.

00208     {
00209         switch( aType )
00210         {
00211             case KEikDynamicLayoutVariantSwitch:
00212                     SetExtentToWholeScreen();
00213                     break;
00214             }
00215     }

TInt CHierarchyContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 222 of file HierarchyContainer.cpp.

00223     {
00224     return 0; // return nbr of controls inside this container
00225     }

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

Method from CCoeControl. Does nothing in this implementation.

Definition at line 231 of file HierarchyContainer.cpp.

00232     {
00233     return NULL;
00234     }

void CHierarchyContainer::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 240 of file HierarchyContainer.cpp.

00241     {
00242     CWindowGc& gc = SystemGc();
00243     gc.Clear( Rect() );
00244     }

void CHierarchyContainer::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 285 of file HierarchyContainer.cpp.

00287     {
00288     }


Member Data Documentation

EGLDisplay CHierarchyContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 101 of file HierarchyContainer.h.

EGLSurface CHierarchyContainer::iEglSurface [private]
 

Window surface where the OpenGL ES rendering is blitted to.

Definition at line 104 of file HierarchyContainer.h.

EGLContext CHierarchyContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 107 of file HierarchyContainer.h.

CPeriodic* CHierarchyContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 110 of file HierarchyContainer.h.

TBool CHierarchyContainer::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 116 of file HierarchyContainer.h.

TInt CHierarchyContainer::iFrame [private]
 

Frame counter variable, used in the animation.

Definition at line 119 of file HierarchyContainer.h.

CHierarchy* CHierarchyContainer::iHierarchy
 

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

Definition at line 124 of file HierarchyContainer.h.


© Nokia 2006

Back to top