S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SimpleLightContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SimpleLightContainer.cpp
00004 *  Part of     : OpenGLEx / SimpleLight
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 "SimpleLightContainer.h"
00018 
00019 
00020 // ================= MEMBER FUNCTIONS =======================
00021 
00022 // ---------------------------------------------------------
00023 // CSimpleLightContainer::ConstructL
00024 // Symbian 2nd phase constructor
00025 // ---------------------------------------------------------
00026 //
00027 void CSimpleLightContainer::ConstructL(const TRect& /*aRect*/)
00028     {
00029     CreateWindowL();
00030     iOpenGlInitialized = EFalse;
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(KInitializeFailed, "eglInitialize failed");
00053         User::Panic( KInitializeFailed, 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 
00150     TSize size;
00151     size = this->Size();
00152 
00153     iSimpleLight = CSimpleLight::NewL( size.iWidth, size.iHeight ); // Create an instance of SimpleLight
00154     iSimpleLight->AppInit();                                       // Initialize OpenGL ES
00155 
00156     iOpenGlInitialized = ETrue;
00157     
00158     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );         // Create an active object for
00159                                                                   // animating the scene
00160     iPeriodic->Start( 100, 100, 
00161                       TCallBack( CSimpleLightContainer::DrawCallBack, this ) );
00162     }
00163 
00164 // Destructor
00165 CSimpleLightContainer::~CSimpleLightContainer()
00166     {
00167     delete iPeriodic;
00168 
00169     if ( iSimpleLight )
00170         {
00171         iSimpleLight->AppExit();
00172         delete iSimpleLight;
00173         }
00174     
00175     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00176     eglDestroySurface( iEglDisplay, iEglSurface ); 
00177     eglDestroyContext( iEglDisplay, iEglContext );
00178     eglTerminate( iEglDisplay );                   // Release resources associated
00179                                                    // with EGL and OpenGL ES
00180     }
00181 
00182 // ---------------------------------------------------------
00183 // CSimpleLightContainer::SizeChanged()
00184 // Called by framework when the view size is changed
00185 // ---------------------------------------------------------
00186 //
00187 void CSimpleLightContainer::SizeChanged()
00188     {
00189     if( iOpenGlInitialized && iSimpleLight )
00190         {
00191         TSize size;
00192         size = this->Size();
00193         
00194         iSimpleLight->SetScreenSize( size.iWidth, size.iHeight );
00195         }
00196     }
00197 
00198 
00199 // ---------------------------------------------------------
00200 // CSimpleLightContainer::HandleResourceChange(
00201 //     TInt aType)
00202 // Dynamic screen resize changes by calling the
00203 // SetExtentToWholeScreen() method again.
00204 // ---------------------------------------------------------
00205 //
00206  void CSimpleLightContainer::HandleResourceChange(TInt aType)
00207     {
00208         switch( aType )
00209         {
00210             case KEikDynamicLayoutVariantSwitch:
00211                     SetExtentToWholeScreen();
00212                     break;
00213             }
00214     }
00215 
00216 // ---------------------------------------------------------
00217 // CSimpleLightContainer::CountComponentControls() const
00218 // ---------------------------------------------------------
00219 //
00220 TInt CSimpleLightContainer::CountComponentControls() const
00221     {
00222     return 0; // return nbr of controls inside this container
00223     }
00224 
00225 // ---------------------------------------------------------
00226 // CSimpleLightContainer::ComponentControl(TInt aIndex) const
00227 // ---------------------------------------------------------
00228 //
00229 CCoeControl* CSimpleLightContainer::ComponentControl(TInt /*aIndex*/ ) const
00230     {
00231     return NULL;    
00232     }
00233 
00234 // ---------------------------------------------------------
00235 // CSimpleLightContainer::Draw(const TRect& aRect) const
00236 // ---------------------------------------------------------
00237 //
00238 void CSimpleLightContainer::Draw(const TRect& /*aRect*/ ) const
00239     {
00240     // No need to implement anything here!
00241     }
00242 
00243 // ---------------------------------------------------------
00244 // CSimpleLightContainer::DrawCallBack( TAny* aInstance )
00245 // Called by the CPeriodic in order to draw the graphics 
00246 // ---------------------------------------------------------
00247 //
00248 int CSimpleLightContainer::DrawCallBack( TAny* aInstance )
00249     {
00250     CSimpleLightContainer* instance = (CSimpleLightContainer*) aInstance;
00251     instance->iFrame++;
00252     
00253     // Call the main OpenGL ES Symbian rendering 'loop' 
00254     instance->iSimpleLight->AppCycle( instance->iFrame );
00255     
00256     // Call eglSwapBuffers, which blit the graphics to the window 
00257     eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00258 
00259     // To keep the background light on 
00260     if ( !(instance->iFrame%100) )
00261         {
00262         User::ResetInactivityTime();
00263         }
00264     
00265     /* Suspend the current thread for a short while. Give some time 
00266        to other threads and AOs, avoids the ViewSrv error in ARMI and
00267        THUMB release builds. One may try to decrease the callback 
00268        function instead of this. */ 
00269     if ( !(instance->iFrame%50) )
00270          {
00271          User::After(0);
00272          }
00273     
00274     return 0;
00275     }
00276 
00277 // ---------------------------------------------------------
00278 // CSimpleLightContainer::HandleControlEventL(
00279 //     CCoeControl* aControl,TCoeEvent aEventType)
00280 // ---------------------------------------------------------
00281 //
00282 void CSimpleLightContainer::HandleControlEventL(
00283     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00284     {
00285     }
00286 
00287 // End of File  

© Nokia 2006

Back to top