/** * Copyright (c) 2026 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "input/input.h" #include "assert/assert.h" #include "display/display.h" void inputUpdateSDL2(void) { #ifdef DUSK_INPUT_GAMEPAD INPUT.platform.controller = NULL; for(int32_t i = 0; i < SDL_NumJoysticks(); i++) { if(!SDL_IsGameController(i)) continue; INPUT.platform.controller = SDL_GameControllerOpen(i); if(INPUT.platform.controller) break; } #endif #ifdef DUSK_INPUT_KEYBOARD INPUT.platform.keyboardState = SDL_GetKeyboardState(NULL); #endif #ifdef DUSK_INPUT_POINTER int pointerX, pointerY; SDL_GetMouseState(&pointerX, &pointerY); int windowWidth, windowHeight; SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight); INPUT.platform.mouseX = (float_t)pointerX / (float_t)windowWidth; INPUT.platform.mouseY = (float_t)pointerY / (float_t)windowHeight; INPUT.platform.scrollX = 0.0f; INPUT.platform.scrollY = 0.0f; #endif } float_t inputButtonGetValueSDL2(const inputbutton_t button) { switch(button.type) { #ifdef DUSK_INPUT_KEYBOARD case INPUT_BUTTON_TYPE_KEYBOARD: { return INPUT.platform.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.platform.mouseX; case INPUT_POINTER_AXIS_Y: return INPUT.platform.mouseY; case INPUT_POINTER_AXIS_Z: return 0.0f; // Not supported in SDL2 case INPUT_POINTER_AXIS_WHEEL_X: return INPUT.platform.scrollX; case INPUT_POINTER_AXIS_WHEEL_Y: return INPUT.platform.scrollY; default: assertUnreachable("Unknown pointer axis"); return 0.0f; } } #endif #ifdef DUSK_INPUT_GAMEPAD case INPUT_BUTTON_TYPE_GAMEPAD: { if(SDL_GameControllerGetButton( INPUT.platform.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.platform.controller, button.gpAxis.axis ); value = (float_t)axis / 32767.0f; if(!button.gpAxis.positive) value = -value; value = inputDeadzone(value, inputGetDeadzoneSDL2(button)); return value; } #endif default: { assertUnreachable("Unknown input button type"); return 0.0f; } } }