Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Some code not working.

This thread is locked; no one can reply to it. rss feed Print
Some code not working.
Amaz1ng
Member #12,865
May 2011

What's wrong with this code? It seems be crashing.

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_primitives.h> 6#include <allegro5/allegro_font.h> 7#include <allegro5/allegro_ttf.h> 8 9ALLEGRO_DISPLAY *display = NULL; 10ALLEGRO_EVENT_QUEUE *events = NULL; 11ALLEGRO_EVENT ev; 12ALLEGRO_BITMAP *bmp, *bmp2, *bmp3; 13ALLEGRO_EVENT_SOURCE * timesrc = NULL; 14ALLEGRO_TIMEOUT *timeout = NULL; 15ALLEGRO_TIMER *timer = NULL; 16const int SCREEN_W = 1024; 17const int SCREEN_H = 768; 18const float FPS = 60; 19const char* title = "Fantasy Land Redemption"; 20 21void init_allegro(); 22void load_sounds(); 23void load_images(); 24void redraw(); 25 26int main(int argc, char **argv) 27{ 28 //LOAD CONTENT 29 init_allegro(); 30 load_images(); 31 load_sounds(); 32 33 //timer 34 timer = al_create_timer(1 / 60); 35 timesrc = al_get_timer_event_source(timer); 36 al_start_timer(timer); 37 38 //events 39 events = al_create_event_queue(); 40 al_register_event_source(events, al_get_display_event_source(display)); 41 al_register_event_source(events, timesrc); 42 43 while(1) 44 { 45 al_wait_for_event(events, &ev); 46 47 if(ev.type == ALLEGRO_EVENT_TIMER) 48 { 49 redraw(); 50 } 51 } 52 53 //destroy 54 al_destroy_display(display); 55 al_destroy_event_queue(events); 56 return 0; 57} 58 59void init_allegro() 60{ 61 //initiailize, create window 62 al_init(); 63 display = al_create_display(SCREEN_W, SCREEN_H); 64 al_set_window_title(display, title); 65} 66 67void load_sounds() 68{ 69} 70 71void load_images() 72{ 73 //images? 74 al_init_image_addon(); 75 bmp = al_load_bitmap("hero_stand.png"); 76 al_set_target_bitmap(al_get_backbuffer(display)); 77} 78 79void gametickupdate() 80{ 81 82} 83 84void redraw() 85{ 86 al_draw_bitmap(bmp, SCREEN_W / 2, SCREEN_H / 2, NULL); 87}

Edgar Reynaldo
Member #8,592
May 2007
avatar

What's wrong with your debugger? You seem to not be using it. :P

Try checking the return values of functions that could fail.

Amaz1ing said:

   while(1)
   {
     al_wait_for_event_until(events, &ev, timeout);

     if(ev.type == ALLEGRO_EVENT_TIMER)
     {
     redraw();
     }
   }

If al_wait_for_event_until times out, then the value of ev is most likely unchanged since the last time that it was altered. Check the return value of al_wait_for_event_until and if it timed out, don't check the event value.

Amaz1ing said:

   al_register_event_source(events, timesrc);

You never initialized timesrc to a value other than NULL, I don't know if that would crash or not.

You also never created an ALLEGRO_TIMER, nor did you register it with

so you'll never get any ALLEGRO_EVENT_TIMER events either.

Karadoc ~~
Member #2,749
September 2002
avatar

My suggestion is to put printf statements all through the code so that you can see how far it gets before it crashes. like this:

void main(..)
{
  printf("start of main\n");
  init_allegro();
  ...
  printf("checkpoint 1\n");
  events = al_create_event_queue();
  ...
  printf("and so on\n");
  ...
}

... or just run it through a debugger to see where it crashes. Once you find the exact line that makes it crash, it will probably be more clear what the problem is.

-----------

Arthur Kalliokoski
Second in Command
February 2005
avatar

If you use printf(), you probably want to follow it with an fflush(stdout) to make sure it's printed regardless of crashes.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Karadoc ~~
Member #2,749
September 2002
avatar

I think printf to stdout automatically flushes when there is a newline ('\n'). doesn't it? Maybe I'm wrong, but I use this method of debugging frequently and I've only had flush problems when I make it write to a log file instead of stdout.

-----------

Arthur Kalliokoski
Second in Command
February 2005
avatar

I think printf to stdout automatically flushes when there is a newline ('\n'). doesn't it?

I think it depends on the compiler. DJGPP always flushed printf() to avoid confusing the n00bs, but if you wanted more speed due to buffering, you had to use fprintf().

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Amaz1ng
Member #12,865
May 2011

Yeah I looked w/ the debugger and found the problems. But a new problem is that when I use strcat to get a filename, it strips the slashes off.

void load_images()
{
  //images?
  path = al_get_current_directory();
  strcat(path, "\sprites\hero\hero_stand.png");
   al_init_image_addon();
   hero_stand = al_load_bitmap(path);
   
   al_set_target_bitmap(al_get_backbuffer(display));
}

Karadoc ~~
Member #2,749
September 2002
avatar

Backslash is the escape character. If you actually want the backslash character, you put to put two of them. eg. "\\sprites\\hero\\hero_stand.png"

-----------

Arthur Kalliokoski
Second in Command
February 2005
avatar

Since the C preprocessor sees a backslash as an escape character (e.g. '\n') you have to put two backslashes in a row, such as "\\sprites\\hero\\hero_stand.png" or use forward slashes "/sprites/hero/hero_stand.png".

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Edgar Reynaldo
Member #8,592
May 2007
avatar

Go to: