VERY experimental Nintendo Switch 2 emulator
C++
[hid_core, input_common] use std::numbers::pi{_v} instead of re-defining (#4160)
lizzie7b750bf5bb6f
diff --git a/src/common/math_util.h b/src/common/math_util.hindex 967d755f53..0f100b270c 100644--- a/src/common/math_util.h+++ b/src/common/math_util.h@@ -1,4 +1,4 @@-// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project+// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project// SPDX-License-Identifier: GPL-3.0-or-later// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project@@ -13,8 +13,6 @@namespace Common {-constexpr float PI = 3.1415926535f;-template <class T>struct Rectangle {T left{};diff --git a/src/hid_core/frontend/motion_input.cpp b/src/hid_core/frontend/motion_input.cppindex 417cd03f97..a1e09e3ce9 100644--- a/src/hid_core/frontend/motion_input.cpp+++ b/src/hid_core/frontend/motion_input.cpp@@ -1,7 +1,11 @@+// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project+// SPDX-License-Identifier: GPL-3.0-or-later+// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project// SPDX-License-Identifier: GPL-2.0-or-later#include <cmath>+#include <numbers>#include "common/math_util.h"#include "hid_core/frontend/motion_input.h"@@ -145,7 +149,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) {}const auto normal_accel = accel.Normalized();- auto rad_gyro = gyro * Common::PI * 2;+ auto rad_gyro = gyro * std::numbers::pi_v<float> * 2.f;const f32 swap = rad_gyro.x;rad_gyro.x = rad_gyro.y;rad_gyro.y = -swap;@@ -272,7 +276,7 @@ Common::Vec3f MotionInput::GetEulerAngles() const {return {std::atan2(sinr_cosp, cosr_cosp),- 2 * std::atan2(sinp, cosp) - Common::PI / 2,+ 2 * std::atan2(sinp, cosp) - float(std::numbers::pi_v<float>) / 2,std::atan2(siny_cosp, cosy_cosp),};}diff --git a/src/hid_core/resources/touch_screen/gesture_handler.cpp b/src/hid_core/resources/touch_screen/gesture_handler.cppindex 8e5ed8ff7e..8549c1b91e 100644--- a/src/hid_core/resources/touch_screen/gesture_handler.cpp+++ b/src/hid_core/resources/touch_screen/gesture_handler.cpp@@ -1,9 +1,10 @@-// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project+// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project// SPDX-License-Identifier: GPL-3.0-or-later// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project// SPDX-License-Identifier: GPL-3.0-or-later+#include <numbers>#include "common/math_util.h"#include "hid_core/resources/touch_screen/gesture_handler.h"@@ -214,7 +215,7 @@ void GestureHandler::UpdatePanEvent(GestureState& next_state, GestureType& type)if (std::abs(angle_between_two_lines) > AngleThreshold) {type = GestureType::Rotate;next_state.scale = 0;- next_state.rotation_angle = angle_between_two_lines * 180.0f / Common::PI;+ next_state.rotation_angle = angle_between_two_lines * 180.0f / float(std::numbers::pi_v<float>);}}diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cppindex 9ba9010dc1..a656dcf077 100644--- a/src/input_common/drivers/sdl_driver.cpp+++ b/src/input_common/drivers/sdl_driver.cpp@@ -3,6 +3,8 @@// SPDX-FileCopyrightText: 2018 Citra Emulator Project// SPDX-License-Identifier: GPL-2.0-or-later+#include <array>+#include <numbers>#include "common/logging.h"#include "common/math_util.h"#include "common/param_package.h"@@ -146,9 +148,9 @@ public:break;}case SDL_SENSOR_GYRO: {- motion.gyro_x = event.data[0] / (Common::PI * 2);- motion.gyro_y = -event.data[2] / (Common::PI * 2);- motion.gyro_z = event.data[1] / (Common::PI * 2);+ motion.gyro_x = event.data[0] / (f32(std::numbers::pi_v<float>) * 2);+ motion.gyro_y = -event.data[2] / (f32(std::numbers::pi_v<float>) * 2);+ motion.gyro_z = event.data[1] / (f32(std::numbers::pi_v<float>) * 2);break;}}diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cppindex a6be6dac1b..576fd0244c 100644--- a/src/input_common/helpers/stick_from_buttons.cpp+++ b/src/input_common/helpers/stick_from_buttons.cpp@@ -1,8 +1,12 @@+// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project+// SPDX-License-Identifier: GPL-3.0-or-later+// SPDX-FileCopyrightText: 2017 Citra Emulator Project// SPDX-License-Identifier: GPL-2.0-or-later#include <chrono>#include <cmath>+#include <numbers>#include "common/math_util.h"#include "common/settings.h"#include "input_common/helpers/stick_from_buttons.h"@@ -15,9 +19,8 @@ public:// do not play nicely with the theoretical maximum range.// Using a value one lower from the maximum emulates real stick behavior.static constexpr float MAX_RANGE = 32766.0f / 32767.0f;- static constexpr float TAU = Common::PI * 2.0f;// Use wider angle to ease the transition.- static constexpr float APERTURE = TAU * 0.15f;+ static constexpr float APERTURE = float((std::numbers::pi_v<float> / 2.f)) * 0.15f;using Button = std::unique_ptr<Common::Input::InputDevice>;@@ -66,13 +69,13 @@ public:bool IsAngleGreater(float old_angle, float new_angle) const {const float top_limit = new_angle + APERTURE;return (old_angle > new_angle && old_angle <= top_limit) ||- (old_angle + TAU > new_angle && old_angle + TAU <= top_limit);+ (old_angle + f32((std::numbers::pi_v<float> / 2.f)) > new_angle && old_angle + f32((std::numbers::pi_v<float> / 2.f)) <= top_limit);}bool IsAngleSmaller(float old_angle, float new_angle) const {const float bottom_limit = new_angle - APERTURE;return (old_angle >= bottom_limit && old_angle < new_angle) ||- (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);+ (old_angle - f32((std::numbers::pi_v<float> / 2.f)) >= bottom_limit && old_angle - f32((std::numbers::pi_v<float> / 2.f)) < new_angle);}float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {@@ -87,16 +90,16 @@ public:if (IsAngleGreater(new_angle, goal_angle)) {new_angle -= modifier_angle * time_difference;- if (new_angle < 0) {- new_angle += TAU;+ if (new_angle < 0.f) {+ new_angle += f32((std::numbers::pi_v<float> / 2.f));}if (!IsAngleGreater(new_angle, goal_angle)) {return goal_angle;}} else if (IsAngleSmaller(new_angle, goal_angle)) {new_angle += modifier_angle * time_difference;- if (new_angle >= TAU) {- new_angle -= TAU;+ if (new_angle >= f32((std::numbers::pi_v<float> / 2.f))) {+ new_angle -= f32((std::numbers::pi_v<float> / 2.f));}if (!IsAngleSmaller(new_angle, goal_angle)) {return goal_angle;@@ -108,45 +111,14 @@ public:}void SetGoalAngle(bool r, bool l, bool u, bool d) {- // Move to the right- if (r && !u && !d) {- goal_angle = 0.0f;- }-- // Move to the upper right- if (r && u && !d) {- goal_angle = Common::PI * 0.25f;- }-- // Move up- if (u && !l && !r) {- goal_angle = Common::PI * 0.5f;- }-- // Move to the upper left- if (l && u && !d) {- goal_angle = Common::PI * 0.75f;- }-- // Move to the left- if (l && !u && !d) {- goal_angle = Common::PI;- }-- // Move to the bottom left- if (l && !u && d) {- goal_angle = Common::PI * 1.25f;- }-- // Move down- if (d && !l && !r) {- goal_angle = Common::PI * 1.5f;- }-- // Move to the bottom right- if (r && !u && d) {- goal_angle = Common::PI * 1.75f;- }+ if (r && !u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 0.00f; //right+ if (r && u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 0.25f; //upper right+ if (u && !l && !r) goal_angle = f32(std::numbers::pi_v<float>) * 0.50f; //up+ if (l && u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 0.75f; //upper left+ if (l && !u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 1.00f; //left+ if (l && !u && d) goal_angle = f32(std::numbers::pi_v<float>) * 1.25f; //bottom left+ if (d && !l && !r) goal_angle = f32(std::numbers::pi_v<float>) * 1.50f; //down+ if (r && !u && d) goal_angle = f32(std::numbers::pi_v<float>) * 1.75f; //bottom right}void UpdateUpButtonStatus(const Common::Input::CallbackStatus& button_callback) {diff --git a/src/yuzu/configuration/configure_input_player_widget.cpp b/src/yuzu/configuration/configure_input_player_widget.cppindex 790a860002..a6be2a1dd4 100644--- a/src/yuzu/configuration/configure_input_player_widget.cpp+++ b/src/yuzu/configuration/configure_input_player_widget.cpp@@ -1612,7 +1612,7 @@ void PlayerControlPreview::DrawGCBody(QPainter& p, const QPointF center) {std::array<QPointF, gc_body.size()> qbody;std::array<QPointF, 8> left_hex;std::array<QPointF, 8> right_hex;- constexpr float angle = 2 * 3.1415f / 8;+ constexpr float angle = 2.f * float(M_PI) / 8.f;for (std::size_t point = 0; point < gc_left_body.size() / 2; ++point) {const float body_x = gc_left_body[point * 2 + 0];