input prog
This commit is contained in:
@@ -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
|
||||
)
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -7,77 +7,55 @@
|
||||
|
||||
#pragma once
|
||||
#include "inputaction.h"
|
||||
#include "input/inputplatform.h"
|
||||
|
||||
#if INPUT_SDL2 == 1
|
||||
#include <SDL2/SDL.h>
|
||||
#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;
|
||||
|
||||
|
||||
@@ -11,3 +11,4 @@ target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(input)
|
||||
12
src/dusksdl2/input/CMakeLists.txt
Normal file
12
src/dusksdl2/input/CMakeLists.txt
Normal 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
|
||||
26
src/dusksdl2/input/inputplatform.h
Normal file
26
src/dusksdl2/input/inputplatform.h
Normal 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;
|
||||
59
src/dusksdl2/input/inputsdl2.c
Normal file
59
src/dusksdl2/input/inputsdl2.c
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
54
src/dusksdl2/input/inputsdl2.h
Normal file
54
src/dusksdl2/input/inputsdl2.h
Normal 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);
|
||||
Reference in New Issue
Block a user