|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : SimpleCubeContainer.cpp 00004 * Part of : OpenGLEx / SimpleCube 00005 * 00006 * Copyright (c) 2004-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 00014 // INCLUDE FILES 00015 #include "SimpleCubeContainer.h" 00016 #include <AknUtils.h> // Pen support. 00017 // ================= MEMBER FUNCTIONS ======================= 00018 00019 // --------------------------------------------------------- 00020 // CSimpleCubeContainer::ConstructL(const TRect& aRect) 00021 // EPOC two phased constructor 00022 // --------------------------------------------------------- 00023 // 00024 void CSimpleCubeContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi) 00025 { 00026 iAppUi = aAppUi; 00027 iOpenGlInitialized = EFalse; 00028 CreateWindowL(); 00029 00030 // Do not go to full-screen in touch devices. 00031 if (AknLayoutUtils::PenEnabled()) 00032 { 00033 SetRect(aRect); 00034 } 00035 else 00036 { 00037 SetExtentToWholeScreen(); // Take the whole screen into use 00038 } 00039 ActivateL(); 00040 00041 iFrame = 0; // Initialize frame counter 00042 00043 EGLConfig Config; // Describes the format, type and 00044 // size of the color buffers and 00045 // ancillary buffers for EGLSurface 00046 00047 // Get the display for drawing graphics 00048 iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY ); 00049 if ( iEglDisplay == NULL ) 00050 { 00051 _LIT(KGetDisplayFailed, "eglGetDisplay failed"); 00052 User::Panic( KGetDisplayFailed, 0 ); 00053 } 00054 00055 // Initialize display 00056 if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE ) 00057 { 00058 _LIT(KInitializeFailed, "eglInitialize failed"); 00059 User::Panic( KInitializeFailed, 0 ); 00060 } 00061 00062 EGLConfig *configList = NULL; // Pointer for EGLConfigs 00063 EGLint numOfConfigs = 0; 00064 EGLint configSize = 0; 00065 00066 // Get the number of possible EGLConfigs 00067 if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) 00068 == EGL_FALSE ) 00069 { 00070 _LIT(KGetConfigsFailed, "eglGetConfigs failed"); 00071 User::Panic( KGetConfigsFailed, 0 ); 00072 } 00073 00074 configSize = numOfConfigs; 00075 00076 // Allocate memory for the configList 00077 configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize ); 00078 if ( configList == NULL ) 00079 { 00080 _LIT(KConfigAllocFailed, "config alloc failed"); 00081 User::Panic( KConfigAllocFailed, 0 ); 00082 } 00083 00084 /* Define properties for the wanted EGLSurface. 00085 To get the best possible performance, choose 00086 an EGLConfig with a buffersize matching 00087 the current 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 requesting a full-screen antialiased window surface 00113 const EGLint attrib_list_fsaa[] = 00114 { 00115 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, 00116 EGL_BUFFER_SIZE, BufferSize, 00117 EGL_DEPTH_SIZE, 16, 00118 EGL_SAMPLE_BUFFERS, 1, 00119 EGL_SAMPLES, 4, 00120 EGL_NONE 00121 }; 00122 00123 // Define properties for requesting a non-antialiased window surface 00124 const EGLint attrib_list[] = 00125 { 00126 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, 00127 EGL_BUFFER_SIZE, BufferSize, 00128 EGL_DEPTH_SIZE, 16, 00129 EGL_NONE 00130 }; 00131 00132 // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa 00133 if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE ) 00134 { 00135 _LIT( KChooseConfigFailed, "eglChooseConfig failed" ); 00136 User::Panic( KChooseConfigFailed, 0 ); 00137 } 00138 00139 // Check if no configurations were found 00140 if ( numOfConfigs == 0 ) 00141 { 00142 // No configs with antialising were found. Try to get the non-antialiased config 00143 if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE ) 00144 { 00145 _LIT( KChooseConfigFailed, "eglChooseConfig failed" ); 00146 User::Panic( KChooseConfigFailed, 0 ); 00147 } 00148 00149 if ( numOfConfigs == 0 ) 00150 { 00151 // No configs found without antialiasing 00152 _LIT( KNoConfig, "Can't find the requested config." ); 00153 User::Panic( KNoConfig, 0 ); 00154 } 00155 } 00156 00157 Config = configList[0]; // Choose the best EGLConfig. EGLConfigs 00158 // returned by eglChooseConfig are sorted so 00159 // that the best matching EGLConfig is first in 00160 // the list. 00161 User::Free( configList ); // Free configList, not used anymore. 00162 00163 // Create a window where the graphics are blitted 00164 iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL ); 00165 if ( iEglSurface == NULL ) 00166 { 00167 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed"); 00168 User::Panic( KCreateWindowSurfaceFailed, 0 ); 00169 } 00170 00171 // Create a rendering context 00172 iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL ); 00173 if ( iEglContext == NULL ) 00174 { 00175 _LIT(KCreateContextFailed, "eglCreateContext failed"); 00176 User::Panic( KCreateContextFailed, 0 ); 00177 } 00178 00179 /* Make the context current. Binds context to the current rendering thread 00180 and surface.*/ 00181 if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) 00182 == EGL_FALSE ) 00183 { 00184 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed"); 00185 User::Panic( KMakeCurrentFailed, 0 ); 00186 } 00187 00188 00189 TSize size; 00190 size = this->Size(); 00191 00192 iSimpleCube = CSimpleCube::NewL( size.iWidth, size.iHeight ); // Create an instance of SimpleCube 00193 iSimpleCube->AppInit(); // Initialize OpenGL ES 00194 00195 iOpenGlInitialized = ETrue; 00196 00197 iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle ); // Create an active object for 00198 // animating the scene 00199 iPeriodic->Start( 100, 100, 00200 TCallBack( CSimpleCubeContainer::DrawCallBack, this ) ); 00201 00202 } 00203 00204 // Destructor 00205 CSimpleCubeContainer::~CSimpleCubeContainer() 00206 { 00207 delete iPeriodic; 00208 00209 /* AppExit call is made to release 00210 any allocations made in AppInit. */ 00211 if ( iSimpleCube ) 00212 { 00213 iSimpleCube->AppExit(); 00214 delete iSimpleCube; 00215 } 00216 00217 eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT ); 00218 eglDestroySurface( iEglDisplay, iEglSurface ); 00219 eglDestroyContext( iEglDisplay, iEglContext ); 00220 eglTerminate( iEglDisplay ); // Release resources associated 00221 // with EGL and OpenGL ES 00222 } 00223 00224 // --------------------------------------------------------- 00225 // CSimpleCubeContainer::SizeChanged() 00226 // Called by framework when the view size is changed 00227 // --------------------------------------------------------- 00228 // 00229 void CSimpleCubeContainer::SizeChanged() 00230 { 00231 if( iOpenGlInitialized && iSimpleCube ) 00232 { 00233 TSize size; 00234 size = this->Size(); 00235 00236 iSimpleCube->SetScreenSize( size.iWidth, size.iHeight ); 00237 } 00238 } 00239 00240 00241 // --------------------------------------------------------- 00242 // CSimpleCubeContainer::HandleResourceChange( 00243 // TInt aType) 00244 // Dynamic screen resize changes by calling the 00245 // SetExtentToWholeScreen() method again. 00246 // --------------------------------------------------------- 00247 // 00248 void CSimpleCubeContainer::HandleResourceChange(TInt aType) 00249 { 00250 switch( aType ) 00251 { 00252 case KEikDynamicLayoutVariantSwitch: 00253 // Do not go full-screen in touch devices. 00254 if (AknLayoutUtils::PenEnabled()) 00255 { 00256 SetRect(iAppUi->ClientRect()); 00257 } 00258 else 00259 { 00260 SetExtentToWholeScreen(); // Take the whole screen into use 00261 } 00262 break; 00263 } 00264 } 00265 00266 // --------------------------------------------------------- 00267 // CSimpleCubeContainer::CountComponentControls() const 00268 // --------------------------------------------------------- 00269 // 00270 TInt CSimpleCubeContainer::CountComponentControls() const 00271 { 00272 return 0; 00273 } 00274 00275 // --------------------------------------------------------- 00276 // CSimpleCubeContainer::ComponentControl(TInt aIndex) const 00277 // --------------------------------------------------------- 00278 // 00279 CCoeControl* CSimpleCubeContainer::ComponentControl(TInt /*aIndex*/ ) const 00280 { 00281 return NULL; 00282 } 00283 00284 // --------------------------------------------------------- 00285 // CSimpleCubeContainer::Draw(const TRect& aRect) const 00286 // --------------------------------------------------------- 00287 // 00288 void CSimpleCubeContainer::Draw(const TRect& /*aRect*/ ) const 00289 { 00290 // No need to implement anything here! 00291 } 00292 00293 // --------------------------------------------------------- 00294 // CSimpleCubeContainer::DrawCallBack( TAny* aInstance ) 00295 // Called by the CPeriodic in order to draw the graphics 00296 // --------------------------------------------------------- 00297 // 00298 TInt CSimpleCubeContainer::DrawCallBack( TAny* aInstance ) 00299 { 00300 CSimpleCubeContainer* instance = (CSimpleCubeContainer*) aInstance; 00301 instance->iFrame++; 00302 00303 // Call the main OpenGL ES Symbian rendering 'loop' 00304 instance->iSimpleCube->AppCycle( instance->iFrame ); 00305 00306 // Call eglSwapBuffers, which blit the graphics to the window 00307 eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface ); 00308 00309 // To keep the background light on 00310 if ( !(instance->iFrame%100) ) 00311 { 00312 User::ResetInactivityTime(); 00313 } 00314 00315 /* Suspend the current thread for a short while. Give some time 00316 to other threads and AOs, avoids the ViewSrv error in ARMI and 00317 THUMB release builds. One may try to decrease the callback 00318 function instead of this. */ 00319 if ( !(instance->iFrame%50) ) 00320 { 00321 User::After(0); 00322 } 00323 00324 return 0; 00325 } 00326 00327 // --------------------------------------------------------- 00328 // CSimpleCubeContainer::HandleControlEventL( 00329 // CCoeControl* aControl,TCoeEvent aEventType) 00330 // --------------------------------------------------------- 00331 // 00332 void CSimpleCubeContainer::HandleControlEventL( 00333 CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/) 00334 { 00335 } 00336 00337 // End of File
|
© Nokia 2009 |