简体   繁体   中英

Allegro 5 - creating a custom bitmap with alpha channel

Afternoon everyone,

I was wondering if there's any way I could create a custom bitmap with alpha channel

bitmap = al_create_bitmap(30, 30);
al_set_target_bitmap(bitmap);
al_clear_to_color(al_map_rgb(255,255,255));
....
al_draw_tinted_bitmap(bitmap, al_map_rgba(0, 0, 0, 0.5),  X,  Y, 0);

I'm sure that I'm either not creating or drawing the bitmap correctly, so I could really use some advice.

Thanks in advance, Alex

The only thing wrong with your code snippet is:

al_map_rgba(0, 0, 0, 0.5)

should be:

al_map_rgba_f(0, 0, 0, 0.5)

The former range is an integer from 0 to 255.

Also, keep in mind that Allegro's default blender is pre-multiplied alpha . So if you wanted to tint red at 50%, you'd use:

float a = 0.5;
... al_map_rgba_f(1.0 * a, 0.0 * a, 0.0 * a, a) ...

If you're not thinking about it, you're probably assuming it's interpolating. ie, the more intuitive blender for most people seems to be:

al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA)

but that is not the default for the reasons mentioned in the above link.

after I set the

al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);

it allowed me to draw my "bouncer" bitmap and change its alpha channel using the below function:

al_draw_tinted_bitmap(bouncer, al_map_rgba_f(1, 1, 1, alpha) 40, 0, 0);

This previously did not work , so I guess adding the al_set_blender solved the "mistery".

Thanks for all your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM