S60 3rd Edition SDK for Symbian OS
Example Applications Guide

LensFlareContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : LensFlareContainer.cpp
00004 *  Part of     : OpenGLEx / LensFlare
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include "LensFlareContainer.h"
00018 
00019 #include <eiklabel.h>  // for example label control
00020 
00021 // EXTERNAL DATA STRUCTURES
00022 
00023 // EXTERNAL FUNCTION PROTOTYPES  
00024 
00025 // CONSTANTS
00026 
00027 // MACROS
00028 
00029 // LOCAL CONSTANTS AND MACROS
00030 
00031 // MODULE DATA STRUCTURES
00032 
00033 // LOCAL FUNCTION PROTOTYPES
00034 
00035 // FORWARD DECLARATIONS
00036 
00037 // ================= MEMBER FUNCTIONS =======================
00038 
00039 // ---------------------------------------------------------
00040 // CLensFlareContainer::ConstructL(const TRect& aRect)
00041 // EPOC two phased constructor
00042 // ---------------------------------------------------------
00043 //
00044 void CLensFlareContainer::ConstructL(const TRect& /*aRect*/)
00045     {
00046     iOpenGlInitialized = EFalse;
00047     CreateWindowL();
00048 
00049     SetExtentToWholeScreen();                // Take the whole screen into use
00050     ActivateL();
00051 
00052     
00053     iFrame = 0;                              // Initialize frame counter
00054     
00055     EGLConfig config;                        // Describes the format, type and 
00056                                              // size of the color buffers and
00057                                              // ancillary buffers for EGLSurface
00058 
00059     /* Get the display for drawing graphics*/
00060     iEglDisplay = eglGetDisplay( NULL );
00061     if ( iEglDisplay == NULL )
00062         {
00063                 _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00064         User::Panic( KGetDisplayFailed, 0 );
00065         }
00066     
00067     /* Initialize display */
00068     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00069         {
00070                 _LIT(KInitializeFailed, "eglInitialize failed");
00071         User::Panic( KInitializeFailed, 0 );
00072         }
00073     
00074     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00075     EGLint numOfConfigs = 0; 
00076     EGLint configSize   = 0;
00077 
00078     /* Get the number of possible EGLConfigs */
00079     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00080         {
00081                 _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00082         User::Panic( KGetConfigsFailed, 0 );
00083         }
00084     
00085     configSize = numOfConfigs;
00086 
00087     /* Allocate memory for the configList */
00088     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00089     if ( configList == NULL )
00090         {
00091                 _LIT(KConfigAllocFailed, "config alloc failed");
00092         User::Panic( KConfigAllocFailed, 0 );
00093         }
00094     
00095     /* Define properties for the wanted EGLSurface.
00096        To get the best possible performance, choose
00097        an EGLConfig with a buffersize matching
00098        the current window's display mode*/
00099     TDisplayMode DMode = Window().DisplayMode();
00100     TInt BufferSize = 0;
00101 
00102     switch(DMode)
00103     {
00104     case(EColor4K):
00105         BufferSize = 12;
00106         break;
00107     case(EColor64K):
00108         BufferSize = 16;
00109         break;
00110     case(EColor16M):
00111         BufferSize = 24;
00112         break;
00113     case(EColor16MU):
00114         BufferSize = 32;
00115         break;
00116     default:
00117         _LIT(KDModeError, "unsupported displaymode");
00118         User::Panic( KDModeError, 0 );
00119         break;
00120     }
00121 
00122     const EGLint attrib_list[] = 
00123         { 
00124         EGL_BUFFER_SIZE, BufferSize,   
00125         EGL_DEPTH_SIZE, 16,    
00126         EGL_NONE 
00127         };
00128 
00129     /* Choose an EGLConfig that best matches to the properties in attrib_list */
00130     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00131                 {
00132                 _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00133                 User::Panic( KChooseConfigFailed, 0 );
00134                 }
00135     if ( numOfConfigs == 0 )
00136             {
00137                 _LIT(KNoConfig, "Can't find the requested config.");
00138                 User::Panic( KNoConfig, 0 );
00139                 }
00140 
00141     config = configList[0];   // Choose the best EGLConfig. EGLConfigs 
00142                               // returned by eglChooseConfig are sorted so 
00143                               // that the best matching EGLConfig is first in
00144                               // the list.
00145     User::Free( configList ); // Free configList, not used anymore.
00146 
00147     /* Create a window where the graphics are blitted */
00148     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00149     if ( iEglSurface == NULL )
00150         {
00151                 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00152         User::Panic( KCreateWindowSurfaceFailed, 0 );
00153         }
00154     
00155     /* Create a rendering context */
00156     iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00157     if ( iEglContext == NULL )
00158         {
00159                 _LIT(KCreateContextFailed, "eglCreateContext failed");
00160         User::Panic( KCreateContextFailed, 0 );
00161         }
00162     
00163     /* Make the context current. Binds context to the current rendering thread
00164        and surface.*/
00165     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00166         {
00167                 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00168         User::Panic( KMakeCurrentFailed, 0 );
00169         }
00170     
00171 
00172     TSize size;
00173     size = this->Size();
00174 
00175     iLensflare = CLensflare::NewL( size.iWidth, size.iHeight ); 
00176     iLensflare->AppInitL();
00177 
00178     iOpenGlInitialized = ETrue;
00179     
00180     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );     // Create an active object for
00181                                                                // animating the scene
00182     iPeriodic->Start( 100, 100, TCallBack( CLensFlareContainer::DrawCallBack, this ) );
00183     }
00184 
00185 // Destructor
00186 CLensFlareContainer::~CLensFlareContainer()
00187     {
00188     delete iPeriodic;
00189 
00190     /* AppExit call is made to release any allocations made in AppInit. 
00191      This call has to be made here before we destroy the rendering context. */
00192     if ( !(iLensflare == NULL) )
00193         {
00194         iLensflare->AppExit();
00195         delete iLensflare;
00196         }
00197 
00198     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00199     eglDestroySurface( iEglDisplay, iEglSurface ); 
00200     eglDestroyContext( iEglDisplay, iEglContext );
00201     eglTerminate( iEglDisplay );                   // Release resources associated
00202 
00203     }
00204 
00205 // ---------------------------------------------------------
00206 // CLensFlareContainer::SizeChanged()
00207 // Called by framework when the view size is changed
00208 // ---------------------------------------------------------
00209 //
00210 void CLensFlareContainer::SizeChanged()
00211     {
00212     if( iOpenGlInitialized && iLensflare )
00213         {
00214         TSize size;
00215         size = this->Size();
00216         
00217         iLensflare->SetScreenSize( size.iWidth, size.iHeight );
00218         }
00219     }
00220 
00221 
00222 // ---------------------------------------------------------
00223 // CLensFlareContainer::HandleResourceChange(
00224 //     TInt aType)
00225 // Dynamic screen resize changes by calling the
00226 // SetExtentToWholeScreen() method again.
00227 // ---------------------------------------------------------
00228 //
00229  void CLensFlareContainer::HandleResourceChange(TInt aType)
00230     {
00231         switch( aType )
00232         {
00233             case KEikDynamicLayoutVariantSwitch:
00234                     SetExtentToWholeScreen();
00235                     break;
00236             }
00237     }
00238 
00239 // ---------------------------------------------------------
00240 // CLensFlareContainer::CountComponentControls() const
00241 // ---------------------------------------------------------
00242 //
00243 TInt CLensFlareContainer::CountComponentControls() const
00244     {
00245     return 0; // return nbr of controls inside this container
00246     }
00247 
00248 // ---------------------------------------------------------
00249 // CLensFlareContainer::ComponentControl(TInt aIndex) const
00250 // ---------------------------------------------------------
00251 //
00252 CCoeControl* CLensFlareContainer::ComponentControl(TInt /*aIndex*/) const
00253     {
00254     return NULL;
00255     }
00256 
00257 // ---------------------------------------------------------
00258 // CLensFlareContainer::Draw(const TRect& aRect) const
00259 // ---------------------------------------------------------
00260 //
00261 void CLensFlareContainer::Draw(const TRect& aRect) const
00262         {
00263         CWindowGc& gc = SystemGc();
00264         gc.Clear( aRect );
00265   
00266         if ( iLensflare->GetState() == CLensflare::ELoadingTextures )
00267                 {
00268         _LIT(KLoadingText, "Loading...");
00269         Utils::OutputText(gc, KLoadingText, TPoint(70, 105), KRgbBlack );
00270                 }
00271         }
00272 
00273 // ---------------------------------------------------------
00274 // CLensFlareContainer::HandleControlEventL(
00275 //     CCoeControl* aControl,TCoeEvent aEventType)
00276 // ---------------------------------------------------------
00277 //
00278 void CLensFlareContainer::HandleControlEventL(
00279     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00280     {
00281     }
00282 
00283 
00284 // ---------------------------------------------------------
00285 // CLensFlareContainer::DrawCallBack( TAny* aInstance )
00286 // Called by the CPeriodic in order to draw the graphics 
00287 // ---------------------------------------------------------
00288 //
00289 TInt CLensFlareContainer::DrawCallBack( TAny* aInstance )
00290 {
00291         CLensFlareContainer* instance = (CLensFlareContainer*) aInstance;
00292   
00293         if ( instance->iLensflare->GetState() == CLensflare::ERunning )
00294                 {
00295                 instance->iFrame++;
00296       
00297                 /* Call the main OpenGL ES Symbian rendering 'loop' */
00298                 instance->iLensflare->AppCycle( instance->iFrame );
00299       
00300                 /* Call eglSwapBuffers, which blit the graphics to the window */
00301                 eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00302       
00303                 /* To keep the background light on */
00304                 User::ResetInactivityTime();
00305       
00306                 /* Suspend the current thread for a short while. Give some time 
00307                         to other threads and AOs, avoids the ViewSrv error in ARMI and
00308                         THUMB release builds. One may try to decrease the callback 
00309                         function instead of this. */ 
00310                 if ( !(instance->iFrame % 50) )
00311                         {
00312                         User::After(0);
00313                         }
00314                 }
00315         return 0; 
00316         }
00317 
00318 
00319 // End of File  

© Nokia 2006

Back to top