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/menu
src/collisions
src/keyboard
)
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 "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:
if (keyboard_direction_press(UP, event)) {
m->selected--;
reset_buttons = true;
break;
case SDLK_DOWN:
} else if (keyboard_direction_press(DOWN, event)) {
m->selected++;
reset_buttons = true;
break;
case SDLK_RETURN:
} else if (keyboard_press(SDLK_RETURN, event)) {
trigger_button = true;
default:
break;
}
m->selected = m->selected % linkedlist_size(m->items);
}
if (trigger_button) {
MenuItem *item = linkedlist_get(&m->items, m->selected);

View File

@ -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:
if (keyboard_direction_press(LEFT, event)) {
move_left(player, matrix);
moved = true;
break;
case SDLK_RIGHT:
case SDLK_l:
case SDLK_d:
}
if (keyboard_direction_press(RIGHT, event)) {
move_right(player, matrix);
moved = true;
break;
case SDLK_UP:
case SDLK_k:
case SDLK_w:
}
if (keyboard_direction_press(UP, event)) {
move_up(player, matrix);
moved = true;
break;
case SDLK_DOWN:
case SDLK_j:
case SDLK_s:
}
if (keyboard_direction_press(DOWN, event)) {
move_down(player, matrix);
moved = true;
break;
default:
break;
}
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);
}
}