Keyboard input added.
This commit is contained in:
parent
d57da20ec7
commit
ea4f4a97b6
@ -22,12 +22,20 @@
|
||||
#ifdef DEBUG
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#include <allegro5/allegro.h>
|
||||
|
||||
static ALLEGRO_EVENT_QUEUE *event_queue;
|
||||
static ALLEGRO_TIMER *timer;
|
||||
static int keys[KEY_MAX];
|
||||
|
||||
int evnt_mngr_init() {
|
||||
if(!al_install_keyboard())
|
||||
return 0;
|
||||
#ifdef DEBUG
|
||||
puts("Initialized keyboard.");
|
||||
#endif
|
||||
|
||||
timer = al_create_timer(1.0f / FPS);
|
||||
if(!timer)
|
||||
return 0;
|
||||
@ -37,7 +45,10 @@ int evnt_mngr_init() {
|
||||
|
||||
event_queue = al_create_event_queue();
|
||||
if(!event_queue)
|
||||
{
|
||||
al_destroy_timer(timer);
|
||||
return 0;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
puts("Initialized event queue.");
|
||||
#endif
|
||||
@ -46,6 +57,11 @@ int evnt_mngr_init() {
|
||||
al_register_event_source(event_queue,
|
||||
al_get_timer_event_source(timer));
|
||||
al_start_timer(timer);
|
||||
|
||||
// set all keys to false at initialization
|
||||
for(size_t i = 0; i < KEY_MAX; ++i)
|
||||
keys[i] = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -60,12 +76,59 @@ void evnt_mngr_deinit() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_key(int keycode, int value) {
|
||||
int key_index = -1;
|
||||
switch(keycode)
|
||||
{
|
||||
case ALLEGRO_KEY_UP:
|
||||
key_index = KEY_UP;
|
||||
break;
|
||||
case ALLEGRO_KEY_DOWN:
|
||||
key_index = KEY_DOWN;
|
||||
break;
|
||||
case ALLEGRO_KEY_LEFT:
|
||||
key_index = KEY_LEFT;
|
||||
break;
|
||||
case ALLEGRO_KEY_RIGHT:
|
||||
key_index = KEY_RIGHT;
|
||||
break;
|
||||
case ALLEGRO_KEY_R:
|
||||
key_index = KEY_RESET;
|
||||
break;
|
||||
case ALLEGRO_KEY_H:
|
||||
key_index = KEY_HELP;
|
||||
break;
|
||||
case ALLEGRO_KEY_I:
|
||||
key_index = KEY_INFO;
|
||||
break;
|
||||
case ALLEGRO_KEY_F:
|
||||
key_index = KEY_FULLSCREEN;
|
||||
break;
|
||||
default:
|
||||
key_index = -1;
|
||||
break;
|
||||
}
|
||||
if(key_index != -1)
|
||||
keys[key_index] = value;
|
||||
}
|
||||
|
||||
void handle_event() {
|
||||
ALLEGRO_EVENT evnt;
|
||||
al_wait_for_event(event_queue, &evnt);
|
||||
|
||||
if(evnt.type == ALLEGRO_EVENT_TIMER)
|
||||
redraw = 1;
|
||||
else if(evnt.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
|
||||
run = 0;
|
||||
switch(evnt.type)
|
||||
{
|
||||
case ALLEGRO_EVENT_TIMER:
|
||||
redraw = 1;
|
||||
break;
|
||||
case ALLEGRO_EVENT_DISPLAY_CLOSE:
|
||||
run = 0;
|
||||
break;
|
||||
case ALLEGRO_EVENT_KEY_DOWN:
|
||||
set_key(evnt.keyboard.keycode, 1);
|
||||
break;
|
||||
case ALLEGRO_EVENT_KEY_UP:
|
||||
set_key(evnt.keyboard.keycode, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,18 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
enum {
|
||||
KEY_UP = 0x0,
|
||||
KEY_DOWN = 0x1,
|
||||
KEY_LEFT = 0x2,
|
||||
KEY_RIGHT = 0x3,
|
||||
KEY_RESET = 0x4,
|
||||
KEY_HELP = 0x5,
|
||||
KEY_INFO = 0x6,
|
||||
KEY_FULLSCREEN = 0x7,
|
||||
KEY_MAX = 0x8
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Initialize the event handler.
|
||||
*
|
||||
@ -35,3 +47,12 @@ void evnt_mngr_deinit();
|
||||
* queue is empty).
|
||||
*/
|
||||
void handle_event();
|
||||
|
||||
/**
|
||||
* @brief Receive state of a key.
|
||||
*
|
||||
* @param code The key code.
|
||||
*
|
||||
* @return If down 1 will be returned, else 0.
|
||||
*/
|
||||
int key_is_down(int code);
|
||||
|
@ -53,11 +53,16 @@ int main() {
|
||||
while(run)
|
||||
{
|
||||
handle_event();
|
||||
// TODO: run simulation physics
|
||||
|
||||
// only redraw if the timer event has occurred
|
||||
// only redraw or run simulation if the timer event has occurred
|
||||
if(redraw)
|
||||
{
|
||||
/*
|
||||
* We only run the simulation when the timer goes off so it's
|
||||
* running at a consistent rate, rather than dependent on random
|
||||
* events.
|
||||
*/
|
||||
// TODO: run simulation physics
|
||||
al_clear_to_color(al_map_rgb(0, 0, 0));
|
||||
// TODO: run simulation draw functions
|
||||
al_flip_display();
|
||||
|
Loading…
Reference in New Issue
Block a user