aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/engine/data/vector2.cpp37
-rw-r--r--src/engine/data/vector2.hpp29
-rw-r--r--src/engine/user/cursor.cpp9
-rw-r--r--src/engine/user/cursor.hpp15
-rw-r--r--src/engine/user/cursor.tpp31
-rw-r--r--src/game/input_configurator.cpp16
-rw-r--r--src/game/input_configurator.hpp7
-rw-r--r--src/game/input_configurator.tpp17
-rw-r--r--src/interfaces/direction.hpp13
-rw-r--r--src/util/hash.hpp10
-rw-r--r--src/util/hash.tpp22
11 files changed, 130 insertions, 76 deletions
diff --git a/src/engine/data/vector2.cpp b/src/engine/data/vector2.cpp
index 6dcd7d6..c182fba 100644
--- a/src/engine/data/vector2.cpp
+++ b/src/engine/data/vector2.cpp
@@ -1,5 +1,7 @@
#include "vector2.hpp"
+#include "util/hash.hpp"
+
Vector2::Vector2(const Vector2Options &options) : _x(options.x), _y(options.y) {}
int32_t Vector2::get_x() const noexcept
@@ -37,3 +39,38 @@ const Vector2 &Vector2::operator-=(const Vector2 &vector2) noexcept
return *this;
}
+
+bool Vector2::operator==(const Vector2 &vector2) const noexcept
+{
+ return _x == vector2._x && _y == vector2._y;
+}
+
+Vector2 Vector2::up()
+{
+ return Vector2({.x = 0, .y = 1});
+}
+
+Vector2 Vector2::down()
+{
+ return Vector2({.x = 0, .y = -1});
+}
+
+Vector2 Vector2::left()
+{
+ return Vector2({.x = -1, .y = 0});
+}
+
+Vector2 Vector2::right()
+{
+ return Vector2({.x = 1, .y = 0});
+}
+
+std::size_t Vector2Hasher::operator()(const Vector2 &vector2) const
+{
+ std::size_t result_hash = 0;
+
+ hash_combine(result_hash, vector2.get_x());
+ hash_combine(result_hash, vector2.get_y());
+
+ return result_hash;
+}
diff --git a/src/engine/data/vector2.hpp b/src/engine/data/vector2.hpp
index acfe11f..dff548c 100644
--- a/src/engine/data/vector2.hpp
+++ b/src/engine/data/vector2.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
+#include <functional>
struct Vector2Options
{
@@ -27,7 +28,35 @@ public:
const Vector2 &operator+=(const Vector2 &vector2) noexcept;
const Vector2 &operator-=(const Vector2 &vector2) noexcept;
+ bool operator==(const Vector2 &vector2) const noexcept;
+
+ /**
+ * Returns Vector2({.x = 0, .y = 1})
+ */
+ static Vector2 up();
+
+ /**
+ * Returns Vector2({.x = 0, .y = -1})
+ */
+ static Vector2 down();
+
+ /**
+ * Returns Vector2({.x = -1, .y = 0})
+ */
+ static Vector2 left();
+
+ /**
+ * Returns Vector2({.x = 1, .y = 0})
+ */
+ static Vector2 right();
+
private:
int32_t _x;
int32_t _y;
};
+
+class Vector2Hasher
+{
+public:
+ std::size_t operator()(const Vector2 &vector2) const;
+};
diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp
index 44e24dc..391a91a 100644
--- a/src/engine/user/cursor.cpp
+++ b/src/engine/user/cursor.cpp
@@ -5,6 +5,15 @@
#include <cstdlib>
#include <iostream>
+void CursorController::move(const Vector2 &direction, const uint32_t &amount)
+{
+ auto format = direction_format_map.at(direction);
+
+ fmt::print(fmt::runtime(format.data()), fmt::arg("esc", ESC),
+ fmt::arg("amount", amount));
+ std::cout.flush();
+}
+
void CursorController::move_to(const Vector2 &pos)
{
fmt::print(MOVE_CURSOR_TO, fmt::arg("esc", ESC), fmt::arg("row", pos.get_y()),
diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp
index 70a2bf2..84117c1 100644
--- a/src/engine/user/cursor.hpp
+++ b/src/engine/user/cursor.hpp
@@ -1,13 +1,13 @@
#pragma once
#include "DI/auto_wirable.hpp"
-#include "interfaces/direction.hpp"
#include "engine/data/vector2.hpp"
-#include "fmt/core.h"
#include <array>
+#include <fmt/core.h>
#include <memory>
+#include <string_view>
#include <unordered_map>
constexpr std::string_view MOVE_CURSOR_UP = "{esc}[{amount}A";
@@ -22,13 +22,18 @@ constexpr fmt::string_view REQUEST_CURSOR_POSITION = "{esc}[6n";
constexpr fmt::string_view CURSOR_VISIBLE = "{esc}[?25h";
constexpr fmt::string_view CURSOR_INVISIBLE = "{esc}[?25l";
+const std::unordered_map<Vector2, std::string_view, Vector2Hasher> direction_format_map =
+ {{Vector2::up(), MOVE_CURSOR_UP},
+ {Vector2::down(), MOVE_CURSOR_DOWN},
+ {Vector2::left(), MOVE_CURSOR_LEFT},
+ {Vector2::right(), MOVE_CURSOR_RIGHT}};
+
class CursorController : public AutoWirable<CursorController, CursorController>
{
public:
CursorController() = default;
- template <Direction::value_type direction>
- constexpr void move(const uint32_t &amount) const;
+ static void move(const Vector2 &direction, const uint32_t &amount);
static void move_to(const Vector2 &pos);
@@ -38,5 +43,3 @@ public:
[[nodiscard]] static Vector2 where();
};
-
-#include "cursor.tpp"
diff --git a/src/engine/user/cursor.tpp b/src/engine/user/cursor.tpp
deleted file mode 100644
index 0743ae8..0000000
--- a/src/engine/user/cursor.tpp
+++ /dev/null
@@ -1,31 +0,0 @@
-#pragma once
-
-#include "cursor.hpp"
-
-#include "engine/escape.hpp"
-
-#include <iostream>
-
-constexpr auto get_direction_format_map()
-{
- std::array<std::string_view, 4> direction_format_map;
-
- direction_format_map[Direction::UP] = MOVE_CURSOR_UP;
- direction_format_map[Direction::DOWN] = MOVE_CURSOR_DOWN;
- direction_format_map[Direction::LEFT] = MOVE_CURSOR_LEFT;
- direction_format_map[Direction::RIGHT] = MOVE_CURSOR_RIGHT;
-
- return direction_format_map;
-}
-
-template <Direction::value_type direction>
-constexpr void CursorController::move(const uint32_t &amount) const
-{
- constexpr auto direction_format_map = get_direction_format_map();
-
- constexpr auto format = direction_format_map[direction];
-
- fmt::vprint(format,
- fmt::make_format_args(fmt::arg("esc", ESC), fmt::arg("amount", amount)));
- std::cout.flush();
-}
diff --git a/src/game/input_configurator.cpp b/src/game/input_configurator.cpp
index 4daee21..e8d2fe5 100644
--- a/src/game/input_configurator.cpp
+++ b/src/game/input_configurator.cpp
@@ -8,6 +8,14 @@ void exit_success()
exit(EXIT_SUCCESS);
}
+auto move_cursor(const Vector2 &direction, const CursorController &cursor_controller)
+{
+ return [direction, cursor_controller]()
+ {
+ cursor_controller.move(direction, 1U);
+ };
+}
+
} // namespace InputActions
InputConfigurator::InputConfigurator(std::shared_ptr<CursorController> cursor_controller)
@@ -20,14 +28,14 @@ void InputConfigurator::configure(IInputHandler &input_handler)
input_handler.attach('q', InputActions::exit_success);
input_handler.attach('k',
- InputActions::move_cursor<Direction::UP>(*_cursor_controller));
+ InputActions::move_cursor(Vector2::up(), *_cursor_controller));
input_handler.attach('j',
- InputActions::move_cursor<Direction::DOWN>(*_cursor_controller));
+ InputActions::move_cursor(Vector2::down(), *_cursor_controller));
input_handler.attach('h',
- InputActions::move_cursor<Direction::LEFT>(*_cursor_controller));
+ InputActions::move_cursor(Vector2::left(), *_cursor_controller));
input_handler.attach(
- 'l', InputActions::move_cursor<Direction::RIGHT>(*_cursor_controller));
+ 'l', InputActions::move_cursor(Vector2::right(), *_cursor_controller));
}
diff --git a/src/game/input_configurator.hpp b/src/game/input_configurator.hpp
index 217c21e..3846d50 100644
--- a/src/game/input_configurator.hpp
+++ b/src/game/input_configurator.hpp
@@ -1,10 +1,10 @@
#pragma once
#include "DI/auto_wirable.hpp"
-#include "interfaces/direction.hpp"
#include "interfaces/input.hpp"
#include "interfaces/input_configurator.hpp"
+#include "engine/data/vector2.hpp"
#include "engine/user/cursor.hpp"
#include <array>
@@ -13,8 +13,7 @@
namespace InputActions
{
-template <Direction::value_type direction>
-auto move_cursor(CursorController cursor_controller);
+auto move_cursor(const Vector2 &direction, const CursorController &cursor_controller);
void exit_success();
@@ -32,5 +31,3 @@ public:
private:
std::shared_ptr<CursorController> _cursor_controller;
};
-
-#include "input_configurator.tpp"
diff --git a/src/game/input_configurator.tpp b/src/game/input_configurator.tpp
deleted file mode 100644
index f3c9482..0000000
--- a/src/game/input_configurator.tpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-#include "input_configurator.hpp"
-
-namespace InputActions
-{
-
-template <Direction::value_type direction>
-auto move_cursor(CursorController cursor_controller)
-{
- return [cursor_controller]()
- {
- cursor_controller.move<direction>(1U);
- };
-}
-
-} // namespace InputActions
diff --git a/src/interfaces/direction.hpp b/src/interfaces/direction.hpp
deleted file mode 100644
index 233f17d..0000000
--- a/src/interfaces/direction.hpp
+++ /dev/null
@@ -1,13 +0,0 @@
-#pragma once
-
-#include <cstddef>
-
-struct Direction
-{
- using value_type = std::size_t;
-
- static constexpr value_type UP = 0U;
- static constexpr value_type DOWN = 1U;
- static constexpr value_type LEFT = 2U;
- static constexpr value_type RIGHT = 3U;
-};
diff --git a/src/util/hash.hpp b/src/util/hash.hpp
new file mode 100644
index 0000000..72e9b82
--- /dev/null
+++ b/src/util/hash.hpp
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <cstddef>
+
+constexpr auto GOLDEN_RATIO = 0x9e3779b9;
+
+template <typename Value>
+void hash_combine(std::size_t &seed, const Value &value) noexcept;
+
+#include "hash.tpp"
diff --git a/src/util/hash.tpp b/src/util/hash.tpp
new file mode 100644
index 0000000..146cfa0
--- /dev/null
+++ b/src/util/hash.tpp
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "hash.hpp"
+
+#include <cstdint>
+#include <functional>
+
+/**
+ * Combines the hash 'seed' with the hash of 'value'.
+ *
+ * @param seed A hash that will be mutated
+ * @param value A hashable value
+ */
+template <typename Value>
+void hash_combine(std::size_t &seed, const Value &value) noexcept
+{
+ constexpr uint32_t shift_left = 6;
+ constexpr uint32_t shift_right = 2;
+
+ seed ^= std::hash<Value>()(value) + GOLDEN_RATIO + (seed << shift_left) +
+ (seed >> shift_right);
+}