Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with game project

This thread is locked; no one can reply to it. rss feed Print
Problem with game project
Bater55
Member #16,769
November 2017

Hi! I am trying to write a simple game using Allegro 5. It all works with a square, but it doesn't work with my image. My idea is to have a 'warrior' who explores map avoiding some objects around. It's all of course in progress. I am absolute beginner by the way. Before adding image it was okay, but now it shuts down after less than second. What should I change?

Thanks for help and ideas!

CODE (based on Basic Keyboard Example):

#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
const float FPS = 60;
const int SCREEN_W = 1024;
const int SCREEN_H = 768;
const int WARRIOR_SIZE = 16;
enum MYKEYS {
KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
};

int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *warrior = al_load_bitmap("warrior.png");
float warrior_x = SCREEN_W / 2.0 - WARRIOR_SIZE / 2.0;
float warrior_y = SCREEN_H / 2.0 - WARRIOR_SIZE / 2.0;
bool key[4] = { false, false, false, false };
bool redraw = true;
bool doexit = false;

if (!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}

if (!al_install_keyboard()) {
fprintf(stderr, "failed to initialize the keyboard!\n");
return -1;
}
if (!al_init_image_addon()) {
fprintf(stderr, "Failed to initialize al_init_image_addon!\n");
return -1;
}

timer = al_create_timer(1.0 / FPS);
if (!timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}

display = al_create_display(SCREEN_W, SCREEN_H);
if (!display) {
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer);
return -1;
}

warrior = al_load_bitmap("warrior.png");
if (!warrior) {
fprintf(stderr, "failed to create warrior!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return 0;
}

al_set_target_bitmap(warrior);

al_set_target_bitmap(al_get_backbuffer(display));

event_queue = al_create_event_queue();
if (!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_bitmap(warrior);
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}

al_register_event_source(event_queue, al_get_display_event_source(display));

al_register_event_source(event_queue, al_get_timer_event_source(timer));

al_register_event_source(event_queue, al_get_keyboard_event_source());

al_clear_to_color(al_map_rgb(0, 0, 0));

al_flip_display();

al_start_timer(timer);

while (!doexit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if (ev.type == ALLEGRO_EVENT_TIMER) {
if (key[KEY_UP] && warrior_y >= 4.0) {
warrior_y -= 4.0;
}

if (key[KEY_DOWN] && warrior_y <= SCREEN_H - WARRIOR_SIZE - 4.0) {
warrior_y += 4.0;
}

if (key[KEY_LEFT] && warrior_x >= 4.0) {
warrior_x -= 4.0;
}

if (key[KEY_RIGHT] && warrior_x <= SCREEN_W - WARRIOR_SIZE - 4.0) {
warrior_x += 4.0;
}

redraw = true;
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = true;
break;

case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = true;
break;

case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = true;
break;

case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = true;
break;
}
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = false;
break;

case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = false;
break;

case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = false;
break;

case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = false;
break;

case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
}
}

if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;


al_draw_bitmap(warrior, warrior_x, warrior_y, 0);
al_flip_display();
al_clear_to_color(al_map_rgb(250, 250, 250));
}
}
al_destroy_bitmap(warrior);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);

return 0;
}

André Silva
Member #11,991
May 2010
avatar

I'd say the reason is because you're trying to call al_load_bitmap() before Allegro is even initialized. You should have the al_init() function first, then al_init_image_addon(), and only after those two can you actually call al_load_bitmap().

Other than that, you can also try checking if your "warrior" bitmap is NULL, after the al_load_bitmap() call. If so, that means something went wrong with the loading itself, like the file "warrior.png" not being found, or something.

Chris Katko
Member #1,881
January 2002
avatar

Also, don't forget to use code tags! It makes it much easier for people to read your code and help you!

See:

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_image.h> 4const float FPS = 60; 5const int SCREEN_W = 1024; 6const int SCREEN_H = 768; 7const int WARRIOR_SIZE = 16; 8enum MYKEYS { 9KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT 10}; 11 12int main(int argc, char **argv) 13{ 14ALLEGRO_DISPLAY *display = NULL; 15ALLEGRO_EVENT_QUEUE *event_queue = NULL; 16ALLEGRO_TIMER *timer = NULL; 17ALLEGRO_BITMAP *warrior = al_load_bitmap("warrior.png"); //<------------- Right here is the error! 18float warrior_x = SCREEN_W / 2.0 - WARRIOR_SIZE / 2.0; 19float warrior_y = SCREEN_H / 2.0 - WARRIOR_SIZE / 2.0; 20bool key[4] = { false, false, false, false }; 21bool redraw = true; 22bool doexit = false; 23 24if (!al_init()) { 25fprintf(stderr, "failed to initialize allegro!\n"); 26return -1; 27} 28 29if (!al_install_keyboard()) { 30fprintf(stderr, "failed to initialize the keyboard!\n"); 31return -1; 32} 33if (!al_init_image_addon()) { 34fprintf(stderr, "Failed to initialize al_init_image_addon!\n"); 35return -1; 36} 37timer = al_create_timer(1.0 / FPS); 38if (!timer) { 39fprintf(stderr, "failed to create timer!\n"); 40return -1; 41} 42 43display = al_create_display(SCREEN_W, SCREEN_H); 44if (!display) { 45fprintf(stderr, "failed to create display!\n"); 46al_destroy_timer(timer); 47return -1; 48} 49 50warrior = al_load_bitmap("warrior.png"); 51if (!warrior) { 52fprintf(stderr, "failed to create warrior!\n"); 53al_destroy_display(display); 54al_destroy_timer(timer); 55return 0; 56} 57 58al_set_target_bitmap(warrior); 59 60al_set_target_bitmap(al_get_backbuffer(display)); 61 62event_queue = al_create_event_queue(); 63if (!event_queue) { 64fprintf(stderr, "failed to create event_queue!\n"); 65al_destroy_bitmap(warrior); 66al_destroy_display(display); 67al_destroy_timer(timer); 68return -1; 69} 70 71al_register_event_source(event_queue, al_get_display_event_source(display)); 72 73al_register_event_source(event_queue, al_get_timer_event_source(timer)); 74 75al_register_event_source(event_queue, al_get_keyboard_event_source()); 76 77al_clear_to_color(al_map_rgb(0, 0, 0)); 78 79al_flip_display(); 80 81al_start_timer(timer); 82 83while (!doexit) 84{ 85ALLEGRO_EVENT ev; 86al_wait_for_event(event_queue, &ev); 87 88if (ev.type == ALLEGRO_EVENT_TIMER) { 89if (key[KEY_UP] && warrior_y >= 4.0) { 90warrior_y -= 4.0; 91} 92 93if (key[KEY_DOWN] && warrior_y <= SCREEN_H - WARRIOR_SIZE - 4.0) { 94warrior_y += 4.0; 95} 96 97if (key[KEY_LEFT] && warrior_x >= 4.0) { 98warrior_x -= 4.0; 99} 100 101if (key[KEY_RIGHT] && warrior_x <= SCREEN_W - WARRIOR_SIZE - 4.0) { 102warrior_x += 4.0; 103} 104 105redraw = true; 106} 107else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 108break; 109} 110else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { 111switch (ev.keyboard.keycode) { 112case ALLEGRO_KEY_UP: 113key[KEY_UP] = true; 114break; 115 116case ALLEGRO_KEY_DOWN: 117key[KEY_DOWN] = true; 118break; 119 120case ALLEGRO_KEY_LEFT: 121key[KEY_LEFT] = true; 122break; 123 124case ALLEGRO_KEY_RIGHT: 125key[KEY_RIGHT] = true; 126break; 127} 128} 129else if (ev.type == ALLEGRO_EVENT_KEY_UP) { 130switch (ev.keyboard.keycode) { 131case ALLEGRO_KEY_UP: 132key[KEY_UP] = false; 133break; 134 135case ALLEGRO_KEY_DOWN: 136key[KEY_DOWN] = false; 137break; 138 139case ALLEGRO_KEY_LEFT: 140key[KEY_LEFT] = false; 141break; 142 143case ALLEGRO_KEY_RIGHT: 144key[KEY_RIGHT] = false; 145break; 146 147case ALLEGRO_KEY_ESCAPE: 148doexit = true; 149break; 150} 151} 152 153if (redraw && al_is_event_queue_empty(event_queue)) { 154redraw = false; 155 156 157al_draw_bitmap(warrior, warrior_x, warrior_y, 0); 158al_flip_display(); 159al_clear_to_color(al_map_rgb(250, 250, 250)); 160} 161} 162al_destroy_bitmap(warrior); 163al_destroy_timer(timer); 164al_destroy_display(display); 165al_destroy_event_queue(event_queue); 166 167return 0; 168}

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Bater55
Member #16,769
November 2017

I have al_init before al_init_image_addon, so I don't know exactly what you mean. I still don't know what to do. Can someone post correct code? I will realise where my mistake was.:-/

amarillion
Member #940
January 2001
avatar

Look at the formatted code that Chris Katko posted. In line 17 you do al_load_bitmap. In line 24, you do al_init. And in line 33, you call al_init_image_addon.

You have to move the bitmap loading from line 17 to after line 33.

Bater55
Member #16,769
November 2017

I did it and it still doesn't work.

int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
float warrior_x = SCREEN_W / 2.0 - WARRIOR_SIZE / 2.0;
float warrior_y = SCREEN_H / 2.0 - WARRIOR_SIZE / 2.0;
bool key[4] = { false, false, false, false };
bool redraw = true;
bool doexit = false;

if (!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}

if (!al_install_keyboard()) {
fprintf(stderr, "failed to initialize the keyboard!\n");
return -1;
}
if (!al_init_image_addon()) {
fprintf(stderr, "Failed to initialize al_init_image_addon!\n");
return -1;
}
ALLEGRO_BITMAP *warrior = al_load_bitmap("warrior.png");

timer = al_create_timer(1.0 / FPS);
if (!timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
...

beoran
Member #12,636
March 2011

It is better to also first create a display before loading the bitmap. If the image does not load, it is likely Allegro5 cannot find the bitmap. What does al_get_current_directory return, and does that match your expectations?

Bater55
Member #16,769
November 2017

Okay, guys. It was only problem with a image. I tried it on different one and it works :) Thanks for help! Now I have to focus on other things. ;D

Go to: