×
Namespaces

Variants
Actions

How to draw transparent image without mask

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: Maxying (23 Mar 2007)
Last edited: hamishwillee (30 May 2013)


We often use key color ( RGB(255,0,255) ) to transparent image in game development. But BitBlt function not supports this feature in Symbian OS, so you must do it yourself. Look at these snippets of code:

Note! This example does not lock the heap while drawing so on devices using S60 2nd Edition it will randomly crash. On devices using S60 3rd Edition it most probably won't even run.

To fix this you must use the bitmap's LockHeap() method before using the DataAddress() method and UnlockHeap() after everything is done.

Example

void DrawBitmap( const TPoint& ptPoint, const CFbsBitmap* pBitmap, 
const TRect& rcRect, const TInt nKeyColor )
{
if( pBitmap == NULL ||
rcRect.iTl.iX > pBitmap->SizeInPixels().iWidth ||
rcRect.iTl.iY > pBitmap->SizeInPixels().iHeight ||
ptPoint.iX > m_pOffScreenBmp->SizeInPixels().iWidth ||
ptPoint.iY > m_pOffScreenBmp->SizeInPixels().iHeight ||
ptPoint.iX + rcRect.Width() < 0 ||
ptPoint.iY + rcRect.Height() < 0 )
{
return ;
}
 
TPoint iPoint( ptPoint );
TRect iRect( rcRect );
 
if( iRect.iTl.iX < 0 )
{
iRect.iTl.iX = 0;
}
if( iRect.iTl.iY < 0 )
{
iRect.iTl.iY = 0;
}
if( iPoint.iX < 0 )
{
iRect.iTl.iX -= iPoint.iX;
iPoint.iX = 0;
}
if( iPoint.iY < 0 )
{
iRect.iTl.iY -= iPoint.iY;
iPoint.iY = 0;
}
TSize sBack = m_pOffScreenBmp->SizeInPixels();
TSize sFront = pBitmap->SizeInPixels();
if( iRect.iBr.iX > sFront.iWidth )
{
iRect.iBr.iX = sFront.iWidth;
}
if( iRect.iBr.iY > sFront.iHeight )
{
iRect.iBr.iY = sFront.iHeight;
}
if( iPoint.iX + iRect.Width() > sBack.iWidth )
{
iRect.iBr.iX = sBack.iWidth + iRect.iTl.iX - iPoint.iX;
}
if( iPoint.iY + iRect.Height() > sBack.iHeight )
{
iRect.iBr.iY = sBack.iHeight + iRect.iTl.iY - iPoint.iY;
}
 
TUint16* pBack = ( TUint16* ) m_pOffScreenBmp->DataAddress();
TUint16* pFront = ( TUint16* ) pBitmap->DataAddress();
pBack += iPoint.iY *
( m_pOffScreenBmp->ScanLineLength( sBack.iWidth, EColor4K ) >> 1 )
+ iPoint.iX;
pFront += iRect.iTl.iY *
( pBitmap->ScanLineLength( sFront.iWidth, EColor4K ) >> 1 )
+ iRect.iTl.iX;
 
TInt iRectWidth = iRect.Width();
TInt iRectHeight = iRect.Height();
TInt iStepBack = ( m_pOffScreenBmp->ScanLineLength( sBack.iWidth, EColor4K ) >> 1 )
- iRectWidth;
TInt iStepFront = ( pBitmap->ScanLineLength( sFront.iWidth, EColor4K ) >> 1 )
- iRectWidth;
 
iStepBack += iRectWidth;
iStepFront += iRectWidth;
 
for( TInt i = 0; i < iRectHeight;
pBack += iStepBack, pFront += iStepFront, i++ )
for( TInt j = 0; j < iRectWidth; j++ )
if( ( pFront[j] ) != nKeyColor )
pBack[j] = pFront[j];
}
This page was last modified on 30 May 2013, at 04:33.
30 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.

×