Nokia N97 SDK
Example Applications Guide

Particlescontainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : Particlescontainer.cpp
00004  *  Part of     : OpenGLEx / Particles
00005  *
00006  *  Copyright (c) 2005-2006 Nokia Corporation.
00007  *  This material, including documentation and any related
00008  *  computer programs, is protected by copyright controlled by
00009  *  Nokia Corporation.
00010  * ==============================================================================
00011  */
00012 
00013 // INCLUDE FILES
00014 #include "ParticlesContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 // ================= MEMBER FUNCTIONS =======================
00018 
00019 // ---------------------------------------------------------
00020 // CParticlesContainer::ConstructL(const TRect& aRect)
00021 // EPOC two phased constructor
00022 // ---------------------------------------------------------
00023 //
00024 void CParticlesContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
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     }
00204 
00205 // ---------------------------------------------------------
00206 // CParticlesContainer::~CParticlesContainer()
00207 // Destructor. Releases any used resources.
00208 // ---------------------------------------------------------
00209 //
00210 CParticlesContainer::~CParticlesContainer()
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     }
00227 
00228 // ---------------------------------------------------------
00229 // CParticlesContainer::SizeChanged()
00230 // Called by framework when the view size is changed
00231 // ---------------------------------------------------------
00232 //
00233 void CParticlesContainer::SizeChanged()
00234     {
00235     if( iOpenGlInitialized && iParticles )
00236         {
00237         TSize size;
00238         size = this->Size();
00239 
00240         iParticles->SetScreenSize( size.iWidth, size.iHeight );
00241         }
00242     }
00243 
00244 
00245 // ---------------------------------------------------------
00246 // CParticlesContainer::HandleResourceChange(
00247 //     TInt aType)
00248 // Dynamic screen resize changes by calling the
00249 // SetExtentToWholeScreen() method again.
00250 // ---------------------------------------------------------
00251 //
00252  void CParticlesContainer::HandleResourceChange(TInt aType)
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     }
00269 
00270 // ---------------------------------------------------------
00271 // CParticlesContainer::CountComponentControls() const
00272 // Return number of controls inside this container
00273 // ---------------------------------------------------------
00274 //
00275 TInt CParticlesContainer::CountComponentControls() const
00276     {
00277     return 0;
00278     }
00279 
00280 // ---------------------------------------------------------
00281 // CParticlesContainer::ComponentControl(TInt aIndex) const
00282 // ---------------------------------------------------------
00283 //
00284 CCoeControl* CParticlesContainer::ComponentControl(TInt /*aIndex*/) const
00285     {
00286     return NULL;
00287     }
00288 
00289 // ---------------------------------------------------------
00290 // CParticlesContainer::Draw(const TRect& aRect) const
00291 // ---------------------------------------------------------
00292 //
00293 void CParticlesContainer::Draw(const TRect& aRect) const
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         }
00304 
00305 // ---------------------------------------------------------
00306 // CParticlesContainer::HandleControlEventL(
00307 //     CCoeControl* aControl,TCoeEvent aEventType)
00308 // ---------------------------------------------------------
00309 //
00310 void CParticlesContainer::HandleControlEventL(
00311     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00312     {
00313     // TODO: Add any control event handler code here
00314     }
00315 
00316 // ----------------------------------------------------------------------
00317 // CParticlesContainer::GetTimeTick
00318 // Returns the current time.
00319 // ----------------------------------------------------------------------
00320 //
00321 TTime CParticlesContainer::GetTimeTick()
00322     {
00323     TTime time;
00324     time.UniversalTime();
00325     return time;
00326     }
00327 
00328 
00329 // ---------------------------------------------------------
00330 // CParticlesContainer::DrawCallBack( TAny* aInstance )
00331 // Called by the CPeriodic in order to draw the graphics
00332 // ---------------------------------------------------------
00333 //
00334 TInt CParticlesContainer::DrawCallBack( TAny* aInstance )
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         }
00385 
00386 // End of File

© Nokia 2009

Back to top