Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Loading and draw images

This thread is locked; no one can reply to it. rss feed Print
Loading and draw images
SkrotOxD
Member #13,250
August 2011

Hello,

I´m having some trouble with Images on Allegro. As I wasn´t able to find really helpful tutorials on Allegro 5 I decided to read API specification
As I understood I have to use an external module to handle Images and this should be allegro5/allegro_image.h. Than I used al_init_image_addon.

For loading ALLEGRO_BITMAP *img = al_load_bitmap(char *path);

Still, when I verify the var img (if img == NULL)
I get true ever.

Am I missing something?

P.S. Sorry if I have any grammatical mistakes, still relearning english

William Labbett
Member #4,486
March 2004
avatar

What are you passing for const char * ?

Is it an image in the directory where you're program is ?

SkrotOxD
Member #13,250
August 2011

At first I was passing a fully qualified path

C:\way to my image\img.bmp

When it failed I´ve changed to the relative

img.bmp

And the image was side by side with the .exe on Debug folder

I´m programming on Visual Studio 2010

As a Image worths more than words, I will put the code, hope it helps

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_image.h> 4 5int main(int argc, char **argv) 6{ 7 ALLEGRO_DISPLAY *display = NULL; 8 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 9 10 if(!al_init()) { 11 fprintf(stderr, "failed to initialize allegro!\n"); 12 return -1; 13 } 14 15 display = al_create_display(640, 480); 16 if(!display) { 17 fprintf(stderr, "failed to create display!\n"); 18 return -1; 19 } 20 21 event_queue = al_create_event_queue(); 22 if( event_queue == NULL ){ 23 fprintf(stderr, "failed to create event queue\n"); 24 al_destroy_display(display); 25 } 26 al_register_event_source(event_queue, al_get_display_event_source(display)); 27 28 //Inicia o manipulador de imagens e verifica se teve sucesso 29 if( !al_init_image_addon() ){ 30 fprintf(stderr, "Could not init image addon\n"); 31 al_destroy_display(display); 32 al_destroy_event_queue(event_queue); 33 } 34 35 al_clear_to_color(al_map_rgb(0,0,0)); 36 37 al_flip_display(); 38 39 while(true){ 40 ALLEGRO_EVENT ev; 41 ALLEGRO_TIMEOUT timeout; 42 al_init_timeout(&timeout, 0.06); 43 44 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); 45 46 if( get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ 47 ALLEGRO_BITMAP *img = al_load_bitmap("pic.bmp"); 48 if( img == NULL ){ 49 fprintf(stderr, "Load of \"pic\" image failed \n"); 50 break; 51 } 52 al_draw_bitmap(img, 0 , 0 , NULL); 53 al_flip_display(); 54 al_rest(5.0); 55 break; 56 } 57 58 al_clear_to_color(al_map_rgb(0,0,0)); 59 al_flip_display(); 60 } 61 62 al_destroy_display(display); 63 al_destroy_event_queue(event_queue); 64 al_shutdown_image_addon(); 65 66 return 0; 67}

Matthew Leverton
Supreme Loser
January 1999
avatar

William Labbett
Member #4,486
March 2004
avatar

SkrotOxD said:

"Load of \"pic\" image failed \n"

Does it always print this out then ?

SkrotOxD
Member #13,250
August 2011

I´ve solved the problem

Seems like it doesn´t work with inverted slashes '\' for the fully qualified path.

For the relative, I just was putting on the wrong folder when executing from inside VS (was putting on Debbug, but is the project folder).

Thanks guys.

Matthew Leverton
Supreme Loser
January 1999
avatar

Please read the wiki article if you haven't. It explains how to do it correctly.

Go to: