From b2b0103b4f8a013df5018eb7533eb2d301b6dd18 Mon Sep 17 00:00:00 2001 From: Linus_Probert Date: Mon, 12 Feb 2018 10:55:36 +0100 Subject: [PATCH] Introduced centralized keyboard handling --- CMakeLists.txt | 1 + src/keyboard.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ src/keyboard.h | 20 ++++++++++++++++++ src/menu.c | 26 +++++++++--------------- src/player.c | 55 ++++++++++++++++++-------------------------------- 5 files changed, 104 insertions(+), 51 deletions(-) create mode 100644 src/keyboard.c create mode 100644 src/keyboard.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ca1cb6..5417fdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ add_executable(breakhack src/particle_engine src/menu src/collisions + src/keyboard ) target_link_libraries(breakhack diff --git a/src/keyboard.c b/src/keyboard.c new file mode 100644 index 0000000..994318e --- /dev/null +++ b/src/keyboard.c @@ -0,0 +1,53 @@ +#include "keyboard.h" +#include "util.h" + +bool +keyboard_direction_press(Direction dir, SDL_Event *event) +{ + if (event->type != SDL_KEYDOWN) + return false; + + Uint32 key = event->key.keysym.sym; + switch (dir) { + case UP: + return key == SDLK_UP + || key == SDLK_w + || key == SDLK_k; + case DOWN: + return key == SDLK_DOWN + || key == SDLK_s + || key == SDLK_j; + case LEFT: + return key == SDLK_LEFT + || key == SDLK_a + || key == SDLK_h; + case RIGHT: + return key == SDLK_RIGHT + || key == SDLK_l + || key == SDLK_d; + default: + return false; + } +} + +bool +keyboard_press(Uint32 key, SDL_Event *event) +{ + if (event->type != SDL_KEYDOWN) + return false; + + return key == (Uint32) event->key.keysym.sym; +} + +bool +keyboard_mod_press(Uint32 key, Uint32 mod, SDL_Event *event) +{ + if (event->type != SDL_KEYDOWN) + return false; + + if (!(event->key.keysym.mod & mod)) + return false; + + return key == (Uint32) event->key.keysym.sym; +} + diff --git a/src/keyboard.h b/src/keyboard.h new file mode 100644 index 0000000..77bf04e --- /dev/null +++ b/src/keyboard.h @@ -0,0 +1,20 @@ +#ifndef KEYBOARD_H_ +#define KEYBOARD_H_ + +#include +#include + +typedef enum Direction_t { + UP, DOWN, LEFT, RIGHT +} Direction; + +bool +keyboard_direction_press(Direction, SDL_Event*); + +bool +keyboard_press(Uint32 key, SDL_Event*); + +bool +keyboard_mod_press(Uint32 key, Uint32 mod, SDL_Event*); + +#endif // KEYBOARD_H_ diff --git a/src/menu.c b/src/menu.c index c5dbaca..38cdd3d 100644 --- a/src/menu.c +++ b/src/menu.c @@ -5,6 +5,7 @@ #include "sprite.h" #include "defines.h" #include "gui_button.h" +#include "keyboard.h" typedef struct MenuItems_t { Sprite *sprite; @@ -28,23 +29,16 @@ menu_handle_event(Menu *m, SDL_Event *event) bool reset_buttons = false; bool trigger_button = false; - if (event->type == SDL_KEYDOWN) { - switch (event->key.keysym.sym) { - case SDLK_UP: - m->selected--; - reset_buttons = true; - break; - case SDLK_DOWN: - m->selected++; - reset_buttons = true; - break; - case SDLK_RETURN: - trigger_button = true; - default: - break; - } - m->selected = m->selected % linkedlist_size(m->items); + if (keyboard_direction_press(UP, event)) { + m->selected--; + reset_buttons = true; + } else if (keyboard_direction_press(DOWN, event)) { + m->selected++; + reset_buttons = true; + } else if (keyboard_press(SDLK_RETURN, event)) { + trigger_button = true; } + m->selected = m->selected % linkedlist_size(m->items); if (trigger_button) { MenuItem *item = linkedlist_get(&m->items, m->selected); diff --git a/src/player.c b/src/player.c index e7feafe..963d367 100644 --- a/src/player.c +++ b/src/player.c @@ -8,6 +8,7 @@ #include "gui.h" #include "item.h" #include "particle_engine.h" +#include "keyboard.h" #define ENGINEER_STATS { 12, 12, 5, 7, 2, 1, 1 } #define MAGE_STATS { 12, 12, 5, 7, 2, 1, 1 } @@ -168,38 +169,26 @@ sip_health(Player *player) } static void -handle_movement_input(Player *player, RoomMatrix *matrix, Uint32 key) +handle_movement_input(Player *player, RoomMatrix *matrix, SDL_Event *event) { static unsigned int step = 1; bool moved = false; - switch (key) { - case SDLK_LEFT: - case SDLK_h: - case SDLK_a: - move_left(player, matrix); - moved = true; - break; - case SDLK_RIGHT: - case SDLK_l: - case SDLK_d: - move_right(player, matrix); - moved = true; - break; - case SDLK_UP: - case SDLK_k: - case SDLK_w: - move_up(player, matrix); - moved = true; - break; - case SDLK_DOWN: - case SDLK_j: - case SDLK_s: - move_down(player, matrix); - moved = true; - break; - default: - break; + if (keyboard_direction_press(LEFT, event)) { + move_left(player, matrix); + moved = true; + } + if (keyboard_direction_press(RIGHT, event)) { + move_right(player, matrix); + moved = true; + } + if (keyboard_direction_press(UP, event)) { + move_up(player, matrix); + moved = true; + } + if (keyboard_direction_press(DOWN, event)) { + move_down(player, matrix); + moved = true; } if (moved) { @@ -215,14 +204,10 @@ handle_player_input(Player *player, RoomMatrix *matrix, SDL_Event *event) if (event->type != SDL_KEYDOWN) return; - bool shift = event->key.keysym.mod & (KMOD_RSHIFT | KMOD_LSHIFT); - Uint32 key = event->key.keysym.sym; - - if (!shift) - handle_movement_input(player, matrix, key); - - if (shift && key == SDLK_h) { + if (keyboard_mod_press(SDLK_h, KMOD_SHIFT, event)) { sip_health(player); + } else { + handle_movement_input(player, matrix, event); } }