|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : BillboardContainer.cpp 00004 * Part of : OpenGLEx / Billboard 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 "BillboardContainer.h" 00015 #include <AknUtils.h> // Pen support. 00016 00017 // ================= MEMBER FUNCTIONS ======================= 00018 00019 // --------------------------------------------------------- 00020 // CBillboardContainer::ConstructL(const TRect& aRect) 00021 // EPOC two phased constructor 00022 // --------------------------------------------------------- 00023 // 00024 void CBillboardContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi) 00025 { 00026 iAppUi = aAppUi; 00027 // Open GL hasn't been initialized 00028 iOpenGlInitialized = EFalse; 00029 // Create the input handler 00030 iInputHandler = CBillboardInput::NewL(); 00031 00032 // Create the native window 00033 CreateWindowL(); 00034 00035 // Do not go to full-screen in touch devices. 00036 if (AknLayoutUtils::PenEnabled()) 00037 { 00038 SetRect(aRect); 00039 } 00040 else 00041 { 00042 SetExtentToWholeScreen(); // Take the whole screen into use 00043 } 00044 ActivateL(); 00045 00046 // Describes the format, type and size of the color buffers and 00047 // ancillary buffers for EGLSurface 00048 EGLConfig config; 00049 00050 // Get the display for drawing graphics 00051 iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY ); 00052 if ( iEglDisplay == NULL ) 00053 { 00054 _LIT( KGetDisplayFailed, "eglGetDisplay failed" ); 00055 User::Panic( KGetDisplayFailed, 0 ); 00056 } 00057 00058 // Initialize display 00059 if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE ) 00060 { 00061 _LIT( KInitializeFailed, "eglInitialize failed" ); 00062 User::Panic( KInitializeFailed, 0 ); 00063 } 00064 00065 // Pointer for EGLConfigs 00066 EGLConfig *configList = NULL; 00067 EGLint numOfConfigs = 0; 00068 EGLint configSize = 0; 00069 00070 // Get the number of possible EGLConfigs 00071 if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE ) 00072 { 00073 _LIT( KGetConfigsFailed, "eglGetConfigs failed" ); 00074 User::Panic( KGetConfigsFailed, 0 ); 00075 } 00076 00077 configSize = numOfConfigs; 00078 00079 // Allocate memory for the configList 00080 configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize ); 00081 if ( configList == NULL ) 00082 { 00083 _LIT( KConfigAllocFailed, "config alloc failed" ); 00084 User::Panic( KConfigAllocFailed, 0 ); 00085 } 00086 00087 // Define properties for the wanted EGLSurface 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 // Choose the best EGLConfig. EGLConfigs returned by eglChooseConfig are 00158 // sorted so that the best matching EGLConfig is first in the list. 00159 config = configList[0]; 00160 00161 // Free configList, as it's not used anymore. 00162 User::Free( configList ); 00163 00164 // Create a window where the graphics are blitted 00165 iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL ); 00166 if ( iEglSurface == NULL ) 00167 { 00168 _LIT( KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed" ); 00169 User::Panic( KCreateWindowSurfaceFailed, 0 ); 00170 } 00171 00172 // Create a rendering context 00173 iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL ); 00174 if ( iEglContext == NULL ) 00175 { 00176 _LIT( KCreateContextFailed, "eglCreateContext failed" ); 00177 User::Panic( KCreateContextFailed, 0 ); 00178 } 00179 00180 // Make the context current. Binds context to the current rendering thread 00181 // and surface. 00182 if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE ) 00183 { 00184 _LIT( KMakeCurrentFailed, "eglMakeCurrent failed" ); 00185 User::Panic( KMakeCurrentFailed, 0 ); 00186 } 00187 00188 00189 TSize size = this->Size(); 00190 iBillboard = CBillboard::NewL( size.iWidth, size.iHeight, iInputHandler ); 00191 iBillboard->AppInitL(); 00192 00193 iOpenGlInitialized = ETrue; 00194 00195 // Initialize timers and counters 00196 iFrame = 0; 00197 iStartTimeTicks = User::NTickCount(); 00198 00199 #ifdef __WINS__ 00200 00201 // In the emulator the tickcount runs at 200Hz 00202 iLastFrameTimeSecs = ( (GLfloat) iStartTimeTicks ) / 200.f; 00203 00204 #else 00205 00206 // In the device the tickcount runs at 1000hz (as intended) 00207 iLastFrameTimeSecs = ( (GLfloat) iStartTimeTicks ) / 1000.f; 00208 00209 #endif 00210 00211 // Create an active object for animating the scene 00212 iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle ); 00213 iPeriodic->Start( 100, 100, TCallBack( CBillboardContainer::DrawCallBack, this ) ); 00214 } 00215 00216 // ------------------------------------------------------------------------------ 00217 // CBillboardContainer::OfferKeyEventL() 00218 // Handles joystick movements. 00219 // ------------------------------------------------------------------------------ 00220 // 00221 TKeyResponse CBillboardContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent,TEventCode aType ) 00222 { 00223 switch( aType ) 00224 { 00225 case EEventKeyDown: 00226 { 00227 return iInputHandler->KeyDown( aKeyEvent.iScanCode ); 00228 } 00229 case EEventKeyUp: 00230 { 00231 return iInputHandler->KeyUp( aKeyEvent.iScanCode ); 00232 } 00233 } 00234 return EKeyWasNotConsumed; 00235 } 00236 00237 // --------------------------------------------------------- 00238 // CBillboardContainer::~CBillboardContainer() 00239 // Destructor. Releases any used resources. 00240 // --------------------------------------------------------- 00241 // 00242 CBillboardContainer::~CBillboardContainer() 00243 { 00244 delete iPeriodic; 00245 00246 /* AppExit call is made to release any allocations made in AppInit. 00247 This call has to be made here before we destroy the rendering context. */ 00248 if ( iBillboard ) 00249 { 00250 iBillboard->AppExit(); 00251 delete iBillboard; 00252 } 00253 00254 delete iInputHandler; 00255 00256 eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT ); 00257 eglDestroySurface( iEglDisplay, iEglSurface ); 00258 eglDestroyContext( iEglDisplay, iEglContext ); 00259 eglTerminate( iEglDisplay ); 00260 } 00261 00262 // --------------------------------------------------------- 00263 // CBillboardContainer::SizeChanged() 00264 // Called by framework when the view size is changed 00265 // --------------------------------------------------------- 00266 // 00267 void CBillboardContainer::SizeChanged() 00268 { 00269 if( iOpenGlInitialized && iBillboard ) 00270 { 00271 // If example is running and OpenGL ES is initialized 00272 // notify it of the screen size change 00273 TSize size = this->Size(); 00274 iBillboard->SetScreenSize( size.iWidth, size.iHeight ); 00275 } 00276 } 00277 00278 00279 // --------------------------------------------------------- 00280 // CBillboardContainer::HandleResourceChange( 00281 // TInt aType) 00282 // Dynamic screen resize changes by calling the 00283 // SetExtentToWholeScreen() method again. 00284 // --------------------------------------------------------- 00285 // 00286 void CBillboardContainer::HandleResourceChange(TInt aType) 00287 { 00288 switch( aType ) 00289 { 00290 case KEikDynamicLayoutVariantSwitch: 00291 // Do not go full-screen in touch devices. 00292 if (AknLayoutUtils::PenEnabled()) 00293 { 00294 SetRect(iAppUi->ClientRect()); 00295 } 00296 else 00297 { 00298 SetExtentToWholeScreen(); // Take the whole screen into use 00299 } 00300 break; 00301 } 00302 } 00303 00304 // --------------------------------------------------------- 00305 // CBillboardContainer::CountComponentControls() const 00306 // Return number of controls inside this container 00307 // --------------------------------------------------------- 00308 // 00309 TInt CBillboardContainer::CountComponentControls() const 00310 { 00311 return 0; 00312 } 00313 00314 // --------------------------------------------------------- 00315 // CBillboardContainer::ComponentControl(TInt aIndex) const 00316 // --------------------------------------------------------- 00317 // 00318 CCoeControl* CBillboardContainer::ComponentControl(TInt /*aIndex*/) const 00319 { 00320 return NULL; 00321 } 00322 00323 // --------------------------------------------------------- 00324 // CBillboardContainer::Draw(const TRect& aRect) const 00325 // --------------------------------------------------------- 00326 // 00327 void CBillboardContainer::Draw(const TRect& aRect) const 00328 { 00329 CWindowGc& gc = SystemGc(); 00330 gc.Clear( aRect ); 00331 00332 if ( iBillboard->GetState() == CBillboard::ELoadingTextures ) 00333 { 00334 _LIT(KLoading, "Loading..."); 00335 utilOutputText(SystemGc(), KLoading, TPoint(70, 105), KRgbBlack ); 00336 } 00337 } 00338 00339 // --------------------------------------------------------- 00340 // CBillboardContainer::HandleControlEventL( 00341 // CCoeControl* aControl,TCoeEvent aEventType) 00342 // --------------------------------------------------------- 00343 // 00344 void CBillboardContainer::HandleControlEventL( 00345 CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/) 00346 { 00347 // TODO: Add any control event handler code here 00348 } 00349 00350 // ---------------------------------------------------------------------- 00351 // CBillboardContainer::GetTimeTick 00352 // Returns the current time. 00353 // ---------------------------------------------------------------------- 00354 // 00355 TTime CBillboardContainer::GetTimeTick() 00356 { 00357 TTime time; 00358 time.UniversalTime(); 00359 return time; 00360 } 00361 00362 00363 // --------------------------------------------------------- 00364 // CBillboardContainer::DrawCallBack( TAny* aInstance ) 00365 // Called by the CPeriodic in order to draw the graphics 00366 // --------------------------------------------------------- 00367 // 00368 TInt CBillboardContainer::DrawCallBack( TAny* aInstance ) 00369 { 00370 // Cast parameter to container instance 00371 CBillboardContainer* instance = (CBillboardContainer*) aInstance; 00372 00373 // Update the frame counts 00374 instance->iFrame++; 00375 00376 // Compute the elapsed time in seconds since the startup of the example 00377 #ifdef __WINS__ 00378 00379 // In the emulator the tickcount runs at 200Hz 00380 GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTimeTicks ) ) / 200.f; 00381 00382 #else 00383 00384 // In the device the tickcount runs at 1000hz (as intended) 00385 GLfloat timeSecs = ( (GLfloat) ( User::NTickCount() - instance->iStartTimeTicks ) ) / 1000.f; 00386 00387 #endif 00388 00389 // Compute the elapsed time since last frame was drawn. Note that due to the 00390 // resolution of the system timer this value can be 0.0 even though new frames are being rendered. 00391 // This applies especially when running the emulator on a PC. 00392 GLfloat deltaTimeSecs = timeSecs - instance->iLastFrameTimeSecs; 00393 00394 // Set the current time to be the last frame time for the upcoming frame 00395 instance->iLastFrameTimeSecs = timeSecs; 00396 00397 if ( instance->iBillboard->GetState() == CBillboard::ERunning ) 00398 { 00399 // Call the main OpenGL ES Symbian rendering 'loop' 00400 instance->iBillboard->AppCycle( instance->iFrame, timeSecs, deltaTimeSecs ); 00401 00402 // Call eglSwapBuffers, which blit the graphics to the window 00403 eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface ); 00404 00405 if ( !(instance->iFrame % 50 ) ) 00406 { 00407 // Reset inactivity timer to keep the background light on 00408 User::ResetInactivityTime(); 00409 00410 // Suspend the current thread for a short while. Give some time 00411 // to other threads and AOs, avoids the ViewSrv error in ARMI and 00412 // THUMB release builds. One may try to decrease the callback 00413 // function instead of this. 00414 User::After( 0 ); 00415 } 00416 } 00417 00418 return 0; 00419 } 00420 00421 // End of File
|
© Nokia 2009 |