S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SimpleCubeContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SimpleCubeContainer.cpp
00004 *  Part of     : OpenGLEx / SimpleCube
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 "SimpleCubeContainer.h"
00019 // ================= MEMBER FUNCTIONS =======================
00020 
00021 // ---------------------------------------------------------
00022 // CSimpleCubeContainer::ConstructL(const TRect& aRect)
00023 // EPOC two phased constructor
00024 // ---------------------------------------------------------
00025 //
00026 void CSimpleCubeContainer::ConstructL(const TRect& /*aRect*/)
00027     {
00028     iOpenGlInitialized = EFalse;
00029     CreateWindowL();
00030 
00031     SetExtentToWholeScreen();                // Take the whole screen into use
00032     ActivateL();
00033     
00034     iFrame = 0;                              // Initialize frame counter
00035     
00036     EGLConfig Config;                        // Describes the format, type and 
00037                                              // size of the color buffers and
00038                                              // ancillary buffers for EGLSurface
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,BufferSize,   
00106                                    EGL_NONE };
00107 
00108     // Choose an EGLConfig that best matches to the properties in attrib_list 
00109     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, 
00110                          &numOfConfigs ) == EGL_FALSE )
00111         {
00112         _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00113         User::Panic( KChooseConfigFailed, 0 );
00114         }
00115 
00116     Config = configList[0];   // Choose the best EGLConfig. EGLConfigs 
00117                               // returned by eglChooseConfig are sorted so 
00118                               // that the best matching EGLConfig is first in
00119                               // the list.
00120     User::Free( configList ); // Free configList, not used anymore.
00121 
00122     // Create a window where the graphics are blitted 
00123     iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL );
00124     if ( iEglSurface == NULL )
00125         {
00126         _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00127         User::Panic( KCreateWindowSurfaceFailed, 0 );
00128         }
00129 
00130     // Create a rendering context 
00131     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00132     if ( iEglContext == NULL )
00133         {
00134         _LIT(KCreateContextFailed, "eglCreateContext failed");
00135         User::Panic( KCreateContextFailed, 0 );
00136         }
00137 
00138     /* Make the context current. Binds context to the current rendering thread
00139        and surface.*/
00140     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) 
00141         == EGL_FALSE )
00142         {
00143         _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00144         User::Panic( KMakeCurrentFailed, 0 );
00145         }
00146 
00147 
00148     TSize size;
00149     size = this->Size();
00150 
00151     iSimpleCube = CSimpleCube::NewL( size.iWidth, size.iHeight ); // Create an instance of SimpleCube
00152     iSimpleCube->AppInit();                                       // Initialize OpenGL ES
00153 
00154     iOpenGlInitialized = ETrue;
00155     
00156     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );         // Create an active object for
00157                                                                   // animating the scene
00158     iPeriodic->Start( 100, 100, 
00159                       TCallBack( CSimpleCubeContainer::DrawCallBack, this ) );
00160 
00161     }
00162 
00163 // Destructor
00164 CSimpleCubeContainer::~CSimpleCubeContainer()
00165     {
00166     delete iPeriodic;
00167 
00168     /* AppExit call is made to release 
00169        any allocations made in AppInit. */
00170     if ( iSimpleCube )
00171         {
00172         iSimpleCube->AppExit();
00173         delete iSimpleCube;
00174         }
00175 
00176     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00177     eglDestroySurface( iEglDisplay, iEglSurface ); 
00178     eglDestroyContext( iEglDisplay, iEglContext );
00179     eglTerminate( iEglDisplay );                   // Release resources associated
00180                                                    // with EGL and OpenGL ES
00181     }
00182 
00183 // ---------------------------------------------------------
00184 // CSimpleCubeContainer::SizeChanged()
00185 // Called by framework when the view size is changed
00186 // ---------------------------------------------------------
00187 //
00188 void CSimpleCubeContainer::SizeChanged()
00189     {
00190     if( iOpenGlInitialized && iSimpleCube )
00191         {
00192         TSize size;
00193         size = this->Size();
00194         
00195         iSimpleCube->SetScreenSize( size.iWidth, size.iHeight );
00196         }
00197     }
00198 
00199 
00200 // ---------------------------------------------------------
00201 // CSimpleCubeContainer::HandleResourceChange(
00202 //     TInt aType)
00203 // Dynamic screen resize changes by calling the
00204 // SetExtentToWholeScreen() method again.
00205 // ---------------------------------------------------------
00206 //
00207  void CSimpleCubeContainer::HandleResourceChange(TInt aType)
00208     {
00209         switch( aType )
00210         {
00211             case KEikDynamicLayoutVariantSwitch:
00212                     SetExtentToWholeScreen();
00213                     break;
00214             }
00215     }
00216 
00217 // ---------------------------------------------------------
00218 // CSimpleCubeContainer::CountComponentControls() const
00219 // ---------------------------------------------------------
00220 //
00221 TInt CSimpleCubeContainer::CountComponentControls() const
00222     {
00223     return 0;
00224     }
00225 
00226 // ---------------------------------------------------------
00227 // CSimpleCubeContainer::ComponentControl(TInt aIndex) const
00228 // ---------------------------------------------------------
00229 //
00230 CCoeControl* CSimpleCubeContainer::ComponentControl(TInt /*aIndex*/ ) const
00231     {
00232     return NULL;
00233     }
00234 
00235 // ---------------------------------------------------------
00236 // CSimpleCubeContainer::Draw(const TRect& aRect) const
00237 // ---------------------------------------------------------
00238 //
00239 void CSimpleCubeContainer::Draw(const TRect& /*aRect*/ ) const
00240     {
00241     CWindowGc& gc = SystemGc();
00242     gc.Clear( Rect() );
00243     }
00244 
00245 // ---------------------------------------------------------
00246 // CSimpleCubeContainer::DrawCallBack( TAny* aInstance )
00247 // Called by the CPeriodic in order to draw the graphics 
00248 // ---------------------------------------------------------
00249 //
00250 TInt CSimpleCubeContainer::DrawCallBack( TAny* aInstance )
00251     {
00252     CSimpleCubeContainer* instance = (CSimpleCubeContainer*) aInstance;
00253     instance->iFrame++;
00254     
00255     // Call the main OpenGL ES Symbian rendering 'loop' 
00256     instance->iSimpleCube->AppCycle( instance->iFrame );
00257     
00258     // Call eglSwapBuffers, which blit the graphics to the window 
00259     eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00260     
00261     // To keep the background light on 
00262     if ( !(instance->iFrame%100) )
00263         {
00264         User::ResetInactivityTime();
00265         }
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) )
00272         {
00273         User::After(0);
00274         }
00275     
00276     return 0; 
00277     }
00278 
00279 // ---------------------------------------------------------
00280 // CSimpleCubeContainer::HandleControlEventL(
00281 //     CCoeControl* aControl,TCoeEvent aEventType)
00282 // ---------------------------------------------------------
00283 //
00284 void CSimpleCubeContainer::HandleControlEventL(
00285     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00286     {
00287     }
00288 
00289 // End of File  

© Nokia 2006

Back to top