Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Loop doesn't work.

This thread is locked; no one can reply to it. rss feed Print
Loop doesn't work.
SaSSolino
Member #16,806
February 2018

Hi guys, I'm trying to create a simple tic tac toe game. The inputs are supposed to come from the keyboard, but I'm having issues making it work as I always seem to land on event.type = ALLEGRO_EVENT_TIMER.

What am I doing wrong?

Here's my events related code:

ALLEGRO_TIMER *timer = NULL;
ALLEGRO_EVENT_QUEUE *event_q = NULL;

al_install_keyboard();

timer = al_create_timer(0.04);
if (!timer) {
fprintf(stderr, "impossibile creare il timer\n");
return -1;
}

event_q = al_create_event_queue();
if (!event_q) {
fprintf(stderr, "impossibile avviare event queue\n");
return -1;
}

al_register_event_source(event_q, al_get_keyboard_event_source());
al_register_event_source(event_q, al_get_timer_event_source(timer));

al_start_timer(timer);

loop

ALLEGRO_EVENT event;

al_wait_for_event(event_q, &event);

if (event.type = ALLEGRO_EVENT_TIMER) {
redraw = true;
}
else if (event.type = ALLEGRO_EVENT_KEY_DOWN) {
switch (event.keyboard.keycode) {
case ALLEGRO_KEY_Q:
things
break;
etc
}
}

if (redraw == true && al_is_event_queue_empty(event_q)) {
redraw = false;
} else if (redraw == false) {
more things
}

loop ends

al_destroy_timer(timer);
al_destroy_event_queue(event_q);

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

SaSSolino said:

if (event.type = ALLEGRO_EVENT_TIMER) {
redraw = true;
}
else if (event.type = ALLEGRO_EVENT_KEY_DOWN) {

See anything wrong with that code? You're performing assignment, and then the result gets cast to bool, which is probably always true. That's why your event.type is never ALLEGRO_EVENT_KEY_DOWN. You need to test for equivalence with the == operator.

SaSSolino
Member #16,806
February 2018

Oh god, I'm quite embarrassed about this one. Thanks!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

SaSSolino
Member #16,806
February 2018

Solved this, but now I have another issue that I recreated the events to solve but apparently it's bugged still.

Basically when I touch a key, it triggers multiple times, as if I was pressing up to move forward for example. I just want single key presses though, how do I do that?

EDIT: nvm got it

Go to: