84 lines
1.9 KiB
C
84 lines
1.9 KiB
C
#ifndef TWN_INPUT_C_H
|
|
#define TWN_INPUT_C_H
|
|
|
|
#include "twn_types.h"
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
#define KEYBIND_SLOTS_DEFAULT 3
|
|
|
|
|
|
union ButtonCode {
|
|
SDL_Scancode scancode;
|
|
SDL_Keycode keycode;
|
|
SDL_GameControllerButton gamepad_button;
|
|
uint8_t mouse_button; /* SDL_BUTTON_ enum */
|
|
};
|
|
|
|
|
|
typedef enum ButtonSource {
|
|
BUTTON_SOURCE_NOT_SET,
|
|
BUTTON_SOURCE_KEYBOARD_PHYSICAL,
|
|
BUTTON_SOURCE_KEYBOARD_CHARACTER,
|
|
BUTTON_SOURCE_GAMEPAD,
|
|
BUTTON_SOURCE_MOUSE,
|
|
} ButtonSource;
|
|
|
|
|
|
/* an input to which an action is bound */
|
|
/* it is not limited to literal buttons */
|
|
typedef struct Button {
|
|
enum ButtonSource source;
|
|
union ButtonCode code;
|
|
bool in_use;
|
|
} Button;
|
|
|
|
|
|
/* represents the collective state of a group of buttons */
|
|
/* that is, changes in the states of any of the bound buttons will affect it */
|
|
typedef struct Action {
|
|
size_t num_bindings;
|
|
|
|
/* if you bind more than the number set in the configuration file */
|
|
/* it forgets the first Button to add the new one at the end */
|
|
Button *bindings;
|
|
|
|
Vec2 position; /* set if applicable, e.g. mouse click */
|
|
bool is_pressed;
|
|
bool just_changed;
|
|
} Action;
|
|
|
|
|
|
typedef struct ActionHashItem {
|
|
char *key;
|
|
Action value;
|
|
} ActionHashItem;
|
|
|
|
|
|
typedef struct InputState {
|
|
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
|
|
ActionHashItem *action_hash;
|
|
Vec2 mouse_window_position;
|
|
Vec2 mouse_relative_position;
|
|
uint32_t mouse_state; /* SDL mouse button bitmask */
|
|
ButtonSource last_active_source;
|
|
bool is_anything_just_pressed;
|
|
bool mouse_captured;
|
|
} InputState;
|
|
|
|
|
|
void input_state_init(InputState *input);
|
|
|
|
void input_state_deinit(InputState *input);
|
|
|
|
void input_state_update(InputState *input);
|
|
|
|
void input_state_update_postframe(InputState *input);
|
|
|
|
void input_reset_state(InputState *input);
|
|
|
|
#endif
|