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

CSimpleCubeContainer Class Reference

#include <SimpleCubeContainer.h>

Inherits CCoeControl, and MCoeControlObserver.

Inheritance diagram for CSimpleCubeContainer:

Inheritance graph
List of all members.

Detailed Description

Container control class that handles the OpenGL ES initialization and deinitializations. Also uses the CSimpleCube class to do the actual OpenGL ES rendering.

Definition at line 28 of file SimpleCubeContainer.h.

Public Member Functions

void ConstructL (const TRect &aRect)
virtual ~CSimpleCubeContainer ()

Static Public Member Functions

static TInt DrawCallBack (TAny *aInstance)

Public Attributes

CSimpleCubeiSimpleCube

Private Member Functions

void SizeChanged ()
void HandleResourceChange (TInt aType)
TInt CountComponentControls () const
CCoeControl * ComponentControl (TInt aIndex) const
void Draw (const TRect &aRect) const
void HandleControlEventL (CCoeControl *aControl, TCoeEvent aEventType)

Private Attributes

EGLDisplay iEglDisplay
EGLSurface iEglSurface
EGLContext iEglContext
CPeriodic * iPeriodic
TBool iOpenGlInitialized
TInt iFrame


Constructor & Destructor Documentation

CSimpleCubeContainer::~CSimpleCubeContainer  )  [virtual]
 

Destructor. Destroys the CPeriodic, CSimpleCube and uninitializes OpenGL ES.

Definition at line 195 of file SimpleCubeContainer.cpp.

References CSimpleCube::AppExit(), iEglContext, iEglDisplay, iEglSurface, iPeriodic, and iSimpleCube.

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     }


Member Function Documentation

void CSimpleCubeContainer::ConstructL const TRect &  aRect  ) 
 

EPOC default constructor. Initializes the OpenGL ES for rendering to the window surface.

Parameters:
aRect Screen rectangle for container.

Definition at line 23 of file SimpleCubeContainer.cpp.

References CSimpleCube::AppInit(), DrawCallBack(), iEglContext, iEglDisplay, iEglSurface, iFrame, iOpenGlInitialized, iPeriodic, iSimpleCube, and CSimpleCube::NewL().

Referenced by CSimpleCubeAppUi::ConstructL().

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     }

TInt CSimpleCubeContainer::DrawCallBack TAny *  aInstance  )  [static]
 

Callback function for the CPeriodic. Calculates the current frame, keeps the background light from turning off and orders the CSimpleCube to do the rendering for each frame.

Parameters:
aInstance Pointer to this instance of CSimpleCubeContainer.

Definition at line 280 of file SimpleCubeContainer.cpp.

References CSimpleCube::AppCycle(), iEglDisplay, iEglSurface, iFrame, and iSimpleCube.

Referenced by ConstructL().

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     }

void CSimpleCubeContainer::SizeChanged  )  [private]
 

Method from CoeControl that gets called when the display size changes. If OpenGL has been initialized, notifies the renderer class that the screen size has changed.

Definition at line 219 of file SimpleCubeContainer.cpp.

References iOpenGlInitialized, iSimpleCube, and CSimpleCube::SetScreenSize().

00220     {
00221     if( iOpenGlInitialized && iSimpleCube )
00222         {
00223         TSize size;
00224         size = this->Size();
00225 
00226         iSimpleCube->SetScreenSize( size.iWidth, size.iHeight );
00227         }
00228     }

void CSimpleCubeContainer::HandleResourceChange TInt  aType  )  [private]
 

Handles a change to the control's resources. This method reacts to the KEikDynamicLayoutVariantSwitch event (that notifies of screen size change) by calling the SetExtentToWholeScreen() again so that this control fills the new screen size. This will then trigger a call to the SizeChanged() method.

Parameters:
aType Message UID value, only KEikDynamicLayoutVariantSwitch is handled by this method.

Definition at line 238 of file SimpleCubeContainer.cpp.

00239     {
00240         switch( aType )
00241         {
00242             case KEikDynamicLayoutVariantSwitch:
00243                     SetExtentToWholeScreen();
00244                     break;
00245             }
00246     }

TInt CSimpleCubeContainer::CountComponentControls  )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 252 of file SimpleCubeContainer.cpp.

00253     {
00254     return 0;
00255     }

CCoeControl * CSimpleCubeContainer::ComponentControl TInt  aIndex  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation.

Definition at line 261 of file SimpleCubeContainer.cpp.

00262     {
00263     return NULL;
00264     }

void CSimpleCubeContainer::Draw const TRect &  aRect  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation. All rendering is done in the DrawCallBack() method.

Definition at line 270 of file SimpleCubeContainer.cpp.

00271     {
00272     // No need to implement anything here!
00273     }

void CSimpleCubeContainer::HandleControlEventL CCoeControl *  aControl,
TCoeEvent  aEventType
[private]
 

Method from MCoeControlObserver that handles an event from the observed control. Does nothing in this implementation.

Parameters:
aControl Control changing its state.
aEventType Type of the control event.

Definition at line 314 of file SimpleCubeContainer.cpp.

00316     {
00317     }


Member Data Documentation

EGLDisplay CSimpleCubeContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 98 of file SimpleCubeContainer.h.

Referenced by ConstructL(), DrawCallBack(), and ~CSimpleCubeContainer().

EGLSurface CSimpleCubeContainer::iEglSurface [private]
 

Window surface where the OpenGL ES rendering is blitted to.

Definition at line 101 of file SimpleCubeContainer.h.

Referenced by ConstructL(), DrawCallBack(), and ~CSimpleCubeContainer().

EGLContext CSimpleCubeContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 104 of file SimpleCubeContainer.h.

Referenced by ConstructL(), and ~CSimpleCubeContainer().

CPeriodic* CSimpleCubeContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 107 of file SimpleCubeContainer.h.

Referenced by ConstructL(), and ~CSimpleCubeContainer().

TBool CSimpleCubeContainer::iOpenGlInitialized [private]
 

Flag that indicates if OpenGL ES has been initialized or not. Used to check if SizeChanged() can react to incoming notifications.

Definition at line 113 of file SimpleCubeContainer.h.

Referenced by ConstructL(), and SizeChanged().

TInt CSimpleCubeContainer::iFrame [private]
 

Frame counter variable, used in the animation.

Definition at line 116 of file SimpleCubeContainer.h.

Referenced by ConstructL(), and DrawCallBack().

CSimpleCube* CSimpleCubeContainer::iSimpleCube
 

Used in DrawCallBack() method to do the actual OpenGL ES rendering.

Definition at line 121 of file SimpleCubeContainer.h.

Referenced by ConstructL(), DrawCallBack(), CSimpleCubeAppUi::HandleCommandL(), SizeChanged(), and ~CSimpleCubeContainer().


The documentation for this class was generated from the following files:

© Nokia 2006

Back to top