Introduced centralized keyboard handling
This commit is contained in:
parent
1bb8514a8d
commit
b2b0103b4f
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
@ -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_
|
14
src/menu.c
14
src/menu.c
|
@ -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) {
|
|
||||||
case SDLK_UP:
|
|
||||||
m->selected--;
|
m->selected--;
|
||||||
reset_buttons = true;
|
reset_buttons = true;
|
||||||
break;
|
} else if (keyboard_direction_press(DOWN, event)) {
|
||||||
case SDLK_DOWN:
|
|
||||||
m->selected++;
|
m->selected++;
|
||||||
reset_buttons = true;
|
reset_buttons = true;
|
||||||
break;
|
} else if (keyboard_press(SDLK_RETURN, event)) {
|
||||||
case SDLK_RETURN:
|
|
||||||
trigger_button = true;
|
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);
|
||||||
|
|
39
src/player.c
39
src/player.c
|
@ -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:
|
|
||||||
case SDLK_h:
|
|
||||||
case SDLK_a:
|
|
||||||
move_left(player, matrix);
|
move_left(player, matrix);
|
||||||
moved = true;
|
moved = true;
|
||||||
break;
|
}
|
||||||
case SDLK_RIGHT:
|
if (keyboard_direction_press(RIGHT, event)) {
|
||||||
case SDLK_l:
|
|
||||||
case SDLK_d:
|
|
||||||
move_right(player, matrix);
|
move_right(player, matrix);
|
||||||
moved = true;
|
moved = true;
|
||||||
break;
|
}
|
||||||
case SDLK_UP:
|
if (keyboard_direction_press(UP, event)) {
|
||||||
case SDLK_k:
|
|
||||||
case SDLK_w:
|
|
||||||
move_up(player, matrix);
|
move_up(player, matrix);
|
||||||
moved = true;
|
moved = true;
|
||||||
break;
|
}
|
||||||
case SDLK_DOWN:
|
if (keyboard_direction_press(DOWN, event)) {
|
||||||
case SDLK_j:
|
|
||||||
case SDLK_s:
|
|
||||||
move_down(player, matrix);
|
move_down(player, matrix);
|
||||||
moved = true;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue