Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » bitmap loading problems

Credits go to Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
bitmap loading problems
Fworg64
Member #12,077
June 2010

Ive had trouble with this function since i first started using allegro 5 (a month ago :) ).
It seems that al_load_bitmap refuses to load a bitmap for me, i had gotten it to work for a while but then after i changed a couple lines somewhere it stopped,
heres my code

#SelectExpand
1 2#include <stdio.h> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_image.h> 5#include <time.h> 6#include <cstdlib> 7 8const int SCREEN_W = 640; 9const int SCREEN_H = 480; 10const int FPS = 60; 11const int OBJECTD = 16; 12const float UPR=1; 13const float UPL=2; 14const float DNR=3; 15const float DNL=0; 16int num_of_enemy; 17 18class Game 19{ 20 public: 21 ALLEGRO_DISPLAY *display; 22 ALLEGRO_EVENT_QUEUE *event_queue; 23 ALLEGRO_TIMER *timer; 24 bool Init(); 25}; 26 27bool Game::Init() 28{ 29 if (!al_init()) {fprintf(stderr,"failed to initialize allegro!\n");return false;} 30 if (!al_install_keyboard()) {fprintf(stderr,"failed to install keyboard drivers!\n");return false;} 31 if (!al_init_image_addon()) {fprintf(stderr,"failed to initialize image addon\n");return false;} 32 33 display = al_create_display(SCREEN_W,SCREEN_H); 34 event_queue = al_create_event_queue(); 35 timer = al_create_timer(1.0/FPS); 36 37 if (!display) {fprintf(stderr,"failed to create display!\n"); return false;} 38 if (!event_queue) {fprintf(stderr,"failed to create event queue!\n"); al_destroy_display(display); return false;} 39 if (!timer) {fprintf(stderr,"failed to create timer!\n"); al_destroy_display(display); al_destroy_event_queue(event_queue);return false;} 40 41 al_register_event_source(event_queue, al_get_display_event_source(display)); 42 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 43 al_register_event_source(event_queue, al_get_keyboard_event_source()); 44 45 return true; 46} 47 48class Object 49{ 50 int x; 51 int y; 52 int d; 53 ALLEGRO_BITMAP *bitmap; 54 public: 55 Object(bool player); 56 Object(); 57 void Move(ALLEGRO_KEYBOARD_STATE *state); 58 void Move(); 59 void Draw(); 60 bool Collide(Object victim); 61}; 62 63Object::Object(bool player) 64{ 65 if (player) 66 { 67 x = (SCREEN_W - OBJECTD)/2; 68 y = (SCREEN_H - OBJECTD)/2; 69 bitmap = al_load_bitmap("playerball.bmp"); 70 if (bitmap == NULL) printf("erroer loding bitmap\n"); //displays this, meaning bitmap failed to load 71 al_convert_mask_to_alpha(bitmap,al_map_rgb(255,255,255)); // actually crashes on this line, most likely because the bitmap is null 72 } 73} 74 75Object::Object() 76{ 77 d = rand() % 4; 78 x = rand() %SCREEN_H; 79 y = rand() %SCREEN_W; 80 bitmap = al_load_bitmap("cpuball.bmp"); 81 al_convert_mask_to_alpha(bitmap,al_map_rgb(255,255,255)); 82} 83 84void Object::Move(ALLEGRO_KEYBOARD_STATE *state) 85{ 86 printf("1 "); 87 if ((al_key_down(state,ALLEGRO_KEY_UP) || al_key_down(state,ALLEGRO_KEY_W)) && y>0) y--; 88 printf("2 "); 89 if ((al_key_down(state,ALLEGRO_KEY_DOWN) || al_key_down(state,ALLEGRO_KEY_S)) && (y+OBJECTD)<SCREEN_H) y++; 90 printf("3 "); 91 if ((al_key_down(state,ALLEGRO_KEY_LEFT) || al_key_down(state,ALLEGRO_KEY_A)) && x>0) x--; 92 printf("4 "); 93 if ((al_key_down(state,ALLEGRO_KEY_RIGHT) || al_key_down(state,ALLEGRO_KEY_D)) && (x+OBJECTD)<SCREEN_W) x++; 94 printf("5 \n"); 95} 96 97void Object::Move() 98{ 99 if (x==0 && d==UPL) {d=UPR;} 100 if (x==0 && d==DNL) {d=DNR;} 101 if (x==(SCREEN_W-OBJECTD) && d==DNR) {d=DNL;} 102 if (x==(SCREEN_W-OBJECTD) && d==UPR) {d=UPL;} 103 if (y==0 && d==UPL) {d=DNL;} 104 if (y==0 && d==UPR) {d=DNR;} 105 if (y==(SCREEN_H-OBJECTD) && d==DNR) {d=UPR;} 106 if (y==(SCREEN_H-OBJECTD) && d==DNL) {d=UPL;} 107 if (d == UPR) {y--;x++;} 108 if (d == UPL) {y--;x--;} 109 if (d == DNR) {y++;x++;} 110 if (d == DNL) {y++;x--;} 111} 112 113void Object::Draw() 114{ 115 printf("object.draw\n"); 116 al_draw_bitmap(bitmap,x,y,0); 117} 118 119bool Object::Collide(Object victim) 120{ 121 122} 123 124int main() 125{ 126 srand(time(NULL)); 127 128 Game Sally; 129 if (!Sally.Init()) {printf("Your Screwed\n"); return -1;} 130 131 Object Billy(true); 132 num_of_enemy = 5; 133 Object *Enemy; 134 Enemy = new Object[num_of_enemy]; 135 bool redraw = true; 136 137 al_set_target_bitmap(al_get_backbuffer(Sally.display)); 138 al_set_window_title(Sally.display,"TITLE"); 139 al_clear_to_color(al_map_rgb(250,100,19)); 140 al_flip_display(); 141 142 al_start_timer(Sally.timer); 143 144 printf("starting loop\n"); 145 146 ALLEGRO_KEYBOARD_STATE *ret_state; 147 ret_state = new ALLEGRO_KEYBOARD_STATE; 148 149 while (true) 150 { 151 ALLEGRO_EVENT ev; 152 al_wait_for_event(Sally.event_queue,&ev); 153 154 if (ev.type == ALLEGRO_EVENT_TIMER) 155 { 156 printf("timer\n"); 157 Billy.Move(ret_state); 158 for (int a=0;a<num_of_enemy;a++) 159 { 160 Enemy[a].Move(); 161 } 162 redraw = true; 163 } 164 165 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 166 { 167 printf("keypressfghjiulkyjrtyuiiuytrtyuiuiy\n"); 168 al_get_keyboard_state(ret_state); 169 } 170 171 else if (ev.type == ALLEGRO_EVENT_KEY_UP) 172 { 173 printf("keyrelaesesfghjiulkyjrtyuiiuytrtyuiuiy\n"); 174 al_get_keyboard_state(ret_state); 175 } 176 177 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 178 { 179 printf("close\n"); 180 al_destroy_timer(Sally.timer); 181 al_destroy_display(Sally.display); 182 al_destroy_event_queue(Sally.event_queue); 183 return 0; 184 } 185 186 if (redraw && al_is_event_queue_empty(Sally.event_queue)) 187 { 188 printf("redraw\n"); 189 al_clear_to_color(al_map_rgb(50,50,50)); 190 Billy.Draw(); 191 for (int a=0;a<num_of_enemy;a++) 192 { 193 Enemy[a].Draw(); 194 } 195 al_flip_display(); 196 redraw = false; 197 } 198 } 199 200 return 0; 201}

lines 69-71 are where it crashes

as a side note i double checked the spelling and the image file is in fact next to the .exe, Ive also attached the two image files.

I know what I'm doing, I'm just not very good at it. -Austin Oltmanns

Matthew Leverton
Supreme Loser
January 1999
avatar

Throw a fprintf in Game::init to make sure Allegro is being initialized before any bitmaps are loaded.

It could also be a path issue. al_filename_exists() will return true if the file is where it should be.

someone972
Member #7,719
August 2006
avatar

If Allegro isn't being initialized first I'd try changing Object Billy(true); to Object* Billy = new Object(true); and see if that works.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Fworg64
Member #12,077
June 2010

After following your guys's advice i found it must be a path issue, allegro is initialized judging by the al_is_system_installed() function i added right before the al_load_bitmap. It must then be a path error which i cant grasp why, the file is named how it should and is right next to the .exe. the only modifications ive made to the program is now Object::Object(bool player) is as follows.

Object::Object(bool player)
{
    if (player)
    {
        x = (SCREEN_W - OBJECTD)/2;
        y = (SCREEN_H - OBJECTD)/2;
        if (al_is_system_installed()) printf("allegro is installed\n");// true
        if (!al_filename_exists("playerball.bmp")) printf("it doesnt exist\n");// the file does not exist
        bitmap = al_load_bitmap("playerball.bmp");
        if (bitmap == NULL) printf("erroer loding bitmap\n");// the pointer is null
        al_convert_mask_to_alpha(bitmap,al_map_rgb(255,255,255));
    }
}

not much changed (nothing functionally at least)

EDIT::Found the problem, was in fact a Path problem but not my fault,
my IDE code::blocks which i was running my program from, uses the project folder to look for resources and files, not the debug folder that holds the .exe where most would assume. The program runs when launched normally but will only function when launched from the IDE if the resources are in the main project folder! 8-)

I know what I'm doing, I'm just not very good at it. -Austin Oltmanns

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You can fix this by setting the execution working directory of each build type of your project. Go to Project->Properties->Build Targets and set that to the directory where your program is built. You can see this by looking slightly up to the Output Filename textbox :

{"name":"604019","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/5\/c5d1dbcc351316d031ee7874dec1ebaf.png","w":702,"h":483,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/5\/c5d1dbcc351316d031ee7874dec1ebaf"}604019

Go to: