2018-03-25 12:53:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
|
|
|
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "event_manager.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
# include <stdio.h>
|
|
|
|
#endif
|
2018-03-25 13:22:43 +00:00
|
|
|
#include <stddef.h>
|
2018-03-25 15:47:31 +00:00
|
|
|
#include <assert.h>
|
2018-03-25 12:53:40 +00:00
|
|
|
#include <allegro5/allegro.h>
|
|
|
|
|
|
|
|
static ALLEGRO_EVENT_QUEUE *event_queue;
|
|
|
|
static ALLEGRO_TIMER *timer;
|
2018-03-25 13:22:43 +00:00
|
|
|
static int keys[KEY_MAX];
|
2018-03-25 12:53:40 +00:00
|
|
|
|
2018-03-25 15:14:44 +00:00
|
|
|
int evnt_mngr_init(ALLEGRO_DISPLAY *display) {
|
2018-03-25 15:47:31 +00:00
|
|
|
assert(display);
|
2018-03-25 13:22:43 +00:00
|
|
|
if(!al_install_keyboard())
|
|
|
|
return 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts("Initialized keyboard.");
|
|
|
|
#endif
|
|
|
|
|
2018-03-25 12:53:40 +00:00
|
|
|
timer = al_create_timer(1.0f / FPS);
|
|
|
|
if(!timer)
|
|
|
|
return 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts("Initialized timer.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
event_queue = al_create_event_queue();
|
|
|
|
if(!event_queue)
|
2018-03-25 13:22:43 +00:00
|
|
|
{
|
|
|
|
al_destroy_timer(timer);
|
2018-03-25 12:53:40 +00:00
|
|
|
return 0;
|
2018-03-25 13:22:43 +00:00
|
|
|
}
|
2018-03-25 12:53:40 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
puts("Initialized event queue.");
|
|
|
|
#endif
|
|
|
|
al_register_event_source(event_queue,
|
|
|
|
al_get_display_event_source(display));
|
|
|
|
al_register_event_source(event_queue,
|
|
|
|
al_get_timer_event_source(timer));
|
2018-03-25 15:57:21 +00:00
|
|
|
al_register_event_source(event_queue,
|
|
|
|
al_get_keyboard_event_source());
|
2018-03-25 12:53:40 +00:00
|
|
|
al_start_timer(timer);
|
2018-03-25 13:22:43 +00:00
|
|
|
|
|
|
|
// set all keys to false at initialization
|
|
|
|
for(size_t i = 0; i < KEY_MAX; ++i)
|
|
|
|
keys[i] = 0;
|
|
|
|
|
2018-03-25 12:53:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void evnt_mngr_deinit() {
|
|
|
|
al_destroy_timer(timer);
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts("Destroyed timer.");
|
|
|
|
#endif
|
|
|
|
al_destroy_event_queue(event_queue);
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts("Destroyed event queue.");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-25 13:22:43 +00:00
|
|
|
void set_key(int keycode, int value) {
|
|
|
|
switch(keycode)
|
|
|
|
{
|
|
|
|
case ALLEGRO_KEY_UP:
|
2018-03-25 15:57:21 +00:00
|
|
|
keys[KEY_UP] = value;
|
2018-03-25 13:22:43 +00:00
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_DOWN:
|
2018-03-25 15:57:21 +00:00
|
|
|
keys[KEY_DOWN] = value;
|
2018-03-25 13:22:43 +00:00
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_LEFT:
|
2018-03-25 15:57:21 +00:00
|
|
|
keys[KEY_LEFT] = value;
|
2018-03-25 13:22:43 +00:00
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_RIGHT:
|
2018-03-25 15:57:21 +00:00
|
|
|
keys[KEY_RIGHT] = value;
|
2018-03-25 13:22:43 +00:00
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_R:
|
2018-03-25 15:57:21 +00:00
|
|
|
keys[KEY_RESET] = value;
|
2018-03-25 13:22:43 +00:00
|
|
|
break;
|
|
|
|
case ALLEGRO_KEY_F:
|
2018-03-25 15:57:21 +00:00
|
|
|
keys[KEY_FULLSCREEN] = value;
|
2018-03-25 13:22:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:53:40 +00:00
|
|
|
void handle_event() {
|
|
|
|
ALLEGRO_EVENT evnt;
|
|
|
|
al_wait_for_event(event_queue, &evnt);
|
|
|
|
|
2018-03-25 13:22:43 +00:00
|
|
|
switch(evnt.type)
|
|
|
|
{
|
|
|
|
case ALLEGRO_EVENT_TIMER:
|
|
|
|
redraw = 1;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_EVENT_DISPLAY_CLOSE:
|
|
|
|
run = 0;
|
|
|
|
break;
|
|
|
|
case ALLEGRO_EVENT_KEY_DOWN:
|
2018-03-25 15:04:14 +00:00
|
|
|
if(evnt.keyboard.keycode == ALLEGRO_KEY_H)
|
|
|
|
show_help = !show_help;
|
|
|
|
else if(evnt.keyboard.keycode == ALLEGRO_KEY_I)
|
|
|
|
show_info = !show_info;
|
|
|
|
else
|
|
|
|
set_key(evnt.keyboard.keycode, 1);
|
2018-03-25 13:22:43 +00:00
|
|
|
break;
|
|
|
|
case ALLEGRO_EVENT_KEY_UP:
|
|
|
|
set_key(evnt.keyboard.keycode, 0);
|
|
|
|
break;
|
|
|
|
}
|
2018-03-25 12:53:40 +00:00
|
|
|
}
|
2018-03-25 13:29:48 +00:00
|
|
|
|
|
|
|
int key_is_down(int code) {
|
2018-03-25 15:47:31 +00:00
|
|
|
assert(code >= 0 && code <= KEY_MAX);
|
|
|
|
return keys[code];
|
2018-03-25 13:29:48 +00:00
|
|
|
}
|