Sign in to follow this  

Allegro Events - Nothing happening

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


[CODE]
#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;
}
[/CODE] Edited by robbiewoods05

Share this post


Link to post
Share on other sites
The display is blank because [url=http://www.allegro.cc/manual/5/al_wait_for_event]al_wait_for_event[/url] 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 [url=http://www.allegro.cc/manual/5/al_get_next_event]al_get_next_event[/url] is what you're looking for.

Share this post


Link to post
Share on other sites

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this