Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Cant load bitmap

This thread is locked; no one can reply to it. rss feed Print
Cant load bitmap
kebapmanager
Member #14,863
January 2013
avatar

I am really new here , but I would really appriciet help , since I am trying to figure out teh problem for 2 days :(

#SelectExpand
1 2#include <allegro5\allegro.h> 3#include <allegro5\allegro_primitives.h> 4#include <allegro5\allegro_image.h> 5#include "objects.h" 6 7//GLOBALS============================== 8const int WIDTH = 800; 9const int HEIGHT = 400; 10enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE}; 11bool keys[5] = {false, false, false, false, false}; 12 13//prototypes 14void InitPlayer(Player &mario); 15void DrawPlayer(Player &mario, ALLEGRO_BITMAP *image); 16void MovePlayerLeft(Player &mario); 17void MovePlayerUp(Player &mario); 18void MovePlayerDown(Player &mario); 19void MovePlayerRight(Player &mario); 20 21struct Player 22{ 23 int ID; 24 int x; 25 int y; 26 int lives; 27 int speed; 28 int boundx; 29 int boundy; 30 int score; 31}; 32int main(void) 33{ 34 //primitive variable 35 const int FPS = 60; 36 bool done = false; 37 bool redraw = true; 38 39 int imageWidth = 0; 40 int imageHeight = 0; 41 42 //object variables 43 Player mario; 44 45 //Allegro variables 46 ALLEGRO_DISPLAY *display = NULL; 47 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 48 ALLEGRO_TIMER *timer = NULL; 49 ALLEGRO_BITMAP *image = NULL; 50 51 //Initialization Functions 52 if(!al_init()) //initialize Allegro 53 return -1; 54 55 display = al_create_display(WIDTH, HEIGHT); //create our display object 56 57 if(!display) //test display object 58 return -1; 59 60 //Allegro Module Init 61 al_init_primitives_addon(); 62 al_install_keyboard(); 63 al_init_image_addon(); 64 65 event_queue = al_create_event_queue(); 66 timer = al_create_timer(1.0 / FPS); 67 image = al_load_bitmap("mario.jpg"); 68 69 70 71 //Game Init 72 InitPlayer(mario); 73 74 al_register_event_source(event_queue, al_get_keyboard_event_source()); 75 al_register_event_source(event_queue, al_get_display_event_source(display)); 76 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 77 78 al_start_timer(timer); 79 80 while(!done) 81 { 82 ALLEGRO_EVENT ev; 83 al_wait_for_event(event_queue, &ev); 84 85 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 86 { 87 done = true; 88 } 89 else if(ev.type == ALLEGRO_EVENT_TIMER) 90 { 91 redraw = true; 92 if(keys[UP]) 93 MovePlayerUp(mario); 94 if(keys[DOWN]) 95 MovePlayerDown(mario); 96 if(keys[LEFT]) 97 MovePlayerLeft(mario); 98 if(keys[RIGHT]) 99 MovePlayerRight(mario); 100 } 101 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 102 { 103 switch(ev.keyboard.keycode) 104 { 105 case ALLEGRO_KEY_ESCAPE: 106 done = true; 107 break; 108 case ALLEGRO_KEY_UP: 109 keys[UP] = true; 110 break; 111 case ALLEGRO_KEY_DOWN: 112 keys[DOWN] = true; 113 break; 114 case ALLEGRO_KEY_RIGHT: 115 keys[RIGHT] = true; 116 break; 117 case ALLEGRO_KEY_LEFT: 118 keys[LEFT] = true; 119 break; 120 case ALLEGRO_KEY_SPACE: 121 keys[SPACE] = true; 122 break; 123 124 } 125 } 126 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 127 { 128 switch(ev.keyboard.keycode) 129 { 130 case ALLEGRO_KEY_UP: 131 keys[UP] = false; 132 break; 133 case ALLEGRO_KEY_DOWN: 134 keys[DOWN] = false; 135 break; 136 case ALLEGRO_KEY_RIGHT: 137 keys[RIGHT] = false; 138 break; 139 case ALLEGRO_KEY_LEFT: 140 keys[LEFT] = false; 141 break; 142 case ALLEGRO_KEY_SPACE: 143 keys[SPACE] = false; 144 break; 145 146 } 147 } 148 149 150 if(redraw && al_is_event_queue_empty(event_queue)) 151 { 152 redraw = false; 153 154 al_flip_display(); 155 al_clear_to_color(al_map_rgb(0,0,0)); 156 } 157 } 158 159 al_destroy_event_queue(event_queue); 160 al_destroy_timer(timer); 161 al_destroy_display(display); 162 al_destroy_bitmap(image); 163 164 return 0; 165} 166 167void InitPlayer(Player &mario) 168{ 169 mario.x = 20; 170 mario.y = HEIGHT; 171 mario.ID = PLAYER; 172 mario.lives = 3; 173 mario.speed = 7; 174 mario.boundx = 6; 175 mario.boundy = 7; 176 mario.score = 0; 177} 178void DrawPlayer(Player &mario, ALLEGRO_BITMAP *image) 179{ 180 al_draw_bitmap(image,200,200, 0); 181}

Jeff Bernard
Member #6,698
December 2005
avatar

Is your Allegro Image addon compiled with JPG support? BMP is supported natively. Try resaving the image as a BMP to see if that's the issue.

Otherwise, you probably don't have the image in the right spot. You're loading it from the current working directory, which, if you're using an IDE, is probably where your project file is located which is probably not where your binary is built.

--
I thought I was wrong once, but I was mistaken.

Thomas Fjellstrom
Member #476
June 2000
avatar

Could you explain the problem you're having?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Neil Roy
Member #2,229
April 2002
avatar

I see the problem.

image = al_load_bitmap("mario.jpg");

You're assuming it will look in the current folder. You should probably insert some code to check the current working directory and then specify it as where you wish to load from...

perhaps something like this (untested)

This should change the current working directory to the current directory you're running your program from. Insert this code before you try and load anything.

---
“I love you too.” - last words of Wanda Roy

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

NiteHackr said:

perhaps something like this (untested)
al_change_directory(al_get_current_directory());

This should change the current working directory to the current directory you're running your program from. Insert this code before you try and load anything.

No, sorry that won't change the directory at all. You have to get the exe path from allegro's al_get_standard_path(ALLEGRO_RESOURCES_PATH);

So,

ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
const char* pathstr = al_path_cstr(path , ALLEGRO_NATIVE_PATH_SEP);
al_change_directory(pathstr);
al_destroy_path(path);

Neil Roy
Member #2,229
April 2002
avatar

Like I said, untested... I don't have this problem myself. Come to think of it, when I run the game I am currently working on, I just open a file from the same folder the game is running in without having to use any code to switch to that folder at all.

It may have something to do with the fact that I use PHYSFS, I don't know, but when I switch to the standard interface so I can load a file from outside of the ZIP I have my data stored in, it loads by default from the directory the game is running in. Perhaps the filename of the graphic you're trying to load is wrong? Or there is a problem with the bitmap? Make sure it's in the same folder as the executable, or specify a folder to load it from. Also, if you're using something like Code::Blocks, and you're running a DEBUG version which was compiled into it's own folder, make sure you specify which folder the exe is to use when it is run or it may be looking inside the debug folder.

---
“I love you too.” - last words of Wanda Roy

Go to: