Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Fading effect with RGA

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.


Article Metadata
Tested with
Devices(s): Nokia N93, Nokia N95
Compatibility
Platform(s): S60 3rd Edition
Article
Created: stolik (24 Sep 2008)
Last edited: lpvalente (18 Sep 2012)

Contents

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).

This page was last modified on 18 September 2012, at 18:08.
47 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×