Im searching for a good alpha routine, and this is the second one ive tested. This one has an 200-400 ms execution advantage over the last one I tested. The execution time is about 800 ms, still very unusable for a game.
Code archives/Graphics/AlphaBlit
This code has not been declared by its author to be Public Domain code, and therefore should not be assumed to be so.
Download source code
| Somewhat more flexable, maybe faster, maybe not... You'll have to try and compare... This function alpha blits any small image onto a desitantion buffer of equal or lesser size, It has no error handling to speed up the function. DestBuffer is the pointer to the desitnation image. ImgBuffer is the pointer to the image to be Alpha Blited. xOff, yOff are the ofsets of the image on the destination image. Alpha ranges from 0 to 1 and is the opacity of the image. |
Dim pixelData(500, 500) Function AlphaBitBlit(DestBuffer, Image, xOff, yOff, Alpha#) ;This function alpha blits any image smaller than 100,50 onto a desitantion ;buffer of equal or lesser size, It has no error handling to speed up the ;function. ;DestBuffer is the pointer to the desitnation image. ;ImgBuffer is the pointer to the image to be Alpha Blited. ;xOff, yOff are the ofsets of the image on the destination image. ;Alpha ranges from 0 to 1 and is the opacity of the image. InvAlpha# = 1.0 - Alpha# ImgBuffer = ImageBuffer(Image) iWidth = ImageWidth(Image) iHeight = ImageHeight(Image) LockBuffer ImgBuffer For x = 0 To iWidth-1 For y = 0 To iHeight-1 pixelData(x, y) = ReadPixelFast(x,y,ImgBuffer) Next Next UnlockBuffer ImgBuffer LockBuffer DestBuffer For x = 0 To iWidth-1 For y = 0 To iHeight-1 PixA# = ReadPixelFast(x + xOff, y + yOff) PixB# = pixelData(x, y) ;Calculate Alphas with the least number of computations aR = Int(PixA Sar 16) aRI = aR Shl 16 aG = Int((PixA - aRI) Sar 8) aB = PixA - (aRI + (aG Shl 8)) ;Calculate Alphas with the least number of computations bR = Int(PixB Sar 16) bRI = bR Shl 16 bG = Int((PixB - bRI) Sar 8) bB = PixB - (bRI + (bG Shl 8)) ;Apply Alphas If bR > 0 Or bG > 0 Or bB > 0 nR = (aR * InvAlpha) + (bR * Alpha) nG = (aG * InvAlpha) + (bG * Alpha) nB = (aB * InvAlpha) + (bB * Alpha) ;Write Pixel to Buffer WritePixelFast x + xOff, y + yOff, (nR Shl 16) + (nG Shl 8) + nB EndIf Next Next UnlockBuffer DestBuffer End Function |
Comments
check my DLL in toolbox-section
This is the only one that has worked very well for my program. It actually blends the image, whereas the others seemed to change the image's color (I think it actually inverted it on white). Thanks!
Here's an alpha-blending routine that is fairly fast and could be used real time for 64x64 or smaller sprites. I optimized the existing code to use bank memory instead of using SHR and SHL to extract RGB values from 'ReadPixelFast'. Simple demo code included.
http://blitzmax.com/codearcs/codearcs.php?code=913
http://blitzmax.com/codearcs/codearcs.php?code=913
Ryudin is this game you are making 3d or 2d?
If this is in blitz 3d I would advise you to take advantage of the 3d engine, giving you a lot more flexibility.
If this is in blitz 3d I would advise you to take advantage of the 3d engine, giving you a lot more flexibility.
Just curious about the meaning of the word "desitantion". If WebDext meant destination, then it's one heck of a typo!
wow I never noticed that :) hehe
You did it twice in your original post.