diff options
| author | Jordan Woyak <jordan.woyak@gmail.com> | 2024-03-14 21:10:21 -0500 |
|---|---|---|
| committer | Jordan Woyak <jordan.woyak@gmail.com> | 2024-03-14 23:43:58 -0500 |
| commit | 05383663263242c912329d882290a2cb5f0fde3d (patch) | |
| tree | be6e6cb0ae6edbae27a3af6b2f102b5dfefdbbb3 /Source/Core/InputCommon/ControllerInterface | |
| parent | 0fff8374d03e0551e42ddbd21f5b18e671310274 (diff) | |
SDL: Deduplicate Motor logic with templates.
Diffstat (limited to 'Source/Core/InputCommon/ControllerInterface')
| -rw-r--r-- | Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp | 73 |
1 files changed, 16 insertions, 57 deletions
diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index dfd5fc639d..fa5a2602cb 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -115,38 +115,30 @@ private: }; // Rumble - class Motor : public Output + template <int LowEnable, int HighEnable, int SuffixIndex> + class GenericMotor : public Output { public: - explicit Motor(SDL_GameController* gc) : m_gc(gc) {} - std::string GetName() const override; - void SetState(ControlState state) override; - - private: - SDL_GameController* const m_gc; - }; - - class MotorL : public Output - { - public: - explicit MotorL(SDL_GameController* gc) : m_gc(gc) {} - std::string GetName() const override; - void SetState(ControlState state) override; + explicit GenericMotor(SDL_GameController* gc) : m_gc(gc) {} + std::string GetName() const override + { + return std::string("Motor") + motor_suffixes[SuffixIndex]; + } + void SetState(ControlState state) override + { + Uint16 rumble = state * std::numeric_limits<Uint16>::max(); + SDL_GameControllerRumble(m_gc, rumble * LowEnable, rumble * HighEnable, RUMBLE_LENGTH_MS); + } private: SDL_GameController* const m_gc; }; - class MotorR : public Output - { - public: - explicit MotorR(SDL_GameController* gc) : m_gc(gc) {} - std::string GetName() const override; - void SetState(ControlState state) override; + static constexpr const char* motor_suffixes[] = {"", " L", " R"}; - private: - SDL_GameController* const m_gc; - }; + using Motor = GenericMotor<1, 1, 0>; + using MotorL = GenericMotor<1, 0, 1>; + using MotorR = GenericMotor<0, 1, 2>; class HapticEffect : public Output { @@ -764,39 +756,6 @@ GameController::~GameController() SDL_JoystickClose(m_joystick); } -std::string GameController::Motor::GetName() const -{ - return "Motor"; -} - -void GameController::Motor::SetState(ControlState state) -{ - Uint16 rumble = state * std::numeric_limits<Uint16>::max(); - SDL_GameControllerRumble(m_gc, rumble, rumble, std::numeric_limits<Uint32>::max()); -} - -std::string GameController::MotorL::GetName() const -{ - return "Motor L"; -} - -void GameController::MotorL::SetState(ControlState state) -{ - Uint16 rumble = state * std::numeric_limits<Uint16>::max(); - SDL_GameControllerRumble(m_gc, rumble, 0, std::numeric_limits<Uint32>::max()); -} - -std::string GameController::MotorR::GetName() const -{ - return "Motor R"; -} - -void GameController::MotorR::SetState(ControlState state) -{ - Uint16 rumble = state * std::numeric_limits<Uint16>::max(); - SDL_GameControllerRumble(m_gc, 0, rumble, std::numeric_limits<Uint32>::max()); -} - void InputBackend::UpdateInput(std::vector<std::weak_ptr<ciface::Core::Device>>& devices_to_remove) { SDL_GameControllerUpdate(); |
