Extract math-related functions from common.h to Math.hpp

Saves wide inclusion of <stdexcept> header, which pulls in <string>
This commit is contained in:
Michał Janiszewski 2021-10-09 21:43:06 +02:00
parent b8c751e300
commit 1b4875e528
15 changed files with 51 additions and 29 deletions

View File

@ -19,6 +19,7 @@
#include <openrct2/ride/RideData.h>
#include <openrct2/scenario/Scenario.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Math.hpp>
#include <openrct2/util/Util.h>
#include <openrct2/world/Park.h>
#include <openrct2/world/Sprite.h>

View File

@ -32,6 +32,7 @@
#include <openrct2/ride/Track.h>
#include <openrct2/ride/TrackData.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Math.hpp>
#include <openrct2/windows/Intent.h>
#include <openrct2/world/Entrance.h>
#include <openrct2/world/Footpath.h>

View File

@ -56,7 +56,7 @@
#include <openrct2/peep/Staff.h>
#include <openrct2/scenario/Scenario.h>
#include <openrct2/ui/UiContext.h>
#include <openrct2/util/Util.h>
#include <openrct2/util/Math.hpp>
#include <openrct2/windows/Intent.h>
#include <openrct2/world/Footpath.h>
#include <openrct2/world/LargeScenery.h>

View File

@ -18,7 +18,7 @@
#include "../localisation/StringIds.h"
#include "../management/Finance.h"
#include "../ride/RideData.h"
#include "../util/Util.h"
#include "../util/Math.hpp"
#include "../windows/Intent.h"
#include "../world/Park.h"
#include "../world/Scenery.h"

View File

@ -15,7 +15,7 @@
#include "../ride/Track.h"
#include "../ride/TrackData.h"
#include "../ride/TrackDesign.h"
#include "../util/Util.h"
#include "../util/Math.hpp"
#include "../world/MapAnimation.h"
#include "../world/Surface.h"
#include "RideSetSettingAction.h"

View File

@ -23,7 +23,6 @@
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
using utf8 = char;
using utf8string = utf8*;
@ -38,30 +37,6 @@ using const_utf8string = const utf8*;
using codepoint_t = uint32_t;
using colour_t = uint8_t;
namespace
{
[[maybe_unused]] constexpr bool is_power_of_2(int v)
{
return v && ((v & (v - 1)) == 0);
}
// Rounds an integer down to the given power of 2. y must be a power of 2.
[[maybe_unused]] constexpr int floor2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("floor2 should only operate on power of 2");
return x & ~(y - 1);
}
// Rounds an integer up to the given power of 2. y must be a power of 2.
[[maybe_unused]] constexpr int ceil2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("ceil2 should only operate on power of 2");
return (x + y - 1) & ~(y - 1);
}
} // namespace
// Gets the name of a symbol as a C string
#define nameof(symbol) #symbol

View File

@ -11,6 +11,8 @@
#include "../common.h"
#include <string>
enum class CursorID : uint8_t
{
Arrow,

View File

@ -25,6 +25,7 @@
#include "../ride/Vehicle.h"
#include "../ui/UiContext.h"
#include "../ui/WindowManager.h"
#include "../util/Math.hpp"
#include "../world/Climate.h"
#include "../world/EntityList.h"
#include "../world/Map.h"

View File

@ -8,6 +8,7 @@
*****************************************************************************/
#include "../Game.h"
#include "../util/Math.hpp"
#include "Date.h"
#include "StringIds.h"

View File

@ -17,6 +17,7 @@
#include "../localisation/Localisation.h"
#include "../localisation/LocalisationService.h"
#include "../paint/Painter.h"
#include "../util/Math.hpp"
#include "sprite/Paint.Sprite.h"
#include "tile_element/Paint.TileElement.h"

View File

@ -12,6 +12,7 @@
#include "../interface/Viewport.h"
#include "../object/FootpathRailingsObject.h"
#include "../sprites.h"
#include "../util/Math.hpp"
#include "../world/Surface.h"
#include "Paint.h"
#include "tile_element/Paint.TileElement.h"

View File

@ -30,7 +30,7 @@
#include "../scenario/Scenario.h"
#include "../scripting/HookEngine.h"
#include "../scripting/ScriptEngine.h"
#include "../util/Util.h"
#include "../util/Math.hpp"
#include "../windows/Intent.h"
#include "../world/Balloon.h"
#include "../world/Climate.h"

View File

@ -0,0 +1,37 @@
/*****************************************************************************
* Copyright (c) 2014-2021 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include <stdexcept>
namespace
{
[[maybe_unused]] constexpr bool is_power_of_2(int v)
{
return v && ((v & (v - 1)) == 0);
}
// Rounds an integer down to the given power of 2. y must be a power of 2.
[[maybe_unused]] constexpr int floor2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("floor2 should only operate on power of 2");
return x & ~(y - 1);
}
// Rounds an integer up to the given power of 2. y must be a power of 2.
[[maybe_unused]] constexpr int ceil2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("ceil2 should only operate on power of 2");
return (x + y - 1) & ~(y - 1);
}
} // namespace

View File

@ -20,6 +20,7 @@
#include "../ride/RideData.h"
#include "../ride/Track.h"
#include "../ride/TrackData.h"
#include "../util/Math.hpp"
#include "../world/Banner.h"
#include "../world/Scenery.h"
#include "Intent.h"

View File

@ -10,6 +10,7 @@
#pragma once
#include "../common.h"
#include "../util/Math.hpp"
#include <algorithm>