Nokia N97 SDK
Example Applications Guide

HierarchyContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : HierarchyContainer.cpp
00004  *  Part of     : OpenGLEx / Hierarchy
00005  *
00006  *  Copyright (c) 2004-2006 Nokia Corporation.
00007  *  This material, including documentation and any related
00008  *  computer programs, is protected by copyright controlled by
00009  *  Nokia Corporation.
00010  * ==============================================================================
00011  */
00012 
00013 // INCLUDE FILES
00014 #include "HierarchyContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 // ================= MEMBER FUNCTIONS =======================
00018 
00019 // ---------------------------------------------------------
00020 // CHierarchyContainer::ConstructL
00021 // Symbian two phased constructor
00022 // ---------------------------------------------------------
00023 //
00024 void CHierarchyContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
00025     {
00026     iAppUi = aAppUi;
00027     iOpenGlInitialized = EFalse;
00028 
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(KGetDisplayFailed, "eglGetDisplay failed");
00053         User::Panic( KGetDisplayFailed, 0 );
00054         }
00055 
00056     // Initialize display
00057     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00058         {
00059         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00060         User::Panic( KGetDisplayFailed, 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 )
00069         == 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        To get the best possible performance, choose
00087        an EGLConfig with a buffersize matching
00088        the current window's display mode*/
00089     TDisplayMode DMode = Window().DisplayMode();
00090     TInt BufferSize = 0;
00091 
00092     switch ( DMode )
00093          {
00094          case(EColor4K):
00095              BufferSize = 12;
00096              break;
00097          case(EColor64K):
00098              BufferSize = 16;
00099              break;
00100          case(EColor16M):
00101              BufferSize = 24;
00102              break;
00103          case(EColor16MU):
00104          case(EColor16MA):
00105              BufferSize = 32;
00106              break;
00107          default:
00108              _LIT(KDModeError,       "unsupported displaymode");
00109              User::Panic( KDModeError, 0 );
00110              break;
00111          }
00112 
00113     // Define properties for requesting a full-screen antialiased window surface
00114     const EGLint attrib_list_fsaa[] =
00115         {
00116                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00117         EGL_BUFFER_SIZE,  BufferSize,
00118         EGL_DEPTH_SIZE,   16,
00119                                 EGL_SAMPLE_BUFFERS, 1,
00120                                 EGL_SAMPLES,        4,
00121         EGL_NONE
00122         };
00123 
00124     // Define properties for requesting a non-antialiased window surface
00125     const EGLint attrib_list[] =
00126         {
00127                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00128         EGL_BUFFER_SIZE,  BufferSize,
00129         EGL_DEPTH_SIZE,   16,
00130         EGL_NONE
00131         };
00132 
00133     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00134     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00135         {
00136         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00137         User::Panic( KChooseConfigFailed, 0 );
00138         }
00139 
00140     // Check if no configurations were found
00141     if ( numOfConfigs == 0 )
00142         {
00143         // No configs with antialising were found. Try to get the non-antialiased config
00144                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00145                               {
00146                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00147                   User::Panic( KChooseConfigFailed, 0 );
00148                               }
00149 
00150                     if ( numOfConfigs == 0 )
00151                               {
00152                               // No configs found without antialiasing
00153                   _LIT( KNoConfig, "Can't find the requested config." );
00154                   User::Panic( KNoConfig, 0 );
00155                   }
00156               }
00157 
00158     Config = configList[0];  // Choose the best EGLConfig. EGLConfigs
00159                               // returned by eglChooseConfig are sorted so
00160                               // that the best matching EGLConfig is first in
00161                               // the list.
00162     User::Free( configList ); // Free configList, not used anymore.
00163 
00164     // Create a window where the graphics are blitted
00165     iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL );
00166     if ( iEglSurface == NULL )
00167         {
00168         _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00169         User::Panic( KCreateWindowSurfaceFailed, 0 );
00170         }
00171 
00172     // Create a rendering context
00173     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00174     if ( iEglContext == NULL )
00175         {
00176         _LIT(KCreateContextFailed, "eglCreateContext failed");
00177         User::Panic( KCreateContextFailed, 0 );
00178         }
00179 
00180     /* Make the context current. Binds context to the current rendering thread
00181        and surface.*/
00182     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext )
00183         == EGL_FALSE )
00184         {
00185         _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00186         User::Panic( KMakeCurrentFailed, 0 );
00187         }
00188 
00189     TSize size;
00190     size = this->Size();
00191 
00192     iHierarchy = CHierarchy::NewL( size.iWidth, size.iHeight );
00193     iHierarchy->AppInit();
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( CHierarchyContainer::DrawCallBack, this ) );
00201     }
00202 
00203 // Destructor
00204 CHierarchyContainer::~CHierarchyContainer()
00205     {
00206     delete iPeriodic;
00207 
00208     /* AppExit call is made to release
00209        any allocations made in AppInit. */
00210     if ( iHierarchy )
00211         {
00212         iHierarchy->AppExit();
00213         delete iHierarchy;
00214         }
00215 
00216     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00217     eglDestroySurface( iEglDisplay, iEglSurface );
00218     eglDestroyContext( iEglDisplay, iEglContext );
00219     eglTerminate( iEglDisplay );                   // Release resources associated
00220                                                    // with EGL and OpenGL ES
00221     }
00222 
00223 // ---------------------------------------------------------
00224 // CHierarchyContainer::SizeChanged()
00225 // Called by framework when the view size is changed
00226 // ---------------------------------------------------------
00227 //
00228 void CHierarchyContainer::SizeChanged()
00229     {
00230     if( iOpenGlInitialized && iHierarchy )
00231         {
00232         TSize size;
00233         size = this->Size();
00234 
00235         iHierarchy->SetScreenSize( size.iWidth, size.iHeight );
00236         }
00237     }
00238 
00239 
00240 // ---------------------------------------------------------
00241 // CHierarchyContainer::HandleResourceChange(
00242 //     TInt aType)
00243 // Dynamic screen resize changes by calling the
00244 // SetExtentToWholeScreen() method again.
00245 // ---------------------------------------------------------
00246 //
00247  void CHierarchyContainer::HandleResourceChange(TInt aType)
00248     {
00249         switch( aType )
00250         {
00251             case KEikDynamicLayoutVariantSwitch:
00252                 // Do not go full-screen in touch devices.    
00253             if (AknLayoutUtils::PenEnabled())
00254             {
00255                 SetRect(iAppUi->ClientRect());
00256             }
00257             else
00258             {
00259                 SetExtentToWholeScreen();                // Take the whole screen into use
00260             }
00261                 break;
00262             }
00263     }
00264 
00265 
00266 // ---------------------------------------------------------
00267 // CHierarchyContainer::CountComponentControls() const
00268 // ---------------------------------------------------------
00269 //
00270 TInt CHierarchyContainer::CountComponentControls() const
00271     {
00272     return 0; // return nbr of controls inside this container
00273     }
00274 
00275 // ---------------------------------------------------------
00276 // CHierarchyContainer::ComponentControl(TInt aIndex) const
00277 // ---------------------------------------------------------
00278 //
00279 CCoeControl* CHierarchyContainer::ComponentControl(TInt /*aIndex*/ ) const
00280     {
00281     return NULL;
00282     }
00283 
00284 // ---------------------------------------------------------
00285 // CHierarchyContainer::Draw(const TRect& aRect) const
00286 // ---------------------------------------------------------
00287 //
00288 void CHierarchyContainer::Draw(const TRect& /*aRect*/ ) const
00289     {
00290     // No need to implement anything here!
00291     }
00292 
00293 // ---------------------------------------------------------
00294 // CHierarchyContainer::DrawCallBack( TAny* aInstance )
00295 // Called by the CPeriodic in order to draw the graphics
00296 // ---------------------------------------------------------
00297 //
00298 int CHierarchyContainer::DrawCallBack( TAny* aInstance )
00299     {
00300     CHierarchyContainer* instance = (CHierarchyContainer*) aInstance;
00301     instance->iFrame++;
00302 
00303     // Call the main OpengGL ES Symbian rendering 'loop'
00304     instance->iHierarchy->AppCycle( instance->iFrame );
00305 
00306     // Call eglSwapBuffers, which blit the graphics to the window
00307     eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00308 
00309     // To keep the background light on
00310     if ( !(instance->iFrame%100) )
00311         {
00312         User::ResetInactivityTime();
00313         }
00314 
00315     /* Suspend the current thread for a short while. Give some time
00316        to other threads and AOs, avoids the ViewSrv error in ARMI and
00317        THUMB release builds. One may try to decrease the callback
00318        function instead of this. */
00319     if ( !(instance->iFrame%50) )
00320         {
00321         User::After(0);
00322         }
00323 
00324     return 0;
00325     }
00326 
00327 // ---------------------------------------------------------
00328 // CHierarchyContainer::HandleControlEventL(
00329 //     CCoeControl* aControl,TCoeEvent aEventType)
00330 // ---------------------------------------------------------
00331 //
00332 void CHierarchyContainer::HandleControlEventL(
00333     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00334     {
00335     }
00336 
00337 // End of File

© Nokia 2009

Back to top