PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

alpha blending - image to image

In PocketFrog, I know how to use alpha blending to fade an image to black. But, I can't figure how to fade from one image to another. Does anyone know how to do this?

For example: if I had 2 pictures, a house and a dog. I want to display the picture of the house and have it slowly fade into the picture of the dog.

Thanks! :)
To do fade from dog to house on a black background, Start with Alpha == 255 and decrease to Alpha == 0:
If you want to see some fast results it's better to make it with SetPixel first, and then, if you are satisfied, write your own blitter.

Pixel = (Alpha/255) * dogPixel + (( 255 - Alpha )/255) * housePixel

This would work on overlapping areas with any background(where dog and house overlap). On non-overlapping areas this would work as images blended on a black background.

You can look in the code from rasterizer.cpp from Jason's AlphaMods.zip(somewhere on this forum) to know how to make fast alpha-blitter.

To make it blit on any background you should check is there any dog or house Pixel on some point. If there is no house pixel for example, just do the standart blit:

Pixel = (Alpha/255) * dogPixel + (( 255 - Alpha )/255) * BackgroundPixel
But how do I show both images? Do I need to display each image on alternating frames and then adjust alpha to transition between both?

Example: House-frame#1, Dog-frame#2, House-frame#3, Dog-frame#4, etc, etc.

When I fade an image to black, I just draw the image and then adjust alpha. But, that is only with 1 image. What do I need to do for 2 images?
I think you're making things more complicated than they are...

Draw house with alpha = 255
adjust alpha to fade amount and draw dog.

Basically the same as you were doing with the fade to black except you have an image already there instead of black.

Of course, this is assuming you don't had a color mask set.
Okay, I understand. I guess I was making this more complicated that it really was. :D

So, I guess what I'm really doing is drawing picture #1 on TOP of picture #2 and then adjusting alpha of pic#1 to allow pic#2 to show thru. Makes sense to me!

Thanks for your help...
I think this method wouldn't work as fading from one To another.
This works only as alpha-blittig one image on another.
That couldn't be called as fading from one to another because the house is always 100% alpha, which is wrong.

But if you want to make non-overlapping parts of BOTH images non transparent- do the following:

Draw the dog 100% alpha
Draw the house on the dog 100% Alpha
Draw the dog again with desired Alpha

This method would make the dog and the house have the same blitting rights
:wink:
The overlapping parts would overlap with some alpha, the non-overlapping parts would be 100% visible.

But this also isn't the fading from one to another :lol:
I explained it in my previous post.

Another simple method that 'could' be called as fading from one to another is this:

FOR every Loop of your game(Alpha = 255; Alpha >0; Alpha++)
{
Set Dog alpha to Alpha
Set House alpha to 255-Alpha
Blit dog, blit house
}
BIGBEN, you are absolutely right! It isn't correct fading if I keep the alpha at 100% for one of the images. They BOTH need to transition at the same time.

I don't have enough knowledge to write my own blitter, but I do like your example code in your last message that lowers the alpha of pic#1 while increasing alpha of pic #2. I think that is close enough to the effect that I'm trying to achieve.

Thanks for all your help! :D
My last method would have some error in overlapping area - because you actually mix three images - dog&house;&background.; Non overlapping areas would be correct.

Note: The 'higher rights' would have the image that you blit last, it's color would affect more than color of previous image.

If you make your custom blitter-it would only correct the overlapping area with mixing dog&house; only.
But any of the above methods would work in "some manner", so it's up to you to choose :wink:
To lower CPU consuming, you may do it like I did(I believe i did, though i'm not 100% sure about it, it was a year ago):

Draw Pic1 100%
while (true)
{
Draw Pic2 5% (or another)
}

So be it - 5% first, 10% second, and so on, and so on.
Simple.
Though implementation is somewhat more complicated, you'll probably need to render to surface like I did.