Nokia N97 SDK
Example Applications Guide

Slideshow.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : Slideshow.cpp
00004  *  Part of     : OpenGLEx / Slideshow
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 
00015 #include <aknnotewrappers.h>
00016 #include <e32math.h>
00017 #include "slideshow.h"
00018 
00019 // CONSTANTS
00020 
00021 // ============================= MEMBER FUNCTIONS ==============================
00022 
00023 // -----------------------------------------------------------------------------
00024 // CSlideshow::NewL
00025 // Symbian 2-phase constructor. Can leave.
00026 // -----------------------------------------------------------------------------
00027 //
00028 CSlideshow* CSlideshow::NewL( TUint aWidth, TUint aHeight )
00029         {
00030     // Symbian 2-phase constructor. Calls both the default
00031     // C++ constructor and Symbian ConstructL methods
00032     CSlideshow* self = new (ELeave) CSlideshow( aWidth, aHeight );
00033     CleanupStack::PushL( self );
00034     self->ConstructL();
00035     CleanupStack::Pop();
00036 
00037     return self;
00038         }
00039 
00040 // ----------------------------------------------------------------------------
00041 // CSlideshow::CSlideshow
00042 // C++ default constructor can NOT contain any code, that
00043 // might leave.
00044 // ----------------------------------------------------------------------------
00045 //
00046 CSlideshow::CSlideshow( TUint aWidth, TUint aHeight )
00047         : CFiniteStateMachine(),
00048       iCurrentPicture(0),
00049       iNextPicture(0),
00050       iRequestedPictureDelta(0),
00051       iTransitionRenderer(0),
00052       iTransitionTime(0)
00053         {
00054     iScreenWidth  = aWidth;
00055     iScreenHeight = aHeight;
00056         }
00057 
00058 // ----------------------------------------------------------------------------
00059 // CSlideshow::ConstructL
00060 // Symbian 2nd phase constructor can leave.
00061 // ----------------------------------------------------------------------------
00062 //
00063 void CSlideshow::ConstructL( void )
00064         {
00065         }
00066 
00067 // ----------------------------------------------------------------------------
00068 // CSlideshow::~CSlideshow()
00069 // Destructor.
00070 // ----------------------------------------------------------------------------
00071 //
00072 CSlideshow::~CSlideshow()
00073         {
00074         }
00075 
00076 // ----------------------------------------------------------------------------
00077 // CSlideshow::AppInit
00078 // Application initialization code.
00079 // ----------------------------------------------------------------------------
00080 //
00081 void CSlideshow::AppInitL( void )
00082         {
00083     // Construct a texture manager that uses the application's private
00084     // directory as the source location for all textures.
00085     iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenHeight, 0, 0, 0, 0, 0,
00086                                               this );
00087     // Initialize viewport and projection.
00088                 SetScreenSize( iScreenWidth, iScreenHeight );
00089 
00090     // Set the screen background color.
00091     glClearColor( 0.f, 0.f, 0.f, 1.f );
00092 
00093     // Enable back face culling.
00094     glDisable( GL_CULL_FACE  );
00095     glDisable( GL_DEPTH_TEST );
00096     glEnable( GL_TEXTURE_2D );
00097 
00098     // Disable lighting
00099     glDisable( GL_LIGHTING );
00100 
00101     // Initialize viewport and projection.
00102     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00103     glMatrixMode( GL_PROJECTION );
00104 
00105     // Set best perspective correction
00106     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00107 
00108     // Enable vertex arrays.
00109     glEnableClientState( GL_VERTEX_ARRAY );
00110 
00111     // Enable texture arrays.
00112     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00113 
00114 
00115     // Push the textures into the loading queue
00116     iTextureManager->RequestToLoad( _L("los_cristianos.jpg"), &iPictures[0] );
00117     iTextureManager->RequestToLoad( _L("sky.jpg"), &iPictures[1] );
00118     iTextureManager->RequestToLoad( _L("sugar_loaf.jpg"), &iPictures[2] );
00119     iTextureManager->RequestToLoad( _L("parati.jpg"), &iPictures[3] );
00120     iTextureManager->RequestToLoad( _L("banana_trees.jpg"), &iPictures[4] );
00121     iPictureCount = sizeof(iPictures) / sizeof(iPictures[0]);
00122 
00123     // Generate some masks
00124     glGenTextures( 1, &iMasks[0].iID );
00125     glGenTextures( 1, &iMasks[1].iID );
00126 
00127     TUint8* maskData = new TUint8[64 * 64 * 4];
00128     TUint8* maskDataPtr = maskData;
00129     TInt x, y;
00130 
00131     // The first mask has a band of colors in the middle
00132     for (y = 0; y < 64; y++)
00133         {
00134         for (x = 0; x < 64; x++)
00135             {
00136             *maskDataPtr++ = Min(Max(Abs(24 - y) * 16, 0), 255);
00137             *maskDataPtr++ = Min(Max(Abs(28 - y) * 16, 0), 255);
00138             *maskDataPtr++ = Min(Max(Abs(32 - y) * 16, 0), 255);
00139             *maskDataPtr++ = Min(Max((32 - y) * 16, 0), 255);
00140             }
00141         }
00142 
00143     glBindTexture( GL_TEXTURE_2D, iMasks[0].iID );
00144     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, maskData);
00145 
00146     // The second mask has a wavy alpha threshold in the middle
00147     maskDataPtr = maskData;
00148     for (y = 0; y < 64; y++)
00149         {
00150         for (x = 0; x < 64; x++)
00151             {
00152             TReal s;
00153             Math::Sin(s, x / 4.0);
00154             TInt maskFactor = Min(Max((32 - y - s * 6) * 16, 0), 255);
00155             *maskDataPtr++ = 255;
00156             *maskDataPtr++ = 255;
00157             *maskDataPtr++ = 255;
00158             *maskDataPtr++ = maskFactor;
00159             }
00160         }
00161     glBindTexture( GL_TEXTURE_2D, iMasks[1].iID );
00162     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, maskData);
00163 
00164     delete[] maskData;
00165 
00166     // Start to load the queued textures
00167     iTextureManager->DoLoadL();
00168         }
00169 
00170 // ----------------------------------------------------------------------------
00171 // CSlideshow::AppExit
00172 // Application cleanup code.
00173 // ----------------------------------------------------------------------------
00174 //
00175 void CSlideshow::AppExit( void )
00176         {
00177     delete iTextureManager;
00178     delete iTransitionRenderer;
00179         }
00180 
00181 
00182 // ----------------------------------------------------------------------------
00183 // CSlideshow::AppCycle
00184 // Draws and animates the objects.
00185 // aFrame = Number of the frame to be rendered.
00186 // aTimeSecs = Seconds elapsed since the application started running.
00187 // aDeltaTimeSecs = Seconds elapsed since last call to AppCycle().
00188 // ----------------------------------------------------------------------------
00189 //
00190 void CSlideshow::AppCycle( TInt /*aFrame*/,
00191                        TReal /*aTimeSecs*/,
00192                        TReal aDeltaTimeSecs )
00193         {
00194           // Make sure the timestep is non-zero, since otherwise the animation wouldn't progress at all.
00195           if (aDeltaTimeSecs < 0.04)
00196             aDeltaTimeSecs = 0.04;
00197 
00198     // Stop the transition if it's complete
00199     if (iTransitionRenderer && iTransitionTime <= 0)
00200         {
00201         delete iTransitionRenderer;
00202         iTransitionRenderer = 0;
00203         iCurrentPicture = iNextPicture;
00204         }
00205 
00206     // Advance the slideshow if we have been requested to
00207     if (iRequestedPictureDelta && !iTransitionRenderer)
00208         {
00209         iTransitionRenderer = CreateTransitionRenderer((iRequestedPictureDelta > 0) ? ENext : EPrevious);
00210         iTransitionTime = 1.0;
00211 
00212         TInt direction = (iRequestedPictureDelta > 0) ? 1 : -1;
00213         iNextPicture = (iCurrentPicture + direction + iPictureCount) % iPictureCount;
00214         iRequestedPictureDelta -= direction;
00215         }
00216 
00217     // Render the current picture. If there is no transition going on, enable
00218     // bilinear filtering to improve image quality.
00219     iStaticRenderer.Render(iPictures[iCurrentPicture], iTransitionRenderer == NULL);
00220 
00221     // Render the transition and the next picture
00222     if (iTransitionTime > 0 && iTransitionRenderer)
00223         {
00224         iTransitionRenderer->Render(iPictures[iNextPicture], iTransitionTime);
00225         // Accelerate the transition speed if we are many pictures behind
00226         iTransitionTime -= aDeltaTimeSecs * (Abs(iRequestedPictureDelta) + 1);
00227         }
00228         }
00229 
00230 void CSlideshow::ShowNextPicture( void )
00231     {
00232     iRequestedPictureDelta++;
00233     }
00234 
00235 void CSlideshow::ShowPreviousPicture( void )
00236     {
00237     iRequestedPictureDelta--;
00238     }
00239 
00240 // ----------------------------------------------------------------------------
00241 // CSlideshow::OnEnterState( TInt aState )
00242 // Called by TFiniteStateMachine when the f.s.m enters a new state
00243 // ----------------------------------------------------------------------------
00244 //
00245 void CSlideshow::OnEnterStateL( TInt /*aState*/ )
00246         {
00247         }
00248 
00249 // ----------------------------------------------------------------------------
00250 // CSlideshow::OnStartLoadingTextures()
00251 // Called for a MTextureLoadingListener by the texture manager when texture
00252 // loading operation starts
00253 // ----------------------------------------------------------------------------
00254 //
00255 void CSlideshow::OnStartLoadingTexturesL()
00256         {
00257     SetStateL( ELoadingTextures );
00258         }
00259 
00260 // ----------------------------------------------------------------------------
00261 // CSlideshow::OnEndLoadingTextures()
00262 // Called for a MTextureLoadingListener by the texture manager when texture
00263 // loading operation is completed
00264 // ----------------------------------------------------------------------------
00265 void CSlideshow::OnEndLoadingTexturesL()
00266         {
00267         if ( GetState() == ELoadingTextures )
00268                 {
00269         SetStateL( ERunning );
00270                 }
00271         }
00272 
00273 // ----------------------------------------------------------------------------
00274 // CSlideshow::SetScreenSize
00275 // Reacts to the dynamic screen size change during execution of this program.
00276 // ----------------------------------------------------------------------------
00277 //
00278 void CSlideshow::SetScreenSize( TUint aWidth, TUint aHeight )
00279     {
00280     iScreenWidth  = aWidth;
00281     iScreenHeight = aHeight;
00282 
00283     // Notify the texture manager of screen size change
00284     iTextureManager->SetScreenSize( aWidth, aHeight );
00285 
00286     // Reinitialize viewport and projection.
00287     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00288     }
00289 
00290 MTransitionRenderer* CSlideshow::CreateTransitionRenderer(TDirection dir)
00291     {
00292     // Choose a transition renderer class randomly
00293     switch (Math::Random() % 3)
00294         {
00295         case 0:
00296             {
00297             // Choose a sweep style randomly
00298             const CMaskTransition::TSweepStyle s[] =
00299                 {
00300                 CMaskTransition::ESweepCurved,
00301                 CMaskTransition::ESweepCurved2,
00302                 CMaskTransition::ESweepHorizontal,
00303                 CMaskTransition::ESweepVertical
00304                 };
00305             return new CMaskTransition(iMasks[Math::Random() % (sizeof(iMasks) / sizeof(iMasks[0]))],
00306                                        s[Math::Random() % (sizeof(s) / sizeof(s[0]))]);
00307             }
00308         case 1:
00309             if (dir == ENext)
00310                 return new CWhiskTransition(CWhiskTransition::ERight);
00311             else
00312                 return new CWhiskTransition(CWhiskTransition::ELeft);
00313         case 2:
00314             if (dir == ENext)
00315                 return new CCurtainTransition(CCurtainTransition::ERight);
00316             else
00317                 return new CCurtainTransition(CCurtainTransition::ELeft);
00318         }
00319     return 0;
00320     }
00321 
00322 // End of File

© Nokia 2009

Back to top