Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » A5 can't get load_bitmap to find image

This thread is locked; no one can reply to it. rss feed Print
A5 can't get load_bitmap to find image
kyle motherwell
Member #11,111
July 2009

Hey people, I was here a while ago learning A4 then I took a short break, I came back just a few days ago and A5 appears to have been out for a bit, I had a look at it and looked at some examples and such, I wanted to see if I could load an image and draw it to the screen so I created a bitmap object and set it to NULL then I tried to assign it an image from my HDD called bob, which is a bitmap image. The problem is when the program runs I get an exception :

First-chance exception at 0x00000000 in AllegroProject.exe: 0xC0000005: Access violation.

Unhandled exception at 0x00000000 in AllegroProject.exe: 0xC0000005: Access violation.

I presume this is because the image assign fails because it can't find my image, I tried quite a few things to get this working, first I tried an absolute path, which did not work, then I put it in the same folder as the .exe that also did not work, I also tried something using the path "hack" someone was calling it although I only partially understood what it was doing and it kept failing at compile so I left it.

So I would really appreciate it if someone could tell me whats wrong with my program.

Line that is the main culprit: al_load_bitmap("bob.bmp");

Trent Gamblin
Member #261
April 2000
avatar

Please post some source code. Use <code></code> tags.

Matthew Leverton
Supreme Loser
January 1999
avatar

kyle motherwell
Member #11,111
July 2009

To mat, I just put it in now and it still seems to give exceptions.

#SelectExpand
1#include "stdafx.h" 2#include <stdio.h> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_image.h> 5 6int main(int argc, char **argv) 7{ 8 9//Create a display and an event queue 10 ALLEGRO_DISPLAY *display = NULL; 11 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 12 ALLEGRO_BITMAP * testBMP = NULL; 13 14 al_init_image_addon(); 15 16al_load_bitmap("bob.bmp"); 17 18 if(!testBMP) 19 { 20 fprintf(stderr,"failed to initialize testBMP!\n"); 21 return -1; 22 23 } 24 25 26 if(!al_init_image_addon()) 27 { 28 fprintf(stderr,"failed to initialize image addon!\n"); 29 return -1; 30 31 } 32 33 34 35 36 //Test if allegro failed to initiliaze, if it did print a message to the console and leave with error code -1 37 if(!al_init()) { 38 fprintf(stderr, "failed to initialize allegro!\n"); 39 return -1; 40 } 41 //Set the display by assisnging it the properties returned from the function al_create_display(640, 480) this sets the display to a resoloution of 640x480 42 display = al_create_display(640, 480); 43 44 //check to see if the display was properly set 45 if(!display) { 46 fprintf(stderr, "failed to create display!\n"); 47 return -1; 48 } 49 50 //Create an even queue, event queues can be filled with different evens that happen from sources, sources include keyboard, mouse and display, we will use display 51 event_queue = al_create_event_queue(); 52 53 //We check if the queue was set properly 54 if(!event_queue) { 55 fprintf(stderr, "failed to create event_queue!\n"); 56 al_destroy_display(display); 57 return -1; 58 } 59 60 //We set the queue to watch all the events from the source, our source is the display, so resizing or closing the window would be an event for the display 61 al_register_event_source(event_queue, al_get_display_event_source(display)); 62 63 //We clear the screen to the colour black 64 al_clear_to_color(al_map_rgb(0,0,0)); 65 66 //We swap the drawing buffer to show our changes since we last swapped buffers, search up double buffering to find out more about what this means 67 al_flip_display(); 68 69 70 while(1) 71 { 72 //We createa an event to hold the event 73 ALLEGRO_EVENT ev; 74 //A timeout to be initiliased 75 ALLEGRO_TIMEOUT timeout; 76 //Here we initliase timeout and set it to 0.06, or 60 miliseconds 77 al_init_timeout(&timeout, 0.06); 78 79 //Here the bool is assigned either true of false depending if the event queue is updated with an event or it gets set to false if the timeout occurs before the event finishes. 80 bool got_event = al_wait_for_event_until(event_queue, &ev, &timeout); 81 82 //We check if the event was close 83 if(got_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 84 85 //This ends the program loop, closing the program 86 break; 87 } 88 89 //Clear to black and swap buffers 90 al_clear_to_color(al_map_rgb(0,0,0)); 91 al_flip_display(); 92 } 93 94 //Destroy display object and queue object 95 al_destroy_display(display); 96 al_destroy_event_queue(event_queue); 97 98 99 return 0; 100}

Trent Gamblin
Member #261
April 2000
avatar

You've got it in there twice now, both in the wrong place. Before you use any other allegro functions, call al_init.

Matthew Leverton
Supreme Loser
January 1999
avatar

Call load bitmap after initializing Allegro, after initializing the image addon, and after creating the display. Use an absolute path until you know for sure it is working.

Also, your test is bogus because you never assign the value of load_bitmap to anything.

kyle motherwell
Member #11,111
July 2009

Forgive me for how messy and badly done it all is, I done it pretty quickly and didn't see what I had done wrong. I fixed the mistakes (not all of them I'm sure) and now my test code runs and tells me that it cannot initiliaze the image. I used a absolute path too.

#SelectExpand
1#include "stdafx.h" 2#include <stdio.h> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_image.h> 5 6int main(int argc, char **argv) 7{ 8 9//Create a display and an event queue 10 ALLEGRO_DISPLAY *display = NULL; 11 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 12 ALLEGRO_BITMAP * testBMP = NULL; 13 14//Test if allegro failed to initiliaze, if it did print a message to the console and leave with error code -1 15 if(!al_init()) { 16 fprintf(stderr, "failed to initialize allegro!\n"); 17 return -1; 18 } 19 //Set the display by assisnging it the properties returned from the function al_create_display(640, 480) this sets the display to a resoloution of 640x480 20 display = al_create_display(640, 480); 21 22 //check to see if the display was properly set 23 if(!display) { 24 fprintf(stderr, "failed to create display!\n"); 25 return -1; 26 } 27 28 testBMP = al_load_bitmap("C:\Users\Kyle\Desktop\kyle-and-liams-project\AllegroProject\bob.bmp"); 29 30 if(!testBMP) 31 { 32 fprintf(stderr,"failed to initialize testBMP!\n"); 33 return -1; 34 35 } 36 37 //Create an even queue, event queues can be filled with different evens that happen from sources, sources include keyboard, mouse and display, we will use display 38 event_queue = al_create_event_queue(); 39 40 //We check if the queue was set properly 41 if(!event_queue) { 42 fprintf(stderr, "failed to create event_queue!\n"); 43 al_destroy_display(display); 44 return -1; 45 } 46 47 48 //We set the queue to watch all the events from the source, our source is the display, so resizing or closing the window would be an event for the display 49 al_register_event_source(event_queue, al_get_display_event_source(display)); 50 51 //We clear the screen to the colour black 52 al_clear_to_color(al_map_rgb(0,0,0)); 53 54 //We swap the drawing buffer to show our changes since we last swapped buffers, search up double buffering to find out more about what this means 55 al_flip_display(); 56 57 58 while(1) 59 { 60 //We createa an event to hold the event 61 ALLEGRO_EVENT ev; 62 //A timeout to be initiliased 63 ALLEGRO_TIMEOUT timeout; 64 //Here we initliase timeout and set it to 0.06, or 60 miliseconds 65 al_init_timeout(&timeout, 0.06); 66 67 //Here the bool is assigned either true of false depending if the event queue is updated with an event or it gets set to false if the timeout occurs before the event finishes. 68 bool got_event = al_wait_for_event_until(event_queue, &ev, &timeout); 69 70 //We check if the event was close 71 if(got_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 72 73 //This ends the program loop, closing the program 74 break; 75 } 76 77 //Clear to black and swap buffers 78 al_clear_to_color(al_map_rgb(0,0,0)); 79 al_flip_display(); 80 } 81 82 //Destroy display object and queue object 83 al_destroy_display(display); 84 al_destroy_event_queue(event_queue); 85 86 87 return 0; 88}

Matthew Leverton
Supreme Loser
January 1999
avatar

The answer is in my previous post.

Edit: And use '/' or '\\' for path separators. A single backslash will cause problems.

kyle motherwell
Member #11,111
July 2009

Thanks, it was the slashes that were causing the problem.

Go to: