Transparency examples
BigGiantBrain

I'd like to add transparency to my A5 application's menus, and am seeking a programming example or tutorial.

Thanks.

Trent Gamblin

If you want to draw a transparent rectangle, set the color like this:

float r = red;
float g = green;
float b = blue;
float a = alpha; // 0-1, 0 = fully translucent, 1 = fully opaque
ALLEGRO_COLOR color = al_map_rgba_f(r*a, g*a, b*a, a);
// now draw primitives with 'color'

For images it's almost the same thing, except normally you don't want to tint the image so you use:

ALLEGRO_COLOR color = al_map_rgba_f(a, a, a, a);
al_draw_tinted_bitmap(bitmap, color, x, y, flags);

BigGiantBrain

I'll play with that...

Thanks for the response.

Trent Gamblin

r, g, b are also 0-1 BTW.

BigGiantBrain

I am trying ad a tint to the entire clipped airspeed window below.

The test below generates a non-specific debug error: where am I going wrong?

float r = .5;
float g = .5;
float b = .5;
float a = .5; // 0-1, 0 = fully translucent, 1 = fully opaque
ALLEGRO_COLOR transparent_color = al_map_rgba_f(a, a, a, a);
al_draw_tinted_bitmap(airspeed_tape, transparent_color, 0, 0, 0);

Trent Gamblin

"non-specific"? What is the error.

BigGiantBrain

I get a MSVC Debug Library pop up:

Debug Error!

R6010
-abort() has been called

ABORT RETRY IGNORE

Trent Gamblin

Sounds to me like the bitmap is NULL. Try printing its value before drawing it.

BigGiantBrain

I agree, however commenting out the 6 lines of transparancy test code eliminates the error.

The error is generated on the line that calles al_draw_tinted_bitmap...does the transparency funtionality require a library #include?

Please bear with me: I'm a hack with little modern programming experience.

Trent Gamblin

The only way it could crash is if airspeed_tape is invalid. Make sure it's loaded or created correctly by checking return values from al_load_bitmap and/or al_create_bitmap (or however you're loading that bitmap.)

Posting more code would help us determine the problem.

BigGiantBrain

I tested for the existence of the airspeed_tape bitmap, and it exists.

Trent Gamblin

Post more code.

BigGiantBrain

Is there a supporting #include required to support tinting?

Trent Gamblin

No.

The only way it could crash is if airspeed_tape is invalid.

This is a true statement with one extra condition -- there may be an error somewhere else in your code that could cause it too.

BigGiantBrain

Very curious.

Eliminating the tinted bitmap call allows it to run just fine...AND the bitmap exists.

I'll keep digging...thanks

Trent Gamblin

How are you determining that the bitmap "exists"?

BigGiantBrain

Two ways:

First, I comment out only the al_draw_tinted_bitmap line, and the code runs, alters the existing bitmap, and shows it on the display.

Second, I added this test:
if(!airspeed_tape){
fprintf(stderr, "Failed to initialize airspeed.\n");
return -1;
}
When run as above, the code operates normally, and proceeds until it generates the aforementioned debug error. When I changed the test to "(airspeed_tape)" it exits on the error. (eliminating the !)

I tried substituting any of the other 6 clipped bitmaps, and they all generate the debug error. There's no doubt that they exist.

Should it matter that they are all sub bitmaps?

I thought I'd read somewhere in the docs that tint support required an include.

Trent Gamblin

Ok, without seeing code I can't help anymore but

I thought I'd read somewhere in the docs that tint support required an include.

This doesn't really make sense. You'll understand why when you learn more about C (hint: feature support can't be turned on or off with an include, includes are used to tell the compiler it's ok to use a function named x with parameters y -- if you don't get a compile error, the include has nothing to do with it.)

BigGiantBrain

EDIT:

Ok, problem solved. The code posted below is working, more or less. I was not properly locating objects on the underlying plane to benefit from the transparency.

Thanks for the help.

I'll go set up a dedicated test routine to get it working, so that if I can't figure it out I can post a short block of code.

It won't do any good to post anthing now, as its a large program, and short of showing the bitmap creation lines, most is not relevant.

I'll come back when I can better use your time...thanks.

Here is a test which runs but does not tint: It prints a red square on the target bitmap.

Note: this code loops endlessly...I use a breakpoint in the debugger to halt it to view the output.

#include <allegro5/allegro.h>
#include <allegro5/allegro_color.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
#include <stdio.h>
#include <math.h>
#include "allegro5/allegro_native_dialog.h"

ALLEGRO_BITMAP *airspeed_tape = NULL;
ALLEGRO_COLOR color_255 = al_map_rgb(255, 255, 255);
ALLEGRO_DISPLAY *display = NULL;

ALLEGRO_FONT *font = NULL;

int main(int argc, char **argv)
{
al_init();
al_init_font_addon();
al_init_ttf_addon();
al_install_keyboard();
al_init_primitives_addon();
al_init_image_addon();

display = al_create_display(1280, 800);

airspeed_tape = al_create_bitmap(1280, 800);

al_draw_filled_rectangle(50,50,200,200,al_map_rgb(255,0,0));

while (1==1){

float r =.5;
float g =.5;
float b = .5;
float a = .5;

ALLEGRO_COLOR transparent_color = al_map_rgba_f(a, a, a, a);

al_set_target_backbuffer(display);

al_draw_tinted_bitmap(airspeed_tape, transparent_color, 0, 0, 0);

al_flip_display();

al_destroy_bitmap(airspeed_tape);

return 0;

}
}

Trent Gamblin

If you draw a transparent image over and over without clearing the backbuffer, it'll eventually turn solid.

Dizzy Egg

Hmmm...returning on the first loop...and this works does it? ;D

Trent Gamblin

Hi Dizzy.

Dizzy Egg

Hi Trent, how's the duck platformer coming along?

Trent Gamblin

I'm sorry that's off topic. :D

(but good)

Dizzy Egg

Off: cool, looking forward!

On: Try this in a loop; clear screen, draw images on bitmap(buffer), draw buffer(bitmap) on screen, check to see if it's time to stop, if not, clear screen, draw images on bitmap(buffer), draw buffer(bitmap) on screen, check to see if it's time to stop, if not, clear screen, draw images on bitmap(buffer), draw buffer(bitmap) on screen, check to see if it's time to stop, if not, clear screen, draw images on bitmap(buffer), draw buffer(bitmap) on screen, check to see if it's time to stop, if no....if it IS, exit loop and then return.

Thread #612802. Printed from Allegro.cc