0

The above is a problem I am having. I am using Visual Studio 2010, in C++, with the Allegro library for game development. I'm having an issue where my test program is always all-white, until I hit a button, in some cases. I don't really understand how to fix this, nor what's wrong. I know for a fact that it's not related to the code being used, as this never used to happen with some example code from a website; now it seems to happen all the time with this project. If someone could point me to a possible error fix, it would be much appreciated.

Below is the code:

#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h>                //Our primitive header file

int main(void)
{
    int width = 640;
    int height = 480;

    bool done = false;
    int pos_x = width / 2;
    int pos_y = height / 2;

    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;

    if(!al_init())                                      //initialize Allegro
        return -1;

    display = al_create_display(width, height);         //create our display object

    if(!display)                                        //test display object
        return -1;

    al_init_primitives_addon();
    al_install_keyboard();

    event_queue = al_create_event_queue();

    al_register_event_source(event_queue, al_get_keyboard_event_source());

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

        if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            switch(ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_UP:
                    pos_y -= 10;
                    break;
                case ALLEGRO_KEY_DOWN:
                    pos_y += 10;
                    break;
                case ALLEGRO_KEY_RIGHT:
                    pos_x += 10;
                    break;
                case ALLEGRO_KEY_LEFT:
                    pos_x -= 10;
                    break;
            }
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
                done = true;
        }

        al_draw_filled_rectangle(pos_x, pos_y, pos_x + 30, pos_y + 30, al_map_rgb(255,0,255));
        al_flip_display();
        al_clear_to_color(al_map_rgb(0,0,0));
    }

    al_destroy_event_queue(event_queue);
    al_destroy_display(display);                        //destroy our display object

    return 0;
}

As I said, it is example code from an online tutorial.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Can you give the links to download that lib u mentioned? so that i can try locally – AnandVeeramani Oct 28 '12 at 08:07
  • The library? It is Allegro: http://alleg.sourceforge.net/ I am not entirely sure that it at all has to do with Allegro, though. –  Oct 28 '12 at 08:16
  • 2
    The example above only does any drawing after blocking in al_wait_for_event. – mythagel Oct 28 '12 at 08:27
  • I'm sorry, but I'm a little bit new to C++ and I don't really know how best to go about fixing this! What would you suggest? Thanks again. –  Oct 28 '12 at 11:55
  • @devin-raposo For this very simple example, you could move the three lines starting with al_draw_filled_rectangle to the top of the while loop (before al_wait_for_event). – mythagel Oct 28 '12 at 21:36

1 Answers1

0

The quick fix is exactly what @mythagel has mentioned in the comments.

Generally with Allegro games, you will want to create a timer and have it tick at some fixed interval (e.g., 60 times per second) and attach it to your event queue. Every time it ticks, you advance your game's logic by one frame, and then redraw the graphics (if there is nothing else to do).

So instead of processing input directly during the key events, you simply mark that a key is down or up. So you may have a variable called key_left that represents moving left and set it to true in the keydown event and false in the keyup event.

Then in your timer event (ALLEGRO_EVENT_TIMER), you can check if (key_left == true) pos_x -= 10; This will cause your game to run at the same speed on every computer, since you always processing input exactly X times per second.

There are more advanced ways of handling input that don't involve a fixed frame rate, but this is the easiest approach and usually works just fine.

Matthew
  • 47,584
  • 11
  • 86
  • 98