Hi everybody,
I have played around with pocketfrog a bit and produced some code that does full alpha-masked blitting. I've seen some discussion on the forum about this feature, so I thought I might share my code, which borrows heavily from the pocketfrog code.
It is user-land code, as I decided not to mess with the core framework, but the performance overhead should be minimal. Example code:
Defining the mask in the Game-derived main class:
Loading the mask from an 8-bit grayscale RAW resource. If you are using photoshop, convert the image to grayscale and save as ".raw". As the raw format doeas not contain any information about the size of the image, in the code the size of the mask is derived from the actual bmp image it is masking (bmp image is called m_gui in the code and must be loaded before the mask).
Blitting the masked image:
x and y are the offset in the destination surface, srcRect is the rectangle that should be blitted from the source surface.
I have played around with pocketfrog a bit and produced some code that does full alpha-masked blitting. I've seen some discussion on the forum about this feature, so I thought I might share my code, which borrows heavily from the pocketfrog code.
It is user-land code, as I decided not to mess with the core framework, but the performance overhead should be minimal. Example code:
Defining the mask in the Game-derived main class:
- Code:
private:
uint8_t * mask;
Loading the mask from an 8-bit grayscale RAW resource. If you are using photoshop, convert the image to grayscale and save as ".raw". As the raw format doeas not contain any information about the size of the image, in the code the size of the mask is derived from the actual bmp image it is masking (bmp image is called m_gui in the code and must be loaded before the mask).
- Code:
// set up alpha mask
mask = (uint8_t*)malloc(m_gui->GetHeight()*m_gui->GetWidth()*sizeof(uint8_t));
// Load mask
HRSRC hResource = ::FindResource( _Module.GetModuleInstance(), MAKEINTRESOURCE(IDR_MASK1), _T("mask") );
if (!hResource) return false;
DWORD size = ::SizeofResource( _Module.GetModuleInstance(), hResource );
HGLOBAL hMemory = ::LoadResource( _Module.GetModuleInstance(), hResource );
uint8_t* data = (uint8_t*)::LockResource( hMemory );
Blitting the masked image:
- Code:
void MyGame::BlitAlphaMask( int x, int y, Surface * source, Surface * dest, const Rect* srcRect, uint8_t * alphaMask)
{
DisplayDevice* display = GetDisplay();
Surface::LockInfo bufferlock;
Surface::LockInfo sourcelock;
dest->Lock( bufferlock, false, srcRect);
source->Lock( sourcelock, false, srcRect);
Pixel* pSrc = sourcelock.pixels;
Pixel* pDest = bufferlock.pixels + y * dest->GetWidth() + x;
int width = srcRect->GetWidth();
uint8_t * tmask = alphaMask;
for ( int h = srcRect->GetHeight(); h; --h )
{
pSrc = (Pixel*)((uint8_t*)pSrc + (source->GetWidth() - srcRect->GetWidth()));
pDest = (Pixel*)((uint8_t*)pDest + (dest->GetWidth() - srcRect->GetWidth()));
alphaMask = ((uint8_t*)pDest + (source->GetWidth() - srcRect->GetWidth()));
for (int w = srcRect->GetWidth(); w; --w)
{
Pixel RB1 = *pDest & (RED_MASK | BLUE_MASK);
Pixel G1 = *pDest & (GREEN_MASK );
Pixel RB2 = *pSrc & (RED_MASK | BLUE_MASK);
Pixel G2 = *pSrc & (GREEN_MASK );
Pixel RB = RB1 + (((RB2-RB1) * (*tmask>>3)) >> 5);
Pixel G = G1 + (((G2-G1)*(*tmask>>2))>>6);
RB &= (RED_MASK | BLUE_MASK);
G &= (GREEN_MASK);
*pDest++ = RB | G;
pSrc++;
tmask++;
}
}
}
x and y are the offset in the destination surface, srcRect is the rectangle that should be blitted from the source surface.