twn_input: add twn_scancode.h as port of SDL_scancode.h, remove SDL2/SDL.h header usage sa for plans on having clean public headers

This commit is contained in:
2024-10-06 23:49:05 +03:00
parent 61caec7258
commit 3e972b2dad
4 changed files with 451 additions and 22 deletions

View File

@@ -4,12 +4,11 @@
#include "twn_config.h"
#include "twn_vec.h"
#include "twn_engine_api.h"
#include <SDL2/SDL.h>
#include "twn_scancode.h"
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
typedef enum ButtonSource {
BUTTON_SOURCE_NOT_SET,
@@ -20,20 +19,8 @@ typedef enum ButtonSource {
} ButtonSource;
union ButtonCode {
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 */
typedef struct Button {
enum ButtonSource source;
union ButtonCode code;
} Button;
/* internal */
typedef struct Button Button;
/* represents the collective state of a group of buttons */
@@ -58,13 +45,15 @@ typedef struct ActionHashItem {
typedef struct InputState {
ActionHashItem *action_hash;
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
uint32_t mouse_state; /* SDL mouse button bitmask */
Vec2i mouse_window_position;
Vec2i mouse_relative_position;
ButtonSource last_active_source;
bool is_anything_just_pressed;
/* engine state */
ActionHashItem *action_hash;
} InputState;
@@ -74,10 +63,10 @@ TWN_API void input_state_update(InputState *input);
TWN_API void input_bind_action_scancode(InputState *input,
char *action_name,
SDL_Scancode scancode);
Scancode scancode);
TWN_API void input_unbind_action_scancode(InputState *input,
char *action_name,
SDL_Scancode scancode);
Scancode scancode);
TWN_API void input_bind_action_mouse(InputState *input,
char *action_name,
uint8_t mouse_button);