Clear bitmap to transparent

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
Edgar Reynaldo

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.

Chris Katko

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.


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.
I have a buffer that I used for cut/paste operations. I will copy a bitmap with transparencies to this buffer and then copy from this buffer to somewhere else later. However, where on the buffer things are cut/paste are random so I need to clear the buffer back to transparent so it can accept another paste.

The logic goes like this
clear buffer to transparent -> paste graphic to hold in the buffer -> copy from buffer to target bitmap.

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.

Got it!

Took me a bit, but it was simpler then I thought.

Edgar Reynaldo

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.


The al_map_rbga() clear worked. Thanks!

Thread #617270. Printed from