How to create transparent bitmap in Allegro5?
I have try something like this:
And it doesn't work correct :/
You can just draw any bitmap transparently like this:
al_draw_tinted_bitmap(bitmap, al_map_rgba_f(a, a, a, a), x, y, 0);
Where a is a value from 0 (transparent) to 1 (solid).
Thank you for your reply.
In fact, this works, but this is not what i mean...
I want to get this effect ( in comment, in real this not working :/ )
That should work. Are you sure it isn't working? (Grey slightly tinted white doesn't really stand out
) You could try higher alpha values to be sure.
Else you could explicitly set the blending mode. Or you could change the pre-multiplied option of your bitmaps. Neither of that should be necessary; I can see no mistakes, and what you have there should work.
No, it is not working, but i found solution:
// And this must be almost transparent float alpha = 0.1f; al_draw_filled_rectangle( 2, 2, 150, 20, al_map_rgba( 255*alpha, 255*alpha, 255*alpha, alpha ) );
http://www.allegro.cc/forums/thread/606149
It's strange...
Why do you have to alter your RGB to get the alpha/transparency channel working? seems daft to me. I'd have thought white with a alpha of 0.5 would be semi-transparent white?
Ah. I take my words back.
I expected the primitive drawing operations to actually respect the pre-multiplied-alpha flag. Have you read the linked topic in your link?
Pre-multiplied color blending is enabled by default. This mode expects the colors to be "multiplied" by the alpha value of the pixel. (Which you are doing now.)
You can get a "normal" mode by doing al_set_new_bitmap_flags(ALLEGRO_NO_PREMULTIPLIED_ALPHA) before creating bitmaps, and setting the blending mode like this: al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA).
(The main difference is the blending mode, which is al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA) when using "pre-multiplied-alpha", which is default.)
BTW I personally would set the blending mode to "replace", when creating/preparing semi-transparent bitmaps with al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO). That's because (obviously >_>') I have no idea of how the primitives treat the alpha value exactly, unless using this blending mode.
Thanks, its working now normal when I add this before creating bitmaps:
al_set_new_bitmap_flags( ALLEGRO_NO_PREMULTIPLIED_ALPHA ); al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
You should look at ex_premul_alpha before doing that though, in many cases it's a bad idea.
By keyword ex_premul_alpha ( because i don't know what is this
) i found only this post:
http://www.allegro.cc/forums/thread/608171
And i see, why this option is bad...
So, this method is better in your opinion?:
float alpha = 0.1f; al_draw_filled_rectangle( 2, 2, 150, 20, al_map_rgba( 255*alpha, 255*alpha, 255*alpha, alpha ) );