S60 3rd Edition SDK for Symbian OS
Example Applications Guide

RainContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : RainContainer.cpp
00004 *  Part of     : OpenGLEx / Rain
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 "RainContainer.h"
00018 
00019 #include <eiklabel.h>  // for example label control
00020 
00021 // ================= MEMBER FUNCTIONS =======================
00022 
00023 // ---------------------------------------------------------
00024 // CRainContainer::ConstructL(const TRect& aRect)
00025 // EPOC two phased constructor
00026 // ---------------------------------------------------------
00027 //
00028 void CRainContainer::ConstructL(const TRect& /*aRect*/)
00029     {
00030     iOpenGlInitialized = EFalse;
00031     CreateWindowL();
00032 
00033     SetExtentToWholeScreen();                // Take the whole screen into use
00034     ActivateL();
00035     
00036     iFrame = 0;                              // Initialize frame counter
00037     
00038     EGLConfig config;                       // Describes the format, type and 
00039                                              // size of the color buffers and
00040                                              // ancillary buffers for EGLSurface
00041 
00042     /* Get the display for drawing graphics*/
00043     iEglDisplay = eglGetDisplay( NULL );
00044     if ( iEglDisplay == NULL )
00045                 {
00046                 _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00047         User::Panic( KGetDisplayFailed, 0 );
00048                 }
00049 
00050     /* Initialize display */
00051     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00052                 {
00053                 _LIT(KInitializeFailed, "eglInitialize failed");
00054         User::Panic( KInitializeFailed, 0 );
00055                 }
00056 
00057     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00058     TInt numOfConfigs = 0; 
00059     TInt configSize   = 0;
00060 
00061     /* Get the number of possible EGLConfigs */
00062     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00063                 {
00064                 _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00065         User::Panic( KGetConfigsFailed, 0 );
00066                 }
00067 
00068     configSize = numOfConfigs;
00069 
00070     /* Allocate memory for the configList */
00071     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00072     if ( configList == NULL )
00073                 {
00074                 _LIT(KConfigAllocFailed, "config alloc failed");
00075         User::Panic( KConfigAllocFailed, 0 );
00076                 }
00077 
00078     /* Define properties for the wanted EGLSurface.
00079        To get the best possible performance, choose
00080        an EGLConfig with a buffersize matching
00081        the current window's display mode*/
00082     TDisplayMode DMode = Window().DisplayMode();
00083     TInt BufferSize = 0;
00084 
00085     switch(DMode)
00086     {
00087     case(EColor4K):
00088         BufferSize = 12;
00089         break;
00090     case(EColor64K):
00091         BufferSize = 16;
00092         break;
00093     case(EColor16M):
00094         BufferSize = 24;
00095         break;
00096     case(EColor16MU):
00097         BufferSize = 32;
00098         break;
00099     default:
00100         _LIT(KDModeError, "unsupported displaymode");
00101         User::Panic( KDModeError, 0 );
00102         break;
00103     }
00104 
00105     const EGLint attrib_list[] = 
00106         { 
00107         EGL_BUFFER_SIZE, BufferSize,   
00108         EGL_DEPTH_SIZE, 16,    
00109         EGL_NONE 
00110         };
00111 
00112     /* Choose an EGLConfig that best matches to the properties in attrib_list */
00113     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00114                 {
00115                 _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00116                 User::Panic( KChooseConfigFailed, 0 );
00117                 }
00118     if ( numOfConfigs == 0 )
00119             {
00120                 _LIT(KNoConfig, "Can't find the requested config.");
00121                 User::Panic( KNoConfig, 0 );
00122                 }
00123 
00124     config = configList[0];   // Choose the best EGLConfig. EGLConfigs 
00125                               // returned by eglChooseConfig are sorted so 
00126                               // that the best matching EGLConfig is first in
00127                               // the list.
00128     User::Free( configList ); // Free configList, not used anymore.
00129 
00130     /* Create a window where the graphics are blitted */
00131     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00132     if ( iEglSurface == NULL )
00133                 {
00134                 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00135         User::Panic( KCreateWindowSurfaceFailed, 0 );
00136                 }
00137 
00138     /* Create a rendering context */
00139     iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00140     if ( iEglContext == NULL )
00141                 {
00142                 _LIT(KCreateContextFailed, "eglCreateContext failed");
00143         User::Panic( KCreateContextFailed, 0 );
00144                 }
00145 
00146     /* Make the context current. Binds context to the current rendering thread
00147        and surface.*/
00148     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00149                 {
00150                 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00151         User::Panic( KMakeCurrentFailed, 0 );
00152                 }
00153 
00154 
00155     TSize size;
00156     size = this->Size();
00157     iRain = CRain::NewL( size.iWidth, size.iHeight ); 
00158     iRain->AppInitL();
00159     
00160     iOpenGlInitialized = ETrue;
00161 
00162     iPeriodic = CPeriodic::NewL( -100 );         // Create an active object for
00163                                                  // animating the scene
00164     iPeriodic->Start( 100, 100, TCallBack( CRainContainer::DrawCallBack, this ) );
00165     }
00166 
00167 // Destructor
00168 CRainContainer::~CRainContainer()
00169     {
00170     delete iPeriodic;
00171 
00172     /* AppExit call is made to release any allocations made in AppInit. 
00173      This call has to be made here before we destroy the rendering context. */
00174         if( iRain )
00175                 {
00176                 iRain->AppExit();
00177                 delete iRain;
00178                 }
00179 
00180     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00181     eglDestroySurface( iEglDisplay, iEglSurface ); 
00182     eglDestroyContext( iEglDisplay, iEglContext );
00183     eglTerminate( iEglDisplay );                   // Release resources associated
00184 
00185     }
00186 
00187 // ---------------------------------------------------------
00188 // CRainContainer::SizeChanged()
00189 // Called by framework when the view size is changed
00190 // ---------------------------------------------------------
00191 //
00192 void CRainContainer::SizeChanged()
00193     {
00194     if( iOpenGlInitialized && iRain )
00195         {
00196         TSize size;
00197         size = this->Size();
00198         
00199         iRain->SetScreenSize( size.iWidth, size.iHeight );
00200         }
00201     }
00202 
00203 
00204 // ---------------------------------------------------------
00205 // ::HandleResourceChange(
00206 //     TInt aType)
00207 // Dynamic screen resize changes by calling the
00208 // SetExtentToWholeScreen() method again.
00209 // ---------------------------------------------------------
00210 //
00211  void CRainContainer::HandleResourceChange(TInt aType)
00212     {
00213         switch( aType )
00214         {
00215             case KEikDynamicLayoutVariantSwitch:
00216                     SetExtentToWholeScreen();
00217                     break;
00218             }
00219     }
00220 
00221 // ---------------------------------------------------------
00222 // CRainContainer::CountComponentControls() const
00223 // ---------------------------------------------------------
00224 //
00225 TInt CRainContainer::CountComponentControls() const
00226     {
00227     return 0; // return nbr of controls inside this container
00228     }
00229 
00230 // ---------------------------------------------------------
00231 // CRainContainer::ComponentControl(TInt aIndex) const
00232 // ---------------------------------------------------------
00233 //
00234 CCoeControl* CRainContainer::ComponentControl(TInt /*aIndex*/) const
00235     {
00236         return NULL;
00237     }
00238 
00239 // ---------------------------------------------------------
00240 // CRainContainer::Draw(const TRect& aRect) const
00241 // ---------------------------------------------------------
00242 //
00243 void CRainContainer::Draw(const TRect& aRect) const
00244         {
00245         CWindowGc& gc = SystemGc();
00246         gc.Clear( aRect );
00247 
00248     if ( iRain->GetState() == CRain::ELoadingTextures )
00249                 {
00250                 _LIT(KLoading, "Loading...");
00251                 Utils::OutputText(SystemGc(), KLoading, TPoint(70, 105), KRgbBlack );
00252                 }
00253         }
00254 
00255 // ---------------------------------------------------------
00256 // CRainContainer::HandleControlEventL(
00257 //     CCoeControl* aControl,TCoeEvent aEventType)
00258 // ---------------------------------------------------------
00259 //
00260 void CRainContainer::HandleControlEventL(
00261     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00262     {
00263     }
00264 
00265 // ---------------------------------------------------------
00266 // CRainContainer::DrawCallBack( TAny* aInstance )
00267 // Called by the CPeriodic in order to draw the graphics 
00268 // ---------------------------------------------------------
00269 //
00270 TInt CRainContainer::DrawCallBack( TAny* aInstance )
00271         {
00272         CRainContainer* instance = (CRainContainer*) aInstance;
00273 
00274     if ( instance->iRain->GetState() == CRain::ERunning )
00275                 {
00276                 instance->iFrame++;
00277     
00278                 /* Call the main OpenGL ES Symbian rendering 'loop' */
00279                 instance->iRain->AppCycle( instance->iFrame );
00280     
00281                 /* Call eglSwapBuffers, which blit the graphics to the window */
00282                 eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00283     
00284                 /* To keep the background light on */
00285                 User::ResetInactivityTime();
00286     
00287                 /* Suspend the current thread for a short while. Give some time 
00288                    to other threads and AOs, avoids the ViewSrv error in ARMI and
00289                    THUMB release builds. One may try to decrease the callback 
00290                    function instead of this. */ 
00291                 if ( !(instance->iFrame % 50) )   
00292                         {
00293                         User::After(0);
00294                         }       
00295                 }
00296     return 0; 
00297         }
00298 
00299 // End of File  

© Nokia 2006

Back to top