This code has been declared by its author to be Public Domain code.
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 |
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.
|
|
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
|
|
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.
|
|
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.
|
|
Sadly, it seems these Alpha routines are becoming more and more useful, given the current (May 2010) bugs with Sprites in Win7 + NVIDIA GFX cards.
It kinda kicks "lets do 2D in 3D using sprites in B3D" into touch. No? :O/
|
|
@Cold Storage Yeah, new technology just ruins everything for everyone :'(
[ontopic] If you needed real speed from a function like this, you should use RTLMoveMemory manipulation rather than pixel by pixel reads and writes. I would modify this one to do that but I can't be bothered at the moment. [/ontopic]
|
Code Archives Forum