From f8df7d1b5e14ce480903a45f09b72d866c1bc3b1 Mon Sep 17 00:00:00 2001 From: George Tokmaji Date: Mon, 9 May 2022 02:36:33 +0200 Subject: [PATCH] Replace M_PI with constants from --- CMakeLists.txt | 5 ----- src/C4Effect.cpp | 6 ++++-- src/C4Map.cpp | 6 ++++-- src/C4Script.cpp | 7 ++++--- src/Standard.cpp | 3 ++- src/StdDDraw2.cpp | 3 ++- src/StdJoystick.cpp | 4 +++- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a342cfd78..7024b932f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -400,11 +400,6 @@ if (NOT MSVC) $<$>:NDEBUG>) endif () -# Get non-standard constants (M_PI etc.) -if (MSVC) - set(_USE_MATH_DEFINES ON) -endif () - # Create config.h and make sure it will be used and found target_compile_definitions(clonk PRIVATE HAVE_CONFIG_H) configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) diff --git a/src/C4Effect.cpp b/src/C4Effect.cpp index c893db361..c6aa77c6b 100644 --- a/src/C4Effect.cpp +++ b/src/C4Effect.cpp @@ -25,6 +25,8 @@ #include #include +#include + void C4Effect::AssignCallbackFunctions() { C4AulScript *pSrcScript = GetCallbackScript(); @@ -684,8 +686,8 @@ int32_t FnFxFireTimer(C4AulContext *ctx, C4Object *pObj, int32_t iNumber, int32_ float fRot[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; if (pObj->r && pObj->Def->Rotateable) { - fRot[0] = cosf(static_cast(pObj->r * M_PI / 180.0)); - fRot[1] = -sinf(static_cast(pObj->r * M_PI / 180.0)); + fRot[0] = cosf(static_cast(pObj->r * std::numbers::pi_v / 180.0)); + fRot[1] = -sinf(static_cast(pObj->r * std::numbers::pi_v / 180.0)); fRot[2] = -fRot[1]; fRot[3] = fRot[0]; // rotated objects usually better burn from the center diff --git a/src/C4Map.cpp b/src/C4Map.cpp index bc70ae45d..2af2590cf 100644 --- a/src/C4Map.cpp +++ b/src/C4Map.cpp @@ -25,6 +25,8 @@ #include +#include + C4MapCreator::C4MapCreator() { Reset(); @@ -72,7 +74,7 @@ void C4MapCreator::Create(CSurface8 *sfcMap, C4SLandscape &rLScape, C4TextureMap &rTexMap, bool fLayers, int32_t iPlayerNum) { - double fullperiod = 20.0 * M_PI; + double fullperiod = 20.0 * std::numbers::pi; uint8_t ccol; int32_t cx, cy; @@ -113,7 +115,7 @@ void C4MapCreator::Create(CSurface8 *sfcMap, cy_natural = rnd_cy * natural / 100.0; cy_curve = sin(fullperiod * period / 100.0 * cx / static_cast(MapWdt) + - 2.0 * M_PI * phase / 100.0) * amplitude / 100.0; + 2.0 * std::numbers::pi * phase / 100.0) * amplitude / 100.0; cy = level0 + BoundBy(static_cast(maxrange * (cy_curve + cy_natural)), -maxrange, +maxrange); diff --git a/src/C4Script.cpp b/src/C4Script.cpp index 0cd1a3fd6..2a0307b18 100644 --- a/src/C4Script.cpp +++ b/src/C4Script.cpp @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -3253,7 +3254,7 @@ static C4ValueInt FnAngle(C4AulContext *cthr, C4ValueInt iX1, C4ValueInt iY1, C4 if (!dx) if (dy > 0) return 180 * iPrec; else return 0; if (!dy) if (dx > 0) return 90 * iPrec; else return 270 * iPrec; - iAngle = static_cast(180.0 * iPrec * atan2(static_cast(Abs(dy)), static_cast(Abs(dx))) / M_PI); + iAngle = static_cast(180.0 * iPrec * atan2(static_cast(Abs(dy)), static_cast(Abs(dx))) * std::numbers::inv_pi); if (iX2 > iX1) { @@ -3276,7 +3277,7 @@ static C4ValueInt FnArcSin(C4AulContext *cthr, C4ValueInt iVal, C4ValueInt iRadi if (iVal > iRadius) return 0; // calc arcsin double f1 = iVal; - f1 = asin(f1 / iRadius) * 180.0 / M_PI; + f1 = asin(f1 / iRadius) * 180.0 * std::numbers::inv_pi; // return rounded angle return static_cast(floor(f1 + 0.5)); } @@ -3288,7 +3289,7 @@ static C4ValueInt FnArcCos(C4AulContext *cthr, C4ValueInt iVal, C4ValueInt iRadi if (iVal > iRadius) return 0; // calc arccos double f1 = iVal; - f1 = acos(f1 / iRadius) * 180.0 / M_PI; + f1 = acos(f1 / iRadius) * 180.0 * std::numbers::inv_pi; // return rounded angle return static_cast(floor(f1 + 0.5)); } diff --git a/src/Standard.cpp b/src/Standard.cpp index 903dae147..ceff4e730 100644 --- a/src/Standard.cpp +++ b/src/Standard.cpp @@ -34,6 +34,7 @@ #include #include #include +#include // Basics @@ -50,7 +51,7 @@ int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2) int Angle(int iX1, int iY1, int iX2, int iY2) { - int iAngle = static_cast(180.0 * atan2f(float(Abs(iY1 - iY2)), float(Abs(iX1 - iX2))) / M_PI); + int iAngle = static_cast(180.0 * atan2f(float(Abs(iY1 - iY2)), float(Abs(iX1 - iX2))) * std::numbers::inv_pi_v); if (iX2 > iX1) { if (iY2 < iY1) iAngle = 90 - iAngle; else iAngle = 90 + iAngle; diff --git a/src/StdDDraw2.cpp b/src/StdDDraw2.cpp index 19bc98ad4..c48cfd49c 100644 --- a/src/StdDDraw2.cpp +++ b/src/StdDDraw2.cpp @@ -29,6 +29,7 @@ #include #include #include +#include // Global access pointer CStdDDraw *lpDDraw = nullptr; @@ -914,7 +915,7 @@ bool CStdDDraw::BlitRotate(C4Surface *sfcSource, int fx, int fy, int fwdt, int f default: // Calculate rotation matrix - dang = M_PI * iAngle / 18000.0; + dang = std::numbers::pi * iAngle / 18000.0; mtx[0] = cos(dang); mtx[1] = -sin(dang); mtx[2] = sin(dang); mtx[3] = cos(dang); // Blit source rect diff --git a/src/StdJoystick.cpp b/src/StdJoystick.cpp index cd9109682..3d748f6bc 100644 --- a/src/StdJoystick.cpp +++ b/src/StdJoystick.cpp @@ -22,6 +22,8 @@ #include "C4Windows.h" #include +#include + uint32_t POV2Position(DWORD dwPOV, bool fVertical) { // POV value is a 360° angle multiplied by 100 @@ -31,7 +33,7 @@ uint32_t POV2Position(DWORD dwPOV, bool fVertical) dAxis = 0.0; // Angle: convert to linear value -100 to +100 else - dAxis = (fVertical ? -cos((dwPOV / 100) * M_PI / 180.0) : sin((dwPOV / 100) * M_PI / 180.0)) * 100.0; + dAxis = (fVertical ? -cos((dwPOV / 100) * std::numbers::pi / 180.0) : sin((dwPOV / 100) * std::numbers::pi / 180.0)) * 100.0; // Gamepad configuration wants unsigned and gets 0 to 200 return (uint32_t)(dAxis + 100.0); }