Skip to content

Commit

Permalink
Replace M_PI with constants from <numbers>
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed May 9, 2022
1 parent 8a26355 commit 6a87254
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 15 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,6 @@ if (NOT MSVC)
$<$<NOT:$<CONFIG:Debug>>:NDEBUG>)
endif ()

# Get non-standard <cmath> 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)
Expand Down
6 changes: 4 additions & 2 deletions src/C4Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <C4Game.h>
#include <C4Wrappers.h>

#include <numbers>

void C4Effect::AssignCallbackFunctions()
{
C4AulScript *pSrcScript = GetCallbackScript();
Expand Down Expand Up @@ -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<float>(pObj->r * M_PI / 180.0));
fRot[1] = -sinf(static_cast<float>(pObj->r * M_PI / 180.0));
fRot[0] = cosf(static_cast<float>(pObj->r * std::numbers::pi_v<float> / 180.0));
fRot[1] = -sinf(static_cast<float>(pObj->r * std::numbers::pi_v<float> / 180.0));
fRot[2] = -fRot[1];
fRot[3] = fRot[0];
// rotated objects usually better burn from the center
Expand Down
6 changes: 4 additions & 2 deletions src/C4Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <Bitmap256.h>

#include <numbers>

C4MapCreator::C4MapCreator()
{
Reset();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<double>(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<int32_t>(maxrange * (cy_curve + cy_natural)),
-maxrange, +maxrange);
Expand Down
7 changes: 4 additions & 3 deletions src/C4Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include <array>
#include <cinttypes>
#include <numbers>
#include <optional>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -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<C4ValueInt>(180.0 * iPrec * atan2(static_cast<double>(Abs(dy)), static_cast<double>(Abs(dx))) / M_PI);
iAngle = static_cast<C4ValueInt>(180.0 * iPrec * atan2(static_cast<double>(Abs(dy)), static_cast<double>(Abs(dx))) * std::numbers::inv_pi);

if (iX2 > iX1)
{
Expand All @@ -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<C4ValueInt>(floor(f1 + 0.5));
}
Expand All @@ -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<C4ValueInt>(floor(f1 + 0.5));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <cassert>
#include <cctype>
#include <cstring>
#include <numbers>

// Basics

Expand All @@ -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<int>(180.0 * atan2f(float(Abs(iY1 - iY2)), float(Abs(iX1 - iX2))) / M_PI);
int iAngle = static_cast<int>(180.0 * atan2f(float(Abs(iY1 - iY2)), float(Abs(iX1 - iX2))) * std::numbers::inv_pi_v<float>);
if (iX2 > iX1)
{
if (iY2 < iY1) iAngle = 90 - iAngle; else iAngle = 90 + iAngle;
Expand Down
3 changes: 2 additions & 1 deletion src/StdDDraw2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdio.h>
#include <limits.h>
#include <cmath>
#include <numbers>

// Global access pointer
CStdDDraw *lpDDraw = nullptr;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/StdJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "C4Windows.h"
#include <windowsx.h>

#include <numbers>

uint32_t POV2Position(DWORD dwPOV, bool fVertical)
{
// POV value is a 360° angle multiplied by 100
Expand All @@ -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);
}
Expand Down

0 comments on commit 6a87254

Please sign in to comment.