S60 3rd Edition SDK for Symbian OS
Example Applications Guide

EnvMappingContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : EnvMappingContainer.cpp
00004 *  Part of     : OpenGLEx / EnvMapping
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 "EnvMappingContainer.h"
00018 
00019 
00020 // ================= MEMBER FUNCTIONS =======================
00021 
00022 // ---------------------------------------------------------
00023 // CEnvMappingContainer::ConstructL(const TRect& aRect)
00024 // Symbian two phased constructor
00025 // ---------------------------------------------------------
00026 //
00027 void CEnvMappingContainer::ConstructL(const TRect& /*aRect*/)
00028 {
00029         iOpenGlInitialized = EFalse;
00030     CreateWindowL();
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(KEglGetDisplay,    "eglGetDisplay failed");
00046         User::Panic( KEglGetDisplay, 0 );
00047     }
00048 
00049     /* Initialize display */
00050     if( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00051     {
00052         _LIT(KEglInitialize,    "eglInitialize failed");
00053         User::Panic( KEglInitialize, 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 ) == EGL_FALSE )
00062     {
00063         _LIT(KEglGetConfigs,    "eglGetconfigs failed");
00064         User::Panic( KEglGetConfigs, 0 );
00065     }
00066 
00067     configSize = numOfConfigs;
00068 
00069     /* Allocate memory for the configList */
00070     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00071     if( configList == NULL )
00072     {
00073         _LIT(KConfigAlloc,      "config alloc failed");
00074         User::Panic( KConfigAlloc, 0 );
00075     }
00076 
00077     /* Define properties for the wanted EGLSurface.
00078        To get the best possible performance, choose
00079        an EGLConfig with a buffersize matching
00080        the current window's display mode*/
00081     TDisplayMode DMode = Window().DisplayMode();
00082     TInt BufferSize = 0;
00083 
00084     switch(DMode)
00085     {
00086     case(EColor4K):
00087         BufferSize = 12;
00088         break;
00089     case(EColor64K):
00090         BufferSize = 16;
00091         break;
00092     case(EColor16M):
00093         BufferSize = 24;
00094         break;
00095     case(EColor16MU):
00096         BufferSize = 32;
00097         break;
00098     default:
00099         _LIT(KDModeError, "unsupported displaymode");
00100         User::Panic( KDModeError, 0 );
00101         break;
00102     }
00103 
00104     const EGLint attrib_list[] =
00105         {
00106         EGL_BUFFER_SIZE, BufferSize,   
00107         EGL_DEPTH_SIZE, 16,    
00108         EGL_NONE
00109         };
00110 
00111     /* Choose an EGLConfig that best matches to the properties in attrib_list */
00112     if( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00113     {
00114         _LIT(KEglChooseConfig,  "eglChooseConfig failed");
00115         User::Panic( KEglChooseConfig, 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     
00127     if( iEglSurface == NULL )
00128     {
00129         _LIT(KEglCreateWindow,  "eglCreateWindow failed");
00130         User::Panic( KEglCreateWindow, 0 );
00131     }
00132     
00133     
00134     /* Create a rendering context */
00135     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00136     if( iEglContext == NULL )
00137     {
00138         _LIT(KEglCreateContext, "eglCreateContext failed");
00139         User::Panic( KEglCreateContext, 0 );
00140     }
00141     
00142     /* Make the context current. Binds context to the current rendering thread
00143        and surface.*/
00144     if( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00145     {
00146         _LIT(KEglMakeCurrent,   "eglMakeCurrent failed");
00147         User::Panic( KEglMakeCurrent, 0 );
00148     }
00149     
00150     TSize size;
00151     size = this->Size();
00152     
00153     iEnvMapping = CEnvMapping::NewL( size.iWidth, size.iHeight ); 
00154     iEnvMapping->AppInitL();
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, TCallBack( CEnvMappingContainer::DrawCallBack, this ) );
00161 }
00162 
00163 // Destructor
00164 CEnvMappingContainer::~CEnvMappingContainer()
00165 {
00166     delete iPeriodic;
00167 
00168     /* AppExit call is made to release any allocations made in AppInit. 
00169      This call has to be made here before we destroy the rendering context. */
00170     if ( !(iEnvMapping == NULL) )
00171     {
00172         iEnvMapping->AppExit();
00173         delete iEnvMapping;
00174     }
00175 
00176     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00177     eglDestroySurface( iEglDisplay, iEglSurface ); 
00178     eglDestroyContext( iEglDisplay, iEglContext );
00179     eglTerminate( iEglDisplay );                   // Release resources associated
00180                                                    // with EGL and OpenGL ES
00181 
00182 }
00183 
00184 // ---------------------------------------------------------
00185 // CEnvMappingContainer::SizeChanged()
00186 // Called by framework when the view size is changed
00187 // ---------------------------------------------------------
00188 //
00189 void CEnvMappingContainer::SizeChanged()
00190     {
00191     if( iOpenGlInitialized && iEnvMapping )
00192         {
00193         TSize size;
00194         size = this->Size();
00195         
00196         iEnvMapping->SetScreenSize( size.iWidth, size.iHeight );
00197         }
00198     }
00199 
00200 // ---------------------------------------------------------
00201 // CEnvMappingContainer::CountComponentControls() const
00202 // ---------------------------------------------------------
00203 //
00204 TInt CEnvMappingContainer::CountComponentControls() const
00205 {
00206     return 0;
00207 }
00208 
00209 // ---------------------------------------------------------
00210 // CEnvMappingContainer::ComponentControl(TInt aIndex) const
00211 // ---------------------------------------------------------
00212 //
00213 CCoeControl* CEnvMappingContainer::ComponentControl(TInt /*aIndex*/ ) const
00214 {
00215     return NULL;
00216 }
00217 
00218 // ---------------------------------------------------------
00219 // CEnvMappingContainer::Draw(const TRect& aRect) const
00220 // ---------------------------------------------------------
00221 //
00222 void CEnvMappingContainer::Draw(const TRect& /*aRect*/ ) const
00223 {
00224     CWindowGc& gc = SystemGc();
00225     gc.Clear( Rect() );
00226 }
00227 
00228 // ---------------------------------------------------------
00229 // CEnvMappingContainer::DrawCallBack( TAny* aInstance )
00230 // Called by the CPeriodic in order to draw the graphics 
00231 // ---------------------------------------------------------
00232 //
00233 int CEnvMappingContainer::DrawCallBack( TAny* aInstance )
00234 {
00235     CEnvMappingContainer* instance = (CEnvMappingContainer*) aInstance;
00236     
00237     if ( instance->iEnvMapping->GetState() == CEnvMapping::ERunning )
00238     {
00239         instance->iFrame++;
00240         
00241         /* Call the main OpenGL ES Symbian rendering 'loop' */
00242         instance->iEnvMapping->AppCycle( instance->iFrame );
00243         
00244         /* Call eglSwapBuffers, which blit the graphics to the window */
00245         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00246         
00247         /* To keep the background light on */
00248         if(!(instance->iFrame%100))
00249         {
00250             User::ResetInactivityTime();
00251         }
00252         
00253         /* Suspend the current thread for a short while. Give some time 
00254            to other threads and AOs, avoids the ViewSrv error in ARMI and
00255            THUMB release builds. One may try to decrease the callback 
00256            function instead of this. */ 
00257         if(!(instance->iFrame%50))
00258         {
00259             User::After(0);
00260         }
00261     }
00262     
00263     return 0;
00264 }
00265 
00266 // ---------------------------------------------------------
00267 // CEnvMappingContainer::HandleControlEventL(
00268 //     CCoeControl* aControl,TCoeEvent aEventType)
00269 // ---------------------------------------------------------
00270 //
00271 void CEnvMappingContainer::HandleControlEventL(
00272     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00273 {
00274 }
00275 
00276 // ---------------------------------------------------------
00277 // CEnvMappingContainer::HandleResourceChange(
00278 //     TInt aType)
00279 // Dynamic screen resize changes by calling the
00280 // SetExtentToWholeScreen() method again.
00281 // ---------------------------------------------------------
00282 //
00283  void CEnvMappingContainer::HandleResourceChange(TInt aType)
00284     {
00285         switch( aType )
00286         {
00287             case KEikDynamicLayoutVariantSwitch:
00288                     SetExtentToWholeScreen();
00289                     break;
00290             }
00291     }
00292 
00293 // End of File  

© Nokia 2006

Back to top