Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Transparent pixel

Credits go to Audric and RPG Hacker for helping out!
This thread is locked; no one can reply to it. rss feed Print
Transparent pixel
blakat
Member #15,991
June 2015

My question regards going about the transparent pixel of my sprite, I use 24 bit .tga's with no alpha channel(obviously) and load them using al_load_bitmap(*char);
I recall reading that there is a specific color (a shade of pink i believe) that allegros renderer omits from being rendered to create transparency, I wondered if there was any way i could tell the engine what color to omit from each sprite either individually or globally and if not then which rgb value is by default omitted to create transparency?

RPG Hacker
Member #12,492
January 2011
avatar

I'm asuming you're using Allegro 5. In case you're actually using Allegro 4, "magic pink" (R: 255, G: 0, B: 255) is the color you want for transparency. If you're using Allegro 5, though, this is the function you want:

void al_convert_mask_to_alpha(ALLEGRO_BITMAP *bitmap, ALLEGRO_COLOR mask_color);

It converts all pixels on an image with a certain color to transparent.

blakat
Member #15,991
June 2015

I am using Allegro 5
I used al_convert_mask_to_alpha to no avail, i passed it a the pointer to my ALLEGRO_BITMAP and my ALLEGRO_COLOR with the values 0, 255, 114 and it failed to create transparency on my 24 bit tga(the green color pixals still rendered, would this function only work if my tga had a alpha channel? If so is there another way passed this? I tried placing it both inside and outside the loop i swap buffers by the way.
???

RPG Hacker
Member #12,492
January 2011
avatar

blakat said:

would this function only work if my tga had a alpha channel

Maybe Allegro automatically picks a bitmap format without Alpha when loading an image that doesn't have an alpha channel. You can override the bitmap format, though. Just call this function before loading your image:

void al_set_new_bitmap_format(int format);

Just pass it any format that supports alpha like ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA, ALLEGRO_PIXEL_FORMAT_ARGB_8888 etc.

I recommend first calling

int al_get_new_bitmap_format(void);

to backup the currently active bitmap format and then restore this format after loading your bitmap (so that all bitmaps loader afterwards will be created with the originally set bitmap format).

Polybios
Member #12,293
October 2010

Well... Are you sure you got the color right (maybe try full black / white)? Are you sure you use the function on the very bitmap you draw later?

blakat
Member #15,991
June 2015

I got it to work, I simply had the wrong rgb value. My final question is how come when pass a ALLEGRO_COLOR variable directly into the second arguement of al_convert_mask_to_alpha() it doesnt seem to have effect? It only seems to work when I write al_convert_mask_to_alpha(*, al_map_rgb(short, short, short));

RPG Hacker
Member #12,492
January 2011
avatar

ALLEGRO_COLOR uses float values for color components (0.0f to 1.0f). Could that have been your mistake?

blakat
Member #15,991
June 2015

I tried modifying my operation with the info you shared
transparentPixal = al_map_rgb(0.0f, 255.0f, 114.0f);
It did not help
al_convert_mask_to_alpha(bmp, transparentPixal);
It compiles fine, but no transparency, this line however works
al_convert_mask_to_alpha(bmp, al_map_rgb(0.0f, 255.0f, 114.0f));
But I would like to pass a variable as a argument instead, I guess if all else fails I can use a array to store objects rgb values to pass as arguement to al_map_rgb(), but before i succumb to that any suggestions?

Audric
Member #907
January 2001

It should work...
caller:
YourFunction(al_map_rgb(0, 255, 114));
or

ALLEGRO_COLOR col = al_map_rgb(0, 255, 114);
YourFunction(col);

Definition:

void Yourfunction(ALLEGRO_COLOR transparent_color)
{
...
al_convert_mask_to_alpha(bmp, transparent_color);
}

RPG Hacker
Member #12,492
January 2011
avatar

blakat said:

transparentPixal = al_map_rgb(0.0f, 255.0f, 114.0f);

You don't need to use floats, unless you use al_map_rgb_f() (in which case you use values from 0.0f to 1.0f). If you use al_map_rgb(), integers will do (ranging from 0 to 255).

Aside from that, nothing wrong with your code. It should work the way you posted it here. Is that the exact code from your game? Can you copy & paste the snippet directly from your game?

blakat
Member #15,991
June 2015

Line 56 and 57 are the ones of focus, transparentPixal is declared on line 20. This is just a test program, once test succeed i paste it's snippets into my game.

RPG Hacker
Member #12,492
January 2011
avatar

blakat said:

I'm stumped

As I suggested:

Can you copy & paste the snippet directly from your game?

blakat
Member #15,991
June 2015

Sorry i posted that without refreshing the page, I only saw Audric's message. That edited post has a file attachment.

RPG Hacker
Member #12,492
January 2011
avatar

I'm seeing nothing that strikes me as obviously wrong with your code. Are you sure that passing transparentPixal to the function doesn't work and passing al_map_rgb(0, 255, 114) to the function directly DOES work? That is the only thing that is puzzling me. I'm not currently at my home computer, so I can't test the code myself. If you look at transparentPixal in your debugger after asigning the value to it, what does it say? And in case you have access to the Allegro source while debugging, if you step into the call

al_convert_mask_to_alpha(bmp, al_map_rgb(0, 255, 114));

What does the color argument say when looking at it in the debugger?

Also, what OS are you on, what version of Allegro are you using and which compiler did you compile this with?

blakat
Member #15,991
June 2015

Edit: Here are the results of both calls.
I actually dont have a debugger, I'm on ubuntu 14.04 (64 bit) allegro 5 and g++ as my compiler.
I've never got into using debuggers, any way you could direct me to one?

RPG Hacker
Member #12,492
January 2011
avatar

I don't have too much experience with Linux systems unfortunately, so maybe someone else can provide better help. In any case, two IDEs for Linux I know of are Eclipse and Code::Blocks. Just pick whichever IDE you like best. I haven't worked much with either of those, so I can't give any specific instructions on how to do it, but basically you'll want to find out how to set breakpoints and view the value of a variable. In many IDEs, both things are pretty straight-forward. For example, in Visual Studio you just click a line number to set a breakpoint and hover the mouse over a variable to view its value. I can imagine it's similar with the two aforementioned IDEs, but you'll have to find that out for yourself.

Quote:

allegro 5

Yeah, I figured as much. But which specific version of Allegro 5? As in, Allegro 5.X.X?

Audric
Member #907
January 2001

int main(int argc, char **argv)
{
  (...)
  ALLEGRO_COLOR transparentPixal = al_map_rgb(0, 255, 114);

This calls al_map_rgb() before allegro and its graphic mode get initialized.
I wouldn't be surprized if the pixel format (RGB? BGR? etc) was modified in-between, which would result in a different color number.
If this is the issue, you can easily fix it by calling transparentPixal = al_map_rgb(0, 255, 114); after the block where you perform al_create_display().

RPG Hacker
Member #12,492
January 2011
avatar

Oh, well spotted! I totally overlooked that! al_map_rgb(a) seems to convert colors by taking its values from an array called "_al_u8_to_float" (using the integer color component value as an index). I can imagine that said array isn't initialized until either Allegro is installed or the graphics system is.

blakat
Member #15,991
June 2015

That was a complete duh moment, thanks guys for all the help, it works as I intend it to.

Polybios
Member #12,293
October 2010

I can imagine that said array isn't initialized until either Allegro is installed or the graphics system is.

Yes, I can confirm that. The float variants will work without initialization, but converting from integers won't work without calling al_init().

Bruce Pascoe
Member #15,931
April 2015
avatar

@Polybios
That bit me too, with my recent addition of constant color blending to Allegro. I was initializing the blend color internally using al_map_rgba(), and it took me a while to realize what was wrong when the color wasn't set properly.

The reason, incidentally, is that Allegro uses a lookup table for the integer variants instead of calculating it directly (which would require 3-4 expensive FP divisions), and the tables aren't initialized until you call al_init().

Go to: