From cbe268a0fcbd92eef9d46dd2f5097143ecfc2f10 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Sat, 19 Feb 2022 06:34:12 -0800 Subject: [PATCH] aurora: Add controller input (no remapping yet) --- aurora/lib/input.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++ aurora/lib/input.hpp | 16 ++++++ 2 files changed, 130 insertions(+) create mode 100644 aurora/lib/input.cpp create mode 100644 aurora/lib/input.hpp diff --git a/aurora/lib/input.cpp b/aurora/lib/input.cpp new file mode 100644 index 000000000..edea48d72 --- /dev/null +++ b/aurora/lib/input.cpp @@ -0,0 +1,114 @@ +#include "input.hpp" + +namespace aurora::input { +struct GameController { + SDL_GameController* m_controller; + bool m_isGameCube = false; + Sint32 m_index; +}; +std::unordered_map g_GameControllers; + +Sint32 add_controller(Uint32 which) { + auto* ctrl = SDL_GameControllerOpen(which); + if (ctrl != nullptr) { + GameController controller; + controller.m_controller = ctrl; + controller.m_index = which; + controller.m_isGameCube = + SDL_GameControllerGetVendor(ctrl) == 0x057E && SDL_GameControllerGetProduct(ctrl) == 0x0337; + + Sint32 instance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ctrl)); + g_GameControllers[instance] = controller; + return instance; + } + + return -1; +} + +void remove_controller(Uint32 which) { + if (g_GameControllers.find(which) != g_GameControllers.end()) { + SDL_GameControllerClose(g_GameControllers[which].m_controller); + g_GameControllers.erase(which); + } +} + +bool is_gamecube(Uint32 which) { + if (g_GameControllers.find(which) != g_GameControllers.end()) { + return g_GameControllers[which].m_isGameCube; + } + return false; +} + +int32_t player_index(Uint32 which) { + if (g_GameControllers.find(which) != g_GameControllers.end()) { + return SDL_GameControllerGetPlayerIndex(g_GameControllers[which].m_controller); + } + return -1; +} + +void set_player_index(Uint32 which, Sint32 index) { + if (g_GameControllers.find(which) != g_GameControllers.end()) { + SDL_GameControllerSetPlayerIndex(g_GameControllers[which].m_controller, index); + } +} + +std::string name(Uint32 which) { + if (g_GameControllers.find(which) != g_GameControllers.end()) { + auto* name = SDL_GameControllerName(g_GameControllers[which].m_controller); + if (name) { + return std::string(name); + } + } + return {}; +} + +ControllerButton translate_button(SDL_GameControllerButton btn) { + switch (btn) { + case SDL_CONTROLLER_BUTTON_A: + return ControllerButton::A; + case SDL_CONTROLLER_BUTTON_B: + return ControllerButton::B; + case SDL_CONTROLLER_BUTTON_X: + return ControllerButton::X; + case SDL_CONTROLLER_BUTTON_Y: + return ControllerButton::Y; + case SDL_CONTROLLER_BUTTON_BACK: + return ControllerButton::Back; + case SDL_CONTROLLER_BUTTON_GUIDE: + return ControllerButton::Guide; + case SDL_CONTROLLER_BUTTON_LEFTSTICK: + return ControllerButton::LeftStick; + case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + return ControllerButton::RightStick; + case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + return ControllerButton::LeftShoulder; + case SDL_CONTROLLER_BUTTON_DPAD_UP: + return ControllerButton::DPadUp; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + return ControllerButton::DPadDown; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + return ControllerButton::DPadRight; + default: + return ControllerButton::Other; + } +} + +ControllerAxis translate_axis(SDL_GameControllerAxis axis) { + switch (axis) { + case SDL_CONTROLLER_AXIS_LEFTX: + return ControllerAxis::LeftX; + case SDL_CONTROLLER_AXIS_LEFTY: + return ControllerAxis::LeftY; + case SDL_CONTROLLER_AXIS_RIGHTX: + return ControllerAxis::RightX; + case SDL_CONTROLLER_AXIS_RIGHTY: + return ControllerAxis::RightY; + case SDL_CONTROLLER_AXIS_TRIGGERLEFT: + return ControllerAxis::TriggerLeft; + case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: + return ControllerAxis::TriggerRight; + default: + return ControllerAxis::MAX; + } +} +} // namespace aurora::input \ No newline at end of file diff --git a/aurora/lib/input.hpp b/aurora/lib/input.hpp new file mode 100644 index 000000000..672e97d1d --- /dev/null +++ b/aurora/lib/input.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include "aurora/aurora.hpp" +#include "SDL_gamecontroller.h" + +namespace aurora::input { +Sint32 add_controller(Uint32 which); +void remove_controller(Uint32 which); +Sint32 player_index(Uint32 which); +void set_player_index(Uint32 which, Sint32 index); +std::string name(Uint32 which); +bool is_gamecube(Uint32 which); +ControllerButton translate_button(SDL_GameControllerButton button); +ControllerAxis translate_axis(SDL_GameControllerAxis axis); +} \ No newline at end of file