From 5c4537b2fae2f6878b863aaf884b7d8bcecaa592 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 7 Mar 2026 22:11:11 -0600 Subject: [PATCH] input prog --- cmake/targets/linux.cmake | 3 ++ cmake/targets/psp.cmake | 1 + src/dusk/input/input.h | 25 ++------- src/dusk/input/inputbutton.h | 81 +++++++++++------------------- src/dusksdl2/CMakeLists.txt | 3 +- src/dusksdl2/input/CMakeLists.txt | 12 +++++ src/dusksdl2/input/inputplatform.h | 26 ++++++++++ src/dusksdl2/input/inputsdl2.c | 59 ++++++++++++++++++++++ src/dusksdl2/input/inputsdl2.h | 54 ++++++++++++++++++++ 9 files changed, 189 insertions(+), 75 deletions(-) create mode 100644 src/dusksdl2/input/CMakeLists.txt create mode 100644 src/dusksdl2/input/inputplatform.h create mode 100644 src/dusksdl2/input/inputsdl2.c create mode 100644 src/dusksdl2/input/inputsdl2.h diff --git a/cmake/targets/linux.cmake b/cmake/targets/linux.cmake index 830edaf..346a797 100644 --- a/cmake/targets/linux.cmake +++ b/cmake/targets/linux.cmake @@ -18,4 +18,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC DUSK_DISPLAY_WIDTH_DEFAULT=640 DUSK_DISPLAY_HEIGHT_DEFAULT=480 DUSK_DISPLAY_SCREEN_HEIGHT=240 + DUSK_INPUT_KEYBOARD + DUSK_INPUT_POINTER + DUSK_INPUT_GAMEPAD ) \ No newline at end of file diff --git a/cmake/targets/psp.cmake b/cmake/targets/psp.cmake index e67e085..9c5c5ca 100644 --- a/cmake/targets/psp.cmake +++ b/cmake/targets/psp.cmake @@ -22,6 +22,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC DUSK_SDL2 DUSK_OPENGL DUSK_PSP + DUSK_INPUT_GAMEPAD ) # Postbuild, create .pbp file for PSP. diff --git a/src/dusk/input/input.h b/src/dusk/input/input.h index 4472404..3201aea 100644 --- a/src/dusk/input/input.h +++ b/src/dusk/input/input.h @@ -30,30 +30,11 @@ typedef struct { eventlistener_t releasedListeners[INPUT_LISTENER_RELEASED_MAX]; event_t eventReleased; - #if INPUT_GAMEPAD == 1 + inputplatform_t platform; + + #ifdef DUSK_INPUT_GAMEPAD float_t deadzone; #endif - - #if INPUT_SDL2 == 1 - #if INPUT_GAMEPAD == 1 - SDL_GameController *controller; - #endif - - #if INPUT_KEYBOARD == 1 - const uint8_t *keyboardState; - #endif - - #if INPUT_POINTER == 1 - #if INPUT_SDL2 == 1 - float_t mouseX, mouseY; - #endif - #endif - - #elif DOLPHIN - int padState[INPUT_PAD_COUNT]; - float_t pads[INPUT_PAD_COUNT][INPUT_GAMEPAD_AXIS_COUNT]; - - #endif } input_t; extern input_t INPUT; diff --git a/src/dusk/input/inputbutton.h b/src/dusk/input/inputbutton.h index ec2bfa8..470fdfe 100644 --- a/src/dusk/input/inputbutton.h +++ b/src/dusk/input/inputbutton.h @@ -7,77 +7,55 @@ #pragma once #include "inputaction.h" +#include "input/inputplatform.h" -#if INPUT_SDL2 == 1 - #include -#elif DOLPHIN - -#else - #error "No input backend defined" +#ifndef inputButtonGetValuePlatform + #error "inputButtonGetValuePlatform is not defined" #endif -// Keyboard defs -#if INPUT_KEYBOARD == 1 - #if INPUT_SDL2 == 1 - typedef SDL_Scancode inputscancode_t; - #endif +#ifdef DUSK_INPUT_KEYBOARD + typedef inputscancodeplatform_t inputscancode_t; #endif -// Gamepad defs -#if INPUT_GAMEPAD == 1 - #if INPUT_SDL2 == 1 - typedef SDL_GameControllerButton inputgamepadbutton_t; - typedef SDL_GameControllerAxis inputgamepadaxis_t; - typedef enum { - INPUT_POINTER_AXIS_X, - INPUT_POINTER_AXIS_Y, - INPUT_POINTER_AXIS_Z, - INPUT_POINTER_AXIS_WHEEL_X, - INPUT_POINTER_AXIS_WHEEL_Y, - } inputpointeraxis_t; - #elif DOLPHIN == 1 - typedef u16 inputgamepadbutton_t; - typedef enum { - INPUT_GAMEPAD_AXIS_LEFT_X, - INPUT_GAMEPAD_AXIS_LEFT_Y, - INPUT_GAMEPAD_AXIS_C_X, - INPUT_GAMEPAD_AXIS_C_Y, - INPUT_GAMEPAD_AXIS_TRIGGER_LEFT, - INPUT_GAMEPAD_AXIS_TRIGGER_RIGHT, - - INPUT_GAMEPAD_AXIS_COUNT - } inputgamepadaxis_t; - #endif +#ifdef DUSK_INPUT_GAMEPAD + typedef inputgamepadbuttonplatform_t inputgamepadbutton_t; + typedef inputgamepadaxissplatform_t inputgamepadaxis_t; #endif -typedef enum { +#ifdef DUSK_INPUT_POINTER + typedef inputpointeraxisplatform_t inputpointeraxis_t; +#endif + +typedef enum inputbuttontype_e { INPUT_BUTTON_TYPE_NONE, - - #if INPUT_KEYBOARD == 1 + + #ifdef DUSK_INPUT_KEYBOARD INPUT_BUTTON_TYPE_KEYBOARD, #endif - #if INPUT_POINTER == 1 + #ifdef DUSK_INPUT_POINTER INPUT_BUTTON_TYPE_POINTER, #endif - #if INPUT_TOUCH == 1 + #ifdef DUSK_INPUT_TOUCH INPUT_BUTTON_TYPE_TOUCH, #endif - - #if INPUT_GAMEPAD == 1 + + #ifdef DUSK_INPUT_GAMEPAD INPUT_BUTTON_TYPE_GAMEPAD, INPUT_BUTTON_TYPE_GAMEPAD_AXIS, #endif - - INPUT_BUTTON_TYPE_COUNT } inputbuttontype_t; -typedef struct { +typedef struct inputbutton_s { inputbuttontype_t type; union { - #if INPUT_GAMEPAD == 1 + #ifdef DUSK_INPUT_KEYBOARD + inputscancode_t scancode; + #endif + + #ifdef DUSK_INPUT_GAMEPAD inputgamepadbutton_t gpButton; struct { inputgamepadaxis_t axis; @@ -85,13 +63,12 @@ typedef struct { } gpAxis; #endif - #if INPUT_KEYBOARD == 1 - inputscancode_t scancode; - #endif - - #if INPUT_POINTER == 1 + #ifdef DUSK_INPUT_POINTER inputpointeraxis_t pointerAxis; #endif + + // Unusued incase all input types are disabled. + uint8_t unused; }; } inputbutton_t; diff --git a/src/dusksdl2/CMakeLists.txt b/src/dusksdl2/CMakeLists.txt index a968cc8..3e86b70 100644 --- a/src/dusksdl2/CMakeLists.txt +++ b/src/dusksdl2/CMakeLists.txt @@ -10,4 +10,5 @@ target_include_directories(${DUSK_LIBRARY_TARGET_NAME} ) # Subdirs -add_subdirectory(display) \ No newline at end of file +add_subdirectory(display) +add_subdirectory(input) \ No newline at end of file diff --git a/src/dusksdl2/input/CMakeLists.txt b/src/dusksdl2/input/CMakeLists.txt new file mode 100644 index 0000000..d64d14c --- /dev/null +++ b/src/dusksdl2/input/CMakeLists.txt @@ -0,0 +1,12 @@ +# Copyright (c) 2026 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_LIBRARY_TARGET_NAME} + PUBLIC + inputsdl2.c +) + +# Subdirs \ No newline at end of file diff --git a/src/dusksdl2/input/inputplatform.h b/src/dusksdl2/input/inputplatform.h new file mode 100644 index 0000000..b6a852c --- /dev/null +++ b/src/dusksdl2/input/inputplatform.h @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "inputsdl2.h" + +#ifdef DUSK_INPUT_KEYBOARD + typedef inputscancodesdl2_t inputscancodeplatform_t; +#endif + +#ifdef DUSK_INPUT_GAMEPAD + typedef inputgamepadbuttonsdl2_t inputgamepadbuttonplatform_t; + typedef inputgamepadaxissdl2_t inputgamepadaxissplatform_t; +#endif + +#ifdef DUSK_INPUT_POINTER + typedef inputpointeraxissdl2_t inputpointeraxisplatform_t; +#endif + +#define inputButtonGetValuePlatform inputButtonGetValueSDL2 + +typedef inputsdl2_t inputplatform_t; \ No newline at end of file diff --git a/src/dusksdl2/input/inputsdl2.c b/src/dusksdl2/input/inputsdl2.c new file mode 100644 index 0000000..dabb1ae --- /dev/null +++ b/src/dusksdl2/input/inputsdl2.c @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "input/input.h" + +float_t inputButtonGetValue(const inputbutton_t button) { + switch(button.type) { + #ifdef DUSK_INPUT_KEYBOARD + case INPUT_BUTTON_TYPE_KEYBOARD: { + return INPUT.keyboardState[button.scancode] ? 1.0f : 0.0f; + } + #endif + + #ifdef DUSK_INPUT_POINTER + case INPUT_BUTTON_TYPE_POINTER: { + switch(button.pointerAxis) { + case INPUT_POINTER_AXIS_X: + return INPUT.mouseX; + + case INPUT_POINTER_AXIS_Y: + return INPUT.mouseY; + + default: + assertUnreachable("Unknown pointer axis"); + return 0.0f; + } + } + #endif + + #ifdef DUSK_INPUT_GAMEPAD + case INPUT_BUTTON_TYPE_GAMEPAD: { + if(SDL_GameControllerGetButton(INPUT.controller, button.gpButton)) { + return 1.0f; + } + return 0.0f; + } + + case INPUT_BUTTON_TYPE_GAMEPAD_AXIS: { + float_t value = 0.0f; + + Sint16 axis = SDL_GameControllerGetAxis(INPUT.controller, button.gpAxis.axis); + value = (float_t)axis / 32767.0f; + + if(!button.gpAxis.positive) value = -value; + if(value >= INPUT.deadzone) return value; + return 0.0f; + } + #endif + + default: { + assertUnreachable("Unknown input button type"); + return 0.0f; + } + } +} \ No newline at end of file diff --git a/src/dusksdl2/input/inputsdl2.h b/src/dusksdl2/input/inputsdl2.h new file mode 100644 index 0000000..f529325 --- /dev/null +++ b/src/dusksdl2/input/inputsdl2.h @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dusk.h" + +typedef struct inputbutton_s inputbutton_t; + +#ifdef DUSK_INPUT_KEYBOARD + typedef SDL_Scancode inputscancodesdl2_t; +#endif + +#ifdef DUSK_INPUT_GAMEPAD + typedef SDL_GameControllerButton inputgamepadbuttonsdl2_t; + typedef SDL_GameControllerAxis inputgamepadaxissdl2_t; +#endif + +#ifdef DUSK_INPUT_POINTER + typedef enum { + INPUT_POINTER_AXIS_X, + INPUT_POINTER_AXIS_Y, + INPUT_POINTER_AXIS_Z, + INPUT_POINTER_AXIS_WHEEL_X, + INPUT_POINTER_AXIS_WHEEL_Y, + } inputpointeraxissdl2_t; +#endif + +typedef struct { + #if INPUT_GAMEPAD == 1 + SDL_GameController *controller; + #endif + + #if INPUT_KEYBOARD == 1 + const uint8_t *keyboardState; + #endif + + #if INPUT_POINTER == 1 + #if INPUT_SDL2 == 1 + float_t mouseX, mouseY; + #endif + #endif +} inputsdl2_t; + +/** + * Returns the input value (between 0 and 1) of the given button. + * + * @param button The button to get the value of. + * @return The value of the button, between 0 and 1. + */ +float_t inputButtonGetValueSDL2(const inputbutton_t button); \ No newline at end of file