Nokia N97 SDK
Example Applications Guide

ShadowsContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : ShadowsContainer.cpp
00004  *  Part of     : OpenGLEx / Shadows
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 "ShadowsContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 // ================= MEMBER FUNCTIONS =======================
00018 
00019 // ---------------------------------------------------------
00020 // CShadowsContainer::ConstructL
00021 // EPOC two phased constructor
00022 // ---------------------------------------------------------
00023 //
00024 void CShadowsContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
00025     {
00026     iAppUi = aAppUi;
00027     iOpenGlInitialized = EFalse;
00028     CreateWindowL();
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 
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(KInitializeFailed, "eglInitialize failed");
00060         User::Panic( KInitializeFailed, 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     iShadows = CShadows::NewL( size.iWidth, size.iHeight );
00193     iShadows->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, TCallBack( DrawCallBack, this ) );
00200 }
00201 
00202 // Destructor
00203 CShadowsContainer::~CShadowsContainer()
00204     {
00205     delete iPeriodic;
00206 
00207     /* AppExit call is made to release
00208        any allocations made in AppInit. */
00209     if ( iShadows )
00210         {
00211         iShadows->AppExit();
00212         delete iShadows;
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 
00222 // ---------------------------------------------------------
00223 // CShadowsContainer::SizeChanged
00224 // Called by framework when the view size is changed
00225 // ---------------------------------------------------------
00226 //
00227 void CShadowsContainer::SizeChanged()
00228     {
00229     if( iOpenGlInitialized && iShadows )
00230         {
00231         TSize size;
00232         size = this->Size();
00233 
00234         iShadows->SetScreenSize( size.iWidth, size.iHeight );
00235         }
00236     }
00237 
00238 
00239 // ---------------------------------------------------------
00240 // CShadowsContainer::HandleResourceChange(
00241 //     TInt aType)
00242 // Dynamic screen resize changes by calling the
00243 // SetExtentToWholeScreen() method again.
00244 // ---------------------------------------------------------
00245 //
00246  void CShadowsContainer::HandleResourceChange(TInt aType)
00247     {
00248         switch( aType )
00249         {
00250             case KEikDynamicLayoutVariantSwitch:
00251                 // Do not go full-screen in touch devices.    
00252             if (AknLayoutUtils::PenEnabled())
00253             {
00254                 SetRect(iAppUi->ClientRect());
00255             }
00256             else
00257             {
00258                 SetExtentToWholeScreen();                // Take the whole screen into use
00259             }
00260                 break;
00261             }
00262     }
00263 
00264 // ---------------------------------------------------------
00265 // CShadowsContainer::CountComponentControls
00266 // ---------------------------------------------------------
00267 //
00268 TInt CShadowsContainer::CountComponentControls() const
00269     {
00270     return 0; // return nbr of controls inside this container
00271     }
00272 
00273 // ---------------------------------------------------------
00274 // CShadowsContainer::ComponentControl
00275 // ---------------------------------------------------------
00276 //
00277 CCoeControl* CShadowsContainer::ComponentControl(TInt /*aIndex*/ ) const
00278     {
00279     return NULL;
00280     }
00281 
00282 // ---------------------------------------------------------
00283 // CShadowsContainer::Draw
00284 // ---------------------------------------------------------
00285 //
00286 void CShadowsContainer::Draw(const TRect& /*aRect*/ ) const
00287     {
00288     // No need to implement anything here!
00289     }
00290 
00291 // ---------------------------------------------------------
00292 // CShadowsContainer::DrawCallBack
00293 // Called by the CPeriodic in order to draw the graphics
00294 // ---------------------------------------------------------
00295 //
00296 int CShadowsContainer::DrawCallBack( TAny* aInstance )
00297     {
00298     CShadowsContainer* instance = (CShadowsContainer*) aInstance;
00299     instance->iFrame++;
00300 
00301     // Call the main OpenGL ES Symbian rendering 'loop'
00302     instance->iShadows->AppCycle( instance->iFrame );
00303 
00304     // Call eglSwapBuffers, which blit the graphics to the window
00305     eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00306 
00307     // To keep the background light on
00308     if ( !(instance->iFrame%100) )
00309         {
00310         User::ResetInactivityTime();
00311         }
00312 
00313     /* Suspend the current thread for a short while. Give some time
00314        to other threads and AOs, avoids the ViewSrv error in ARMI and
00315        THUMB release builds. One may try to decrease the callback
00316        function instead of this. */
00317     if ( !(instance->iFrame%50) )
00318         {
00319         User::After(0);
00320         }
00321     return 0;
00322     }
00323 
00324 // ---------------------------------------------------------
00325 // CShadowsContainer::HandleControlEventL
00326 // ---------------------------------------------------------
00327 //
00328 void CShadowsContainer::HandleControlEventL(
00329     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00330     {
00331     }
00332 
00333 // End of File

© Nokia 2009

Back to top