Nokia N97 SDK
Example Applications Guide

EnvMappingContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : EnvMappingContainer.cpp
00004  *  Part of     : OpenGLEx / EnvMapping
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 "EnvMappingContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 
00018 // ================= MEMBER FUNCTIONS =======================
00019 
00020 // ---------------------------------------------------------
00021 // CEnvMappingContainer::ConstructL(const TRect& aRect)
00022 // Symbian two phased constructor
00023 // ---------------------------------------------------------
00024 //
00025 void CEnvMappingContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
00026     {
00027     iAppUi = aAppUi;
00028         iOpenGlInitialized = EFalse;
00029     CreateWindowL();
00030 
00031     // Do not go to full-screen in touch devices.    
00032     if (AknLayoutUtils::PenEnabled())
00033     {
00034         SetRect(aRect);
00035     }
00036     else
00037     {
00038         SetExtentToWholeScreen();                // Take the whole screen into use
00039     }
00040     ActivateL();
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(KEglGetDisplay,    "eglGetDisplay failed");
00053         User::Panic( KEglGetDisplay, 0 );
00054     }
00055 
00056     /* Initialize display */
00057     if( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00058     {
00059         _LIT(KEglInitialize,    "eglInitialize failed");
00060         User::Panic( KEglInitialize, 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 ) == EGL_FALSE )
00069     {
00070         _LIT(KEglGetConfigs,    "eglGetconfigs failed");
00071         User::Panic( KEglGetConfigs, 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(KConfigAlloc,      "config alloc failed");
00081         User::Panic( KConfigAlloc, 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 
00166     if( iEglSurface == NULL )
00167     {
00168         _LIT(KEglCreateWindow,  "eglCreateWindow failed");
00169         User::Panic( KEglCreateWindow, 0 );
00170     }
00171 
00172 
00173     /* Create a rendering context */
00174     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00175     if( iEglContext == NULL )
00176     {
00177         _LIT(KEglCreateContext, "eglCreateContext failed");
00178         User::Panic( KEglCreateContext, 0 );
00179     }
00180 
00181     /* Make the context current. Binds context to the current rendering thread
00182        and surface.*/
00183     if( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00184     {
00185         _LIT(KEglMakeCurrent,   "eglMakeCurrent failed");
00186         User::Panic( KEglMakeCurrent, 0 );
00187     }
00188 
00189     TSize size;
00190     size = this->Size();
00191 
00192     iEnvMapping = CEnvMapping::NewL( size.iWidth, size.iHeight );
00193     iEnvMapping->AppInitL();
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( CEnvMappingContainer::DrawCallBack, this ) );
00200 }
00201 
00202 // Destructor
00203 CEnvMappingContainer::~CEnvMappingContainer()
00204 {
00205     delete iPeriodic;
00206 
00207     /* AppExit call is made to release any allocations made in AppInit.
00208      This call has to be made here before we destroy the rendering context. */
00209     if ( !(iEnvMapping == NULL) )
00210     {
00211         iEnvMapping->AppExit();
00212         delete iEnvMapping;
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 // ---------------------------------------------------------
00224 // CEnvMappingContainer::SizeChanged()
00225 // Called by framework when the view size is changed
00226 // ---------------------------------------------------------
00227 //
00228 void CEnvMappingContainer::SizeChanged()
00229     {
00230     if( iOpenGlInitialized && iEnvMapping )
00231         {
00232         TSize size;
00233         size = this->Size();
00234 
00235         iEnvMapping->SetScreenSize( size.iWidth, size.iHeight );
00236         }
00237     }
00238 
00239 // ---------------------------------------------------------
00240 // CEnvMappingContainer::CountComponentControls() const
00241 // ---------------------------------------------------------
00242 //
00243 TInt CEnvMappingContainer::CountComponentControls() const
00244 {
00245     return 0;
00246 }
00247 
00248 // ---------------------------------------------------------
00249 // CEnvMappingContainer::ComponentControl(TInt aIndex) const
00250 // ---------------------------------------------------------
00251 //
00252 CCoeControl* CEnvMappingContainer::ComponentControl(TInt /*aIndex*/ ) const
00253 {
00254     return NULL;
00255 }
00256 
00257 // ---------------------------------------------------------
00258 // CEnvMappingContainer::Draw(const TRect& aRect) const
00259 // ---------------------------------------------------------
00260 //
00261 void CEnvMappingContainer::Draw(const TRect& /*aRect*/ ) const
00262 {
00263     // No need to implement anything here!
00264 }
00265 
00266 // ---------------------------------------------------------
00267 // CEnvMappingContainer::DrawCallBack( TAny* aInstance )
00268 // Called by the CPeriodic in order to draw the graphics
00269 // ---------------------------------------------------------
00270 //
00271 int CEnvMappingContainer::DrawCallBack( TAny* aInstance )
00272 {
00273     CEnvMappingContainer* instance = (CEnvMappingContainer*) aInstance;
00274 
00275     if ( instance->iEnvMapping->GetState() == CEnvMapping::ERunning )
00276     {
00277         instance->iFrame++;
00278 
00279         /* Call the main OpenGL ES Symbian rendering 'loop' */
00280         instance->iEnvMapping->AppCycle( instance->iFrame );
00281 
00282         /* Call eglSwapBuffers, which blit the graphics to the window */
00283         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00284 
00285         /* To keep the background light on */
00286         if(!(instance->iFrame%100))
00287         {
00288             User::ResetInactivityTime();
00289         }
00290 
00291         /* Suspend the current thread for a short while. Give some time
00292            to other threads and AOs, avoids the ViewSrv error in ARMI and
00293            THUMB release builds. One may try to decrease the callback
00294            function instead of this. */
00295         if(!(instance->iFrame%50))
00296         {
00297             User::After(0);
00298         }
00299     }
00300 
00301     return 0;
00302 }
00303 
00304 // ---------------------------------------------------------
00305 // CEnvMappingContainer::HandleControlEventL(
00306 //     CCoeControl* aControl,TCoeEvent aEventType)
00307 // ---------------------------------------------------------
00308 //
00309 void CEnvMappingContainer::HandleControlEventL(
00310     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00311 {
00312 }
00313 
00314 // ---------------------------------------------------------
00315 // CEnvMappingContainer::HandleResourceChange(
00316 //     TInt aType)
00317 // Dynamic screen resize changes by calling the
00318 // SetExtentToWholeScreen() method again.
00319 // ---------------------------------------------------------
00320 //
00321  void CEnvMappingContainer::HandleResourceChange(TInt aType)
00322     {
00323         switch( aType )
00324         {
00325             case KEikDynamicLayoutVariantSwitch:
00326                 // Do not go full-screen in touch devices.    
00327             if (AknLayoutUtils::PenEnabled())
00328             {
00329                 SetRect(iAppUi->ClientRect());
00330             }
00331             else
00332             {
00333                 SetExtentToWholeScreen();                // Take the whole screen into use
00334             }
00335                 break;
00336             }
00337     }
00338 
00339 // End of File

© Nokia 2009

Back to top