S60 3rd Edition SDK FP1 for Symbian OS
Example Applications Guide

SimpleCubeContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : SimpleCubeContainer.cpp
00004  *  Part of     : OpenGLEx / SimpleCube
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 
00014 // INCLUDE FILES
00015 #include "SimpleCubeContainer.h"
00016 // ================= MEMBER FUNCTIONS =======================
00017 
00018 // ---------------------------------------------------------
00019 // CSimpleCubeContainer::ConstructL(const TRect& aRect)
00020 // EPOC two phased constructor
00021 // ---------------------------------------------------------
00022 //
00023 void CSimpleCubeContainer::ConstructL(const TRect& /*aRect*/)
00024     {
00025     iOpenGlInitialized = EFalse;
00026     CreateWindowL();
00027 
00028     SetExtentToWholeScreen();                // Take the whole screen into use
00029     ActivateL();
00030 
00031     iFrame = 0;                              // Initialize frame counter
00032 
00033     EGLConfig Config;                        // Describes the format, type and
00034                                              // size of the color buffers and
00035                                              // ancillary buffers for EGLSurface
00036 
00037     // Get the display for drawing graphics
00038     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00039     if ( iEglDisplay == NULL )
00040         {
00041         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00042         User::Panic( KGetDisplayFailed, 0 );
00043         }
00044 
00045     // Initialize display
00046     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00047         {
00048         _LIT(KInitializeFailed, "eglInitialize failed");
00049         User::Panic( KInitializeFailed, 0 );
00050         }
00051 
00052     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00053     EGLint numOfConfigs = 0;
00054     EGLint configSize   = 0;
00055 
00056     // Get the number of possible EGLConfigs
00057     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs )
00058         == EGL_FALSE )
00059         {
00060         _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00061         User::Panic( KGetConfigsFailed, 0 );
00062         }
00063 
00064     configSize = numOfConfigs;
00065 
00066     // Allocate memory for the configList
00067     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00068     if ( configList == NULL )
00069         {
00070         _LIT(KConfigAllocFailed, "config alloc failed");
00071         User::Panic( KConfigAllocFailed, 0 );
00072         }
00073 
00074     /* Define properties for the wanted EGLSurface.
00075        To get the best possible performance, choose
00076        an EGLConfig with a buffersize matching
00077        the current window's display mode*/
00078     TDisplayMode DMode = Window().DisplayMode();
00079     TInt BufferSize = 0;
00080 
00081     switch ( DMode )
00082          {
00083          case(EColor4K):
00084              BufferSize = 12;
00085              break;
00086          case(EColor64K):
00087              BufferSize = 16;
00088              break;
00089          case(EColor16M):
00090              BufferSize = 24;
00091              break;
00092          case(EColor16MU):
00093                      case(EColor16MA):
00094              BufferSize = 32;
00095              break;
00096          default:
00097              _LIT(KDModeError,       "unsupported displaymode");
00098              User::Panic( KDModeError, 0 );
00099              break;
00100          }
00101 
00102     // Define properties for requesting a full-screen antialiased window surface
00103     const EGLint attrib_list_fsaa[] =
00104         {
00105                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00106         EGL_BUFFER_SIZE,  BufferSize,
00107         EGL_DEPTH_SIZE,   16,
00108                                 EGL_SAMPLE_BUFFERS, 1,
00109                                 EGL_SAMPLES,        4,
00110         EGL_NONE
00111         };
00112 
00113     // Define properties for requesting a non-antialiased window surface
00114     const EGLint attrib_list[] =
00115         {
00116                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00117         EGL_BUFFER_SIZE,  BufferSize,
00118         EGL_DEPTH_SIZE,   16,
00119         EGL_NONE
00120         };
00121 
00122     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00123     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00124         {
00125         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00126         User::Panic( KChooseConfigFailed, 0 );
00127         }
00128 
00129     // Check if no configurations were found
00130     if ( numOfConfigs == 0 )
00131         {
00132         // No configs with antialising were found. Try to get the non-antialiased config
00133                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00134                               {
00135                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00136                   User::Panic( KChooseConfigFailed, 0 );
00137                               }
00138 
00139                     if ( numOfConfigs == 0 )
00140                               {
00141                               // No configs found without antialiasing
00142                   _LIT( KNoConfig, "Can't find the requested config." );
00143                   User::Panic( KNoConfig, 0 );
00144                   }
00145               }
00146 
00147     Config = configList[0];   // Choose the best EGLConfig. EGLConfigs
00148                               // returned by eglChooseConfig are sorted so
00149                               // that the best matching EGLConfig is first in
00150                               // the list.
00151     User::Free( configList ); // Free configList, not used anymore.
00152 
00153     // Create a window where the graphics are blitted
00154     iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL );
00155     if ( iEglSurface == NULL )
00156         {
00157         _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00158         User::Panic( KCreateWindowSurfaceFailed, 0 );
00159         }
00160 
00161     // Create a rendering context
00162     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00163     if ( iEglContext == NULL )
00164         {
00165         _LIT(KCreateContextFailed, "eglCreateContext failed");
00166         User::Panic( KCreateContextFailed, 0 );
00167         }
00168 
00169     /* Make the context current. Binds context to the current rendering thread
00170        and surface.*/
00171     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext )
00172         == EGL_FALSE )
00173         {
00174         _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00175         User::Panic( KMakeCurrentFailed, 0 );
00176         }
00177 
00178 
00179     TSize size;
00180     size = this->Size();
00181 
00182     iSimpleCube = CSimpleCube::NewL( size.iWidth, size.iHeight ); // Create an instance of SimpleCube
00183     iSimpleCube->AppInit();                                       // Initialize OpenGL ES
00184 
00185     iOpenGlInitialized = ETrue;
00186 
00187     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );         // Create an active object for
00188                                                                   // animating the scene
00189     iPeriodic->Start( 100, 100,
00190                       TCallBack( CSimpleCubeContainer::DrawCallBack, this ) );
00191 
00192     }
00193 
00194 // Destructor
00195 CSimpleCubeContainer::~CSimpleCubeContainer()
00196     {
00197     delete iPeriodic;
00198 
00199     /* AppExit call is made to release
00200        any allocations made in AppInit. */
00201     if ( iSimpleCube )
00202         {
00203         iSimpleCube->AppExit();
00204         delete iSimpleCube;
00205         }
00206 
00207     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00208     eglDestroySurface( iEglDisplay, iEglSurface );
00209     eglDestroyContext( iEglDisplay, iEglContext );
00210     eglTerminate( iEglDisplay );                   // Release resources associated
00211                                                    // with EGL and OpenGL ES
00212     }
00213 
00214 // ---------------------------------------------------------
00215 // CSimpleCubeContainer::SizeChanged()
00216 // Called by framework when the view size is changed
00217 // ---------------------------------------------------------
00218 //
00219 void CSimpleCubeContainer::SizeChanged()
00220     {
00221     if( iOpenGlInitialized && iSimpleCube )
00222         {
00223         TSize size;
00224         size = this->Size();
00225 
00226         iSimpleCube->SetScreenSize( size.iWidth, size.iHeight );
00227         }
00228     }
00229 
00230 
00231 // ---------------------------------------------------------
00232 // CSimpleCubeContainer::HandleResourceChange(
00233 //     TInt aType)
00234 // Dynamic screen resize changes by calling the
00235 // SetExtentToWholeScreen() method again.
00236 // ---------------------------------------------------------
00237 //
00238  void CSimpleCubeContainer::HandleResourceChange(TInt aType)
00239     {
00240         switch( aType )
00241         {
00242             case KEikDynamicLayoutVariantSwitch:
00243                     SetExtentToWholeScreen();
00244                     break;
00245             }
00246     }
00247 
00248 // ---------------------------------------------------------
00249 // CSimpleCubeContainer::CountComponentControls() const
00250 // ---------------------------------------------------------
00251 //
00252 TInt CSimpleCubeContainer::CountComponentControls() const
00253     {
00254     return 0;
00255     }
00256 
00257 // ---------------------------------------------------------
00258 // CSimpleCubeContainer::ComponentControl(TInt aIndex) const
00259 // ---------------------------------------------------------
00260 //
00261 CCoeControl* CSimpleCubeContainer::ComponentControl(TInt /*aIndex*/ ) const
00262     {
00263     return NULL;
00264     }
00265 
00266 // ---------------------------------------------------------
00267 // CSimpleCubeContainer::Draw(const TRect& aRect) const
00268 // ---------------------------------------------------------
00269 //
00270 void CSimpleCubeContainer::Draw(const TRect& /*aRect*/ ) const
00271     {
00272     // No need to implement anything here!
00273     }
00274 
00275 // ---------------------------------------------------------
00276 // CSimpleCubeContainer::DrawCallBack( TAny* aInstance )
00277 // Called by the CPeriodic in order to draw the graphics
00278 // ---------------------------------------------------------
00279 //
00280 TInt CSimpleCubeContainer::DrawCallBack( TAny* aInstance )
00281     {
00282     CSimpleCubeContainer* instance = (CSimpleCubeContainer*) aInstance;
00283     instance->iFrame++;
00284 
00285     // Call the main OpenGL ES Symbian rendering 'loop'
00286     instance->iSimpleCube->AppCycle( instance->iFrame );
00287 
00288     // Call eglSwapBuffers, which blit the graphics to the window
00289     eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00290 
00291     // To keep the background light on
00292     if ( !(instance->iFrame%100) )
00293         {
00294         User::ResetInactivityTime();
00295         }
00296 
00297     /* Suspend the current thread for a short while. Give some time
00298        to other threads and AOs, avoids the ViewSrv error in ARMI and
00299        THUMB release builds. One may try to decrease the callback
00300        function instead of this. */
00301     if ( !(instance->iFrame%50) )
00302         {
00303         User::After(0);
00304         }
00305 
00306     return 0;
00307     }
00308 
00309 // ---------------------------------------------------------
00310 // CSimpleCubeContainer::HandleControlEventL(
00311 //     CCoeControl* aControl,TCoeEvent aEventType)
00312 // ---------------------------------------------------------
00313 //
00314 void CSimpleCubeContainer::HandleControlEventL(
00315     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00316     {
00317     }
00318 
00319 // End of File

© Nokia 2006

Back to top