Beginning input refactor
This commit is contained in:
@@ -7,4 +7,20 @@
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
input.c
|
||||
)
|
||||
)
|
||||
|
||||
if(DUSK_TARGET_SYSTEM STREQUAL "linux")
|
||||
target_compile_definitions(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
INPUT_SDL2=1
|
||||
INPUT_KEYBOARD=1
|
||||
INPUT_MOUSE=1
|
||||
INPUT_GAMEPAD=1
|
||||
)
|
||||
elseif(DUSK_TARGET_SYSTEM STREQUAL "psp")
|
||||
target_compile_definitions(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
INPUT_SDL=1
|
||||
INPUT_GAMEPAD=1
|
||||
)
|
||||
endif()
|
||||
@@ -13,25 +13,57 @@ input_t INPUT;
|
||||
|
||||
void inputInit(void) {
|
||||
memoryZero(&INPUT, sizeof(input_t));
|
||||
|
||||
INPUT.binds[INPUT_BIND_UP].keyboard[0] = SDL_SCANCODE_W;
|
||||
INPUT.binds[INPUT_BIND_UP].keyboard[1] = SDL_SCANCODE_UP;
|
||||
INPUT.binds[INPUT_BIND_CONSOLE].keyboard[0] = SDL_SCANCODE_GRAVE;
|
||||
}
|
||||
|
||||
void inputUpdate(void) {
|
||||
INPUT.previous = INPUT.current;
|
||||
INPUT.current = inputStateGet();
|
||||
#if INPUT_SDL2 == 1
|
||||
const uint8_t *keyboardState = SDL_GetKeyboardState(NULL);
|
||||
#endif
|
||||
|
||||
// For each input bind...
|
||||
inputbinddata_t *data = INPUT.binds;
|
||||
do {
|
||||
data->lastValue = data->currentValue;
|
||||
data->currentValue = 0.0f;
|
||||
|
||||
// Handle keyboard
|
||||
#if INPUT_KEYBOARD == 1
|
||||
for(uint32_t i = 0; i < INPUT_BIND_KEYBOARD_BUTTONS_MAX; i++) {
|
||||
if(data->keyboard[i] == 0) break;
|
||||
|
||||
#if INPUT_SDL2 == 1
|
||||
if(keyboardState[data->keyboard[i]]) {
|
||||
data->currentValue = 1.0f;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
data++;
|
||||
} while(data < INPUT.binds + INPUT_BIND_COUNT);
|
||||
}
|
||||
|
||||
inputstate_t inputStateGet(void) {
|
||||
return 0;
|
||||
float_t inputGetCurrentValue(const inputbind_t bind) {
|
||||
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
|
||||
return INPUT.binds[bind].currentValue;
|
||||
}
|
||||
|
||||
float_t inputGetLast(const inputbind_t bind) {
|
||||
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
|
||||
return INPUT.binds[bind].lastValue;
|
||||
}
|
||||
|
||||
bool_t inputIsDown(const inputbind_t bind) {
|
||||
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
|
||||
return (INPUT.current & bind) != 0;
|
||||
return inputGetCurrentValue(bind) > 0.0f;
|
||||
}
|
||||
|
||||
bool_t inputWasDown(const inputbind_t bind) {
|
||||
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
|
||||
return (INPUT.previous & bind) != 0;
|
||||
return inputGetLast(bind) > 0.0f;
|
||||
}
|
||||
|
||||
bool_t inputPressed(const inputbind_t bind) {
|
||||
|
||||
@@ -8,23 +8,60 @@
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef uint8_t inputbind_t;
|
||||
typedef inputbind_t inputstate_t;
|
||||
#if INPUT_SDL2 == 1
|
||||
#include <SDL2/SDL.h>
|
||||
#else
|
||||
#error "No input backend defined"
|
||||
#endif
|
||||
|
||||
#define INPUT_BIND_UP (1 << 0)
|
||||
#define INPUT_BIND_DOWN (1 << 1)
|
||||
#define INPUT_BIND_LEFT (1 << 2)
|
||||
#define INPUT_BIND_RIGHT (1 << 3)
|
||||
#define INPUT_BIND_ACTION (1 << 4)
|
||||
#define INPUT_BIND_CANCEL (1 << 5)
|
||||
#define INPUT_BIND_CONSOLE (1 << 6)
|
||||
#define INPUT_BIND_QUIT (1 << 7)
|
||||
// Keyboard defs
|
||||
#if INPUT_KEYBOARD == 1
|
||||
#define INPUT_BIND_KEYBOARD_BUTTONS_MAX 32
|
||||
|
||||
#define INPUT_BIND_COUNT (INPUT_BIND_QUIT + 1)
|
||||
#if INPUT_SDL2 == 1
|
||||
typedef SDL_Scancode inputscancode_t;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Gamepad defs
|
||||
#if INPUT_GAMEPAD == 1
|
||||
#define INPUT_GAMEPAD_BUTTON_NAME_MAX 8
|
||||
#define INPUT_BIND_GAMEPAD_BUTTONS_MAX 8
|
||||
|
||||
#if INPUT_SDL2 == 1
|
||||
typedef SDL_GameControllerButton inputgamepadbutton_t;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
INPUT_BIND_UP,
|
||||
INPUT_BIND_DOWN,
|
||||
INPUT_BIND_LEFT,
|
||||
INPUT_BIND_RIGHT,
|
||||
INPUT_BIND_ACCEPT,
|
||||
INPUT_BIND_CANCEL,
|
||||
INPUT_BIND_CONSOLE,
|
||||
|
||||
INPUT_BIND_COUNT
|
||||
} inputbind_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t current;
|
||||
uint8_t previous;
|
||||
inputbind_t bind;
|
||||
float_t lastValue;
|
||||
float_t currentValue;
|
||||
|
||||
#if INPUT_GAMEPAD == 1
|
||||
inputgamepadbutton_t gamepad[INPUT_BIND_GAMEPAD_BUTTONS_MAX];
|
||||
uint8_t gamepadButtonCount;
|
||||
#endif
|
||||
|
||||
#if INPUT_KEYBOARD == 1
|
||||
inputscancode_t keyboard[INPUT_BIND_KEYBOARD_BUTTONS_MAX];
|
||||
#endif
|
||||
} inputbinddata_t;
|
||||
|
||||
typedef struct {
|
||||
inputbinddata_t binds[INPUT_BIND_COUNT];
|
||||
} input_t;
|
||||
|
||||
extern input_t INPUT;
|
||||
@@ -40,11 +77,20 @@ void inputInit(void);
|
||||
void inputUpdate(void);
|
||||
|
||||
/**
|
||||
* Gets the current input state as a bitmask.
|
||||
* Gets the current value of a specific input bind.
|
||||
*
|
||||
* @return The current input state as a bitmask.
|
||||
* @param bind The input bind to get the value for.
|
||||
* @return The current value of the bind (0.0f to 1.0f).
|
||||
*/
|
||||
inputstate_t inputStateGet(void);
|
||||
float_t inputGetCurrentValue(const inputbind_t bind);
|
||||
|
||||
/**
|
||||
* Gets the last value of a specific input bind.
|
||||
*
|
||||
* @param bind The input bind to get the value for.
|
||||
* @return The last value of the bind (0.0f to 1.0f).
|
||||
*/
|
||||
float_t inputGetLast(const inputbind_t bind);
|
||||
|
||||
/**
|
||||
* Checks if a specific input bind is currently pressed.
|
||||
|
||||
Reference in New Issue
Block a user