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