S60 3rd Edition SDK for Symbian OS
Example Applications Guide

TextureContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : TextureContainer.cpp
00004 *  Part of     : OpenGLEx / Texture
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 "TextureContainer.h"
00018 
00019 
00020 // ================= MEMBER FUNCTIONS =======================
00021 
00022 // ---------------------------------------------------------
00023 // CTextureContainer::ConstructL(const TRect& aRect)
00024 // EPOC two phased constructor
00025 // EGL code, e.g., OpenGL ES context creation, surface 
00026 // creation etc. are located here.
00027 // ---------------------------------------------------------
00028 //
00029 void CTextureContainer::ConstructL(const TRect& /*aRect*/)
00030 {
00031     iOpenGlInitialized = EFalse;
00032     CreateWindowL();
00033 
00034     SetExtentToWholeScreen();                // Take the whole screen into use
00035     ActivateL();
00036 
00037     iFrame = 0;                              // Frame counter
00038 
00039     EGLConfig Config;                        // Describes the format, type and 
00040                                              // size of the color buffers and
00041                                              // ancillary buffers for EGLSurface
00042 
00043     /* Get the display for drawing graphics*/
00044     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00045     if( iEglDisplay == NULL )
00046     {
00047             _LIT(KEglGetDisplay,    "eglGetDisplay failed");
00048         User::Panic( KEglGetDisplay, 0 );
00049     }
00050 
00051     /* Initialize display */
00052     if( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00053     {
00054             _LIT(KEglInitialize,    "eglInitialize failed");
00055         User::Panic( KEglInitialize, 0 );
00056     }
00057 
00058     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00059     TInt numOfConfigs = 0; 
00060     TInt configSize   = 0;
00061 
00062     /* Get the number of possible EGLConfigs */
00063     if( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00064     {
00065             _LIT(KEglGetConfigs,    "eglGetconfigs failed");
00066         User::Panic( KEglGetConfigs, 0 );
00067     }
00068 
00069     configSize = numOfConfigs;
00070 
00071     /* Allocate memory for the configList */
00072     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00073     if( configList == NULL )
00074     {
00075             _LIT(KConfigAlloc,      "config alloc failed");
00076         User::Panic( KConfigAlloc, 0 );
00077     }
00078 
00079     /* Define properties for the wanted EGLSurface.
00080        To get the best possible performance, choose
00081        an EGLConfig with a buffersize matching
00082        the current window's display mode*/
00083     TDisplayMode DMode = Window().DisplayMode();
00084     TInt BufferSize = 0;
00085 
00086     switch(DMode)
00087     {
00088     case(EColor4K):
00089         BufferSize = 12;
00090         break;
00091     case(EColor64K):
00092         BufferSize = 16;
00093         break;
00094     case(EColor16M):
00095         BufferSize = 24;
00096         break;
00097     case(EColor16MU):
00098         BufferSize = 32;
00099         break;
00100     default:
00101         _LIT(KDModeError,       "unsupported displaymode");
00102         User::Panic( KDModeError, 0 );
00103         break;
00104     }
00105 
00106     const EGLint attrib_list[] =
00107         {
00108         EGL_BUFFER_SIZE,BufferSize,   
00109         EGL_DEPTH_SIZE,16,    
00110         EGL_NONE
00111         };
00112 
00113     /* Choose an EGLConfig that best matches to the properties in attrib_list */
00114     if( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00115     {
00116             _LIT(KEglChooseConfig,  "eglChooseConfig failed");
00117         User::Panic( KEglChooseConfig, 0 );
00118     }
00119 
00120     Config = configList[0];   // Choose the best EGLConfig. EGLConfigs 
00121                               // returned by eglChooseConfig are sorted so 
00122                               // that the best matching EGLConfig is first in
00123                               // the list.
00124     User::Free( configList ); // Free configList, not used anymore.
00125 
00126     /* Create a window where the graphics are blitted */
00127     iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL );
00128     if( iEglSurface == NULL )
00129     {
00130             _LIT(KEglCreateWindow,  "eglCreateWindow failed");
00131         User::Panic( KEglCreateWindow, 0 );
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     iTexture = CTexture::NewL( size.iWidth, size.iHeight );
00154     iTexture->AppInitL();
00155 
00156     iOpenGlInitialized = ETrue;
00157     
00158     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle ); // Create an active object for
00159                                                            // animating the scene
00160                                                            
00161     iPeriodic->Start( 100, 100, TCallBack( CTextureContainer::DrawCallBack, this ) );
00162 }
00163 
00164 // Destructor
00165 CTextureContainer::~CTextureContainer()
00166 {
00167     delete iPeriodic;
00168 
00169     /* AppExit call is made to release any allocations made in AppInit. 
00170      This call has to be made here before we destroy the rendering context. */
00171     if ( !(iTexture == NULL) )
00172         {
00173         iTexture->AppExit();
00174         delete iTexture;
00175         }
00176 
00177     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00178     eglDestroySurface( iEglDisplay, iEglSurface ); 
00179     eglDestroyContext( iEglDisplay, iEglContext );
00180     eglTerminate( iEglDisplay );                   // Release resources associated
00181                                                    // with EGL and OpenGL ES
00182 }
00183 
00184 // ---------------------------------------------------------
00185 // CTextureContainer::SizeChanged()
00186 // Called by framework when the view size is changed
00187 // ---------------------------------------------------------
00188 //
00189 void CTextureContainer::SizeChanged()
00190     {
00191     if( iOpenGlInitialized && iTexture )
00192         {
00193         TSize size;
00194         size = this->Size();
00195         
00196         iTexture->SetScreenSize( size.iWidth, size.iHeight );
00197         }
00198     }
00199 
00200 
00201 // ---------------------------------------------------------
00202 // CTextureContainer::HandleResourceChange(
00203 //     TInt aType)
00204 // Dynamic screen resize changes by calling the
00205 // SetExtentToWholeScreen() method again.
00206 // ---------------------------------------------------------
00207 //
00208  void CTextureContainer::HandleResourceChange(TInt aType)
00209     {
00210         switch( aType )
00211         {
00212             case KEikDynamicLayoutVariantSwitch:
00213                     SetExtentToWholeScreen();
00214                     break;
00215             }
00216     }
00217 
00218 // ---------------------------------------------------------
00219 // CTextureContainer::CountComponentControls() const
00220 // ---------------------------------------------------------
00221 //
00222 TInt CTextureContainer::CountComponentControls() const
00223 {
00224     return 0;
00225 }
00226 
00227 // ---------------------------------------------------------
00228 // CTextureContainer::ComponentControl(TInt aIndex) const
00229 // ---------------------------------------------------------
00230 //
00231 CCoeControl* CTextureContainer::ComponentControl(TInt /*aIndex*/) const
00232 {
00233     return NULL;
00234 }
00235 
00236 // ---------------------------------------------------------
00237 // CTextureContainer::Draw(const TRect& aRect) const
00238 // ---------------------------------------------------------
00239 //
00240 void CTextureContainer::Draw(const TRect& /*aRect*/ ) const
00241 {
00242     // Nothing to do here...
00243 }
00244 
00245 // ---------------------------------------------------------
00246 // CTextureContainer::DrawCallBack( TAny* aInstance )
00247 // Called by the CPeriodic in order to draw the graphics 
00248 // ---------------------------------------------------------
00249 //
00250 TInt CTextureContainer::DrawCallBack( TAny* aInstance )
00251 {
00252     CTextureContainer* instance = (CTextureContainer*) aInstance;
00253     if ( instance->iTexture->GetState() == CTexture::ERunning )
00254                 {
00255             /* Check if the textures are loaded before doing any rendering. */
00256             instance->iFrame++;
00257             
00258             /* Call the main OpenGL ES Symbian rendering 'loop' */
00259             instance->iTexture->AppCycle( instance->iFrame );
00260     
00261             /* Call eglSwapBuffers, which blit the graphics to the window */
00262             eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00263             
00264             /* To keep the background light on */
00265             if(!(instance->iFrame%100))  User::ResetInactivityTime();
00266             
00267             /* Suspend the current thread for a short while. Give some time 
00268                to other threads and AOs, avoids the ViewSrv error in ARMI and
00269                THUMB release builds. One may try to decrease the callback 
00270                function instead of this. */ 
00271             if(!(instance->iFrame%50))   User::After(0);
00272                 }
00273     return 0; 
00274 }
00275 
00276 // ---------------------------------------------------------
00277 // CTextureContainer::HandleControlEventL(
00278 //     CCoeControl* aControl,TCoeEvent aEventType)
00279 // ---------------------------------------------------------
00280 //
00281 void CTextureContainer::HandleControlEventL(
00282     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00283 {
00284 }
00285 
00286 // End of File  

© Nokia 2006

Back to top