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
+2 -1
View File
@@ -10,4 +10,5 @@ target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
)
# Subdirs
add_subdirectory(display)
add_subdirectory(display)
add_subdirectory(input)
+12
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
+26
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;
+59
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;
}
}
}
+54
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);