Nokia N97 SDK
Example Applications Guide

SimpleCubePixmapContainer.cpp

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

© Nokia 2009

Back to top