Nokia N97 SDK
Example Applications Guide

CParticlesContainer Class Reference

#include <Particlescontainer.h>

Inheritance diagram for CParticlesContainer:

List of all members.

Detailed Description

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

Definition at line 29 of file Particlescontainer.h.


Public Member Functions

void  ConstructL (const TRect &aRect, CAknAppUi *aAppUi)
virtual  ~CParticlesContainer ()

Static Public Member Functions

static TInt  DrawCallBack (TAny *aInstance)
static TTime  GetTimeTick ()

Public Attributes

CParticles *  iParticles

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
TUint32  iStartTimeTicks
TUint32  iLastFrameTimeSecs
TReal  iSecondsPerFrame
TInt  iFrame
CAknAppUi *  iAppUi


Constructor & Destructor Documentation

CParticlesContainer::~CParticlesContainer (   )  [virtual]
 

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

Definition at line 210 of file Particlescontainer.cpp.

References CParticles::AppExit(), iEglContext, iEglDisplay, iEglSurface, iParticles, and iPeriodic.

00211     {
00212     delete iPeriodic;
00213 
00214     /* AppExit call is made to release any allocations made in AppInit.
00215      This call has to be made here before we destroy the rendering context. */
00216     if ( iParticles )
00217                 {
00218         iParticles->AppExit();
00219         delete iParticles;
00220                 }
00221 
00222     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00223     eglDestroySurface( iEglDisplay, iEglSurface );
00224     eglDestroyContext( iEglDisplay, iEglContext );
00225     eglTerminate( iEglDisplay );
00226     }


Member Function Documentation

void CParticlesContainer::ConstructL (  const TRect &  aRect,
CAknAppUi *  aAppUi
) 
 

Second phase constructor that can call methods that may leave. Initializes the OpenGL ES for rendering to the window surface.

Parameters:
aRect  Screen rectangle for container.

Definition at line 24 of file Particlescontainer.cpp.

References iAppUi, iEglDisplay, iFrame, iOpenGlInitialized, and iStartTimeTicks.

Referenced by CParticlesAppUi::ConstructL().

00025     {
00026     iAppUi = aAppUi;
00027         // Open GL hasn't been initialized
00028     iOpenGlInitialized = EFalse;
00029 
00030         // Create the native window
00031     CreateWindowL();
00032 
00033         // Do not go to full-screen in touch devices.    
00034     if (AknLayoutUtils::PenEnabled())
00035     {
00036         SetRect(aRect);
00037     }
00038     else
00039     {
00040         SetExtentToWholeScreen();                // Take the whole screen into use
00041     }
00042     ActivateL();
00043 
00044     // Initialize timers and counters
00045     iFrame             = 0;
00046     iStartTimeTicks    = User::NTickCount();
00047 
00048         // Describes the format, type and size of the color buffers and
00049         // ancillary buffers for EGLSurface
00050     EGLConfig config;
00051 
00052     // Get the display for drawing graphics
00053     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00054     if ( iEglDisplay == NULL )
00055             {
00056                 _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00057         User::Panic( KGetDisplayFailed, 0 );
00058                 }
00059 
00060     // Initialize display
00061     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00062                 {
00063                 _LIT(KInitializeFailed, "eglInitialize failed");
00064         User::Panic( KInitializeFailed, 0 );
00065                 }
00066 
00067         // Pointer for EGLConfigs
00068     EGLConfig *configList = NULL;
00069     EGLint numOfConfigs   = 0;
00070     EGLint configSize     = 0;
00071 
00072     // Get the number of possible EGLConfigs
00073     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00074             {
00075                 _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00076         User::Panic( KGetConfigsFailed, 0 );
00077                 }
00078 
00079     configSize = numOfConfigs;
00080 
00081     // Allocate memory for the configList
00082     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00083     if ( configList == NULL )
00084                 {
00085                 _LIT(KConfigAllocFailed, "config alloc failed");
00086         User::Panic( KConfigAllocFailed, 0 );
00087                 }
00088 
00089     // Define properties for the wanted EGLSurface. To get the best possible
00090         // performance, choose an EGLConfig with a buffersize matching the current
00091           // window's display mode
00092     TDisplayMode DMode = Window().DisplayMode();
00093     TInt BufferSize = 0;
00094 
00095     switch(DMode)
00096     {
00097     case(EColor4K):
00098         BufferSize = 12;
00099         break;
00100     case(EColor64K):
00101         BufferSize = 16;
00102         break;
00103     case(EColor16M):
00104         BufferSize = 24;
00105         break;
00106     case(EColor16MU):
00107     case(EColor16MA):
00108         BufferSize = 32;
00109         break;
00110     default:
00111         _LIT(KDModeError, "unsupported displaymode");
00112         User::Panic( KDModeError, 0 );
00113         break;
00114     }
00115 
00116     // Define properties for requesting a full-screen antialiased window surface
00117     const EGLint attrib_list_fsaa[] =
00118         {
00119                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00120         EGL_BUFFER_SIZE,  BufferSize,
00121         EGL_DEPTH_SIZE,   16,
00122                                 EGL_SAMPLE_BUFFERS, 1,
00123                                 EGL_SAMPLES,        4,
00124         EGL_NONE
00125         };
00126 
00127     // Define properties for requesting a non-antialiased window surface
00128     const EGLint attrib_list[] =
00129         {
00130                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00131         EGL_BUFFER_SIZE,  BufferSize,
00132         EGL_DEPTH_SIZE,   16,
00133         EGL_NONE
00134         };
00135 
00136     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00137     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00138         {
00139         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00140         User::Panic( KChooseConfigFailed, 0 );
00141         }
00142 
00143     // Check if no configurations were found
00144     if ( numOfConfigs == 0 )
00145         {
00146         // No configs with antialising were found. Try to get the non-antialiased config
00147                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00148                               {
00149                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00150                   User::Panic( KChooseConfigFailed, 0 );
00151                               }
00152 
00153                     if ( numOfConfigs == 0 )
00154                               {
00155                               // No configs found without antialiasing
00156                   _LIT( KNoConfig, "Can't find the requested config." );
00157                   User::Panic( KNoConfig, 0 );
00158                   }
00159               }
00160 
00161         // Choose the best EGLConfig. EGLConfigs returned by eglChooseConfig are
00162         // sorted so that the best matching EGLConfig is first in the list.
00163     config = configList[0];
00164 
00165         // Free configList, as it's not used anymore.
00166     User::Free( configList );
00167 
00168     // Create a window where the graphics are blitted
00169     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00170     if ( iEglSurface == NULL )
00171             {
00172                 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00173         User::Panic( KCreateWindowSurfaceFailed, 0 );
00174                 }
00175 
00176     // Create a rendering context
00177     iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00178     if ( iEglContext == NULL )
00179                 {
00180                 _LIT(KCreateContextFailed, "eglCreateContext failed");
00181         User::Panic( KCreateContextFailed, 0 );
00182                 }
00183 
00184     // Make the context current. Binds context to the current rendering thread
00185     // and surface.
00186     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00187                 {
00188                 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00189         User::Panic( KMakeCurrentFailed, 0 );
00190                 }
00191 
00192 
00193     TSize size;
00194     size = this->Size();
00195     iParticles = CParticles::NewL( size.iWidth, size.iHeight );
00196     iParticles->AppInitL();
00197 
00198     iOpenGlInitialized = ETrue;
00199 
00200         // Create an active object for animating the scene
00201     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );
00202     iPeriodic->Start( 100, 100, TCallBack( CParticlesContainer::DrawCallBack, this ) );
00203     }

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

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

Parameters:
aInstance  Pointer to this instance of CParticlesContainer.

Definition at line 334 of file Particlescontainer.cpp.

References CParticles::AppCycle(), iEglDisplay, iEglSurface, iFrame, iLastFrameTimeSecs, iParticles, and iStartTimeTicks.

00335         {
00336     CParticlesContainer* instance = (CParticlesContainer*) aInstance;
00337 
00338     // Update the frame counts
00339     instance->iFrame++;
00340 
00341     // Compute the elapsed time in seconds since the startup of the example
00342 #ifdef __WINS__
00343 
00344     // In the emulator the tickcount runs at 200Hz
00345     GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTimeTicks ) ) / 200.f;
00346 
00347 #else
00348 
00349     // In the device the tickcount runs at 1000hz (as intended)
00350     GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTimeTicks ) ) / 1000.f;
00351 
00352 #endif
00353 
00354     // Compute the elapsed time since last frame was drawn. Note that due to the
00355     // resolution of the system timer this value can be 0.0 even though new frames are being rendered.
00356     // This applies especially when running the emulator on a PC.
00357     GLfloat deltaTimeSecs = instance->iLastFrameTimeSecs - timeSecs;
00358 
00359     // Set the current time to be the last frame time for the upcoming frame
00360     instance->iLastFrameTimeSecs = timeSecs;
00361 
00362     if ( instance->iParticles->GetState() == CParticles::ERunning )
00363                 {
00364         // Call the main OpenGL ES Symbian rendering 'loop'
00365         instance->iParticles->AppCycle( instance->iFrame, timeSecs, deltaTimeSecs );
00366 
00367         // Call eglSwapBuffers, which blit the graphics to the window
00368         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00369 
00370         // Suspend the current thread for a short while. Give some time
00371         // to other threads and AOs, avoids the ViewSrv error in ARMI and
00372         // THUMB release builds. One may try to decrease the callback
00373                 // function instead of this.
00374         if ( !(instance->iFrame % 50) )
00375                         {
00376                 // Reset inactivity timer to keep the background light on
00377                     User::ResetInactivityTime();
00378 
00379             User::After(0);
00380                         }
00381                 }
00382 
00383     return 0;
00384         }

TTime CParticlesContainer::GetTimeTick (   )  [static]
 

Returns the current system time.

Returns:
System time.

Definition at line 321 of file Particlescontainer.cpp.

00322     {
00323     TTime time;
00324     time.UniversalTime();
00325     return time;
00326     }

void CParticlesContainer::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 233 of file Particlescontainer.cpp.

References iOpenGlInitialized, iParticles, and CParticles::SetScreenSize().

00234     {
00235     if( iOpenGlInitialized && iParticles )
00236         {
00237         TSize size;
00238         size = this->Size();
00239 
00240         iParticles->SetScreenSize( size.iWidth, size.iHeight );
00241         }
00242     }

void CParticlesContainer::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 252 of file Particlescontainer.cpp.

References iAppUi.

00253     {
00254         switch( aType )
00255         {
00256             case KEikDynamicLayoutVariantSwitch:
00257                 // Do not go full-screen in touch devices.    
00258             if (AknLayoutUtils::PenEnabled())
00259             {
00260                 SetRect(iAppUi->ClientRect());
00261             }
00262             else
00263             {
00264                 SetExtentToWholeScreen();                // Take the whole screen into use
00265             }
00266                 break;
00267             }
00268     }

TInt CParticlesContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 275 of file Particlescontainer.cpp.

00276     {
00277     return 0;
00278     }

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

Method from CCoeControl. Does nothing in this implementation.

Definition at line 284 of file Particlescontainer.cpp.

00285     {
00286     return NULL;
00287     }

void CParticlesContainer::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 293 of file Particlescontainer.cpp.

References iParticles.

00294         {
00295     CWindowGc& gc = SystemGc();
00296     gc.Clear( aRect );
00297 
00298     if ( iParticles->GetState() == CParticles::ELoadingTextures )
00299                 {
00300                 _LIT(KLoading, "Loading...");
00301         utilOutputText(SystemGc(), KLoading, TPoint(70, 105), KRgbBlack );
00302                 }
00303         }

void CParticlesContainer::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 310 of file Particlescontainer.cpp.

00312     {
00313     // TODO: Add any control event handler code here
00314     }


Member Data Documentation

EGLDisplay CParticlesContainer::iEglDisplay [private]
 

Display where the OpenGL ES window surface resides.

Definition at line 106 of file Particlescontainer.h.

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

EGLSurface CParticlesContainer::iEglSurface [private]
 

Window surface where the OpenGL ES rendering is blitted to.

Definition at line 108 of file Particlescontainer.h.

Referenced by DrawCallBack(), and ~CParticlesContainer().

EGLContext CParticlesContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 110 of file Particlescontainer.h.

Referenced by ~CParticlesContainer().

CPeriodic* CParticlesContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 112 of file Particlescontainer.h.

Referenced by ~CParticlesContainer().

TBool CParticlesContainer::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 117 of file Particlescontainer.h.

Referenced by ConstructL(), and SizeChanged().

TUint32 CParticlesContainer::iStartTimeTicks [private]
 

Time when the example started running (rendering frames).

Definition at line 120 of file Particlescontainer.h.

Referenced by ConstructL(), and DrawCallBack().

TUint32 CParticlesContainer::iLastFrameTimeSecs [private]
 

Time when the previous frame was rendered.

Definition at line 122 of file Particlescontainer.h.

Referenced by DrawCallBack().

TReal CParticlesContainer::iSecondsPerFrame [private]
 

Current average seconds per frame. Used to interpolated during frames when the internal clock hasn't updated.

Definition at line 124 of file Particlescontainer.h.

TInt CParticlesContainer::iFrame [private]
 

Counts the number of frames rendered.

Definition at line 126 of file Particlescontainer.h.

Referenced by ConstructL(), and DrawCallBack().

CAknAppUi* CParticlesContainer::iAppUi [private]
 

Application UI class for querying the client rectangle size.

Definition at line 129 of file Particlescontainer.h.

Referenced by ConstructL(), and HandleResourceChange().

CParticles* CParticlesContainer::iParticles
 

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

Definition at line 134 of file Particlescontainer.h.

Referenced by Draw(), DrawCallBack(), SizeChanged(), and ~CParticlesContainer().


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

© Nokia 2009

Back to top