diff --git a/Source/.clang-tidy b/Source/.clang-tidy index a8627b56..753e0a90 100644 --- a/Source/.clang-tidy +++ b/Source/.clang-tidy @@ -81,3 +81,5 @@ CheckOptions: # `int8_t` aren't used as chars, disable misleading warning. - { key: bugprone-signed-char-misuse.CharTypdefsToIgnore, value: "std::int8_t" } + + - { key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic, value: true } diff --git a/Source/automap.cpp b/Source/automap.cpp index 89b103c8..0da4f90b 100644 --- a/Source/automap.cpp +++ b/Source/automap.cpp @@ -5,8 +5,6 @@ */ #include "automap.h" -#include - #include #include "control.h" @@ -17,6 +15,7 @@ #include "player.h" #include "setmaps.h" #include "utils/language.h" +#include "utils/stdcompat/algorithm.hpp" #include "utils/ui_fwd.h" namespace devilution { diff --git a/Source/automap.h b/Source/automap.h index 0ea00c22..10935547 100644 --- a/Source/automap.h +++ b/Source/automap.h @@ -8,6 +8,7 @@ #include #include "engine.h" +#include "engine/point.hpp" #include "gendung.h" namespace devilution { diff --git a/Source/control.h b/Source/control.h index b6620e3b..4e75b714 100644 --- a/Source/control.h +++ b/Source/control.h @@ -8,6 +8,7 @@ #include #include "engine.h" +#include "engine/point.hpp" #include "engine/render/text_render.hpp" #include "spelldat.h" #include "spells.h" diff --git a/Source/controls/plrctrls.cpp b/Source/controls/plrctrls.cpp index 734dce86..f4d6c420 100644 --- a/Source/controls/plrctrls.cpp +++ b/Source/controls/plrctrls.cpp @@ -6,11 +6,12 @@ #include "automap.h" #include "control.h" -#include "controls/controller.h" #include "controls/controller_motion.h" +#include "controls/controller.h" #include "controls/game_controls.h" #include "cursor.h" #include "doom.h" +#include "engine/point.hpp" #include "gmenu.h" #include "help.h" #include "inv.h" diff --git a/Source/cursor.cpp b/Source/cursor.cpp index 4b53afd9..1036e056 100644 --- a/Source/cursor.cpp +++ b/Source/cursor.cpp @@ -9,6 +9,7 @@ #include "control.h" #include "doom.h" +#include "engine/point.hpp" #include "engine.h" #include "engine/render/cel_render.hpp" #include "hwcursor.hpp" diff --git a/Source/dead.h b/Source/dead.h index ebe95eb8..e9684e91 100644 --- a/Source/dead.h +++ b/Source/dead.h @@ -8,6 +8,7 @@ #include #include +#include "engine/point.hpp" #include "engine.h" namespace devilution { diff --git a/Source/debug.cpp b/Source/debug.cpp index 47d668ac..9a78904f 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -5,6 +5,7 @@ */ #include "cursor.h" +#include "engine/point.hpp" #include "inv.h" #include "spells.h" #include "utils/language.h" diff --git a/Source/drlg_l1.cpp b/Source/drlg_l1.cpp index 154f5bdc..d0f4d1c2 100644 --- a/Source/drlg_l1.cpp +++ b/Source/drlg_l1.cpp @@ -5,6 +5,7 @@ */ #include "drlg_l1.h" +#include "engine/point.hpp" #include "gendung.h" #include "lighting.h" #include "player.h" diff --git a/Source/effects.cpp b/Source/effects.cpp index 350ff973..f069b311 100644 --- a/Source/effects.cpp +++ b/Source/effects.cpp @@ -3,9 +3,12 @@ * * Implementation of functions for loading and playing sounds. */ +#include "effects.h" + #include "init.h" #include "player.h" #include "sound.h" +#include "utils/stdcompat/algorithm.hpp" namespace devilution { namespace { diff --git a/Source/engine.h b/Source/engine.h index 1f7edb1b..afab6731 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -34,180 +34,14 @@ #endif #include "appfat.h" +#include "engine/point.hpp" #include "miniwin/miniwin.h" #include "utils/stdcompat/cstddef.hpp" -#include "utils/stdcompat/abs.h" #define TILE_WIDTH 64 #define TILE_HEIGHT 32 namespace devilution { -#if defined(__cpp_lib_clamp) -using std::clamp; -#else -template -constexpr const T &clamp(const T &x, const T &lower, const T &upper) -{ - return std::min(std::max(x, lower), upper); -} -#endif - -enum Direction : uint8_t { - DIR_S, - DIR_SW, - DIR_W, - DIR_NW, - DIR_N, - DIR_NE, - DIR_E, - DIR_SE, - DIR_OMNI, -}; - -struct Point { - int x; - int y; - - constexpr bool operator==(const Point &other) const - { - return x == other.x && y == other.y; - } - - constexpr bool operator!=(const Point &other) const - { - return !(*this == other); - } - - constexpr Point &operator+=(const Point &other) - { - x += other.x; - y += other.y; - return *this; - } - - constexpr Point &operator+=(Direction direction) - { - constexpr auto toPoint = [](Direction direction) -> Point { - switch (direction) { - case DIR_S: - return { 1, 1 }; - case DIR_SW: - return { 0, 1 }; - case DIR_W: - return { -1, 1 }; - case DIR_NW: - return { -1, 0 }; - case DIR_N: - return { -1, -1 }; - case DIR_NE: - return { 0, -1 }; - case DIR_E: - return { 1, -1 }; - case DIR_SE: - return { 1, 0 }; - default: - return { 0, 0 }; - } - }; - - return (*this) += toPoint(direction); - } - - constexpr Point &operator-=(const Point &other) - { - x -= other.x; - y -= other.y; - return *this; - } - - constexpr Point &operator*=(const float factor) - { - x *= factor; - y *= factor; - return *this; - } - - constexpr Point &operator*=(const int factor) - { - x *= factor; - y *= factor; - return *this; - } - - constexpr friend Point operator+(Point a, const Point &b) - { - a += b; - return a; - } - - constexpr friend Point operator+(Point a, Direction direction) - { - a += direction; - return a; - } - - constexpr friend Point operator-(Point a, const Point &b) - { - a -= b; - return a; - } - - constexpr friend Point operator-(const Point &a) - { - return { -a.x, -a.y }; - } - - constexpr friend Point operator*(Point a, const float factor) - { - a *= factor; - return a; - } - - constexpr friend Point operator*(Point a, const int factor) - { - a *= factor; - return a; - } - - /** - * @brief Fast approximate distance between two points, using only integer arithmetic, with less than ~5% error - * @param other Pointer to which we want the distance - * @return Magnitude of vector this -> other - */ - - constexpr int ApproxDistance(Point other) const - { - Point offset = abs(other - *this); - auto minMax = std::minmax(offset.x, offset.y); - int min = minMax.first; - int max = minMax.second; - - int approx = max * 1007 + min * 441; - if (max < (min * 16)) - approx -= max * 40; - - return (approx + 512) / 1024; - } - - constexpr friend Point abs(Point a) - { - return { abs(a.x), abs(a.y) }; - } - - constexpr int ManhattanDistance(Point other) const - { - Point offset = abs(*this - other); - - return offset.x + offset.y; - } - - constexpr int WalkingDistance(Point other) const - { - Point offset = abs(*this - other); - - return std::max(offset.x, offset.y); - } -}; struct Size { int width; diff --git a/Source/engine/direction.hpp b/Source/engine/direction.hpp new file mode 100644 index 00000000..f0a4ca6c --- /dev/null +++ b/Source/engine/direction.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace devilution { + +enum Direction : std::uint8_t { + DIR_S, + DIR_SW, + DIR_W, + DIR_NW, + DIR_N, + DIR_NE, + DIR_E, + DIR_SE, + DIR_OMNI, +}; + +} // namespace devilution diff --git a/Source/engine/point.hpp b/Source/engine/point.hpp new file mode 100644 index 00000000..2616f76e --- /dev/null +++ b/Source/engine/point.hpp @@ -0,0 +1,154 @@ +#pragma once + +#include "engine/direction.hpp" +#include "utils/stdcompat/abs.hpp" +#include "utils/stdcompat/algorithm.hpp" + +namespace devilution { + +struct Point { + int x; + int y; + + constexpr bool operator==(const Point &other) const + { + return x == other.x && y == other.y; + } + + constexpr bool operator!=(const Point &other) const + { + return !(*this == other); + } + + constexpr Point &operator+=(const Point &other) + { + x += other.x; + y += other.y; + return *this; + } + + constexpr Point &operator+=(Direction direction) + { + constexpr auto toPoint = [](Direction direction) -> Point { + switch (direction) { + case DIR_S: + return { 1, 1 }; + case DIR_SW: + return { 0, 1 }; + case DIR_W: + return { -1, 1 }; + case DIR_NW: + return { -1, 0 }; + case DIR_N: + return { -1, -1 }; + case DIR_NE: + return { 0, -1 }; + case DIR_E: + return { 1, -1 }; + case DIR_SE: + return { 1, 0 }; + default: + return { 0, 0 }; + } + }; + + return (*this) += toPoint(direction); + } + + constexpr Point &operator-=(const Point &other) + { + x -= other.x; + y -= other.y; + return *this; + } + + constexpr Point &operator*=(const float factor) + { + x *= factor; + y *= factor; + return *this; + } + + constexpr Point &operator*=(const int factor) + { + x *= factor; + y *= factor; + return *this; + } + + constexpr friend Point operator+(Point a, const Point &b) + { + a += b; + return a; + } + + constexpr friend Point operator+(Point a, Direction direction) + { + a += direction; + return a; + } + + constexpr friend Point operator-(Point a, const Point &b) + { + a -= b; + return a; + } + + constexpr friend Point operator-(const Point &a) + { + return { -a.x, -a.y }; + } + + constexpr friend Point operator*(Point a, const float factor) + { + a *= factor; + return a; + } + + constexpr friend Point operator*(Point a, const int factor) + { + a *= factor; + return a; + } + + /** + * @brief Fast approximate distance between two points, using only integer arithmetic, with less than ~5% error + * @param other Pointer to which we want the distance + * @return Magnitude of vector this -> other + */ + + constexpr int ApproxDistance(Point other) const + { + Point offset = abs(other - *this); + auto minMax = std::minmax(offset.x, offset.y); + int min = minMax.first; + int max = minMax.second; + + int approx = max * 1007 + min * 441; + if (max < (min * 16)) + approx -= max * 40; + + return (approx + 512) / 1024; + } + + constexpr friend Point abs(Point a) + { + return { abs(a.x), abs(a.y) }; + } + + constexpr int ManhattanDistance(Point other) const + { + Point offset = abs(*this - other); + + return offset.x + offset.y; + } + + constexpr int WalkingDistance(Point other) const + { + Point offset = abs(*this - other); + + return std::max(offset.x, offset.y); + } +}; + +} // namespace devilution diff --git a/Source/engine/render/automap_render.hpp b/Source/engine/render/automap_render.hpp index a37579f9..d8ff87c7 100644 --- a/Source/engine/render/automap_render.hpp +++ b/Source/engine/render/automap_render.hpp @@ -11,6 +11,7 @@ #pragma once #include "engine.h" +#include "engine/point.hpp" namespace devilution { diff --git a/Source/engine/render/cel_render.hpp b/Source/engine/render/cel_render.hpp index 389b0df3..fe699e6d 100644 --- a/Source/engine/render/cel_render.hpp +++ b/Source/engine/render/cel_render.hpp @@ -8,6 +8,7 @@ #include #include "engine.h" +#include "engine/point.hpp" namespace devilution { diff --git a/Source/engine/render/cl2_render.hpp b/Source/engine/render/cl2_render.hpp index 1eb28a9f..086613e4 100644 --- a/Source/engine/render/cl2_render.hpp +++ b/Source/engine/render/cl2_render.hpp @@ -9,6 +9,7 @@ #include #include "engine.h" +#include "engine/point.hpp" namespace devilution { diff --git a/Source/engine/render/text_render.cpp b/Source/engine/render/text_render.cpp index 38d51297..30e8d23e 100644 --- a/Source/engine/render/text_render.cpp +++ b/Source/engine/render/text_render.cpp @@ -3,11 +3,12 @@ * * Text rendering. */ - #include "text_render.hpp" + #include "DiabloUI/ui_item.h" #include "cel_render.hpp" #include "engine.h" +#include "engine/point.hpp" #include "palette.h" namespace devilution { diff --git a/Source/gendung.h b/Source/gendung.h index ccb82adf..5b4a7aa4 100644 --- a/Source/gendung.h +++ b/Source/gendung.h @@ -9,6 +9,7 @@ #include #include "engine.h" +#include "engine/point.hpp" #include "scrollrt.h" #include "utils/stdcompat/optional.hpp" diff --git a/Source/inv.h b/Source/inv.h index 8809e3dd..9da8f82d 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -7,6 +7,7 @@ #include +#include "engine/point.hpp" #include "items.h" #include "palette.h" #include "player.h" diff --git a/Source/items.h b/Source/items.h index 2b2194c7..07a6687d 100644 --- a/Source/items.h +++ b/Source/items.h @@ -9,6 +9,7 @@ #include "DiabloUI/ui_item.h" #include "engine/animationinfo.h" +#include "engine/point.hpp" #include "engine.h" #include "itemdat.h" #include "utils/stdcompat/optional.hpp" diff --git a/Source/lighting.h b/Source/lighting.h index 8fa47f9b..180fba9f 100644 --- a/Source/lighting.h +++ b/Source/lighting.h @@ -8,6 +8,7 @@ #include #include "engine.h" +#include "engine/point.hpp" #include "miniwin/miniwin.h" namespace devilution { diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 99917cb4..039b9627 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -15,6 +15,7 @@ #include "dead.h" #include "doom.h" #include "engine.h" +#include "engine/point.hpp" #include "init.h" #include "inv.h" #include "lighting.h" diff --git a/Source/missiles.h b/Source/missiles.h index d1fa4c1d..c372ff0e 100644 --- a/Source/missiles.h +++ b/Source/missiles.h @@ -9,6 +9,7 @@ #include "miniwin/miniwin.h" #include "engine.h" +#include "engine/point.hpp" #include "misdat.h" #include "spelldat.h" diff --git a/Source/monster.h b/Source/monster.h index 25592a82..a1015bc8 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -9,6 +9,7 @@ #include #include "engine.h" +#include "engine/point.hpp" #include "miniwin/miniwin.h" #include "utils/stdcompat/optional.hpp" #include "monstdat.h" diff --git a/Source/msg.h b/Source/msg.h index d40a4797..b41e640b 100644 --- a/Source/msg.h +++ b/Source/msg.h @@ -7,11 +7,12 @@ #include -#include "quests.h" -#include "objects.h" -#include "monster.h" -#include "portal.h" +#include "engine/point.hpp" #include "items.h" +#include "monster.h" +#include "objects.h" +#include "portal.h" +#include "quests.h" namespace devilution { diff --git a/Source/multi.cpp b/Source/multi.cpp index 811008c1..81c7a738 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -12,6 +12,7 @@ #include "DiabloUI/diabloui.h" #include "diablo.h" #include "dthread.h" +#include "engine/point.hpp" #include "mainmenu.h" #include "nthread.h" #include "options.h" diff --git a/Source/objects.h b/Source/objects.h index 9b4c0bb5..59b34ff1 100644 --- a/Source/objects.h +++ b/Source/objects.h @@ -7,9 +7,10 @@ #include +#include "engine/point.hpp" +#include "itemdat.h" #include "objdat.h" #include "textdat.h" -#include "itemdat.h" namespace devilution { @@ -93,7 +94,7 @@ void objects_rnd_454BEA(); * reconstructed note). This function both updates the state of the variable that tracks progress * and also determines whether the spawn conditions are met (i.e. all tomes have been triggered * in the correct order). - * + * * @param s the id of the spell tome * @return true if the player has activated all three tomes in the correct order, false otherwise */ diff --git a/Source/path.h b/Source/path.h index 72b7b66e..06cc17a9 100644 --- a/Source/path.h +++ b/Source/path.h @@ -7,7 +7,7 @@ #include -#include "engine.h" +#include "engine/point.hpp" namespace devilution { diff --git a/Source/player.h b/Source/player.h index 77cbaca7..c0b2337f 100644 --- a/Source/player.h +++ b/Source/player.h @@ -10,6 +10,7 @@ #include "diablo.h" #include "engine.h" +#include "engine/point.hpp" #include "gendung.h" #include "items.h" #include "multi.h" diff --git a/Source/portal.h b/Source/portal.h index 8e154df5..3d37a0e5 100644 --- a/Source/portal.h +++ b/Source/portal.h @@ -5,6 +5,7 @@ */ #pragma once +#include "engine/point.hpp" #include "gendung.h" namespace devilution { diff --git a/Source/qol/itemlabels.cpp b/Source/qol/itemlabels.cpp index f5dd472e..76ac2288 100644 --- a/Source/qol/itemlabels.cpp +++ b/Source/qol/itemlabels.cpp @@ -1,15 +1,18 @@ +#include "itemlabels.h" + #include #include #include -#include "inv.h" -#include "gmenu.h" -#include "cursor.h" #include "common.h" #include "control.h" +#include "cursor.h" +#include "engine/point.hpp" +#include "engine/render/cel_render.hpp" +#include "gmenu.h" +#include "inv.h" #include "itemlabels.h" #include "utils/language.h" -#include "engine/render/cel_render.hpp" namespace devilution { diff --git a/Source/qol/itemlabels.h b/Source/qol/itemlabels.h index a63a57d4..15425b76 100644 --- a/Source/qol/itemlabels.h +++ b/Source/qol/itemlabels.h @@ -5,6 +5,8 @@ */ #pragma once +#include "engine.h" + namespace devilution { void ToggleItemLabelHighlight(); diff --git a/Source/qol/xpbar.cpp b/Source/qol/xpbar.cpp index e4cbca02..f2650922 100644 --- a/Source/qol/xpbar.cpp +++ b/Source/qol/xpbar.cpp @@ -3,6 +3,7 @@ * * Adds XP bar QoL feature */ +#include "xpbar.h" #include @@ -11,6 +12,7 @@ #include "DiabloUI/art_draw.h" #include "common.h" #include "control.h" +#include "engine/point.hpp" #include "options.h" #include "utils/language.h" diff --git a/Source/quests.h b/Source/quests.h index 5c1035d1..55a05be4 100644 --- a/Source/quests.h +++ b/Source/quests.h @@ -8,6 +8,7 @@ #include #include "engine.h" +#include "engine/point.hpp" #include "gendung.h" #include "textdat.h" #include "utils/stdcompat/optional.hpp" diff --git a/Source/scrollrt.h b/Source/scrollrt.h index 1becf0b5..a1f12060 100644 --- a/Source/scrollrt.h +++ b/Source/scrollrt.h @@ -7,8 +7,9 @@ #include -#include "engine/animationinfo.h" #include "engine.h" +#include "engine/animationinfo.h" +#include "engine/point.hpp" namespace devilution { diff --git a/Source/sound.cpp b/Source/sound.cpp index cd55d904..daca1e92 100644 --- a/Source/sound.cpp +++ b/Source/sound.cpp @@ -23,6 +23,7 @@ #include "utils/log.hpp" #include "utils/math.h" #include "utils/sdl_mutex.h" +#include "utils/stdcompat/algorithm.hpp" #include "utils/stdcompat/optional.hpp" #include "utils/stdcompat/shared_ptr_array.hpp" #include "utils/stubs.h" diff --git a/Source/spells.cpp b/Source/spells.cpp index 95a42dca..493f6af4 100644 --- a/Source/spells.cpp +++ b/Source/spells.cpp @@ -3,9 +3,11 @@ * * Implementation of functionality for casting player spells. */ +#include "spells.h" #include "control.h" #include "cursor.h" +#include "engine/point.hpp" #include "gamemenu.h" #include "inv.h" #include "missiles.h" diff --git a/Source/track.cpp b/Source/track.cpp index ad89c0ab..adabe2f6 100644 --- a/Source/track.cpp +++ b/Source/track.cpp @@ -3,9 +3,12 @@ * * Implementation of functionality tracking what the mouse cursor is pointing at. */ +#include "track.h" + #include #include "cursor.h" +#include "engine/point.hpp" #include "player.h" namespace devilution { diff --git a/Source/trigs.h b/Source/trigs.h index 5df5fbcd..67b57c1e 100644 --- a/Source/trigs.h +++ b/Source/trigs.h @@ -5,7 +5,7 @@ */ #pragma once -#include "engine.h" +#include "engine/point.hpp" #include "interfac.h" namespace devilution { diff --git a/Source/utils/stdcompat/abs.h b/Source/utils/stdcompat/abs.hpp similarity index 77% rename from Source/utils/stdcompat/abs.h rename to Source/utils/stdcompat/abs.hpp index cebf718a..eaf350cc 100644 --- a/Source/utils/stdcompat/abs.h +++ b/Source/utils/stdcompat/abs.hpp @@ -1,17 +1,17 @@ -#pragma once - -#include - -namespace devilution { - -template -constexpr T abs(const T &a) -{ -#if defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) - return std::abs(a); -#else - return (a < 0) ? -a : a; -#endif -} - -} +#pragma once + +#include + +namespace devilution { + +template +constexpr T abs(const T &a) +{ +#if defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) + return std::abs(a); +#else + return (a < 0) ? -a : a; +#endif +} + +} // namespace devilution diff --git a/Source/utils/stdcompat/algorithm.hpp b/Source/utils/stdcompat/algorithm.hpp new file mode 100644 index 00000000..acf315cf --- /dev/null +++ b/Source/utils/stdcompat/algorithm.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include // IWYU pragma: export + +namespace devilution { +#if defined(__cplusplus) && __cplusplus >= 201703L +using std::clamp; // NOLINT(misc-unused-using-decls) +#else +template +constexpr const T &clamp(const T &x, const T &lower, const T &upper) +{ + return std::min(std::max(x, lower), upper); +} +#endif +} // namespace devilution diff --git a/Source/utils/stdcompat/cstddef.hpp b/Source/utils/stdcompat/cstddef.hpp index 493fbc7d..fead367b 100644 --- a/Source/utils/stdcompat/cstddef.hpp +++ b/Source/utils/stdcompat/cstddef.hpp @@ -1,15 +1,14 @@ -#pragma once - -#include -#ifdef __has_include -#if defined(__cplusplus) && __cplusplus >= 201703L -namespace devilution { -using byte = std::byte; -} -#else -#include -namespace devilution { -using byte = std::uint8_t; -} -#endif -#endif +#pragma once + +#include // IWYU pragma: export + +#if defined(__cplusplus) && __cplusplus >= 201703L +namespace devilution { +using byte = std::byte; +} // namespace devilution +#else +#include +namespace devilution { +using byte = std::uint8_t; +} // namespace devilution +#endif