Browse Source

🚚 engine.h: Extract `Point`, `Direction`, `clamp`

`engine.h` is getting quite bloated. Moves this code to their own files.
pull/2204/head
Gleb Mazovetskiy 5 years ago committed by Anders Jenbo
parent
commit
f9f301b054
  1. 2
      Source/.clang-tidy
  2. 3
      Source/automap.cpp
  3. 1
      Source/automap.h
  4. 1
      Source/control.h
  5. 3
      Source/controls/plrctrls.cpp
  6. 1
      Source/cursor.cpp
  7. 1
      Source/dead.h
  8. 1
      Source/debug.cpp
  9. 1
      Source/drlg_l1.cpp
  10. 3
      Source/effects.cpp
  11. 168
      Source/engine.h
  12. 19
      Source/engine/direction.hpp
  13. 154
      Source/engine/point.hpp
  14. 1
      Source/engine/render/automap_render.hpp
  15. 1
      Source/engine/render/cel_render.hpp
  16. 1
      Source/engine/render/cl2_render.hpp
  17. 3
      Source/engine/render/text_render.cpp
  18. 1
      Source/gendung.h
  19. 1
      Source/inv.h
  20. 1
      Source/items.h
  21. 1
      Source/lighting.h
  22. 1
      Source/loadsave.cpp
  23. 1
      Source/missiles.h
  24. 1
      Source/monster.h
  25. 7
      Source/msg.h
  26. 1
      Source/multi.cpp
  27. 5
      Source/objects.h
  28. 2
      Source/path.h
  29. 1
      Source/player.h
  30. 1
      Source/portal.h
  31. 11
      Source/qol/itemlabels.cpp
  32. 2
      Source/qol/itemlabels.h
  33. 2
      Source/qol/xpbar.cpp
  34. 1
      Source/quests.h
  35. 3
      Source/scrollrt.h
  36. 1
      Source/sound.cpp
  37. 2
      Source/spells.cpp
  38. 3
      Source/track.cpp
  39. 2
      Source/trigs.h
  40. 34
      Source/utils/stdcompat/abs.hpp
  41. 15
      Source/utils/stdcompat/algorithm.hpp
  42. 29
      Source/utils/stdcompat/cstddef.hpp

2
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 }

3
Source/automap.cpp

@ -5,8 +5,6 @@
*/
#include "automap.h"
#include <algorithm>
#include <fmt/format.h>
#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 {

1
Source/automap.h

@ -8,6 +8,7 @@
#include <cstdint>
#include "engine.h"
#include "engine/point.hpp"
#include "gendung.h"
namespace devilution {

1
Source/control.h

@ -8,6 +8,7 @@
#include <cstdint>
#include "engine.h"
#include "engine/point.hpp"
#include "engine/render/text_render.hpp"
#include "spelldat.h"
#include "spells.h"

3
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"

1
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"

1
Source/dead.h

@ -8,6 +8,7 @@
#include <array>
#include <cstdint>
#include "engine/point.hpp"
#include "engine.h"
namespace devilution {

1
Source/debug.cpp

@ -5,6 +5,7 @@
*/
#include "cursor.h"
#include "engine/point.hpp"
#include "inv.h"
#include "spells.h"
#include "utils/language.h"

1
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"

3
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 {

168
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 <typename T>
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<int>(offset.x, offset.y);
}
};
struct Size {
int width;

19
Source/engine/direction.hpp

@ -0,0 +1,19 @@
#pragma once
#include <cstdint>
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

154
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<int>(offset.x, offset.y);
}
};
} // namespace devilution

1
Source/engine/render/automap_render.hpp

@ -11,6 +11,7 @@
#pragma once
#include "engine.h"
#include "engine/point.hpp"
namespace devilution {

1
Source/engine/render/cel_render.hpp

@ -8,6 +8,7 @@
#include <utility>
#include "engine.h"
#include "engine/point.hpp"
namespace devilution {

1
Source/engine/render/cl2_render.hpp

@ -9,6 +9,7 @@
#include <cstdint>
#include "engine.h"
#include "engine/point.hpp"
namespace devilution {

3
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 {

1
Source/gendung.h

@ -9,6 +9,7 @@
#include <memory>
#include "engine.h"
#include "engine/point.hpp"
#include "scrollrt.h"
#include "utils/stdcompat/optional.hpp"

1
Source/inv.h

@ -7,6 +7,7 @@
#include <cstdint>
#include "engine/point.hpp"
#include "items.h"
#include "palette.h"
#include "player.h"

1
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"

1
Source/lighting.h

@ -8,6 +8,7 @@
#include <array>
#include "engine.h"
#include "engine/point.hpp"
#include "miniwin/miniwin.h"
namespace devilution {

1
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"

1
Source/missiles.h

@ -9,6 +9,7 @@
#include "miniwin/miniwin.h"
#include "engine.h"
#include "engine/point.hpp"
#include "misdat.h"
#include "spelldat.h"

1
Source/monster.h

@ -9,6 +9,7 @@
#include <array>
#include "engine.h"
#include "engine/point.hpp"
#include "miniwin/miniwin.h"
#include "utils/stdcompat/optional.hpp"
#include "monstdat.h"

7
Source/msg.h

@ -7,11 +7,12 @@
#include <cstdint>
#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 {

1
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"

5
Source/objects.h

@ -7,9 +7,10 @@
#include <cstdint>
#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
*/

2
Source/path.h

@ -7,7 +7,7 @@
#include <SDL.h>
#include "engine.h"
#include "engine/point.hpp"
namespace devilution {

1
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"

1
Source/portal.h

@ -5,6 +5,7 @@
*/
#pragma once
#include "engine/point.hpp"
#include "gendung.h"
namespace devilution {

11
Source/qol/itemlabels.cpp

@ -1,15 +1,18 @@
#include "itemlabels.h"
#include <vector>
#include <string>
#include <unordered_set>
#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 {

2
Source/qol/itemlabels.h

@ -5,6 +5,8 @@
*/
#pragma once
#include "engine.h"
namespace devilution {
void ToggleItemLabelHighlight();

2
Source/qol/xpbar.cpp

@ -3,6 +3,7 @@
*
* Adds XP bar QoL feature
*/
#include "xpbar.h"
#include <array>
@ -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"

1
Source/quests.h

@ -8,6 +8,7 @@
#include <cstdint>
#include "engine.h"
#include "engine/point.hpp"
#include "gendung.h"
#include "textdat.h"
#include "utils/stdcompat/optional.hpp"

3
Source/scrollrt.h

@ -7,8 +7,9 @@
#include <cstdint>
#include "engine/animationinfo.h"
#include "engine.h"
#include "engine/animationinfo.h"
#include "engine/point.hpp"
namespace devilution {

1
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"

2
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"

3
Source/track.cpp

@ -3,9 +3,12 @@
*
* Implementation of functionality tracking what the mouse cursor is pointing at.
*/
#include "track.h"
#include <SDL.h>
#include "cursor.h"
#include "engine/point.hpp"
#include "player.h"
namespace devilution {

2
Source/trigs.h

@ -5,7 +5,7 @@
*/
#pragma once
#include "engine.h"
#include "engine/point.hpp"
#include "interfac.h"
namespace devilution {

34
Source/utils/stdcompat/abs.h → Source/utils/stdcompat/abs.hpp

@ -1,17 +1,17 @@
#pragma once
#include <algorithm>
namespace devilution {
template <typename T>
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 <cstdlib>
namespace devilution {
template <typename T>
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

15
Source/utils/stdcompat/algorithm.hpp

@ -0,0 +1,15 @@
#pragma once
#include <algorithm> // IWYU pragma: export
namespace devilution {
#if defined(__cplusplus) && __cplusplus >= 201703L
using std::clamp; // NOLINT(misc-unused-using-decls)
#else
template <typename T>
constexpr const T &clamp(const T &x, const T &lower, const T &upper)
{
return std::min(std::max(x, lower), upper);
}
#endif
} // namespace devilution

29
Source/utils/stdcompat/cstddef.hpp

@ -1,15 +1,14 @@
#pragma once
#include <cstddef>
#ifdef __has_include
#if defined(__cplusplus) && __cplusplus >= 201703L
namespace devilution {
using byte = std::byte;
}
#else
#include <cstdint>
namespace devilution {
using byte = std::uint8_t;
}
#endif
#endif
#pragma once
#include <cstddef> // IWYU pragma: export
#if defined(__cplusplus) && __cplusplus >= 201703L
namespace devilution {
using byte = std::byte;
} // namespace devilution
#else
#include <cstdint>
namespace devilution {
using byte = std::uint8_t;
} // namespace devilution
#endif

Loading…
Cancel
Save