Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » My code work but no load the sword.bmp image

This thread is locked; no one can reply to it. rss feed Print
My code work but no load the sword.bmp image
Jefferson Almeida
Member #12,659
March 2011

I based the tutorial which is on the wiki!
Files for download: http://www.megaupload.com/?d=YRXGH41L

This compiles fine without errors but when "Build and Run" show the message of the code: "Failed to load sword image!"

#SelectExpand
1/* 2====================================== 3 4Tela inicial com alguns testes do meu aprendizado. 5Autor: Jefferson [allos_nerelin] 6 7====================================== 8*/ 9 10#include <stdio.h> 11#include <allegro5/allegro.h> 12#include <allegro5/allegro_image.h> 13#include <allegro5/allegro_primitives.h> 14 15const float FPS = 60; 16const int SCREEN_W = 640; 17const int SCREEN_H = 480; 18const int BOUNCER_SIZE = 32; 19 20 21 22int main(int argc, char **argv) 23{ 24 ALLEGRO_DISPLAY *display = NULL; 25 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 26 ALLEGRO_TIMER *timer = NULL; 27 ALLEGRO_BITMAP *sword; 28 ALLEGRO_MOUSE_CURSOR *cursor; 29 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; 30 float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0; 31 bool redraw = true; 32 33 if(!al_init()) { 34 fprintf(stderr, "failed to initialize allegro!\n"); 35 return -1; 36 } 37 38 if(!al_install_mouse()) { 39 fprintf(stderr, "failed to initialize the mouse!\n"); 40 return -1; 41 } 42 43 if(!al_init_primitives_addon()) { 44 fprintf(stderr, "Failed to initialize the primitives addon!\n"); 45 return -1; 46 } 47 48 if (!al_init_image_addon()) { 49 fprintf(stderr, "Failed to initialize the image addon!\n"); 50 return -1; 51 } 52 53 54 timer = al_create_timer(1.0 / FPS); 55 if(!timer) { 56 fprintf(stderr, "failed to create timer!\n"); 57 return -1; 58 } 59 60 display = al_create_display(SCREEN_W, SCREEN_H); 61 if(!display) { 62 fprintf(stderr, "failed to create display!\n"); 63 al_destroy_timer(timer); 64 return -1; 65 } 66 67 sword = al_load_bitmap("sword.bmp"); 68 if(!sword) { 69 fprintf(stderr, "Failed to load sword image!\n"); 70 return -1; 71 } 72 73 cursor = al_create_mouse_cursor(sword, 16, 16); 74 if(!cursor) { 75 fprintf(stderr, "Failed to load the cursor!\n"); 76 return -1; 77 } 78 79 al_clear_to_color(al_map_rgb(255, 0, 255)); 80 81 al_set_target_bitmap(al_get_backbuffer(display)); 82 83 event_queue = al_create_event_queue(); 84 if(!event_queue) { 85 fprintf(stderr, "failed to create event_queue!\n"); 86 al_destroy_display(display); 87 al_destroy_timer(timer); 88 return -1; 89 } 90 91 al_register_event_source(event_queue, al_get_display_event_source(display)); 92 93 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 94 95 al_register_event_source(event_queue, al_get_mouse_event_source()); 96 97 al_clear_to_color(al_map_rgb(0,0,0)); 98 99 al_flip_display(); 100 101 al_start_timer(timer); 102 103 while(1) 104 { 105 ALLEGRO_EVENT ev; 106 al_wait_for_event(event_queue, &ev); 107 108 if(ev.type == ALLEGRO_EVENT_TIMER) { 109 redraw = true; 110 } 111 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 112 break; 113 } 114 else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES || 115 ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) { 116 117 bouncer_x = ev.mouse.x; 118 bouncer_y = ev.mouse.y; 119 } 120 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 121 break; 122 } 123 124 if(redraw && al_is_event_queue_empty(event_queue)) { 125 redraw = false; 126 127 al_clear_to_color(al_map_rgb(0,0,0)); 128 129 al_flip_display(); 130 } 131 } 132 133 al_destroy_timer(timer); 134 al_destroy_display(display); 135 al_destroy_event_queue(event_queue); 136 137 return 0; 138}

Matthew Leverton
Supreme Loser
January 1999
avatar

Site notes:

  1. use <code> </code> tags when posting source code

  2. use the built-in file attachment functionality instead of third party sites

Regarding the problem at hand:

  • Does the image exist on your file system?

  • If so, then the working directory isn't set properly.

Solutions:

  • Use an absolute path for the file name. (temporary solution)

  • Use al_get_standard_path() and related functions to switch to the appropriate location where the file is located before loading it.

Neil Walker
Member #210
April 2000
avatar

That's the second image load error due to relative/working paths I've seen this week.

Perhaps on the next document update, somebody could mention this in the load methods?

I can see a helper method something like:
al_load_bitmap_relative("resources/sword.bmp", ALLEGRO_EXENAME_PATH)

being quite useful in the wiki.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Matthew Leverton
Supreme Loser
January 1999
avatar

EXENAME is not the proper constant to use. It would break on OS X bundles. RESOURCES is the place to look for bundled data.

You would need a function for each loader and saver... it's better to understand what's going on. Note that you really probably only need to do this:

ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);

// ... append the media component to path

al_change_directory(al_path_cstr(path));

al_load_bitmap("foo.png");

I assume that works if you are using the standard I/O functions.

Go to: