Nokia N97 SDK
Example Applications Guide

Mipmappingcontainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : Mipmappingcontainer.cpp
00004  *  Part of     : OpenGLEx / Mipmapping
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 "MipmappingContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 
00018 // ================= MEMBER FUNCTIONS =======================
00019 
00020 // ---------------------------------------------------------
00021 // CMipmappingContainer::ConstructL(const TRect& aRect)
00022 // EPOC two phased constructor
00023 // ---------------------------------------------------------
00024 //
00025 void CMipmappingContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
00026     {
00027     iAppUi = aAppUi;
00028         // Open GL hasn't been initialized
00029     iOpenGlInitialized = EFalse;
00030 
00031         // Create the native window
00032     CreateWindowL();
00033 
00034 // Do not go to full-screen in touch devices.    
00035     if (AknLayoutUtils::PenEnabled())
00036     {
00037         SetRect(aRect);
00038     }
00039     else
00040     {
00041         SetExtentToWholeScreen();                // Take the whole screen into use
00042     }
00043     ActivateL();
00044 
00045         // Describes the format, type and size of the color buffers and
00046         // ancillary buffers for EGLSurface
00047     EGLConfig config;
00048 
00049     // Get the display for drawing graphics
00050     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00051     if ( iEglDisplay == NULL )
00052             {
00053                 _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00054         User::Panic( KGetDisplayFailed, 0 );
00055                 }
00056 
00057     // Initialize display
00058     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00059                 {
00060                 _LIT(KInitializeFailed, "eglInitialize failed");
00061         User::Panic( KInitializeFailed, 0 );
00062                 }
00063 
00064         // Pointer for EGLConfigs
00065     EGLConfig *configList = NULL;
00066     EGLint numOfConfigs   = 0;
00067     EGLint configSize     = 0;
00068 
00069     // Get the number of possible EGLConfigs
00070     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00071             {
00072                 _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00073         User::Panic( KGetConfigsFailed, 0 );
00074                 }
00075 
00076     configSize = numOfConfigs;
00077 
00078     // Allocate memory for the configList
00079     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00080     if ( configList == NULL )
00081                 {
00082                 _LIT(KConfigAllocFailed, "config alloc failed");
00083         User::Panic( KConfigAllocFailed, 0 );
00084                 }
00085 
00086     // Define properties for the wanted EGLSurface. To get the best possible
00087         // performance, choose an EGLConfig with a buffersize matching the current
00088         // window's display mode
00089     TDisplayMode DMode = Window().DisplayMode();
00090     TInt BufferSize = 0;
00091 
00092     switch(DMode)
00093     {
00094     case(EColor4K):
00095         BufferSize = 12;
00096         break;
00097     case(EColor64K):
00098         BufferSize = 16;
00099         break;
00100     case(EColor16M):
00101         BufferSize = 24;
00102         break;
00103     case(EColor16MU):
00104     case(EColor16MA):
00105         BufferSize = 32;
00106         break;
00107     default:
00108         _LIT(KDModeError, "unsupported displaymode");
00109         User::Panic( KDModeError, 0 );
00110         break;
00111     }
00112 
00113     // Define properties for requesting a full-screen antialiased window surface
00114     const EGLint attrib_list_fsaa[] =
00115         {
00116                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00117         EGL_BUFFER_SIZE,  BufferSize,
00118         EGL_DEPTH_SIZE,   16,
00119                                 EGL_SAMPLE_BUFFERS, 1,
00120                                 EGL_SAMPLES,        4,
00121         EGL_NONE
00122         };
00123 
00124     // Define properties for requesting a non-antialiased window surface
00125     const EGLint attrib_list[] =
00126         {
00127                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00128         EGL_BUFFER_SIZE,  BufferSize,
00129         EGL_DEPTH_SIZE,   16,
00130         EGL_NONE
00131         };
00132 
00133     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00134     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00135         {
00136         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00137         User::Panic( KChooseConfigFailed, 0 );
00138         }
00139 
00140     // Check if no configurations were found
00141     if ( numOfConfigs == 0 )
00142         {
00143         // No configs with antialising were found. Try to get the non-antialiased config
00144                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00145                               {
00146                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00147                   User::Panic( KChooseConfigFailed, 0 );
00148                               }
00149 
00150                     if ( numOfConfigs == 0 )
00151                               {
00152                               // No configs found without antialiasing
00153                   _LIT( KNoConfig, "Can't find the requested config." );
00154                   User::Panic( KNoConfig, 0 );
00155                   }
00156               }
00157 
00158         // Choose the best EGLConfig. EGLConfigs returned by eglChooseConfig are
00159         // sorted so that the best matching EGLConfig is first in the list.
00160     config = configList[0];
00161 
00162         // Free configList, as it's not used anymore.
00163     User::Free( configList );
00164 
00165     // Create a window where the graphics are blitted
00166     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00167     if ( iEglSurface == NULL )
00168             {
00169                 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00170         User::Panic( KCreateWindowSurfaceFailed, 0 );
00171                 }
00172 
00173     // Create a rendering context
00174     iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00175     if ( iEglContext == NULL )
00176                 {
00177                 _LIT(KCreateContextFailed, "eglCreateContext failed");
00178         User::Panic( KCreateContextFailed, 0 );
00179                 }
00180 
00181     // Make the context current. Binds context to the current rendering thread
00182     // and surface.
00183     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00184                 {
00185                 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00186         User::Panic( KMakeCurrentFailed, 0 );
00187                 }
00188 
00189         // Reset counters
00190     iFrame             = 0;
00191     iStartTimeTicks    = User::NTickCount();
00192 
00193     TSize size;
00194     size = this->Size();
00195     iMipmapping = CMipmapping::NewL( size.iWidth, size.iHeight );
00196     iMipmapping->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( CMipmappingContainer::DrawCallBack, this ) );
00203     }
00204 
00205 // ---------------------------------------------------------
00206 // CMipmappingContainer::~CMipmappingContainer()
00207 // Destructor. Releases any used resources.
00208 // ---------------------------------------------------------
00209 //
00210 CMipmappingContainer::~CMipmappingContainer()
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 ( iMipmapping )
00217                 {
00218         iMipmapping->AppExit();
00219         delete iMipmapping;
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 // CMipmappingContainer::SizeChanged()
00230 // Called by framework when the view size is changed
00231 // ---------------------------------------------------------
00232 //
00233 void CMipmappingContainer::SizeChanged()
00234     {
00235     if( iOpenGlInitialized && iMipmapping )
00236         {
00237         TSize size;
00238         size = this->Size();
00239 
00240         iMipmapping->SetScreenSize( size.iWidth, size.iHeight );
00241         }
00242     }
00243 
00244 
00245 // ---------------------------------------------------------
00246 // CMipmappingContainer::HandleResourceChange(
00247 //     TInt aType)
00248 // Dynamic screen resize changes by calling the
00249 // SetExtentToWholeScreen() method again.
00250 // ---------------------------------------------------------
00251 //
00252  void CMipmappingContainer::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 // CMipmappingContainer::CountComponentControls() const
00272 // Return number of controls inside this container
00273 // ---------------------------------------------------------
00274 //
00275 TInt CMipmappingContainer::CountComponentControls() const
00276     {
00277     return 0;
00278     }
00279 
00280 // ---------------------------------------------------------
00281 // CMipmappingContainer::ComponentControl(TInt aIndex) const
00282 // ---------------------------------------------------------
00283 //
00284 CCoeControl* CMipmappingContainer::ComponentControl(TInt /*aIndex*/) const
00285     {
00286     return NULL;
00287     }
00288 
00289 // ---------------------------------------------------------
00290 // CMipmappingContainer::Draw(const TRect& aRect) const
00291 // ---------------------------------------------------------
00292 //
00293 void CMipmappingContainer::Draw(const TRect& aRect) const
00294         {
00295     CWindowGc& gc = SystemGc();
00296     gc.Clear( aRect );
00297 
00298     if ( iMipmapping->GetState() == CMipmapping::ELoadingTextures )
00299                 {
00300                 _LIT(KLoading, "Loading...");
00301         utilOutputText(SystemGc(), KLoading, TPoint(70, 105), KRgbBlack );
00302                 }
00303         }
00304 
00305 // ---------------------------------------------------------
00306 // CMipmappingContainer::HandleControlEventL(
00307 //     CCoeControl* aControl,TCoeEvent aEventType)
00308 // ---------------------------------------------------------
00309 //
00310 void CMipmappingContainer::HandleControlEventL(
00311     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00312     {
00313     // TODO: Add any control event handler code here
00314     }
00315 
00316 // ----------------------------------------------------------------------
00317 // CMipmappingContainer::GetTimeTick
00318 // Returns the current time.
00319 // ----------------------------------------------------------------------
00320 //
00321 TTime CMipmappingContainer::GetTimeTick()
00322     {
00323     TTime time;
00324     time.UniversalTime();
00325     return time;
00326     }
00327 
00328 
00329 // ---------------------------------------------------------
00330 // CMipmappingContainer::DrawCallBack( TAny* aInstance )
00331 // Called by the CPeriodic in order to draw the graphics
00332 // ---------------------------------------------------------
00333 //
00334 TInt CMipmappingContainer::DrawCallBack( TAny* aInstance )
00335         {
00336     CMipmappingContainer* instance = (CMipmappingContainer*) 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->iMipmapping->GetState() == CMipmapping::ERunning )
00363                 {
00364         // Call the main OpenGL ES Symbian rendering 'loop'
00365         instance->iMipmapping->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