Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Another al_load_bitmap NOT working out...

This thread is locked; no one can reply to it. rss feed Print
Another al_load_bitmap NOT working out...
blackbeard
Member #14,431
July 2012
avatar

Hello, I've been over the passed couple of days setting up Visual Studio with Allegro 5.0.7 and have been learning about the Allegro library. I am a seasoned in c++ but don't usually create gaming codes with it; however, on a colleagues advice I've taken the plunge! While running through some tutorials I've encountered some problems...

THE ISSUE: I am having trouble loading bitmaps/resources in the MSVS 2010 IDE using Allegro 5 without using absolute paths to the directory in which they are stored.
So I did some research and found some code snippets within these forums and others that resolve these issues. These have not worked for me, unfortunately. I will post the code below. Any advice will be well received. Thanks!

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include <allegro5/allegro_native_dialog.h> 4 5int main(){ 6 7 al_init(); 8 al_init_image_addon(); 9 10 ALLEGRO_DISPLAY *display = NULL; 11 ALLEGRO_BITMAP *image = NULL; 12 13 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 14 chdir(al_path_cstr(path, '/')); 15 al_destroy_path(path); 16 17 image = al_load_bitmap("pandaHill.bmp"); 18 19 //image = al_load_bitmap("C:/bitmaps/pandaHill.bmp"); 20 21 if(!al_init()) { 22 al_show_native_message_box(display, "Error", "Error", "Failed to initialize allegro!", 23 NULL, ALLEGRO_MESSAGEBOX_ERROR); 24 return 0; 25 } 26 27 if(!al_init_image_addon()) { 28 al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", 29 NULL, ALLEGRO_MESSAGEBOX_ERROR); 30 return 0; 31 } 32 33 display = al_create_display(800,600); 34 35 if(!display) { 36 al_show_native_message_box(display, "Error", "Error", "Failed to initialize display!", 37 NULL, ALLEGRO_MESSAGEBOX_ERROR); 38 return 0; 39 } 40 41 42 if(!image) { 43 al_show_native_message_box(display, "Error", "Error", "Failed to load image!", 44 NULL, ALLEGRO_MESSAGEBOX_ERROR); 45 al_destroy_display(display); 46 return 0; 47 } 48 49 al_draw_bitmap(image, 0, 0, 0); 50 51 al_flip_display(); 52 al_rest(5); 53 54 al_destroy_display(display); 55 al_destroy_bitmap(image); 56 57 return 0; 58}

I have also tried using these to no avail as well... I gotta be doing something simply wrong:-/

ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_append_path_component(path, "resources");
al_change_directory(al_path_cstr(path, '/'));  
al_destroy_path(path);
 
image = al_load_bitmap("pandaHill.bmp");

AND

ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_append_path_component(path, "resources");
 
al_set_path_filename(path, "pandaHill.bmp");
image = al_load_bitmap(al_path_cstr(path, '/'));
 
al_destroy_path(path);

Matthew Leverton
Supreme Loser
January 1999
avatar

Is your executable really in "C:/bitmaps/"?

weapon_S
Member #7,859
October 2006
avatar

One thing that strikes me is calling al_init and al_init_image_addon twice. (Shouldn't be a problem...) Also, calling al_create_display can better be done before loading.(Might be a problem?)
The code posted should work. The only thing I can think of is there might be space in the real filename?

torhu
Member #2,727
September 2002
avatar

If what the others have said doesn't help, you could run Process Monitor and see exactly what path and file your game is trying to load. It's a very useful debugging tool.

Luiji99
Member #12,254
September 2010

If I recall correctly, it is VERY important that you create the display first, otherwise the bitmaps will be created in memory instead of in the display, and blitting operations will be MUCH slower.

Programming should be fun. That's why I hate Java.

Cassio Renan
Member #14,189
April 2012
avatar

try:

char thepath[200]

sprintf(thepath,"%spandaHill.bmp",al_path_cstr(al_get_standard_path(ALLEGRO_RESOURCES_PATH), '/'));

al_load_bitmap(thepath);

If it doesn't work, try printing the directory variable and see where it is(by using fprintf(stderr, directory) or something similar). Move your image to that folder and it should work fine, even without adding all this stuff.

EDIT: Thanks, Thomas :)
EDIT2: Just checked that this code shouldn't work, due to the return value of al_get_standart_path() not being a char*. Forget about it :P
EDIT3(I'm done editing): now it should work.

Thomas Fjellstrom
Member #476
June 2000
avatar

current directory won't work when you run the application via windows explorer or an IDE. They all run with a current directory that is not your application's folder.

The ALLEGRO_RESOURCES_PATH path should get you the proper directory.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Matthew Leverton
Supreme Loser
January 1999
avatar

Given the commented code "C:/bitmaps/pandaHill.bmp", the duplicate initialize calls, blindly trying to use the "resources" directory, I would guess he is copy-pasting code without even attempting to understand what is going on.

All he probably needs to do is copy his bitmap into the same folder as the executable, and use his original code after fixing the obvious problem of calling functions in the wrong order and multiple times.

Go to: