S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CSimpleCubeContainer Class Reference

#include <SimpleCubeContainer.h>

List of all members.

Public Member Functions

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

Static Public Member Functions

static TInt DrawCallBack (TAny *aInstance)

Public Attributes

CSimpleCube * iSimpleCube

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


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 31 of file SimpleCubeContainer.h.


Constructor & Destructor Documentation

CSimpleCubeContainer::~CSimpleCubeContainer (   )  [virtual]
 

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

Definition at line 164 of file SimpleCubeContainer.cpp.

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

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     }


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 26 of file SimpleCubeContainer.cpp.

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

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     }

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 250 of file SimpleCubeContainer.cpp.

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

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     }

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 188 of file SimpleCubeContainer.cpp.

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

00189     {
00190     if( iOpenGlInitialized && iSimpleCube )
00191         {
00192         TSize size;
00193         size = this->Size();
00194         
00195         iSimpleCube->SetScreenSize( size.iWidth, size.iHeight );
00196         }
00197     }

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 207 of file SimpleCubeContainer.cpp.

00208     {
00209         switch( aType )
00210         {
00211             case KEikDynamicLayoutVariantSwitch:
00212                     SetExtentToWholeScreen();
00213                     break;
00214             }
00215     }

TInt CSimpleCubeContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 221 of file SimpleCubeContainer.cpp.

00222     {
00223     return 0;
00224     }

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

Method from CCoeControl. Does nothing in this implementation.

Definition at line 230 of file SimpleCubeContainer.cpp.

00231     {
00232     return NULL;
00233     }

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 239 of file SimpleCubeContainer.cpp.

00240     {
00241     CWindowGc& gc = SystemGc();
00242     gc.Clear( Rect() );
00243     }

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 284 of file SimpleCubeContainer.cpp.

00286     {
00287     }


Member Data Documentation

EGLDisplay CSimpleCubeContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 101 of file SimpleCubeContainer.h.

EGLSurface CSimpleCubeContainer::iEglSurface [private]
 

Window surface where the OpenGL ES rendering is blitted to.

Definition at line 104 of file SimpleCubeContainer.h.

EGLContext CSimpleCubeContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 107 of file SimpleCubeContainer.h.

CPeriodic* CSimpleCubeContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 110 of file SimpleCubeContainer.h.

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 116 of file SimpleCubeContainer.h.

TInt CSimpleCubeContainer::iFrame [private]
 

Frame counter variable, used in the animation.

Definition at line 119 of file SimpleCubeContainer.h.

CSimpleCube* CSimpleCubeContainer::iSimpleCube
 

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

Definition at line 124 of file SimpleCubeContainer.h.


© Nokia 2006

Back to top