93 lines
2.7 KiB
C
93 lines
2.7 KiB
C
|
#ifndef INPUT_H
|
||
|
#define INPUT_H
|
||
|
|
||
|
#include "config.h"
|
||
|
#include "util.h"
|
||
|
|
||
|
#include <SDL2/SDL.h>
|
||
|
|
||
|
#include <stdint.h>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
|
||
|
enum button_source {
|
||
|
BUTTON_SOURCE_NOT_SET,
|
||
|
BUTTON_SOURCE_KEYBOARD_PHYSICAL,
|
||
|
BUTTON_SOURCE_KEYBOARD_CHARACTER,
|
||
|
BUTTON_SOURCE_GAMEPAD,
|
||
|
BUTTON_SOURCE_MOUSE,
|
||
|
};
|
||
|
|
||
|
|
||
|
union button_code {
|
||
|
SDL_Scancode scancode;
|
||
|
SDL_Keycode keycode;
|
||
|
SDL_GameControllerButton gamepad_button;
|
||
|
uint8_t mouse_button; /* SDL_BUTTON_ enum */
|
||
|
};
|
||
|
|
||
|
|
||
|
/* an input to which an action is bound */
|
||
|
/* it is not limited to literal buttons */
|
||
|
struct button {
|
||
|
enum button_source source;
|
||
|
union button_code code;
|
||
|
};
|
||
|
|
||
|
|
||
|
/* represents the collective state of a group of buttons */
|
||
|
/* that is, changes in the states of any of the bound buttons will affect it */
|
||
|
struct action {
|
||
|
size_t num_bindings;
|
||
|
struct button bindings[NUM_KEYBIND_SLOTS];
|
||
|
t_fvec2 position; /* set if applicable */
|
||
|
bool is_pressed;
|
||
|
bool just_changed;
|
||
|
};
|
||
|
|
||
|
|
||
|
struct action_hash_item {
|
||
|
char *key;
|
||
|
struct action value;
|
||
|
};
|
||
|
|
||
|
|
||
|
struct input_state {
|
||
|
struct action_hash_item *action_hash;
|
||
|
SDL_Renderer *renderer; /* some input relates to the screen in some way */
|
||
|
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
|
||
|
uint32_t mouse_state; /* SDL mouse button bitmask */
|
||
|
t_vec2 mouse_window_position;
|
||
|
enum button_source last_active_source;
|
||
|
bool is_anything_just_pressed;
|
||
|
};
|
||
|
|
||
|
|
||
|
void input_state_init(struct input_state *input, SDL_Renderer *renderer);
|
||
|
void input_state_deinit(struct input_state *input);
|
||
|
void input_state_update(struct input_state *input);
|
||
|
|
||
|
void input_bind_action_scancode(struct input_state *input,
|
||
|
char *action_name,
|
||
|
SDL_Scancode scancode);
|
||
|
void input_unbind_action_scancode(struct input_state *input,
|
||
|
char *action_name,
|
||
|
SDL_Scancode scancode);
|
||
|
void input_bind_action_mouse(struct input_state *input,
|
||
|
char *action_name,
|
||
|
uint8_t mouse_button);
|
||
|
void input_unbind_action_mouse(struct input_state *input,
|
||
|
char *action_name,
|
||
|
uint8_t mouse_button);
|
||
|
|
||
|
void input_add_action(struct input_state *input, char *action_name);
|
||
|
void input_delete_action(struct input_state *input, char *action_name);
|
||
|
|
||
|
bool input_is_action_pressed(struct input_state *input, char *action_name);
|
||
|
bool input_is_action_just_pressed(struct input_state *input, char *action_name);
|
||
|
bool input_is_action_just_released(struct input_state *input, char *action_name);
|
||
|
t_fvec2 input_get_action_position(struct input_state *input, char *action_name);
|
||
|
|
||
|
|
||
|
#endif
|