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