Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A5 basic load .png example code

This thread is locked; no one can reply to it. rss feed Print
A5 basic load .png example code
seanfcarney
Member #14,672
November 2012

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 ??? .

#SelectExpand
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.
Sean

Ubuntu 14.04
C::B 13.12
Allegro 5

RPG Hacker
Member #12,492
January 2011
avatar

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
Member #8,592
May 2007
avatar

Dizzy Egg
Member #10,824
March 2009
avatar

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!
}

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

seanfcarney
Member #14,672
November 2012

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.

Sean

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

USED THE FOLLOWINg:

#SelectExpand
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}

LennyLen
Member #5,313
December 2004
avatar

al_filename_exists could be useful to you.

Dizzy Egg
Member #10,824
March 2009
avatar

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.....

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

seanfcarney
Member #14,672
November 2012

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.

=======
EDIT:
=======

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.

Sean

RPG Hacker
Member #12,492
January 2011
avatar

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.

beoran
Member #12,636
March 2011

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.

Go to: