S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SnowContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SnowContainer.cpp
00004 *  Part of     : OpenGLEx / Snow
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include "SnowContainer.h"
00018 
00019 // ================= MEMBER FUNCTIONS =======================
00020 
00021 // ---------------------------------------------------------
00022 // CSnowContainer::ConstructL(const TRect& aRect)
00023 // EPOC two phased constructor
00024 // ---------------------------------------------------------
00025 //
00026 void CSnowContainer::ConstructL(const TRect& /*aRect*/)
00027     {
00028     iOpenGlInitialized = EFalse;
00029     CreateWindowL();
00030 
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     iSnow = CSnow::NewL( size.iWidth, size.iHeight ); 
00157     iSnow->AppInitL();
00158 
00159     iOpenGlInitialized = ETrue;
00160     
00161     iPeriodic = CPeriodic::NewL( -100 );         // Create an active object for
00162                                                  // animating the scene
00163     iPeriodic->Start( 100, 100, TCallBack( CSnowContainer::DrawCallBack, this ) );
00164     }
00165 
00166 // Destructor
00167 CSnowContainer::~CSnowContainer()
00168     {
00169     delete iPeriodic;
00170 
00171     /* AppExit call is made to release any allocations made in AppInit. 
00172      This call has to be made here before we destroy the rendering context. */
00173     if ( iSnow )
00174                 {
00175         iSnow->AppExit();
00176         delete iSnow;
00177                 }
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     }
00185 
00186 // ---------------------------------------------------------
00187 // CSnowContainer::SizeChanged()
00188 // Called by framework when the view size is changed
00189 // ---------------------------------------------------------
00190 //
00191 void CSnowContainer::SizeChanged()
00192     {
00193     if( iOpenGlInitialized && iSnow )
00194         {
00195         TSize size;
00196         size = this->Size();
00197         
00198         iSnow->SetScreenSize( size.iWidth, size.iHeight );
00199         }
00200     }
00201 
00202 
00203 // ---------------------------------------------------------
00204 // CSnowContainer::HandleResourceChange(
00205 //     TInt aType)
00206 // Dynamic screen resize changes by calling the
00207 // SetExtentToWholeScreen() method again.
00208 // ---------------------------------------------------------
00209 //
00210  void CSnowContainer::HandleResourceChange(TInt aType)
00211     {
00212         switch( aType )
00213         {
00214             case KEikDynamicLayoutVariantSwitch:
00215                     SetExtentToWholeScreen();
00216                     break;
00217             }
00218     }
00219 
00220 
00221 
00222 
00223 // ---------------------------------------------------------
00224 // CSnowContainer::CountComponentControls() const
00225 // ---------------------------------------------------------
00226 //
00227 TInt CSnowContainer::CountComponentControls() const
00228     {
00229     return 0; // return nbr of controls inside this container
00230     }
00231 
00232 // ---------------------------------------------------------
00233 // CSnowContainer::ComponentControl(TInt aIndex) const
00234 // ---------------------------------------------------------
00235 //
00236 CCoeControl* CSnowContainer::ComponentControl(TInt /*aIndex*/) const
00237     {
00238     return NULL;
00239     }
00240 
00241 // ---------------------------------------------------------
00242 // CSnowContainer::Draw(const TRect& aRect) const
00243 // ---------------------------------------------------------
00244 //
00245 void CSnowContainer::Draw(const TRect& aRect) const
00246         {
00247     CWindowGc& gc = SystemGc();
00248     gc.Clear( aRect );
00249 
00250     if ( iSnow->GetState() == CSnow::ELoadingTextures )
00251                 {
00252                 _LIT(KLoading, "Loading...");
00253         Utils::OutputText(SystemGc(), KLoading, TPoint(70, 105), KRgbBlack );
00254                 }
00255         }
00256 
00257 // ---------------------------------------------------------
00258 // CSnowContainer::HandleControlEventL(
00259 //     CCoeControl* aControl,TCoeEvent aEventType)
00260 // ---------------------------------------------------------
00261 //
00262 void CSnowContainer::HandleControlEventL(
00263     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00264     {
00265     // TODO: Add your control event handler code here
00266     }
00267 
00268 // ---------------------------------------------------------
00269 // CSnowContainer::DrawCallBack( TAny* aInstance )
00270 // Called by the CPeriodic in order to draw the graphics 
00271 // ---------------------------------------------------------
00272 //
00273 TInt CSnowContainer::DrawCallBack( TAny* aInstance )
00274         {
00275     CSnowContainer* instance = (CSnowContainer*) aInstance;
00276 
00277     if ( instance->iSnow->GetState() == CSnow::ERunning )
00278                 {
00279         instance->iFrame++;
00280     
00281         /* Call the main OpenGL ES Symbian rendering 'loop' */
00282         instance->iSnow->AppCycle( instance->iFrame );
00283     
00284         /* Call eglSwapBuffers, which blit the graphics to the window */
00285         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00286 
00287         /* To keep the background light on */
00288         User::ResetInactivityTime();
00289     
00290         /* Suspend the current thread for a short while. Give some time 
00291            to other threads and AOs, avoids the ViewSrv error in ARMI and
00292            THUMB release builds. One may try to decrease the callback 
00293            function instead of this. */ 
00294         if ( !(instance->iFrame % 50) )  
00295                         {
00296             User::After(0);
00297                         }
00298                 }
00299     
00300     return 0; 
00301         }
00302 
00303 // End of File  

© Nokia 2006

Back to top