diff --git a/Source/.clang-tidy b/Source/.clang-tidy index a8627b563..753e0a90f 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 89b103c83..0da4f90b3 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 0ea00c221..109355479 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 b6620e3bf..4e75b7145 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 734dce863..f4d6c4201 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 4b53afd9f..1036e0561 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 ebe95eb82..e9684e918 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 47d668acd..9a78904f1 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 154f5bdca..d0f4d1c26 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 350ff9731..f069b3110 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 1f7edb1bd..afab67319 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 000000000..f0a4ca6c6 --- /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 000000000..2616f76e1 --- /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 a37579f94..d8ff87c70 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 389b0df3a..fe699e6d2 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 1eb28a9f2..086613e4b 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 38d51297d..30e8d23e4 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 ccb82adf6..5b4a7aa4d 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 8809e3dd3..9da8f82d1 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 2b2194c7a..07a6687dd 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 8fa47f9be..180fba9f8 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 99917cb44..039b96275 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 d1fa4c1d2..c372ff0ed 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 25592a82f..a1015bc87 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 d40a47975..b41e640b3 100644 --- a/Source/msg.h +++ b/Source/msg.h @@ -7,11 +7,12 @@ #include -#include "quests.h" -#include "objects.h" +#include "engine/point.hpp" +#include "items.h" #include "monster.h" +#include "objects.h" #include "portal.h" -#include "items.h" +#include "quests.h" namespace devilution { diff --git a/Source/multi.cpp b/Source/multi.cpp index 811008c18..81c7a738e 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 9b4c0bb54..59b34ff1e 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 72b7b66e5..06cc17a9d 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 77cbaca71..c0b2337fc 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 8e154df51..3d37a0e52 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 f5dd472ef..76ac22881 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 a63a57d4b..15425b76d 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 e4cbca02a..f26509223 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 5c1035d10..55a05be46 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 1becf0b5c..a1f12060e 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 cd55d9041..daca1e925 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 95a42dca2..493f6af4c 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 ad89c0abb..adabe2f6c 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 5df5fbcd0..67b57c1e1 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 cebf718a0..eaf350cc6 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 000000000..acf315cfe --- /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 493fbc7d8..fead367b8 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