Allegro Events - Nothing happening

Started by
3 comments, last by fastcall22 11 years, 11 months ago
Hi guys, I'm new to allegro and I'm learning about event queues and stuff like that. My program compiles and runs but the event queue error pops up and I don't know why. Could you look through my code and tell me where I've gone wrong? Thanks



#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5\allegro_primitives.h>
int main(void)
{
int height = 480, width = 640;
bool done = false;
int posX = width / 2, posY = height / 2;

if (!al_init())
{
al_show_native_message_box(NULL,"Error", "Error", "Failed to initialise the Allegro library!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
if (!event_queue)
{
al_show_native_message_box(NULL, "Error", "Error", "Failed to create event queue!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
display = al_create_display(width, height);

if (!display)
{
al_show_native_message_box(display, "Error", "Error", "Failed to create display!", "Alright", ALLEGRO_MESSAGEBOX_ERROR);
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:
posY -= 10;
break;
case ALLEGRO_KEY_DOWN:
posY += 10;
break;
case ALLEGRO_KEY_LEFT:
posX -= 10;
break;
case ALLEGRO_KEY_RIGHT:
posX += 10;
break;
}
}
al_draw_filled_rectangle(posX, posY, posX = 30, posY + 30, al_map_rgb(255, 0, 255));
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}

al_destroy_display(display);
return 0;
}
Advertisement

ALLEGRO_EVENT_QUEUE *event_queue = NULL;
if (!event_queue)


You're not creating the event_queue?
Yeah, that's true. Let me try re ordering it.
Re ordered it and nothing happens, just a black allegro window.
The display is blank because al_wait_for_event waits for an event if the event queue is emtpy. In other words, it would appear to "work" if you smashed your keyboard. You should poll the event queue and process events if there are any, then render. It looks like al_get_next_event is what you're looking for.

This topic is closed to new replies.

Advertisement