Function to make blackness transparent

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)
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 }

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?


Kitty Cat

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.


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;
blit(my_pic, screen, 0,0,0,0,my_pic->w,my_pic->h);

But it doesent work. Am I doing something wrong?



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.

Kitty Cat
I said:

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

Thread #591502. Printed from