Nokia N97 SDK
Example Applications Guide

SimpleLightContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : SimpleLightContainer.cpp
00004  *  Part of     : OpenGLEx / SimpleLight
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 "SimpleLightContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 // ================= MEMBER FUNCTIONS =======================
00018 
00019 // ---------------------------------------------------------
00020 // CSimpleLightContainer::ConstructL
00021 // Symbian 2nd phase constructor
00022 // ---------------------------------------------------------
00023 //
00024 void CSimpleLightContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
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     }
00202 
00203 // Destructor
00204 CSimpleLightContainer::~CSimpleLightContainer()
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     }
00220 
00221 // ---------------------------------------------------------
00222 // CSimpleLightContainer::SizeChanged()
00223 // Called by framework when the view size is changed
00224 // ---------------------------------------------------------
00225 //
00226 void CSimpleLightContainer::SizeChanged()
00227     {
00228     if( iOpenGlInitialized && iSimpleLight )
00229         {
00230         TSize size;
00231         size = this->Size();
00232 
00233         iSimpleLight->SetScreenSize( size.iWidth, size.iHeight );
00234         }
00235     }
00236 
00237 
00238 // ---------------------------------------------------------
00239 // CSimpleLightContainer::HandleResourceChange(
00240 //     TInt aType)
00241 // Dynamic screen resize changes by calling the
00242 // SetExtentToWholeScreen() method again.
00243 // ---------------------------------------------------------
00244 //
00245  void CSimpleLightContainer::HandleResourceChange(TInt aType)
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     }
00262 
00263 // ---------------------------------------------------------
00264 // CSimpleLightContainer::CountComponentControls() const
00265 // ---------------------------------------------------------
00266 //
00267 TInt CSimpleLightContainer::CountComponentControls() const
00268     {
00269     return 0; // return nbr of controls inside this container
00270     }
00271 
00272 // ---------------------------------------------------------
00273 // CSimpleLightContainer::ComponentControl(TInt aIndex) const
00274 // ---------------------------------------------------------
00275 //
00276 CCoeControl* CSimpleLightContainer::ComponentControl(TInt /*aIndex*/ ) const
00277     {
00278     return NULL;
00279     }
00280 
00281 // ---------------------------------------------------------
00282 // CSimpleLightContainer::Draw(const TRect& aRect) const
00283 // ---------------------------------------------------------
00284 //
00285 void CSimpleLightContainer::Draw(const TRect& /*aRect*/ ) const
00286     {
00287     // No need to implement anything here!
00288     }
00289 
00290 // ---------------------------------------------------------
00291 // CSimpleLightContainer::DrawCallBack( TAny* aInstance )
00292 // Called by the CPeriodic in order to draw the graphics
00293 // ---------------------------------------------------------
00294 //
00295 int CSimpleLightContainer::DrawCallBack( TAny* aInstance )
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     }
00323 
00324 // ---------------------------------------------------------
00325 // CSimpleLightContainer::HandleControlEventL(
00326 //     CCoeControl* aControl,TCoeEvent aEventType)
00327 // ---------------------------------------------------------
00328 //
00329 void CSimpleLightContainer::HandleControlEventL(
00330     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00331     {
00332     }
00333 
00334 // End of File

© Nokia 2009

Back to top