TheNextGuy
TheNextGuy

Reputation: 21

How do check for keyboard input while continuing to update and draw game items

I am using Allegro and C++ to develop a basic game. What I found is that the common answer for taking in keyboard input is the line

al_wait_for_event(event_queue, &ev);

however, what I don't like about it is that it causes your program to wait for input. If the position of a meteor needs to be continuously updated, then I don't want my keyboard listening to stop its movement.

In my experience with XNA, I would simply update what the keyboard looked like each tick and ask if a key was pressed. What method might I use to continue updating and drawing my game while still checking input similar to how XNA handled it?

Upvotes: 1

Views: 809

Answers (3)

Matthew
Matthew

Reputation: 48304

Most people set up an event loop controlled by a timer:

ALLEGRO_TIMER *timer = al_create_timer(1 / 60.0);
al_register_event_source(event_queue, al_get_timer_event_source(timer));
// register keyboard, other sources

Now when you wait for an event, you'll get a timer tick every 1/60th of a second, which you can react to accordingly and update a frame of logic.

Upvotes: 1

Rick Yorgason
Rick Yorgason

Reputation: 1666

Instead of al_wait_for_event, you should be using al_get_next_event in a loop until it returns false. It returns immediately, and returns false once there's no more events. (Also look at al_is_event_queue_empty and al_peek_next_event, which you might find useful.)

Once you've run out of events, update your game logic, draw the frame, and start all over again. (This is formally knows as the "game loop".)

Upvotes: 1

ComicSansMS
ComicSansMS

Reputation: 54737

Allegro's al_get_keyboard_state does just that: It returns the current state of the keyboard at the time of the call.

Upvotes: 0

Related Questions