input prog

This commit is contained in:
2026-03-07 22:11:11 -06:00
parent 71e6079054
commit 5c4537b2fa
9 changed files with 189 additions and 75 deletions

View File

@@ -18,4 +18,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
DUSK_DISPLAY_WIDTH_DEFAULT=640 DUSK_DISPLAY_WIDTH_DEFAULT=640
DUSK_DISPLAY_HEIGHT_DEFAULT=480 DUSK_DISPLAY_HEIGHT_DEFAULT=480
DUSK_DISPLAY_SCREEN_HEIGHT=240 DUSK_DISPLAY_SCREEN_HEIGHT=240
DUSK_INPUT_KEYBOARD
DUSK_INPUT_POINTER
DUSK_INPUT_GAMEPAD
) )

View File

@@ -22,6 +22,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
DUSK_SDL2 DUSK_SDL2
DUSK_OPENGL DUSK_OPENGL
DUSK_PSP DUSK_PSP
DUSK_INPUT_GAMEPAD
) )
# Postbuild, create .pbp file for PSP. # Postbuild, create .pbp file for PSP.

View File

@@ -30,30 +30,11 @@ typedef struct {
eventlistener_t releasedListeners[INPUT_LISTENER_RELEASED_MAX]; eventlistener_t releasedListeners[INPUT_LISTENER_RELEASED_MAX];
event_t eventReleased; event_t eventReleased;
#if INPUT_GAMEPAD == 1 inputplatform_t platform;
#ifdef DUSK_INPUT_GAMEPAD
float_t deadzone; float_t deadzone;
#endif #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; } input_t;
extern input_t INPUT; extern input_t INPUT;

View File

@@ -7,77 +7,55 @@
#pragma once #pragma once
#include "inputaction.h" #include "inputaction.h"
#include "input/inputplatform.h"
#if INPUT_SDL2 == 1 #ifndef inputButtonGetValuePlatform
#include <SDL2/SDL.h> #error "inputButtonGetValuePlatform is not defined"
#elif DOLPHIN
#else
#error "No input backend defined"
#endif #endif
// Keyboard defs #ifdef DUSK_INPUT_KEYBOARD
#if INPUT_KEYBOARD == 1 typedef inputscancodeplatform_t inputscancode_t;
#if INPUT_SDL2 == 1
typedef SDL_Scancode inputscancode_t;
#endif
#endif #endif
// Gamepad defs #ifdef DUSK_INPUT_GAMEPAD
#if INPUT_GAMEPAD == 1 typedef inputgamepadbuttonplatform_t inputgamepadbutton_t;
#if INPUT_SDL2 == 1 typedef inputgamepadaxissplatform_t inputgamepadaxis_t;
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
#endif #endif
typedef enum { #ifdef DUSK_INPUT_POINTER
typedef inputpointeraxisplatform_t inputpointeraxis_t;
#endif
typedef enum inputbuttontype_e {
INPUT_BUTTON_TYPE_NONE, INPUT_BUTTON_TYPE_NONE,
#if INPUT_KEYBOARD == 1 #ifdef DUSK_INPUT_KEYBOARD
INPUT_BUTTON_TYPE_KEYBOARD, INPUT_BUTTON_TYPE_KEYBOARD,
#endif #endif
#if INPUT_POINTER == 1 #ifdef DUSK_INPUT_POINTER
INPUT_BUTTON_TYPE_POINTER, INPUT_BUTTON_TYPE_POINTER,
#endif #endif
#if INPUT_TOUCH == 1 #ifdef DUSK_INPUT_TOUCH
INPUT_BUTTON_TYPE_TOUCH, INPUT_BUTTON_TYPE_TOUCH,
#endif #endif
#if INPUT_GAMEPAD == 1 #ifdef DUSK_INPUT_GAMEPAD
INPUT_BUTTON_TYPE_GAMEPAD, INPUT_BUTTON_TYPE_GAMEPAD,
INPUT_BUTTON_TYPE_GAMEPAD_AXIS, INPUT_BUTTON_TYPE_GAMEPAD_AXIS,
#endif #endif
INPUT_BUTTON_TYPE_COUNT
} inputbuttontype_t; } inputbuttontype_t;
typedef struct { typedef struct inputbutton_s {
inputbuttontype_t type; inputbuttontype_t type;
union { union {
#if INPUT_GAMEPAD == 1 #ifdef DUSK_INPUT_KEYBOARD
inputscancode_t scancode;
#endif
#ifdef DUSK_INPUT_GAMEPAD
inputgamepadbutton_t gpButton; inputgamepadbutton_t gpButton;
struct { struct {
inputgamepadaxis_t axis; inputgamepadaxis_t axis;
@@ -85,13 +63,12 @@ typedef struct {
} gpAxis; } gpAxis;
#endif #endif
#if INPUT_KEYBOARD == 1 #ifdef DUSK_INPUT_POINTER
inputscancode_t scancode;
#endif
#if INPUT_POINTER == 1
inputpointeraxis_t pointerAxis; inputpointeraxis_t pointerAxis;
#endif #endif
// Unusued incase all input types are disabled.
uint8_t unused;
}; };
} inputbutton_t; } inputbutton_t;

View File

@@ -11,3 +11,4 @@ target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
# Subdirs # Subdirs
add_subdirectory(display) add_subdirectory(display)
add_subdirectory(input)

View File

@@ -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

View File

@@ -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;

View File

@@ -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;
}
}
}

View File

@@ -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);