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