Introduced centralized keyboard handling

This commit is contained in:
Linus_Probert 2018-02-12 10:55:36 +01:00
parent 1bb8514a8d
commit b2b0103b4f
5 changed files with 104 additions and 51 deletions

View File

@ -73,6 +73,7 @@ add_executable(breakhack
src/particle_engine src/particle_engine
src/menu src/menu
src/collisions src/collisions
src/keyboard
) )
target_link_libraries(breakhack target_link_libraries(breakhack

53
src/keyboard.c Normal file
View File

@ -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;
}

20
src/keyboard.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef KEYBOARD_H_
#define KEYBOARD_H_
#include <SDL2/SDL.h>
#include <stdbool.h>
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_

View File

@ -5,6 +5,7 @@
#include "sprite.h" #include "sprite.h"
#include "defines.h" #include "defines.h"
#include "gui_button.h" #include "gui_button.h"
#include "keyboard.h"
typedef struct MenuItems_t { typedef struct MenuItems_t {
Sprite *sprite; Sprite *sprite;
@ -28,23 +29,16 @@ menu_handle_event(Menu *m, SDL_Event *event)
bool reset_buttons = false; bool reset_buttons = false;
bool trigger_button = false; bool trigger_button = false;
if (event->type == SDL_KEYDOWN) { if (keyboard_direction_press(UP, event)) {
switch (event->key.keysym.sym) { m->selected--;
case SDLK_UP: reset_buttons = true;
m->selected--; } else if (keyboard_direction_press(DOWN, event)) {
reset_buttons = true; m->selected++;
break; reset_buttons = true;
case SDLK_DOWN: } else if (keyboard_press(SDLK_RETURN, event)) {
m->selected++; trigger_button = true;
reset_buttons = true;
break;
case SDLK_RETURN:
trigger_button = true;
default:
break;
}
m->selected = m->selected % linkedlist_size(m->items);
} }
m->selected = m->selected % linkedlist_size(m->items);
if (trigger_button) { if (trigger_button) {
MenuItem *item = linkedlist_get(&m->items, m->selected); MenuItem *item = linkedlist_get(&m->items, m->selected);

View File

@ -8,6 +8,7 @@
#include "gui.h" #include "gui.h"
#include "item.h" #include "item.h"
#include "particle_engine.h" #include "particle_engine.h"
#include "keyboard.h"
#define ENGINEER_STATS { 12, 12, 5, 7, 2, 1, 1 } #define ENGINEER_STATS { 12, 12, 5, 7, 2, 1, 1 }
#define MAGE_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 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; static unsigned int step = 1;
bool moved = false; bool moved = false;
switch (key) { if (keyboard_direction_press(LEFT, event)) {
case SDLK_LEFT: move_left(player, matrix);
case SDLK_h: moved = true;
case SDLK_a: }
move_left(player, matrix); if (keyboard_direction_press(RIGHT, event)) {
moved = true; move_right(player, matrix);
break; moved = true;
case SDLK_RIGHT: }
case SDLK_l: if (keyboard_direction_press(UP, event)) {
case SDLK_d: move_up(player, matrix);
move_right(player, matrix); moved = true;
moved = true; }
break; if (keyboard_direction_press(DOWN, event)) {
case SDLK_UP: move_down(player, matrix);
case SDLK_k: moved = true;
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 (moved) { if (moved) {
@ -215,14 +204,10 @@ handle_player_input(Player *player, RoomMatrix *matrix, SDL_Event *event)
if (event->type != SDL_KEYDOWN) if (event->type != SDL_KEYDOWN)
return; return;
bool shift = event->key.keysym.mod & (KMOD_RSHIFT | KMOD_LSHIFT); if (keyboard_mod_press(SDLK_h, KMOD_SHIFT, event)) {
Uint32 key = event->key.keysym.sym;
if (!shift)
handle_movement_input(player, matrix, key);
if (shift && key == SDLK_h) {
sip_health(player); sip_health(player);
} else {
handle_movement_input(player, matrix, event);
} }
} }