CS001110 - Fading effect with RGA
From Forum Nokia Wiki
| ID | CS001110 | Creation date | October 1, 2008 |
| Platform | S60 3rd Edition | Tested on devices | Nokia N93, Nokia N95 |
| Category | Open C/C++ | Subcategory | RGA |
| Keywords (APIs, classes, methods, functions): |
Overview
This snippet shows how to make a fading effect. This is done by creating an alpha bitmap and applying it on the original image with the BitBlitAlphaMask() function. This will influence the opacity of original image and create a fade in or fade out effect.
This example also shows the use of IOneShotTimer timer provided by RGA.
Note: In order to use this code, you need to install the Open C plug-in.
Preconditions
This example relies on RGA API ngi and examplefw frameworks.
To run this snippet you need to use ngi and examplefw frameworks provided with Open C/C++ Plug-ins for S60 3rd Edition. Examplefw sources are located in "\nokia_plugin\rga\s60rgaex\common\gameexfw\src\" folder after the plug-in has been installed.
MMP file
The following capabilities and libraries are required:
CAPABILITY SwEvent
STATICLIBRARY libcrt0.lib LIBRARY euser.lib LIBRARY runtime.lib LIBRARY libc.lib LIBRARY libm.lib LIBRARY libpthread.lib
Note: The snippet itself doesn't require any special capabilities, but due to the use of RGA API ngi framework, the resulting application requires SwEvent capability. (See also Open C/C++ DLL.)
Header file
#include "page.h" //examplefw #include <timing.h> // ngi using namespace ngi; using examplefw::CPage; class CIntroPage : public CPage, public ITimerObserver { public: /** * Default constructor. */ CIntroPage(); /** * Initializes intro page * * @return TRUE if the initialization was successful, * otherwise FALSE. */ bool32 Initialize( ); /** * Draws the page * * This method draws the current page using the given graphics * context. * @param aGraphicsContext the graphics context to be used for drawing */ virtual void Draw( IGraphicsContext& aGraphicsContext ); /** * Draw fade effect * * This method will create fade in and out effect * * @param aGraphicsContext the graphics context to be used for drawing */ void DrawFadeEffect( IGraphicsContext& aGraphicsContext ) /** * Method which is called upon page change. * * @param aPrevPage Pointer to the previously displayed page */ void OnShow( CPage* aPrevPage ); private: /** * Method called by ITimerObserver. * This method will be called when a timer exceeds. * @param aTimerID ID of the timer */ virtual void HandleTimer( uint32 aTimerID ) NO_THROW; /** * Virtual destructor. Cannot be called directly from outside this * class. Use Release() instead. */ virtual ~CIntroPage(); /// bitmaps pointer to intro image IBitmap* mBitmapIntro; /// Pointer to the one shot timer IOneShotTimer* mIntroTimer; ///Timer interval int32 mIntroInterval; /// variable for fading of image IBitmap* mAlphaBuffer; IGraphicsContext* mAlphaBufferContext; float64 mIntroTimePassed; }
Source file
#include <errorcodes.h> // ngi #include <graphics.h> // ngi #include <graphicscontext.h> // ngi #include <ngibitmap.h> // ngi #include <runtime.h> // ngi #include <standardtypes.h> // ngi #include <imageconverter.h> // ngi #include "helper.h" //examplefw ///intro page times const uint32 INTRO_TIMEOUT = 100000; const uint32 INTRO_PERIOD = 5000000; const uint32 FADEIN_ENDTIME = 2000000; const uint32 FADEOUT_STARTTIME = 3000000; /// Private image path const ngi::char16* const PRIVATE_IMAGE_PATH = L"\\private\\a0000001\\"; const ngi::char16* const BITMAP_INTRO_IMAGE = L"some_image_name.png"; CIntroPage::CIntroPage() : CPage( NULL, LABEL_BUTTON_BACK, LABEL_BUTTON_BACK, NULL ), mBitmapIntro( NULL ), mIntroTimer(NULL), mIntroInterval(INTRO_TIMEOUT), mAlphaBuffer(NULL), mAlphaBufferContext(NULL), mIntroTimePassed(0) { } bool32 CIntroPage::Initialize( ) { //Loading image IImageConverter* imageConverter = NULL; ReturnCode ret; ret = CRuntime::CreateInstance( imageConverter ); //loading image if( ret == OK) { efLoadImage( *imageConverter, PRIVATE_IMAGE_PATH, BITMAP_INTRO_IMAGE, mBitmapIntro); imageConverter->Release(); } // preparing for image fading CSize bbsize = CAppSingleton::GetApp().GetBackBufferSize(); ret = CRuntime::CreateInstance( mAlphaBuffer ); if (ret != OK) { return false; } ret = mAlphaBuffer->Reconfigure( bbsize, GRAPHICS_FORMAT_GRAY256); if (ret != OK) { return false; } ret = CRuntime::CreateInstance( mAlphaBufferContext ); if (ret != OK) { return false; } mAlphaBufferContext->SetGraphicsDevice(*mAlphaBuffer); //initializing timer if( !efCheck( CRuntime::CreateInstance( mIntroTimer ) )) { return false; } mIntroTimer->SetObserver( this ); } CIntroPage::~CIntroPage() { if( mBitmapIntro ) { mBitmapIntro->Release(); } if( mIntroTimer ) { mIntroTimer->Stop(); mIntroTimer->SetObserver( NULL ); mIntroTimer->Release(); } if (mAlphaBufferContext) { mAlphaBufferContext->Release(); mAlphaBufferContext = NULL; } if (mAlphaBuffer) { mAlphaBuffer->Release(); mAlphaBuffer = NULL; } } void CIntroPage::Draw( IGraphicsContext& aGraphicsContext ) { // draws the page content CSize resolution = aGraphicsContext.GetGraphicsDevice()->GetSize(); CRect clearRegion = CRect( 0, 0, resolution.mX, resolution.mY ); aGraphicsContext.Clear(0); DrawFadeEffect(aGraphicsContext); } void CIntroPage::DrawFadeEffect( IGraphicsContext& aGraphicsContext ) { // update the fade buffer alpha channel uint8 alpha = 255; if ((mIntroTimePassed >= FADEIN_STARTTIME) && (mIntroTimePassed < FADEIN_ENDTIME)) { // fading in alpha calculation alpha = (uint8)(float64((mIntroTimePassed - FADEIN_STARTTIME)/ (FADEIN_ENDTIME - FADEIN_STARTTIME)) * 255.0); } else if ((mIntroTimePassed >= FADEIN_ENDTIME) && (mIntroTimePassed <= FADEOUT_STARTTIME)) { // showing image without fading alpha = 255; } else if (mIntroTimePassed > FADEOUT_STARTTIME) { // fading out alpha calculation alpha = (uint8)(float64((FADEOUT_ENDTIME - mIntroTimePassed)/ (FADEOUT_ENDTIME - FADEOUT_STARTTIME)) * 255.0); } ReturnCode ret; ret = mAlphaBuffer->Lock(); // fading image if ( ret == OK ) { mAlphaBufferContext->Clear(alpha); //mBitmapIntro should be loaded in at Initialization step ret = mBitmapIntro->Lock(); ret = aGraphicsContext.BitBlitAlphaMask(CPoint(0, 0), *mBitmapIntro, *mAlphaBuffer); ret = mBitmapIntro->Unlock(); ret = mAlphaBuffer->Unlock(); } } void CIntroPage::OnShow( CPage* /*aPrevPage*/ ) { //Starting timer on page display mIntroTimer->SetPeriod(mIntroInterval); mIntroTimer->Start(); } void CIntroPage::HandleTimer( uint32 aTimerID ) NO_THROW { if( aTimerID == mIntroTimer->GetTimerID() ) { mIntroTimePassed+= INTRO_TIMEOUT; if (mIntroTimePassed < INTRO_PERIOD) { //Calling redraw of page Invalidate(); // starting new timer period mIntroTimer->SetPeriod(mIntroInterval); mIntroTimer->Start(); } else { // stopping fading and switching to other page // ... } } }
Postconditions
This class loads the image, initializes the original image and bitmap for applying alpha channel on original bitmap, and initializes the timer. All this is done in the Initialize() function. The timer will be started in the OnShow() function, which is called when the page is actually shown. The HandleTimer() function will be called after INTRO_TIMEOUT. It will handle redrawing the page and start the next timer period if the time passed is less than INTRO_PERIOD. The actual drawing of the image is done in the DrawFadeEffect() function. Alpha channel influences the opacity of image by creating a fading in effect from FADEIN_STARTTIME to FADEIN_ENDTIME (0 to 2 seconds). Then just show the original image from FADEIN_ENDTIME to FADEOUT_STARTTIME(2 to 3 seconds). And then fade out the original image from FADEOUT_STARTTIME to INTRO_PERIOD(3 to 5 seconds).
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 6230 vs 6610 at sound vol. | azizeren | General Discussion | 1 | 2004-08-01 04:08 |
| Paragraph Fill Color | rathodavinash | General Symbian C++ | 2 | 2005-10-06 07:22 |
| 6600 MMAPI midi sound glitches? | marcpalmer | Mobile Java General | 1 | 2005-10-08 10:26 |
| ListBox localization | AbuElElla | General Symbian C++ | 9 | 2007-06-20 21:56 |
| CEikRichTextEditor, background colour and 3rd Edition | mark_williams | Symbian User Interface | 5 | 2007-01-29 15:33 |

