Nokia N97 SDK
Example Applications Guide

Slideshowcontainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : Slideshowcontainer.cpp
00004  *  Part of     : OpenGLEx / Slideshow
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 "SlideshowContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 // ================= MEMBER FUNCTIONS =======================
00018 
00019 // ---------------------------------------------------------
00020 // CSlideshowContainer::ConstructL(const TRect& aRect)
00021 // EPOC two phased constructor
00022 // ---------------------------------------------------------
00023 //
00024 void CSlideshowContainer::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         // Describes the format, type and size of the color buffers and
00045         // ancillary buffers for EGLSurface
00046     EGLConfig config;
00047 
00048     // Get the display for drawing graphics
00049     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00050     if ( iEglDisplay == NULL )
00051             {
00052                 _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00053         User::Panic( KGetDisplayFailed, 0 );
00054                 }
00055 
00056     // Initialize display
00057     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00058                 {
00059                 _LIT(KInitializeFailed, "eglInitialize failed");
00060         User::Panic( KInitializeFailed, 0 );
00061                 }
00062 
00063         // Pointer for EGLConfigs
00064     EGLConfig *configList = NULL;
00065     EGLint numOfConfigs   = 0;
00066     EGLint configSize     = 0;
00067 
00068     // Get the number of possible EGLConfigs
00069     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00070             {
00071                 _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00072         User::Panic( KGetConfigsFailed, 0 );
00073                 }
00074 
00075     configSize = numOfConfigs;
00076 
00077     // Allocate memory for the configList
00078     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00079     if ( configList == NULL )
00080                 {
00081                 _LIT(KConfigAllocFailed, "config alloc failed");
00082         User::Panic( KConfigAllocFailed, 0 );
00083                 }
00084 
00085     // Define properties for the wanted EGLSurface. To get the best possible
00086         // performance, choose an EGLConfig with a buffersize matching the current
00087         // window's display mode
00088     TDisplayMode DMode = Window().DisplayMode();
00089     TInt BufferSize = 0;
00090 
00091     switch(DMode)
00092     {
00093     case(EColor4K):
00094         BufferSize = 12;
00095         break;
00096     case(EColor64K):
00097         BufferSize = 16;
00098         break;
00099     case(EColor16M):
00100         BufferSize = 24;
00101         break;
00102     case(EColor16MU):
00103     case(EColor16MA):
00104         BufferSize = 32;
00105         break;
00106     default:
00107         _LIT(KDModeError, "unsupported displaymode");
00108         User::Panic( KDModeError, 0 );
00109         break;
00110     }
00111 
00112     // Define properties for the wanted EGLSurface
00113     const EGLint attrib_list[] =
00114         {
00115                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00116         EGL_BUFFER_SIZE,  BufferSize,
00117         EGL_DEPTH_SIZE,   16,
00118         EGL_NONE
00119         };
00120 
00121     // Choose an EGLConfig that best matches to the properties in attrib_list
00122     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00123                 {
00124                 _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00125                 User::Panic( KChooseConfigFailed, 0 );
00126                 }
00127 
00128         // Check if no configurations were found
00129         if ( numOfConfigs == 0 )
00130             {
00131                 _LIT(KNoConfig, "Can't find the requested config.");
00132                 User::Panic( KNoConfig, 0 );
00133                 }
00134 
00135         // Choose the best EGLConfig. EGLConfigs returned by eglChooseConfig are
00136         // sorted so that the best matching EGLConfig is first in the list.
00137     config = configList[0];
00138 
00139         // Free configList, as it's not used anymore.
00140     User::Free( configList );
00141 
00142     // Create a window where the graphics are blitted
00143     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00144     if ( iEglSurface == NULL )
00145             {
00146                 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00147         User::Panic( KCreateWindowSurfaceFailed, 0 );
00148                 }
00149 
00150     // Create a rendering context
00151     iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00152     if ( iEglContext == NULL )
00153                 {
00154                 _LIT(KCreateContextFailed, "eglCreateContext failed");
00155         User::Panic( KCreateContextFailed, 0 );
00156                 }
00157 
00158     // Make the context current. Binds context to the current rendering thread
00159     // and surface.
00160     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00161                 {
00162                 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00163         User::Panic( KMakeCurrentFailed, 0 );
00164                 }
00165 
00166 
00167     TSize size;
00168     size = this->Size();
00169     iSlideshow = CSlideshow::NewL( size.iWidth, size.iHeight );
00170     iSlideshow->AppInitL();
00171 
00172     iOpenGlInitialized = ETrue;
00173 
00174         // Reset counters
00175     iFrame             = 0;
00176     iStartTimeTicks    = User::NTickCount();
00177 
00178         // Create an active object for animating the scene
00179     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );
00180     iPeriodic->Start( 100, 100, TCallBack( CSlideshowContainer::DrawCallBack, this ) );
00181     }
00182 
00183 // ---------------------------------------------------------
00184 // CSlideshowContainer::~CSlideshowContainer()
00185 // Destructor. Releases any used resources.
00186 // ---------------------------------------------------------
00187 //
00188 CSlideshowContainer::~CSlideshowContainer()
00189     {
00190     delete iPeriodic;
00191 
00192     /* AppExit call is made to release any allocations made in AppInit.
00193      This call has to be made here before we destroy the rendering context. */
00194     if ( iSlideshow )
00195                 {
00196         iSlideshow->AppExit();
00197         delete iSlideshow;
00198                 }
00199 
00200     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00201     eglDestroySurface( iEglDisplay, iEglSurface );
00202     eglDestroyContext( iEglDisplay, iEglContext );
00203     eglTerminate( iEglDisplay );
00204     }
00205 
00206 // ---------------------------------------------------------
00207 // CSlideshowContainer::SizeChanged()
00208 // Called by framework when the view size is changed
00209 // ---------------------------------------------------------
00210 //
00211 void CSlideshowContainer::SizeChanged()
00212     {
00213     if( iOpenGlInitialized && iSlideshow )
00214         {
00215         TSize size;
00216         size = this->Size();
00217 
00218         iSlideshow->SetScreenSize( size.iWidth, size.iHeight );
00219         }
00220     }
00221 
00222 
00223 // ---------------------------------------------------------
00224 // CSlideshowContainer::HandleResourceChange(
00225 //     TInt aType)
00226 // Dynamic screen resize changes by calling the
00227 // SetExtentToWholeScreen() method again.
00228 // ---------------------------------------------------------
00229 //
00230  void CSlideshowContainer::HandleResourceChange(TInt aType)
00231     {
00232         switch( aType )
00233         {
00234             case KEikDynamicLayoutVariantSwitch:
00235                 // Do not go full-screen in touch devices.    
00236             if (AknLayoutUtils::PenEnabled())
00237             {
00238                 SetRect(iAppUi->ClientRect());
00239             }
00240             else
00241             {
00242                 SetExtentToWholeScreen();                // Take the whole screen into use
00243             }
00244                 break;
00245             }
00246     }
00247 
00248 // ---------------------------------------------------------
00249 // CSlideshowContainer::CountComponentControls() const
00250 // Return number of controls inside this container
00251 // ---------------------------------------------------------
00252 //
00253 TInt CSlideshowContainer::CountComponentControls() const
00254     {
00255     return 0;
00256     }
00257 
00258 // ---------------------------------------------------------
00259 // CSlideshowContainer::ComponentControl(TInt aIndex) const
00260 // ---------------------------------------------------------
00261 //
00262 CCoeControl* CSlideshowContainer::ComponentControl(TInt /*aIndex*/) const
00263     {
00264     return NULL;
00265     }
00266 
00267 // ---------------------------------------------------------
00268 // CSlideshowContainer::Draw(const TRect& aRect) const
00269 // ---------------------------------------------------------
00270 //
00271 void CSlideshowContainer::Draw(const TRect& aRect) const
00272         {
00273     CWindowGc& gc = SystemGc();
00274     gc.Clear( aRect );
00275 
00276     if ( iSlideshow->GetState() == CSlideshow::ELoadingTextures )
00277                 {
00278                 _LIT(KLoading, "Loading...");
00279         utilOutputText(SystemGc(), KLoading, TPoint(Size().iWidth / 2, Size().iHeight / 2), KRgbBlack );
00280                 }
00281         }
00282 
00283 // ---------------------------------------------------------
00284 // CSlideshowContainer::HandleControlEventL(
00285 //     CCoeControl* aControl,TCoeEvent aEventType)
00286 // ---------------------------------------------------------
00287 //
00288 void CSlideshowContainer::HandleControlEventL(
00289     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00290     {
00291     // TODO: Add any control event handler code here
00292     }
00293 
00294 // ----------------------------------------------------------------------
00295 // CSlideshowContainer::GetTimeTick
00296 // Returns the current time.
00297 // ----------------------------------------------------------------------
00298 //
00299 TTime CSlideshowContainer::GetTimeTick()
00300     {
00301     TTime time;
00302     time.UniversalTime();
00303     return time;
00304     }
00305 
00306 
00307 // ---------------------------------------------------------
00308 // CSlideshowContainer::DrawCallBack( TAny* aInstance )
00309 // Called by the CPeriodic in order to draw the graphics
00310 // ---------------------------------------------------------
00311 //
00312 TInt CSlideshowContainer::DrawCallBack( TAny* aInstance )
00313         {
00314     CSlideshowContainer* instance = (CSlideshowContainer*) aInstance;
00315     // Update the frame counts
00316     instance->iFrame++;
00317 
00318     // Compute the elapsed time in seconds since the startup of the example
00319 #ifdef __WINS__
00320 
00321     // In the emulator the tickcount runs at 200Hz
00322     GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTimeTicks ) ) / 200.f;
00323 
00324 #else
00325 
00326     // In the device the tickcount runs at 1000hz (as intended)
00327     GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTimeTicks ) ) / 1000.f;
00328 
00329 #endif
00330 
00331     // Compute the elapsed time since last frame was drawn. Note that due to the
00332     // resolution of the system timer this value can be 0.0 even though new frames are being rendered.
00333     // This applies especially when running the emulator on a PC.
00334     GLfloat deltaTimeSecs = instance->iLastFrameTimeSecs - timeSecs;
00335 
00336     // Set the current time to be the last frame time for the upcoming frame
00337     instance->iLastFrameTimeSecs = timeSecs;
00338 
00339     if ( instance->iSlideshow->GetState() == CSlideshow::ERunning )
00340                 {
00341         // Call the main OpenGL ES Symbian rendering 'loop'
00342         instance->iSlideshow->AppCycle( instance->iFrame, timeSecs, deltaTimeSecs );
00343 
00344         // Call eglSwapBuffers, which blit the graphics to the window
00345         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00346 
00347         // Suspend the current thread for a short while. Give some time
00348         // to other threads and AOs, avoids the ViewSrv error in ARMI and
00349         // THUMB release builds. One may try to decrease the callback
00350                 // function instead of this.
00351         if ( !(instance->iFrame % 50) )
00352                         {
00353                 // Reset inactivity timer to keep the background light on
00354                     User::ResetInactivityTime();
00355 
00356             User::After(0);
00357                         }
00358                 }
00359 
00360     return 0;
00361         }
00362 
00363 // End of File

© Nokia 2009

Back to top