summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon
diff options
context:
space:
mode:
authorTilka <tilkax@gmail.com>2019-02-03 00:56:05 +0000
committerGitHub <noreply@github.com>2019-02-03 00:56:05 +0000
commitcfbc852fb065532ebd2ce01d2cb277b3f8035b33 (patch)
treeeb81d6aecbd333e5d9133af6e38684c94170cd13 /Source/Core/InputCommon
parent6ec11c68017434cdd7dddb681e537aa733ff1464 (diff)
parent3bc4968c1173eee90813fd9b34656e4933de60e9 (diff)
Merge pull request #7777 from jordan-woyak/input-rounding
ControllerEmu: Round input floats instead of casting.
Diffstat (limited to 'Source/Core/InputCommon')
-rw-r--r--Source/Core/InputCommon/ControllerEmu/ControllerEmu.h25
1 files changed, 25 insertions, 0 deletions
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 <cmath>
#include <memory>
#include <mutex>
#include <string>
+#include <type_traits>
#include <vector>
#include "Common/Common.h"
@@ -50,6 +52,29 @@ public:
std::vector<std::unique_ptr<ControlGroup>> groups;
+ // Maps a float from -1.0..+1.0 to an integer of the provided values.
+ template <typename T, typename F>
+ static T MapFloat(F input_value, T zero_value, T neg_1_value = std::numeric_limits<T>::min(),
+ T pos_1_value = std::numeric_limits<T>::max())
+ {
+ static_assert(std::is_integral<T>(), "T is only sane for int types.");
+ static_assert(std::is_floating_point<F>(), "F is only sane for float types.");
+
+ static_assert(std::numeric_limits<long>::min() <= std::numeric_limits<T>::min() &&
+ std::numeric_limits<long>::max() >= std::numeric_limits<T>::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};