What's wrong with this code? It seems be crashing.
What's wrong with your debugger? You seem to not be using it.
Try checking the return values of functions that could fail.
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.
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
al_register_event_source(event , al_get_timer_event_source(timer));
so you'll never get any ALLEGRO_EVENT_TIMER events either.
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.
If you use printf(), you probably want to follow it with an fflush(stdout) to make sure it's printed regardless of crashes.
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.
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().
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)); }
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"
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".
If you compiled your code with all warnings enabled, your compiler would have told you about unrecognized escape sequences. gcc -Wall FTW.