|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : HierarchyContainer.cpp 00004 * Part of : OpenGLEx / Hierarchy 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 "HierarchyContainer.h" 00018 00019 // ================= MEMBER FUNCTIONS ======================= 00020 00021 // --------------------------------------------------------- 00022 // CHierarchyContainer::ConstructL 00023 // Symbian two phased constructor 00024 // --------------------------------------------------------- 00025 // 00026 void CHierarchyContainer::ConstructL(const TRect& /*aRect*/) 00027 { 00028 iOpenGlInitialized = EFalse; 00029 00030 CreateWindowL(); 00031 00032 SetExtentToWholeScreen(); // Take the whole screen into use 00033 ActivateL(); 00034 00035 iFrame = 0; // 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( EGL_DEFAULT_DISPLAY ); 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(KGetDisplayFailed, "eglGetDisplay failed"); 00053 User::Panic( KGetDisplayFailed, 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 ) 00062 == 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 // Define properties for the wanted EGLSurface 00106 const EGLint attrib_list[] = { EGL_BUFFER_SIZE,BufferSize, 00107 EGL_DEPTH_SIZE,16, // 16-bit depthbuffer 00108 EGL_NONE }; 00109 00110 // Choose an EGLConfig that best matches to the properties in attrib_list 00111 if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, 00112 &numOfConfigs ) == EGL_FALSE ) 00113 { 00114 _LIT(KChooseConfigFailed, "eglChooseConfig failed"); 00115 User::Panic( KChooseConfigFailed, 0 ); 00116 } 00117 00118 Config = configList[0]; // Choose the best EGLConfig. EGLConfigs 00119 // returned by eglChooseConfig are sorted so 00120 // that the best matching EGLConfig is first in 00121 // the list. 00122 User::Free( configList ); // Free configList, not used anymore. 00123 00124 // Create a window where the graphics are blitted 00125 iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL ); 00126 if ( iEglSurface == NULL ) 00127 { 00128 _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed"); 00129 User::Panic( KCreateWindowSurfaceFailed, 0 ); 00130 } 00131 00132 // Create a rendering context 00133 iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL ); 00134 if ( iEglContext == NULL ) 00135 { 00136 _LIT(KCreateContextFailed, "eglCreateContext failed"); 00137 User::Panic( KCreateContextFailed, 0 ); 00138 } 00139 00140 /* Make the context current. Binds context to the current rendering thread 00141 and surface.*/ 00142 if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) 00143 == EGL_FALSE ) 00144 { 00145 _LIT(KMakeCurrentFailed, "eglMakeCurrent failed"); 00146 User::Panic( KMakeCurrentFailed, 0 ); 00147 } 00148 00149 TSize size; 00150 size = this->Size(); 00151 00152 iHierarchy = CHierarchy::NewL( size.iWidth, size.iHeight ); 00153 iHierarchy->AppInit(); 00154 00155 iOpenGlInitialized = ETrue; 00156 00157 iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle ); // Create an active object for 00158 // animating the scene 00159 iPeriodic->Start( 100, 100, 00160 TCallBack( CHierarchyContainer::DrawCallBack, this ) ); 00161 } 00162 00163 // Destructor 00164 CHierarchyContainer::~CHierarchyContainer() 00165 { 00166 delete iPeriodic; 00167 00168 /* AppExit call is made to release 00169 any allocations made in AppInit. */ 00170 if ( iHierarchy ) 00171 { 00172 iHierarchy->AppExit(); 00173 delete iHierarchy; 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 // CHierarchyContainer::SizeChanged() 00185 // Called by framework when the view size is changed 00186 // --------------------------------------------------------- 00187 // 00188 void CHierarchyContainer::SizeChanged() 00189 { 00190 if( iOpenGlInitialized && iHierarchy ) 00191 { 00192 TSize size; 00193 size = this->Size(); 00194 00195 iHierarchy->SetScreenSize( size.iWidth, size.iHeight ); 00196 } 00197 } 00198 00199 00200 // --------------------------------------------------------- 00201 // CHierarchyContainer::HandleResourceChange( 00202 // TInt aType) 00203 // Dynamic screen resize changes by calling the 00204 // SetExtentToWholeScreen() method again. 00205 // --------------------------------------------------------- 00206 // 00207 void CHierarchyContainer::HandleResourceChange(TInt aType) 00208 { 00209 switch( aType ) 00210 { 00211 case KEikDynamicLayoutVariantSwitch: 00212 SetExtentToWholeScreen(); 00213 break; 00214 } 00215 } 00216 00217 00218 // --------------------------------------------------------- 00219 // CHierarchyContainer::CountComponentControls() const 00220 // --------------------------------------------------------- 00221 // 00222 TInt CHierarchyContainer::CountComponentControls() const 00223 { 00224 return 0; // return nbr of controls inside this container 00225 } 00226 00227 // --------------------------------------------------------- 00228 // CHierarchyContainer::ComponentControl(TInt aIndex) const 00229 // --------------------------------------------------------- 00230 // 00231 CCoeControl* CHierarchyContainer::ComponentControl(TInt /*aIndex*/ ) const 00232 { 00233 return NULL; 00234 } 00235 00236 // --------------------------------------------------------- 00237 // CHierarchyContainer::Draw(const TRect& aRect) const 00238 // --------------------------------------------------------- 00239 // 00240 void CHierarchyContainer::Draw(const TRect& /*aRect*/ ) const 00241 { 00242 CWindowGc& gc = SystemGc(); 00243 gc.Clear( Rect() ); 00244 } 00245 00246 // --------------------------------------------------------- 00247 // CHierarchyContainer::DrawCallBack( TAny* aInstance ) 00248 // Called by the CPeriodic in order to draw the graphics 00249 // --------------------------------------------------------- 00250 // 00251 int CHierarchyContainer::DrawCallBack( TAny* aInstance ) 00252 { 00253 CHierarchyContainer* instance = (CHierarchyContainer*) aInstance; 00254 instance->iFrame++; 00255 00256 // Call the main OpengGL ES Symbian rendering 'loop' 00257 instance->iHierarchy->AppCycle( instance->iFrame ); 00258 00259 // Call eglSwapBuffers, which blit the graphics to the window 00260 eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface ); 00261 00262 // To keep the background light on 00263 if ( !(instance->iFrame%100) ) 00264 { 00265 User::ResetInactivityTime(); 00266 } 00267 00268 /* Suspend the current thread for a short while. Give some time 00269 to other threads and AOs, avoids the ViewSrv error in ARMI and 00270 THUMB release builds. One may try to decrease the callback 00271 function instead of this. */ 00272 if ( !(instance->iFrame%50) ) 00273 { 00274 User::After(0); 00275 } 00276 00277 return 0; 00278 } 00279 00280 // --------------------------------------------------------- 00281 // CHierarchyContainer::HandleControlEventL( 00282 // CCoeControl* aControl,TCoeEvent aEventType) 00283 // --------------------------------------------------------- 00284 // 00285 void CHierarchyContainer::HandleControlEventL( 00286 CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/) 00287 { 00288 } 00289 00290 // End of File
|
© Nokia 2006 |