How does allegro load transparent background sprite images

Hey guy's, so I solved the problem of loading a bitmap image, thanks to everyone who had helped me but then I came across a question to myself.

Why does sprites have a magenta color background when being used for allegro?
does it have something to do with the bit values

and if so, how do I make this sprite I made in gimp and edited with paint load or show up WITHOUT the white background? I will provide an attachment for you guys to look at (the "kid.bmp" image)

in my code I set the color depth to desktop_color_depth() to support my system, but why does it show the white background which was edited in paint?

How does it work with magenta?

more info: I'm using a 32 bit machine which runs just fine, I've also provided the output of the program when running.

Eric Johnson

Why does sprites have a magenta color background when being used for allegro?

Allegro (at least Allegro 4) uses a magenta (often called "magic pink") background for transparency because it is one of the most unlikely colors to appear naturally, making it a safe color to be removed. This is somewhat similar to how green screens work; when using a green screen, the idea is that the color green is so distant from the rest of the colors present on the actor or props in front of the screen that it makes it easy to remove the green background without also accidentally removing colors from the objects in front of it.

When saving kid.bmp, did it contain a white background? If so, then that background will still be present when used with Allegro. What you need to do is change the background to that magic pink color (255, 0, 255) instead.


+Eric Johnson yeah, I did but then when I loaded it in allegro 4 it crashed with the magenta background so I had to open up paint and erase the magenta background. Do you think it has anything to do with my code? for setting the color depth I do set_color_depth(desktop_color_depth());

Eric Johnson

Interesting. I'm not well-versed with Allegro 4, so I'm not sure. If you suspect it may have something to do with the color depth, then try changing it and see if that corrects the issue.


Do you think it has anything to do with my code? for setting the color depth I do set_color_depth(desktop_color_depth());

That particular line is not going to cause your program to crash, but yes the problem is probably related to your code. You'll need to post your code if you want help with it.


@LennyLen Ok, I will provide the code. I'm know allegro 4 and I only know how to write code in C (not C++). I only think it might have been the color depth because I'm setting it to my desktops color scheme which I'm not sure if that's the proper way to generate transparent backgrounds for sprite in allegro.


Your program works fine for me with a bitmap I made to test it. The problem is your bitmap. If you'd bothered doing some error checking, this would have been obvious.

Also: learn to use a debugger. It would have told you that the code is crashing at line 27, which is after setting the bit depth.


This looks a lot like your former issue, where you accidentally saved the modified image as GIF instead of BMP (despite having a .bmp file extension)


I just noticed that the bmp was attached to the original post. I used it, and the program ran fine:


My guess is that you don't have the bmp in the working directory.


@LennyLen I don't think you understand the problem, it wasn't anything to do with loading the bitmap or it being in the same directory, its the fact that I'm trying to load a bitmap image with a transparent background rather than a white one as you can tell from the sprite, now some would say that I need to set it to magenta (the magic pink) which is what I did, and so that didn't work and it crashed when I put the magenta color in edited with GIMP 2. Then when I try edit it with paint it shows the magenta color in the background which hence why I've asked: "".

I will the attachments in this message.

1) The Output
2) The Image

Edgar Reynaldo

The 'image' you attached is a .jpg file - you can't load that without jpgalleg. Did you initialize the jpgalleg addon? You have to register .jpg files with allegro before you can load them or it will return a null pointer and then when you draw with it it will crash.

Post code.

You can also load pngs with loadpng, another Allegro 4 addon. But similarly, you have to register the png loader with allegro first.


Nope, it is a bitmap The two image were image 50 which was a screen shot of my bitmap in paint and then there was the other one which is just how the program runs with the green background which is what I put as the overall background.

Chris Katko

Wait a minute.


Open the original bitmap that works with GIMP again.

Modify anything in it like a pixel.

Save it.

Does Allegro crash?

Edgar Reynaldo

We've already told you several times now, some .bmp files are simply NOT compatible with Allegro 4. Get it? If you can save it from one program and it crashes, and you can save it from another program and it doesn't, then save it in the second program and move on with your life.


+Edgar dude, it's my own bitmap image of course it should work, and be compatible with allegro 4. I've used the right .bmp extension when edited in paint (24-bit bitmap)

+Chris Katko That does crash because allegro doesn't support the gimps bitmap, I don't even know how to change that, even if it didn't crash it would still show the white background.

draw_sprite(buffer, guy,guy_x, guy_y);

In case you didn't see the code I attached the code I wrote in the previous message.

Edgar Reynaldo

Paint the area you want transparent as hot pink (R = 255 , G = 0 , B = 255). Then save it with paint.

Then try this code :

1 2 3#include "allegro.h" 4 5 6int main(int argc , char** argv) { 7 8 9 if (allegro_init() != 0) {return 1;} 10 11 install_timer(); 12 install_keyboard(); 13 14 set_color_depth(desktop_color_depth()); 15 16 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED , 400 , 300 , 0 , 0) != 0) { 17 return 2; 18 } 19 20 BITMAP* spr = load_bitmap("magic_pink.bmp" , 0); 21 22 if (!spr) {return 3;} 23 24 clear_to_color(screen , makecol(0 , 255 , 255)); 25 26 draw_sprite(screen , spr , 0 , 0); 27 28 readkey(); 29 30 return 0; 31} 32END_OF_MAIN()

If allegro's color depth is 8 bit, you will lose the magic pink. 8-bit is default if you don't set any color depth.

Try the program in the attached zip file. If it works for you, you're still doing something wrong. Show code again.

Thread #616940. Printed from