Bitmap Assertion Failed
Eric Johnson

Hi there.

I just recently switched to Linux and am having some issues running some code that has Allegro 5 in it. Below is my code.

1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3 4int main() { 5 6 al_init(); 7 8 enum KEYS {UP, DOWN, LEFT, RIGHT}; 9 10 int screenW = 640; 11 int screenH = 480; 12 13 int playerX, playerY = 0; 14 15 bool done, draw = false; 16 17 al_set_new_display_flags(ALLEGRO_WINDOWED); 18 19 ALLEGRO_DISPLAY *display = al_create_display(screenW, screenH); 20 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 21 ALLEGRO_TIMER *timer = al_create_timer(1.0 / 60.0); 22 23 al_set_window_title(display, "Test Title"); 24 25 al_init_image_addon(); 26 al_install_keyboard(); 27 al_install_mouse(); 28 29 ALLEGRO_BITMAP *object = al_load_bitmap("./object.png"); 30 31 al_register_event_source(event_queue, al_get_display_event_source(display)); 32 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 33 al_register_event_source(event_queue, al_get_keyboard_event_source()); 34 35 al_start_timer(timer); 36 37 while (!done) { 38 39 ALLEGRO_EVENT ev; 40 41 al_wait_for_event(event_queue, &ev); 42 43 if (ev.type == ALLEGRO_EVENT_TIMER) { 44 45 // Update 46 47 draw = true; 48 } 49 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { 50 51 } 52 else if (ev.type == ALLEGRO_EVENT_KEY_UP) { 53 54 switch (ev.keyboard.keycode) { 55 56 case ALLEGRO_KEY_ESCAPE: 57 58 // Quit the game 59 done = true; 60 break; 61 } 62 } 63 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 64 65 // Quit the game 66 done = true; 67 } 68 69 if (draw && al_is_event_queue_empty(event_queue)) { 70 71 // Draw 72 73 draw = false; 74 75 al_draw_bitmap(object, 0, 0, 0); 76 77 al_flip_display(); 78 al_clear_to_color(al_map_rgb(255, 0, 0)); 79 } 80 } 81 82 al_destroy_timer(timer); 83 al_destroy_display(display); 84 al_destroy_event_queue(event_queue); 85 al_destroy_bitmap(object); 86 87 return 0; 88}

I compile the file as such: "g++ -o mygame game.cpp $(pkg-config --libs allegro-5 allegro_image-5)". It compiles without error. When I go to run my game (./mygame), it returns... "mygame: /home/pi/Desktop/allegro/src/bitmap_draw.c:137: al_draw_tinted_bitmap: Assertion `bitmap' failed."

I'm guessing it means that I am pointing to a NULL bitmap, but I'm not! :o My "object.png" resides on my desktop just as "game.cpp" does. Any ideas?


Well, check for NULL in code then you will know if it's really not NULL.

Arthur Kalliokoski

All the info he needs is already in his previous threads.

Eric Johnson

I checked if my bitmap was NULL, and if it was, I'd end the game; it ended the game. I don't understand why though, because it's right there. I feel like I'm telling an old dog to fetch a stick that's two feet from it, yet it won't. :-/

Anyway, I decided to check out some of the examples that were found in the examples folder within Allegro 5. I ran the ex_draw_bitmap, but it returned this to the terminal: "Error loading data/mysha256x256.png". I checked the corresponding folder, and it was there; it exists, yet it cannot be procured.


Arthur Kalliokoski
Sheegoth said:

My "object.png" resides on my desktop just as "game.cpp" does.

If you're using that path thing I showed you, the data (or relative path) has to be the same as the executable, not the source file.

Eric Johnson

I am slightly confused by your message.

Let's say that all of my game data is located under /home/pi/Desktop/game. Within that directory, I have a sub-directory called "data" (/home/pi/Desktop/game/data). In code, let's say that I load up a bitmap called "player"... ALLEGRO_BITMAP *player = al_load_bitmap("data/player.png");

Now let's assume that I compile my game and am left with "mygame" under home/pi/Desktop/game. I'd run ./mygame (within the directory of my game) to execute it. Are you saying that mygame (the executable) must reside within the same directory as all of my sources? So within /home/pi/Desktop/game, I would have mygame and /data?

Does this look right?


path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_change_directory(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));

ALLEGRO_BITMAP *player = al_load_bitmap("data/player.png");

Is that how I'd use the path code?

Arthur Kalliokoski

Yes, that looks right.

BTW, try this

1 2#include <stdio.h> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_native_dialog.h> 5 6int main(int argc, char **argv) 7{ 8 char buff[1024]; 9 ALLEGRO_PATH *path; 10 11 if (!al_init()) 12 { 13 fprintf(stderr,"Could not init Allegro.\n"); 14 return 1; 15 } 16 17 sprintf(buff,"Right now, this program thinks the current working directory is \"%s\"\n",al_get_current_directory()); 18 al_show_native_message_box(0, "lulz!", buff, "", NULL, 0); 19 //If user runs it from somewhere else... 20 path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 21 al_change_directory(al_path_cstr(path,ALLEGRO_NATIVE_PATH_SEP)); 22 23 sprintf(buff,"Now we've changed it to \"%s\", which is the same directory as the executable\n",al_get_current_directory()); 24 al_show_native_message_box(0, "lulz!", buff, "", NULL, 0); 25 return 0; 26}

Eric Johnson

Hi again.

I tried that, and it said that the current directory is "/home/pi/Desktop", which is right. Alas, it still won't load my images. :'(

Edgar Reynaldo
Sheegoth said:

I tried that, and it said that the current directory is "/home/pi/Desktop", which is right. Alas, it still won't load my images. :'(

That's because you're in the wrong directory! Pay attention dude! That was the whole point of Arthur's test. You're trying to load from data/player.png when the relative path from /home/pi/Desktop is /game/data/player.png.

Which directory did you run Arthur's test program from? I'm just curious is all.

Eric Johnson

I used data/player.png as an example. In reality, all of my files are on /home/pi/Desktop: game.cpp, object.png, etc.

I ran his test program from the Desktop. In the terminal, I went there (cd /home/pi/Desktop).

Edgar Reynaldo

If you use Arthur's code, the current working directory will be the directory where your executable is. If you do not, the cwd will likely be the directory you ran the program from.

I don't care where your stuff is as long as you're using the proper path. If it is in game/data/test.png then use that.

And, it makes zero difference where your source files are. What matters is where your exe is, where you run the program from, and what the relative or absolute paths are.

There is an entry on the wiki titled troubleshooting resources. It's in the A5 tutorial somewhere. You should read it.

Eric Johnson

I honestly do not know why any of this is not working; it's quite troublesome. I am constantly returned with NULL for my resources. :-/

I found an article on the wiki and followed its instructions. Here's what I now have...


ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_append_path_component(path, "data");
al_change_directory(al_path_cstr(path, '/'));

std::cout << al_get_current_directory() << endl;

ALLEGRO_BITMAP *player = al_load_bitmap("player.png");

I compile and run it, and am still returned with "al_draw_tinted_bitmap: Assertion `bitmap' failed." My current directory (from running in the terminal) is (pwd) /home/pi/Desktop/game. The game itself tells the console that it is in /home/pi/Desktop/game/data (from line 10).

Edgar Reynaldo

You shouldn't be getting any more 'assertion 'blah' failed' because you should be checking for NULL returns yourself, and taking action appropriately before that even happens.

Okay. It looks like your directory was data, like you wanted it to be. And player.png is in the data directory right? Well, if it still isn't loading then maybe png support wasn't built into your version of allegro. Did you install zlib and libpng before building allegro?

Eric Johnson

Yes, player.png is within the data directory. No, I do not believe I did install those prior to building Allegro 5, nor after... Perhaps that's my issue (how stupid of me).

Okay, so I'll need to install these. Should I purge Allegro 5 from my system first, then run through its installation again, or could I just build it again now, thus overwriting what I did before?

Dizzy Egg

Why don't you try loading a .bmp first, to be sure.

Eric Johnson

Eureka (ha ha)! I placed a bitmap called "test.bmp" within the data directory, then referenced it in code... It works wonderfully! So then, it appears that .png files are the issue.

Edgar Reynaldo

Don't uninstall anything. :P

Just sudo apt-get zlibdevel whatever and libpng and then rebuild allegro and reinstall it.

Eric Johnson

I sincerely appreciate the assistance, truly I do. ;D

I'll try this out in a bit. I guess I'll have to redo CMake in my build directory, then make, then ultimately make install. Awesome.

That worked wonderfully. ;D Allegro 5 is working just fine now on my machine, as are .png images! Thanks everyone!

Thread #611897. Printed from