|
Clear bitmap to transparent |
nshade
Member #4,372
February 2004
|
I need to load a PNG with a transparency and then use al_draw_bitmap_region() to draw that into another bitmap, but it seems to be putting black for the transparent color. I think it's because my bitmap is being "cleared" to black before the PNG is loaded. Is there a way to al_clear_to_color() where the color is transparent? (Allegro 5 BTW) |
Chris Katko
Member #1,881
January 2002
|
This looks applicable: https://www.allegro.cc/forums/thread/598765 Also https://stackoverflow.com/questions/16964184/allegro-5-creating-a-custom-bitmap-with-alpha-channel -----sig: |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Chris, that thread is way outdated. It's so old the function signature for al_set_blender is different. PNGs are loaded exactly as they are stored. There is no conversion of anything to anything. If your bitmap is drawing black, it's probably because your blender is wrong. The default blender is (ALLEGRO_ADD , ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA) for premultiplied alpha blending. Try saving the png with allegro. It should come out exactly the same as the png you loaded. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Chris Katko
Member #1,881
January 2002
|
I thought he meant he was drawing one image to another, but it wasn't moving the ALPHA, it was just USING the alpha. Hence the blender changes. -----sig: |
nshade
Member #4,372
February 2004
|
Here is some example code (I attached the example picture too) 1#include <allegro5/allegro.h>
2#include <stdio.h>
3
4ALLEGRO_BITMAP *buffer;
5ALLEGRO_BITMAP *white;
6ALLEGRO_BITMAP *load;
7ALLEGRO_DISPLAY *d;
8
9int main(int argc, char **argv)
10{
11 if (!al_init()) {
12 printf("Could not init Allegro.\n");
13 }
14 if (!al_init_image_addon()) {
15 printf("Could not init Allegro.\n");
16 }
17
18 d=al_create_display(1024, 768);
19
20 buffer = al_create_bitmap(1024, 768);
21 al_set_target_bitmap(buffer);
22 al_clear_to_color(al_map_rgb(0,0,0));
23 white = al_create_bitmap(1024, 768);
24 al_set_target_bitmap(white);
25 al_clear_to_color(al_map_rgb(255, 255, 255));
26
27 load = al_load_bitmap("balltest.png");
28 al_set_target_bitmap(buffer);
29 //al_clear_to_color(TRANSPARENT); <--- this is the function I'm looking for
30 al_draw_bitmap(load, 0, 0, 0);
31 al_set_target_backbuffer(d);
32 al_draw_bitmap(white, 0, 0, 0);
33 al_draw_bitmap(buffer,0,0,0);
34 al_flip_display();
35 while (1);
36}
This is just the mockup but here is the idea. The logic goes like this If I paste a graphic on the buffer when the buffer is black, I lose the transparency. So I would like to clear the buffer to transparent before I do a cut and paste. ===EDIT=== al_set_target_bitmap(buffer); al_clear_to_color(al_map_rgb(0, 0, 0)); al_convert_mask_to_alpha(buffer, al_map_rgb(0, 0, 0)); Took me a bit, but it was simpler then I thought. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
In old Allegro 4, using the DX backend, clear to color ignored the alpha channel. I don't think Allegro 5 does that though. You should be able to clear to an alpha color, using al_map_rgba. If that doesn't work, then you can draw a filled rectangle over the entire bitmap using an overwrite blender. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
nshade
Member #4,372
February 2004
|
The al_map_rbga() clear worked. Thanks! |
|