A5 basic load .png example code

I am trying to load a .png file (from hard drive) which will represent my paddle in a pong game. All methods I try result in a "segmentation fault (core dumped)" output in terminal and the allegro display crashes.

I will provide my code so you guys can see my latest incorrect way to display a bitmap to an allegro display ??? .

1void Open_window(){ 2 al_init(); 3 al_init_image_addon(); 4 5 ALLEGRO_DISPLAY *display; 6 ALLEGRO_BITMAP *paddle; 7 8 display = al_create_display(640, 480); 9 10 paddle = al_load_bitmap("/home/sean/Desktop/Pong/Paddle.png"); 11 12 al_draw_bitmap(paddle, 50.0, 50.0 , 0); 13 14 al_set_window_title(display, "PONG MEGA-SUPER-BETA"); 15 16 //al_clear_to_color(al_map_rgb(220,0,0)); 17 18 al_flip_display(); 19 20 al_rest(3.0); 21 22 al_destroy_display(display); /**closes window */ 23 24 return; 25}

Question: What is the correct sequence to simply display a .png file in an ALLEGRO_DISPLAY? I am talking bare bones, just to give a model so I can see where I am going wrong.

Thanks in advance.

Ubuntu 14.04
C::B 13.12
Allegro 5

RPG Hacker

I'm not seeing anything obviously wrong in the code posted. Did you try getting the return code of each function and/or stepping through the code/using print functions to find out where exactly the application is crashing?

Edgar Reynaldo

Yes. You need to check your return values and/or run it through a debugger. That way you can see where it is stopping.

Dizzy Egg

After you load paddle, put in a condition to see if paddle loaded ok...something like:

//load paddle first
if(paddle == NULL){
  //paddle didn't load!! show msg and exit!


I have not. //'ed out the al_draw_bitmap line and this avoided my segmentation fault.

I guess ill use debugger tonight. I'll verify the path is correct. Cannot think of much else I could have screwed up. Obviously I did though :o .

Thanks for the help.

Will let you know what I find.


My .png file is not loading properly. I now have to find out why.


1 paddle = al_load_bitmap("/sean/Desktop/Pong/Paddle.png"); 2 3 t 4if(paddle == NULL){ 5 int x; 6 cout<<"ERROR"; 7 cin>>x; 8 return; 9}


al_filename_exists could be useful to you.

Dizzy Egg

yeah it's not finding your .png because the executable doesn't run from the place you'd expect...if you want more help just say the word! But look up "get executable filename" and stuff like that.....


Hmm I am confused.

Wouldn't supplying the full path to a .png eliminate the need to have the image in some kind of local directory? I was actually trying to avoid the scenario I seemed to make for myself.

I've been sifting through the Allegro5 online manual with no further incite past I need to use al_filenamame_exists(path/paddle.png) to determine if I get the path correct.

I used cout<<ALLEGRO_EXENAME_PATH; and the returned value in the console was "6". Now I'm just not sure what I need to do with this info.

Any clues would be helpful.


You all may now open hand slap me. I found the solution to my problem. Paddle.png was an incorrect file name. paddle.png was the correct file name. I now have "/home/sea/Desktop/paddle.png" as the path.

My 20x100 pixel white paddle is not proudly displayed with a black background.

Thanks for all the help again.


RPG Hacker

Good to hear you solved your problem.
As for the problem you mentioned in your last post (even though it isn't important anymore)

I used cout<<ALLEGRO_EXENAME_PATH; and the returned value in the console was "6". Now I'm just not sure what I need to do with this info.

ALLEGRO_EXENAME_PATH is just a define, as far as I know. It is a constant that is meant to be passed to a certain function to get the ALLEGRO_PATH object containing the actual path. This object can then be used to generate a C string containing the path.


For now using an absolute path is OK, but if you want to later distribute the game, this won't work. Allegro can help you with https://www.allegro.cc/manual/5/al_get_standard_path, but probably you have to decide on some standard path like in /usr/share/pong or such where the resources (image files) of your game should be put and load them from there.

Thread #615391. Printed from Allegro.cc