Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Function to make blackness transparent

This thread is locked; no one can reply to it. rss feed Print
Function to make blackness transparent
Vanneto
Member #8,643
May 2007

I have got this function to make black color around a bitmap black. Is it good? Doesent work for me.

1void set_trans_bitmap(BITMAP *bitmap)
2{
3 int i = 0;
4 for (int t = 0; t < bitmap->h; t++)
5 {
6 for(i; i < bitmap->w; i+=4)
7 {
8 int pixel = getpixel(bitmap, i, t);
9 if(pixel == makecol(0,0,0))
10 {
11 putpixel(bitmap, i, t, makeacol(0,0,0,0));
12 }
13 }
14 }
15}

Im running the game in 32bit mode. Is there a better way at doing this? I guess all the bitmap images must be 32 bit too?

Thanks!

In capitalist America bank robs you.

Kitty Cat
Member #2,815
October 2002
avatar

Instead of checking if the pixel matches makecol (which it may not if the image has alpha data), check r, g, and b explicitly. Also, unless you're using the alpha blender, you need to set the pixel to the mask color and use draw_sprite or masked_blit.

int pixel = getpixel(bitmap, i, t);
if(getr(pixel) === 0 && getg(pixel) === 0 && getb(pixel) === 0))
    putpixel(bitmap, i, t, bitmap_mask_color(bitmap));

If you know for sure you'll have a 32-bit bitmap there, you can use _putpixel32 to speed it up some. Also, you're not resetting 'i' for every row, and you're skipping three pixels for every one you do.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Vanneto
Member #8,643
May 2007

Ok I edited my loop so its only +1 not +4. I used this function like so:

BITMAP *my_pic = NULL;
my_pic = (BITMAP*)data[sprite_x].dat;
set_trans_bitmap(my_pic);
blit(my_pic, screen, 0,0,0,0,my_pic->w,my_pic->h);

But it doesent work. Am I doing something wrong?

Thanks!

In capitalist America bank robs you.

spellcaster
Member #1,493
September 2001
avatar

did you set the color depth before loading the dat file?
Have you set the alpha blender somewhere in your code?
If not, try changing your code like Kitty Cat suggested. This should do the trick as long as you want to make the black completely transparent and you don't need any partial transparency.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Kitty Cat
Member #2,815
October 2002
avatar

I said:

set the pixel to the mask color and use draw_sprite or masked_blit

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Go to: