From 3bc4968c1173eee90813fd9b34656e4933de60e9 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 2 Feb 2019 14:25:48 -0600 Subject: ControllerEmu: Round input floats instead of casting to prevent almost-neutral values from being rounded down. --- .../Core/InputCommon/ControllerEmu/ControllerEmu.h | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Source/Core/InputCommon') diff --git a/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h b/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h index 4f734e2824..1cc9f5fd1e 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h +++ b/Source/Core/InputCommon/ControllerEmu/ControllerEmu.h @@ -4,9 +4,11 @@ #pragma once +#include #include #include #include +#include #include #include "Common/Common.h" @@ -50,6 +52,29 @@ public: std::vector> groups; + // Maps a float from -1.0..+1.0 to an integer of the provided values. + template + static T MapFloat(F input_value, T zero_value, T neg_1_value = std::numeric_limits::min(), + T pos_1_value = std::numeric_limits::max()) + { + static_assert(std::is_integral(), "T is only sane for int types."); + static_assert(std::is_floating_point(), "F is only sane for float types."); + + static_assert(std::numeric_limits::min() <= std::numeric_limits::min() && + std::numeric_limits::max() >= std::numeric_limits::max(), + "long is not a superset of T. use of std::lround is not sane."); + + // Here we round when converting from float to int. + // After applying our deadzone, resizing, and reshaping math + // we sometimes have a near-zero value which is slightly negative. (e.g. -0.0001) + // Casting would round down but rounding will yield our "zero_value". + + if (input_value > 0) + return T(std::lround((pos_1_value - zero_value) * input_value + zero_value)); + else + return T(std::lround((zero_value - neg_1_value) * input_value + zero_value)); + } + private: ciface::Core::DeviceQualifier m_default_device; bool m_default_device_is_connected{false}; -- cgit v1.2.3