S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CBillboardContainer Class Reference

#include <BillboardContainer.h>

List of all members.

Public Member Functions

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

Static Public Member Functions

static TInt DrawCallBack (TAny *aInstance)

Public Attributes

CBillboard * iBillboard

Private Member Functions

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

Private Attributes

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


Detailed Description

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

Definition at line 34 of file BillboardContainer.h.


Constructor & Destructor Documentation

CBillboardContainer::~CBillboardContainer (   )  [virtual]
 

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

Definition at line 168 of file BillboardContainer.cpp.

References CBillboard::AppExit(), iBillboard, iEglContext, iEglDisplay, iEglSurface, and iPeriodic.

00169     {
00170     delete iPeriodic;
00171 
00172     /* AppExit call is made to release any allocations made in AppInit. 
00173      This call has to be made here before we destroy the rendering context. */
00174     if ( !(iBillboard == NULL) )
00175         {
00176         iBillboard->AppExit();
00177         delete iBillboard;
00178         }
00179     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00180     eglDestroySurface( iEglDisplay, iEglSurface ); 
00181     eglDestroyContext( iEglDisplay, iEglContext );
00182     eglTerminate( iEglDisplay );                   // Release resources associated
00183 
00184     }


Member Function Documentation

void CBillboardContainer::ConstructL (  const TRect &  aRect  ) 
 

EPOC default constructor. Initializes the OpenGL ES for rendering.

Parameters:
aRect Screen rectangle for container.

Definition at line 27 of file BillboardContainer.cpp.

References iEglDisplay, iFrame, and iOpenGlInitialized.

00028     {
00029     iOpenGlInitialized = EFalse;
00030     CreateWindowL();
00031 
00032     SetExtentToWholeScreen();                // Take the whole screen into use
00033     ActivateL();
00034     
00035     iFrame = 0;                              // Initialize frame counter
00036     
00037     EGLConfig config;                        // Describes the format, type and 
00038                                              // size of the color buffers and
00039                                              // ancillary buffers for EGLSurface
00040 
00041     /* Get the display for drawing graphics*/
00042     iEglDisplay = eglGetDisplay( NULL );
00043     if ( iEglDisplay == NULL )
00044         {
00045                 _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00046         User::Panic( KGetDisplayFailed, 0 );
00047         }
00048 
00049     /* Initialize display */
00050     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00051         {
00052                 _LIT(KInitializeFailed, "eglInitialize failed");
00053         User::Panic( KInitializeFailed, 0 );
00054         }
00055 
00056     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00057     EGLint numOfConfigs = 0; 
00058     EGLint configSize   = 0;
00059 
00060     /* Get the number of possible EGLConfigs */
00061     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == 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     const EGLint attrib_list[] = 
00105         { 
00106         EGL_BUFFER_SIZE, BufferSize,   
00107         EGL_DEPTH_SIZE, 16,    
00108         EGL_NONE 
00109         };
00110 
00111     /* Choose an EGLConfig that best matches to the properties in attrib_list */
00112     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00113                 {
00114                 _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00115                 User::Panic( KChooseConfigFailed, 0 );
00116                 }
00117     if ( numOfConfigs == 0 )
00118             {
00119                 _LIT(KNoConfig, "Can't find the requested config.");
00120                 User::Panic( KNoConfig, 0 );
00121                 }
00122 
00123     config = configList[0];   // Choose the best EGLConfig. EGLConfigs 
00124                               // returned by eglChooseConfig are sorted so 
00125                               // that the best matching EGLConfig is first in
00126                               // the list.
00127     User::Free( configList ); // Free configList, not used anymore.
00128 
00129     /* Create a window where the graphics are blitted */
00130     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00131     if ( iEglSurface == NULL )
00132         {
00133                 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00134         User::Panic( KCreateWindowSurfaceFailed, 0 );
00135         }
00136 
00137     /* Create a rendering context */
00138     iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00139     if ( iEglContext == NULL )
00140         {
00141                 _LIT(KCreateContextFailed, "eglCreateContext failed");
00142         User::Panic( KCreateContextFailed, 0 );
00143         }
00144 
00145     /* Make the context current. Binds context to the current rendering thread
00146        and surface.*/
00147     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00148         {
00149                 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00150         User::Panic( KMakeCurrentFailed, 0 );
00151         }
00152 
00153 
00154     TSize size;
00155     size = this->Size();
00156     
00157     iBillboard = CBillboard::NewL( size.iWidth, size.iHeight ); 
00158     iBillboard->AppInitL();
00159 
00160     iOpenGlInitialized = ETrue;
00161     
00162     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );     // Create an active object for
00163                                                                // animating the scene  
00164     iPeriodic->Start( 100, 100, TCallBack( CBillboardContainer::DrawCallBack, this ) );
00165     }

TInt CBillboardContainer::DrawCallBack (  TAny *  aInstance  )  [static]
 

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

Parameters:
aInstance Pointer to this instance of CBillboardContainer.

Definition at line 269 of file BillboardContainer.cpp.

References CBillboard::AppCycle(), iBillboard, iEglDisplay, iEglSurface, and iFrame.

00270 {
00271     CBillboardContainer* instance = (CBillboardContainer*) aInstance;
00272 
00273     if ( instance->iBillboard->GetState() == CBillboard::ERunning )
00274                 {
00275         instance->iFrame++;
00276     
00277         /* Call the main OpenGL ES Symbian rendering 'loop' */
00278         instance->iBillboard->AppCycle( instance->iFrame );
00279     
00280         /* Call eglSwapBuffers, which blit the graphics to the window */
00281         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00282     
00283         /* To keep the background light on */
00284         User::ResetInactivityTime();
00285     
00286         /* Suspend the current thread for a short while. Give some time 
00287            to other threads and AOs, avoids the ViewSrv error in ARMI and
00288            THUMB release builds. One may try to decrease the callback 
00289            function instead of this. */ 
00290         if ( !(instance->iFrame % 50) )
00291             {
00292             User::After(0);
00293             }
00294                 }
00295     return 0; 
00296 }

void CBillboardContainer::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 191 of file BillboardContainer.cpp.

References iBillboard, iOpenGlInitialized, and CBillboard::SetScreenSize().

00192     {
00193     if( iOpenGlInitialized && iBillboard )
00194         {
00195         TSize size;
00196         size = this->Size();
00197         
00198         iBillboard->SetScreenSize( size.iWidth, size.iHeight );
00199         }
00200     }

TInt CBillboardContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 206 of file BillboardContainer.cpp.

00207     {
00208     return 0; // return nbr of controls inside this container
00209     }

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

Method from CCoeControl. Does nothing in this implementation.

Definition at line 215 of file BillboardContainer.cpp.

00216     {
00217     return NULL;
00218     }

void CBillboardContainer::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 224 of file BillboardContainer.cpp.

References iBillboard.

00225 {
00226     CWindowGc& gc = SystemGc();
00227     gc.Clear( aRect );
00228 
00229     if ( iBillboard->GetState() == CBillboard::ELoadingTextures )
00230                 {
00231         _LIT(KLoadingText, "Loading...");
00232         Utils::OutputText(gc, KLoadingText, TPoint(70, 105), KRgbBlack );
00233                 }
00234 }

void CBillboardContainer::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 241 of file BillboardContainer.cpp.

00243     {
00244     // TODO: Add your control event handler code here
00245     }

void CBillboardContainer::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 254 of file BillboardContainer.cpp.

00255     {
00256         switch( aType )
00257         {
00258             case KEikDynamicLayoutVariantSwitch:
00259                     SetExtentToWholeScreen();
00260                     break;
00261             }
00262     }


Member Data Documentation

EGLDisplay CBillboardContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 104 of file BillboardContainer.h.

EGLContext CBillboardContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 107 of file BillboardContainer.h.

EGLSurface CBillboardContainer::iEglSurface [private]
 

Window surface where the OpenGL ES rendering is blitted to.

Definition at line 110 of file BillboardContainer.h.

CPeriodic* CBillboardContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 113 of file BillboardContainer.h.

TInt CBillboardContainer::iFrame [private]
 

Frame counter variable, used in the animation.

Definition at line 116 of file BillboardContainer.h.

TBool CBillboardContainer::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 122 of file BillboardContainer.h.

CBillboard* CBillboardContainer::iBillboard
 

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

Definition at line 127 of file BillboardContainer.h.


© Nokia 2006

Back to top