S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SimpleCubePixmapContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SimpleCubePixmapContainer.cpp
00004 *  Part of     : OpenGLEx / SimpleCubePixmap
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 
00017 // INCLUDE FILES
00018 #include "SimpleCubePixmapContainer.h"
00019 #include <Eikenv.h>
00020 #include <w32std.h>
00021 
00022 // ================= MEMBER FUNCTIONS =======================
00023 
00024 // ---------------------------------------------------------
00025 // CSimpleCubePixmapContainer::ConstructL
00026 // EPOC two phased constructor
00027 // ---------------------------------------------------------
00028 //
00029 void CSimpleCubePixmapContainer::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;                              // Initialize frame counter
00038     
00039 
00040     // Get the display for drawing graphics
00041     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00042     if ( iEglDisplay == NULL )
00043         {
00044         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00045         User::Panic( KGetDisplayFailed, 0 );
00046         }
00047 
00048     // Initialize display 
00049     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00050         {
00051         _LIT(KInitializeFailed, "eglInitialize failed");
00052         User::Panic( KInitializeFailed, 0 );
00053         }
00054 
00055     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00056     EGLint numOfConfigs = 0; 
00057     EGLint configSize   = 0;
00058 
00059     // Get the number of possible EGLConfigs 
00060     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) 
00061          == EGL_FALSE )
00062         {
00063         _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00064         User::Panic( KGetConfigsFailed, 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(KConfigAllocFailed, "config alloc failed");
00074         User::Panic( KConfigAllocFailed, 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     // Define properties for the wanted EGLSurface 
00105     const EGLint attrib_list[] = { EGL_BUFFER_SIZE,
00106                                    BufferSize,
00107                                    EGL_SURFACE_TYPE,
00108                                    EGL_PIXMAP_BIT,
00109                                    EGL_NONE};
00110 
00111     // Choose an EGLConfig that best matches to the properties in attrib_list 
00112     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, 
00113                           &numOfConfigs ) == EGL_FALSE )
00114         {
00115         _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00116         User::Panic( KChooseConfigFailed, 0 );
00117         }
00118 
00119     iConfig = configList[0];   // Choose the best EGLConfig. EGLConfigs 
00120                               // returned by eglChooseConfig are sorted so 
00121                               // that the best matching EGLConfig is first in
00122                               // the list.
00123     User::Free( configList ); // Free configList, not used anymore.
00124 
00125     // Create a Symbian bitmap where the graphics are blitted 
00126     iPixmap = new CWsBitmap( iEikonEnv->WsSession() );
00127     iPixmap->Create( Window().Size(), DMode);
00128     iEglSurface = eglCreatePixmapSurface( iEglDisplay, iConfig, iPixmap, NULL );
00129 
00130     if ( iEglSurface == NULL )
00131         {
00132         _LIT(KCreatePixmapSurfaceFailed, "eglCreatePixmapSurface failed");
00133         User::Panic( KCreatePixmapSurfaceFailed, 0 );
00134         }
00135 
00136     // Create a rendering context 
00137     iEglContext = eglCreateContext( iEglDisplay, iConfig, EGL_NO_CONTEXT, NULL );
00138     if ( iEglContext == NULL )
00139         {
00140         _LIT(KCreateContextFailed, "eglCreateContext failed");
00141         User::Panic( KCreateContextFailed, 0 );
00142         }
00143 
00144     /* Make the context current. Binds context to the current rendering thread
00145        and surface.*/
00146     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) 
00147         == EGL_FALSE )
00148         {
00149         _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00150         User::Panic( KMakeCurrentFailed, 0 );
00151         }
00152 
00153 
00154     TSize size;
00155     size = this->Size();
00156 
00157     // Create an instance of SimpleCubePixmap
00158     iSimpleCubePixmap = CSimpleCubePixmap::NewL( size.iWidth, size.iHeight ); 
00159     iSimpleCubePixmap->AppInit();                           // Initialize OpenGL ES
00160 
00161     iOpenGlInitialized = ETrue;
00162     
00163     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );  // Create an active object for
00164                                                             // animating the scene
00165     iPeriodic->Start( 100, 100, TCallBack( CSimpleCubePixmapContainer::DrawCallBack, this ) );
00166     }
00167 
00168 // Destructor
00169 CSimpleCubePixmapContainer::~CSimpleCubePixmapContainer()
00170     {
00171     delete iPeriodic;
00172 
00173     /* AppExit call is made to release 
00174        any allocations made in AppInit. */
00175     if ( iSimpleCubePixmap )
00176         {
00177         iSimpleCubePixmap->AppExit();
00178         delete iSimpleCubePixmap;
00179         }
00180 
00181     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00182     eglDestroySurface( iEglDisplay, iEglSurface ); 
00183     eglDestroyContext( iEglDisplay, iEglContext );
00184     eglTerminate( iEglDisplay );                   // Release resources associated
00185                                                    // with EGL and OpenGL ES
00186 
00187     delete iPixmap;
00188     }
00189 
00190 // ---------------------------------------------------------
00191 // CSimpleCubePixmapContainer::SizeChanged
00192 // Called by framework when the view size is changed
00193 // ---------------------------------------------------------
00194 //
00195 void CSimpleCubePixmapContainer::SizeChanged()
00196     {
00197     if( iOpenGlInitialized && iSimpleCubePixmap )
00198         {
00199         TSize size;
00200         size = this->Size();
00201 
00202         // Destroy the old context, surface and bitmap
00203         eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00204         eglDestroySurface( iEglDisplay, iEglSurface ); 
00205         eglDestroyContext( iEglDisplay, iEglContext );
00206         if( iPixmap )
00207             {
00208             delete iPixmap;
00209             }
00210         
00211         // Recreate the Symbian bitmap where the graphics are blitted 
00212         iPixmap = new CWsBitmap( iEikonEnv->WsSession() );
00213         iPixmap->Create( Window().Size(), Window().DisplayMode() );
00214 
00215         
00216         // Recreate and rebind the new surface
00217         iEglSurface = eglCreatePixmapSurface( iEglDisplay, iConfig, iPixmap, NULL );
00218         if ( iEglSurface == NULL )
00219             {
00220             _LIT(KCreatePixmapSurfaceFailed, "eglCreatePixmapSurface failed");
00221             User::Panic( KCreatePixmapSurfaceFailed, 0 );
00222             }
00223 
00224         // Create a rendering context 
00225         iEglContext = eglCreateContext( iEglDisplay, iConfig, EGL_NO_CONTEXT, NULL );
00226         if ( iEglContext == NULL )
00227             {
00228             _LIT(KCreateContextFailed, "eglCreateContext failed");
00229             User::Panic( KCreateContextFailed, 0 );
00230             }
00231 
00232         // Make the context current. Binds context to the current
00233         // rendering thread and surface.
00234         if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00235             {
00236             _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00237             User::Panic( KMakeCurrentFailed, 0 );
00238             }
00239         
00240         
00241         // Notify the renderer class
00242         iSimpleCubePixmap->SetScreenSize( size.iWidth, size.iHeight );
00243         }
00244     }
00245 
00246 
00247 // ---------------------------------------------------------
00248 // CSimpleCubePixmapContainer::HandleResourceChange(
00249 //     TInt aType)
00250 // Dynamic screen resize changes by calling the
00251 // SetExtentToWholeScreen() method again.
00252 // ---------------------------------------------------------
00253 //
00254  void CSimpleCubePixmapContainer::HandleResourceChange(TInt aType)
00255     {
00256         switch( aType )
00257         {
00258             case KEikDynamicLayoutVariantSwitch:
00259                     SetExtentToWholeScreen();
00260                     break;
00261             }
00262     }
00263 
00264 // ---------------------------------------------------------
00265 // CSimpleCubePixmapContainer::CountComponentControls
00266 // ---------------------------------------------------------
00267 //
00268 TInt CSimpleCubePixmapContainer::CountComponentControls() const
00269     {
00270     return 0;
00271     }
00272 
00273 // ---------------------------------------------------------
00274 // CSimpleCubePixmapContainer::ComponentControl
00275 // ---------------------------------------------------------
00276 //
00277 CCoeControl* CSimpleCubePixmapContainer::ComponentControl(TInt /*aIndex*/) const
00278     {
00279     return NULL;
00280     }
00281 // ---------------------------------------------------------
00282 // CSimpleCubePixmapContainer::Draw
00283 // ---------------------------------------------------------
00284 //
00285 void CSimpleCubePixmapContainer::Draw(const TRect& /*aRect*/ ) const
00286     {
00287     CWindowGc& gc = SystemGc();
00288     gc.BitBlt( TPoint( 0, 0 ), iPixmap );
00289     }
00290 
00291 // ---------------------------------------------------------
00292 // CSimpleCubePixmapContainer::DrawCallBack
00293 // Called by the CPeriodic in order to draw the graphics 
00294 // ---------------------------------------------------------
00295 //
00296 TInt CSimpleCubePixmapContainer::DrawCallBack( TAny* aInstance )
00297     {
00298     CSimpleCubePixmapContainer* instance 
00299       = (CSimpleCubePixmapContainer*) aInstance;
00300     instance->iFrame++;
00301     
00302     // Call the main OpenGL ES Symbian rendering 'loop' 
00303     instance->iSimpleCubePixmap->AppCycle( instance->iFrame );
00304 
00305     /* eglSwapBuffers is not called when usin Pixmap surfaces. 
00306        Instead, standard Symbian BitBlt has to be used. */
00307     instance->DrawNow( );
00308     
00309     // To keep the background light on 
00310     if ( !(instance->iFrame%100) )
00311         {
00312         User::ResetInactivityTime();
00313         }
00314     
00315     /* Suspend the current thread for a short while. Give some time 
00316        to other threads and AOs, avoids the ViewSrv error in ARMI and
00317        THUMB release builds. One may try to decrease the callback 
00318        function instead of this. */ 
00319     if ( !(instance->iFrame%50) )
00320         {
00321         User::After(0);
00322         }
00323     
00324     return 0; 
00325     }
00326 
00327 // ---------------------------------------------------------
00328 // CSimpleCubePixmapContainer::HandleControlEventL
00329 // ---------------------------------------------------------
00330 //
00331 void CSimpleCubePixmapContainer::HandleControlEventL(
00332     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00333     {
00334     }
00335 
00336 
00337 // End of File  

© Nokia 2006

Back to top