From e6bd6587fdd294fa4e5dd189a2c5d5c6155a0b95 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 26 Apr 2020 01:18:42 +1000 Subject: [PATCH] Controller: Add emulation of Namco GunCon --- src/core/CMakeLists.txt | 2 + src/core/controller.cpp | 21 +++- src/core/controller.h | 3 +- src/core/core.vcxproj | 2 + src/core/core.vcxproj.filters | 2 + src/core/namco_guncon.cpp | 219 ++++++++++++++++++++++++++++++++++ src/core/namco_guncon.h | 66 ++++++++++ src/core/pad.cpp | 2 +- src/core/settings.cpp | 7 +- src/core/system.cpp | 2 +- src/core/types.h | 1 + 11 files changed, 320 insertions(+), 7 deletions(-) create mode 100644 src/core/namco_guncon.cpp create mode 100644 src/core/namco_guncon.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 9f9c02ca6..af3b0bb5d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -52,6 +52,8 @@ add_library(core mdec.h memory_card.cpp memory_card.h + namco_guncon.cpp + namco_guncon.h pad.cpp pad.h psf_loader.cpp diff --git a/src/core/controller.cpp b/src/core/controller.cpp index 1ec67eab5..1690a88f8 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -1,6 +1,7 @@ #include "controller.h" #include "analog_controller.h" #include "common/state_wrapper.h" +#include "namco_guncon.h" #include "digital_controller.h" Controller::Controller() = default; @@ -36,7 +37,7 @@ float Controller::GetVibrationMotorStrength(u32 motor) return 0.0f; } -std::unique_ptr Controller::Create(ControllerType type) +std::unique_ptr Controller::Create(System* system, ControllerType type) { switch (type) { @@ -46,6 +47,9 @@ std::unique_ptr Controller::Create(ControllerType type) case ControllerType::AnalogController: return AnalogController::Create(); + case ControllerType::NamcoGunCon: + return NamcoGunCon::Create(system); + case ControllerType::None: default: return {}; @@ -72,6 +76,9 @@ Controller::AxisList Controller::GetAxisNames(ControllerType type) case ControllerType::AnalogController: return AnalogController::StaticGetAxisNames(); + case ControllerType::NamcoGunCon: + return NamcoGunCon::StaticGetAxisNames(); + case ControllerType::None: default: return {}; @@ -88,6 +95,9 @@ Controller::ButtonList Controller::GetButtonNames(ControllerType type) case ControllerType::AnalogController: return AnalogController::StaticGetButtonNames(); + case ControllerType::NamcoGunCon: + return NamcoGunCon::StaticGetButtonNames(); + case ControllerType::None: default: return {}; @@ -104,6 +114,9 @@ u32 Controller::GetVibrationMotorCount(ControllerType type) case ControllerType::AnalogController: return AnalogController::StaticGetVibrationMotorCount(); + case ControllerType::NamcoGunCon: + return NamcoGunCon::StaticGetVibrationMotorCount(); + case ControllerType::None: default: return 0; @@ -120,6 +133,9 @@ std::optional Controller::GetAxisCodeByName(ControllerType type, std::strin case ControllerType::AnalogController: return AnalogController::StaticGetAxisCodeByName(axis_name); + case ControllerType::NamcoGunCon: + return NamcoGunCon::StaticGetAxisCodeByName(axis_name); + case ControllerType::None: default: return std::nullopt; @@ -136,6 +152,9 @@ std::optional Controller::GetButtonCodeByName(ControllerType type, std::str case ControllerType::AnalogController: return AnalogController::StaticGetButtonCodeByName(button_name); + case ControllerType::NamcoGunCon: + return NamcoGunCon::StaticGetButtonCodeByName(button_name); + case ControllerType::None: default: return std::nullopt; diff --git a/src/core/controller.h b/src/core/controller.h index 5f05deae0..1833c29f0 100644 --- a/src/core/controller.h +++ b/src/core/controller.h @@ -7,6 +7,7 @@ #include class StateWrapper; +class System; class Controller { @@ -48,7 +49,7 @@ public: virtual float GetVibrationMotorStrength(u32 motor); /// Creates a new controller of the specified type. - static std::unique_ptr Create(ControllerType type); + static std::unique_ptr Create(System* system, ControllerType type); /// Gets the integer code for an axis in the specified controller type. static std::optional GetAxisCodeByName(ControllerType type, std::string_view axis_name); diff --git a/src/core/core.vcxproj b/src/core/core.vcxproj index b4802b6c1..9aa2e829e 100644 --- a/src/core/core.vcxproj +++ b/src/core/core.vcxproj @@ -75,6 +75,7 @@ + @@ -115,6 +116,7 @@ + diff --git a/src/core/core.vcxproj.filters b/src/core/core.vcxproj.filters index f2bbbf7f2..e15248cc7 100644 --- a/src/core/core.vcxproj.filters +++ b/src/core/core.vcxproj.filters @@ -41,6 +41,7 @@ + @@ -83,6 +84,7 @@ + diff --git a/src/core/namco_guncon.cpp b/src/core/namco_guncon.cpp new file mode 100644 index 000000000..6d90fecc2 --- /dev/null +++ b/src/core/namco_guncon.cpp @@ -0,0 +1,219 @@ +#include "namco_guncon.h" +#include "common/assert.h" +#include "common/log.h" +#include "common/state_wrapper.h" +#include "gpu.h" +#include "imgui.h" +#include "system.h" +#include +Log_SetChannel(NamcoGunCon); + +NamcoGunCon::NamcoGunCon(System* system) : m_system(system) {} + +NamcoGunCon::~NamcoGunCon() = default; + +ControllerType NamcoGunCon::GetType() const +{ + return ControllerType::NamcoGunCon; +} + +std::optional NamcoGunCon::GetAxisCodeByName(std::string_view axis_name) const +{ + return StaticGetAxisCodeByName(axis_name); +} + +std::optional NamcoGunCon::GetButtonCodeByName(std::string_view button_name) const +{ + return StaticGetButtonCodeByName(button_name); +} + +void NamcoGunCon::Reset() +{ + m_transfer_state = TransferState::Idle; +} + +bool NamcoGunCon::DoState(StateWrapper& sw) +{ + if (!Controller::DoState(sw)) + return false; + + sw.Do(&m_button_state); + sw.Do(&m_position_x); + sw.Do(&m_position_y); + sw.Do(&m_transfer_state); + return true; +} + +void NamcoGunCon::SetAxisState(s32 axis_code, float value) {} + +void NamcoGunCon::SetButtonState(Button button, bool pressed) +{ + static constexpr std::array(Button::Count)> indices = {{13, 3, 14}}; + if (pressed) + m_button_state &= ~(u16(1) << indices[static_cast(button)]); + else + m_button_state |= u16(1) << indices[static_cast(button)]; +} + +void NamcoGunCon::SetButtonState(s32 button_code, bool pressed) +{ + if (button_code < 0 || button_code >= static_cast(Button::Count)) + return; + + SetButtonState(static_cast