aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-06 13:16:05 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:54 +0200
commit0e40bc7ce8c3b3be083002f88c3317d65f6570ad (patch)
tree2fcc470a0f1ce1d51ff26c53c8a9a890b3f31b3b
parentf4d812a5b9131e65bb55db7211dc68fc453792df (diff)
refactor: make vector2 & bounds data classes
-rw-r--r--src/CMakeLists.txt4
-rw-r--r--src/bootstrap.cpp21
-rw-r--r--src/engine/data/bounds.cpp (renamed from src/engine/graphics/bounds.cpp)18
-rw-r--r--src/engine/data/bounds.hpp42
-rw-r--r--src/engine/data/vector2.cpp39
-rw-r--r--src/engine/data/vector2.hpp52
-rw-r--r--src/engine/graphics/bounds.hpp28
-rw-r--r--src/engine/graphics/string_matrix.cpp6
-rw-r--r--src/engine/graphics/string_matrix.hpp11
-rw-r--r--src/engine/graphics/vector2.cpp39
-rw-r--r--src/engine/graphics/vector2.hpp48
-rw-r--r--src/engine/user/cursor.cpp13
-rw-r--r--src/engine/user/cursor.hpp15
-rw-r--r--src/interfaces/bounds.hpp41
-rw-r--r--src/interfaces/matrix.hpp10
-rw-r--r--src/interfaces/vector2.hpp44
16 files changed, 171 insertions, 260 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e9b8fee..304aba1 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -16,8 +16,8 @@ file(GLOB SOURCES
game/game.cpp
game/input_configurator.cpp
engine/game_initializer.cpp
- engine/graphics/vector2.cpp
- engine/graphics/bounds.cpp
+ engine/data/vector2.cpp
+ engine/data/bounds.cpp
engine/graphics/scene.cpp
engine/graphics/string_matrix.cpp
engine/user/input.cpp
diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp
index 343f01b..a0d8ab2 100644
--- a/src/bootstrap.cpp
+++ b/src/bootstrap.cpp
@@ -2,7 +2,6 @@
// Interfaces
#include "interfaces/argument_parser.hpp"
-#include "interfaces/bounds.hpp"
#include "interfaces/game.hpp"
#include "interfaces/game_initializer.hpp"
#include "interfaces/input.hpp"
@@ -10,15 +9,14 @@
#include "interfaces/matrix.hpp"
#include "interfaces/randomization.hpp"
#include "interfaces/scene.hpp"
-#include "interfaces/vector2.hpp"
// Implementations
#include "argument_parser.hpp"
+#include "engine/data/bounds.hpp"
+#include "engine/data/vector2.hpp"
#include "engine/game_initializer.hpp"
-#include "engine/graphics/bounds.hpp"
#include "engine/graphics/scene.hpp"
#include "engine/graphics/string_matrix.hpp"
-#include "engine/graphics/vector2.hpp"
#include "engine/user/cursor.hpp"
#include "engine/user/input.hpp"
#include "game/game.hpp"
@@ -56,25 +54,12 @@ Container bootstrap()
std::make_shared<SeedGenerator>(std::make_unique<std::random_device>()));
});
- container.bind<IVector2Factory>().to_factory(
- [](const IVector2Options &options)
- {
- return std::dynamic_pointer_cast<IVector2>(
- std::make_shared<Vector2>(options));
- });
-
container.bind<IMatrixFactory<std::string_view>>().to_factory(
- [](const IBounds &bounds)
+ [](const Bounds &bounds)
{
return std::dynamic_pointer_cast<IMatrix<std::string_view>>(
std::make_shared<StringMatrix>(bounds));
});
- container.bind<IBoundsFactory>().to_factory(
- [](const IBoundsOptions &options)
- {
- return std::dynamic_pointer_cast<IBounds>(std::make_shared<Bounds>(options));
- });
-
return container;
}
diff --git a/src/engine/graphics/bounds.cpp b/src/engine/data/bounds.cpp
index 8cb83eb..cd08a02 100644
--- a/src/engine/graphics/bounds.cpp
+++ b/src/engine/data/bounds.cpp
@@ -1,31 +1,31 @@
#include "bounds.hpp"
-Bounds::Bounds(const IBoundsOptions &options)
+Bounds::Bounds(const BoundsOptions &options)
: _width(options.width), _height(options.height)
{
}
-unsigned int Bounds::width() const
+uint32_t Bounds::width() const noexcept
{
return _width;
}
-void Bounds::width(unsigned int width)
+void Bounds::width(uint32_t width) noexcept
{
_width = width;
}
-unsigned int Bounds::height() const
+uint32_t Bounds::height() const noexcept
{
return _height;
}
-void Bounds::height(unsigned int height)
+void Bounds::height(uint32_t height) noexcept
{
_height = height;
}
-CoordsValidation Bounds::validate_coords(const IVector2 &coords) const
+CoordsValidation Bounds::validate_coords(const Vector2 &coords) const noexcept
{
if (coords.x() >= _width)
{
@@ -40,7 +40,7 @@ CoordsValidation Bounds::validate_coords(const IVector2 &coords) const
return CoordsValidation::VALID;
}
-const IBounds &Bounds::operator*=(const IBounds &bounds)
+const Bounds &Bounds::operator*=(const Bounds &bounds) noexcept
{
_width *= bounds.width();
_height *= bounds.height();
@@ -48,7 +48,7 @@ const IBounds &Bounds::operator*=(const IBounds &bounds)
return *this;
}
-const IBounds &Bounds::operator+=(const IBounds &bounds)
+const Bounds &Bounds::operator+=(const Bounds &bounds) noexcept
{
_width += bounds.width();
_height += bounds.height();
@@ -56,7 +56,7 @@ const IBounds &Bounds::operator+=(const IBounds &bounds)
return *this;
}
-const IBounds &Bounds::operator-=(const IBounds &bounds)
+const Bounds &Bounds::operator-=(const Bounds &bounds) noexcept
{
_width -= bounds.width();
_height -= bounds.height();
diff --git a/src/engine/data/bounds.hpp b/src/engine/data/bounds.hpp
new file mode 100644
index 0000000..1875bf4
--- /dev/null
+++ b/src/engine/data/bounds.hpp
@@ -0,0 +1,42 @@
+#pragma once
+
+#include "engine/data/vector2.hpp"
+
+#include <cstdint>
+
+enum CoordsValidation
+{
+ VALID,
+ X_HIGH,
+ Y_HIGH
+};
+
+struct BoundsOptions
+{
+ uint32_t width;
+ uint32_t height;
+};
+
+class Bounds
+{
+public:
+ explicit Bounds(const BoundsOptions &options);
+
+ [[nodiscard]] uint32_t width() const noexcept;
+
+ void width(uint32_t width) noexcept;
+
+ [[nodiscard]] uint32_t height() const noexcept;
+
+ void height(uint32_t height) noexcept;
+
+ [[nodiscard]] CoordsValidation validate_coords(const Vector2 &coords) const noexcept;
+
+ const Bounds &operator*=(const Bounds &bounds) noexcept;
+ const Bounds &operator+=(const Bounds &bounds) noexcept;
+ const Bounds &operator-=(const Bounds &bounds) noexcept;
+
+private:
+ uint32_t _width = 0U;
+ uint32_t _height = 0U;
+};
diff --git a/src/engine/data/vector2.cpp b/src/engine/data/vector2.cpp
new file mode 100644
index 0000000..5d42913
--- /dev/null
+++ b/src/engine/data/vector2.cpp
@@ -0,0 +1,39 @@
+#include "vector2.hpp"
+
+Vector2::Vector2(const Vector2Options &options) : _x(options.x), _y(options.y) {}
+
+uint32_t Vector2::x() const noexcept
+{
+ return _x;
+}
+
+void Vector2::x(uint32_t x) noexcept
+{
+ _x = x;
+}
+
+uint32_t Vector2::y() const noexcept
+{
+ return _y;
+}
+
+void Vector2::y(uint32_t y) noexcept
+{
+ _y = y;
+}
+
+const Vector2 &Vector2::operator+=(const Vector2 &vector2) noexcept
+{
+ _x += vector2.x();
+ _y += vector2.y();
+
+ return *this;
+}
+
+const Vector2 &Vector2::operator-=(const Vector2 &vector2) noexcept
+{
+ _x -= vector2.x();
+ _y -= vector2.y();
+
+ return *this;
+}
diff --git a/src/engine/data/vector2.hpp b/src/engine/data/vector2.hpp
new file mode 100644
index 0000000..580123c
--- /dev/null
+++ b/src/engine/data/vector2.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <cstdint>
+
+struct Vector2Options
+{
+ uint32_t x;
+ uint32_t y;
+};
+
+/**
+ * A 2D Vector.
+ */
+class Vector2
+{
+public:
+ /**
+ * Creates a 2D vector.
+ */
+ explicit Vector2(const Vector2Options &options);
+
+ /**
+ * Returns the X coordinate.
+ */
+ [[nodiscard]] uint32_t x() const noexcept;
+
+ /**
+ * Sets the X coordinate.
+ *
+ * @param x A new X coordinate
+ */
+ void x(uint32_t x) noexcept;
+
+ /**
+ * Returns the Y coordinate.
+ */
+ [[nodiscard]] uint32_t y() const noexcept;
+
+ /**
+ * Sets the Y coordinate.
+ *
+ * @param Y A new Y coordinate
+ */
+ void y(uint32_t y) noexcept;
+
+ const Vector2 &operator+=(const Vector2 &vector2) noexcept;
+ const Vector2 &operator-=(const Vector2 &vector2) noexcept;
+
+private:
+ uint32_t _x;
+ uint32_t _y;
+};
diff --git a/src/engine/graphics/bounds.hpp b/src/engine/graphics/bounds.hpp
deleted file mode 100644
index 3f4dd5b..0000000
--- a/src/engine/graphics/bounds.hpp
+++ /dev/null
@@ -1,28 +0,0 @@
-#pragma once
-
-#include "interfaces/bounds.hpp"
-#include "interfaces/vector2.hpp"
-
-class Bounds : public IBounds
-{
-public:
- explicit Bounds(const IBoundsOptions &options);
-
- [[nodiscard]] unsigned int width() const override;
-
- void width(unsigned int width) override;
-
- [[nodiscard]] unsigned int height() const override;
-
- void height(unsigned int height) override;
-
- [[nodiscard]] CoordsValidation validate_coords(const IVector2 &coords) const override;
-
- const IBounds &operator*=(const IBounds &bounds) override;
- const IBounds &operator+=(const IBounds &bounds) override;
- const IBounds &operator-=(const IBounds &bounds) override;
-
-private:
- unsigned int _width = 0U;
- unsigned int _height = 0U;
-};
diff --git a/src/engine/graphics/string_matrix.cpp b/src/engine/graphics/string_matrix.cpp
index e259e96..aae2b9d 100644
--- a/src/engine/graphics/string_matrix.cpp
+++ b/src/engine/graphics/string_matrix.cpp
@@ -1,6 +1,6 @@
#include "string_matrix.hpp"
-StringMatrix::StringMatrix(const IBounds &bounds)
+StringMatrix::StringMatrix(const Bounds &bounds)
: _rows(bounds.height()), _columns(bounds.width())
{
_matrix.reserve(bounds.height());
@@ -20,12 +20,12 @@ void StringMatrix::fill(std::string_view element)
}
}
-std::string_view StringMatrix::get(const IVector2 &pos) const
+std::string_view StringMatrix::get(const Vector2 &pos) const
{
return _matrix[pos.y()][pos.x()];
}
-void StringMatrix::set(const IVector2 &pos, std::string_view element)
+void StringMatrix::set(const Vector2 &pos, std::string_view element)
{
_matrix[pos.y()][pos.x()] = element;
}
diff --git a/src/engine/graphics/string_matrix.hpp b/src/engine/graphics/string_matrix.hpp
index 662ea7b..351053e 100644
--- a/src/engine/graphics/string_matrix.hpp
+++ b/src/engine/graphics/string_matrix.hpp
@@ -1,8 +1,9 @@
#pragma once
-#include "interfaces/bounds.hpp"
#include "interfaces/matrix.hpp"
-#include "interfaces/vector2.hpp"
+
+#include "engine/data/bounds.hpp"
+#include "engine/data/vector2.hpp"
#include <string_view>
#include <vector>
@@ -18,7 +19,7 @@ public:
*
* @param bounds The bounds of the matrix
*/
- explicit StringMatrix(const IBounds &bounds);
+ explicit StringMatrix(const Bounds &bounds);
/**
* Fills the matrix with a element.
@@ -32,7 +33,7 @@ public:
*
* @param pos The position of a element
*/
- [[nodiscard]] std::string_view get(const IVector2 &pos) const override;
+ [[nodiscard]] std::string_view get(const Vector2 &pos) const override;
/**
* Sets a element of the matrix.
@@ -40,7 +41,7 @@ public:
* @param pos The position of a element
* @param element A new element
*/
- void set(const IVector2 &pos, std::string_view element) override;
+ void set(const Vector2 &pos, std::string_view element) override;
/**
* Returns the number of rows the matrix has.
diff --git a/src/engine/graphics/vector2.cpp b/src/engine/graphics/vector2.cpp
deleted file mode 100644
index dfdf4bd..0000000
--- a/src/engine/graphics/vector2.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-#include "vector2.hpp"
-
-Vector2::Vector2(const IVector2Options &options) : _x(options.x), _y(options.y) {}
-
-unsigned int Vector2::x() const
-{
- return _x;
-}
-
-void Vector2::x(unsigned int x)
-{
- _x = x;
-}
-
-unsigned int Vector2::y() const
-{
- return _y;
-}
-
-void Vector2::y(unsigned int y)
-{
- _y = y;
-}
-
-const IVector2 &Vector2::operator+=(const IVector2 &vector2)
-{
- _x += vector2.x();
- _y += vector2.y();
-
- return *this;
-}
-
-const IVector2 &Vector2::operator-=(const IVector2 &vector2)
-{
- _x -= vector2.x();
- _y -= vector2.y();
-
- return *this;
-}
diff --git a/src/engine/graphics/vector2.hpp b/src/engine/graphics/vector2.hpp
deleted file mode 100644
index c8c6349..0000000
--- a/src/engine/graphics/vector2.hpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#pragma once
-
-#include "interfaces/vector2.hpp"
-
-#include <memory>
-
-/**
- * A 2D Vector.
- */
-class Vector2 : public IVector2
-{
-public:
- /**
- * Creates a 2D vector.
- */
- explicit Vector2(const IVector2Options &options);
-
- /**
- * Returns the X coordinate.
- */
- [[nodiscard]] unsigned int x() const override;
-
- /**
- * Sets the X coordinate.
- *
- * @param x A new X coordinate
- */
- void x(unsigned int x) override;
-
- /**
- * Returns the Y coordinate.
- */
- [[nodiscard]] unsigned int y() const override;
-
- /**
- * Sets the Y coordinate.
- *
- * @param Y A new Y coordinate
- */
- void y(unsigned int y) override;
-
- const IVector2 &operator+=(const IVector2 &vector2) override;
- const IVector2 &operator-=(const IVector2 &vector2) override;
-
-private:
- unsigned int _x;
- unsigned int _y;
-};
diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp
index 9d6e28c..d4a2997 100644
--- a/src/engine/user/cursor.cpp
+++ b/src/engine/user/cursor.cpp
@@ -5,29 +5,24 @@
#include <cstdlib>
#include <iostream>
-CursorController::CursorController(IVector2Factory vector2_factory)
- : _vector2_factory(vector2_factory)
-{
-}
-
-void CursorController::move_to(const IVector2 &pos)
+void CursorController::move_to(const Vector2 &pos)
{
fmt::print(MOVE_CURSOR_TO, fmt::arg("esc", ESC), fmt::arg("row", pos.y()),
fmt::arg("column", pos.x()));
std::cout.flush();
}
-std::shared_ptr<IVector2> CursorController::where()
+Vector2 CursorController::where()
{
fmt::print(REQUEST_CURSOR_POSITION, fmt::arg("esc", ESC));
std::cout.flush();
- IVector2Options vector2_options = {};
+ Vector2Options vector2_options = {};
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
scanf("\033[%u;%uR", &vector2_options.y, &vector2_options.x);
- return _vector2_factory(vector2_options);
+ return Vector2(vector2_options);
}
void CursorController::hide()
diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp
index 0317dc5..69d639a 100644
--- a/src/engine/user/cursor.hpp
+++ b/src/engine/user/cursor.hpp
@@ -2,7 +2,8 @@
#include "DI/auto_wirable.hpp"
#include "interfaces/direction.hpp"
-#include "interfaces/vector2.hpp"
+
+#include "engine/data/vector2.hpp"
#include "fmt/core.h"
#include <array>
@@ -21,25 +22,21 @@ 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";
-class CursorController
- : public AutoWirable<CursorController, CursorController, IVector2Factory>
+class CursorController : public AutoWirable<CursorController, CursorController>
{
public:
- explicit CursorController(IVector2Factory vector2_factory);
+ CursorController() = default;
template <Direction::value_type direction>
constexpr void move(const unsigned int &amount) const;
- static void move_to(const IVector2 &pos);
+ static void move_to(const Vector2 &pos);
static void hide();
static void show();
- [[nodiscard]] std::shared_ptr<IVector2> where();
-
-private:
- IVector2Factory _vector2_factory;
+ [[nodiscard]] static Vector2 where();
};
#include "cursor.tpp"
diff --git a/src/interfaces/bounds.hpp b/src/interfaces/bounds.hpp
deleted file mode 100644
index b431874..0000000
--- a/src/interfaces/bounds.hpp
+++ /dev/null
@@ -1,41 +0,0 @@
-#pragma once
-
-#include "interfaces/vector2.hpp"
-
-#include <memory>
-
-enum CoordsValidation
-{
- VALID,
- X_HIGH,
- Y_HIGH
-};
-
-class IBounds
-{
-public:
- virtual ~IBounds() = default;
-
- [[nodiscard]] virtual unsigned int width() const = 0;
-
- virtual void width(unsigned int width) = 0;
-
- [[nodiscard]] virtual unsigned int height() const = 0;
-
- virtual void height(unsigned int height) = 0;
-
- [[nodiscard]] virtual CoordsValidation
- validate_coords(const IVector2 &coords) const = 0;
-
- virtual const IBounds &operator*=(const IBounds &bounds) = 0;
- virtual const IBounds &operator+=(const IBounds &bounds) = 0;
- virtual const IBounds &operator-=(const IBounds &bounds) = 0;
-};
-
-struct IBoundsOptions
-{
- unsigned int width;
- unsigned int height;
-};
-
-using IBoundsFactory = std::shared_ptr<IBounds> (*)(const IBoundsOptions &options);
diff --git a/src/interfaces/matrix.hpp b/src/interfaces/matrix.hpp
index 72d18d9..8242b18 100644
--- a/src/interfaces/matrix.hpp
+++ b/src/interfaces/matrix.hpp
@@ -1,7 +1,7 @@
#pragma once
-#include "interfaces/bounds.hpp"
-#include "interfaces/vector2.hpp"
+#include "engine/data/bounds.hpp"
+#include "engine/data/vector2.hpp"
#include <memory>
@@ -23,7 +23,7 @@ public:
*
* @param pos The position of a element
*/
- [[nodiscard]] virtual Element get(const IVector2 &pos) const = 0;
+ [[nodiscard]] virtual Element get(const Vector2 &pos) const = 0;
/**
* Sets a element of the matrix.
@@ -31,7 +31,7 @@ public:
* @param pos The position of a element
* @param element A new element
*/
- virtual void set(const IVector2 &pos, Element element) = 0;
+ virtual void set(const Vector2 &pos, Element element) = 0;
/**
* Returns the number of rows the matrix has.
@@ -45,4 +45,4 @@ public:
};
template <typename Element>
-using IMatrixFactory = std::shared_ptr<IMatrix<Element>> (*)(const IBounds &bounds);
+using IMatrixFactory = std::shared_ptr<IMatrix<Element>> (*)(const Bounds &bounds);
diff --git a/src/interfaces/vector2.hpp b/src/interfaces/vector2.hpp
deleted file mode 100644
index dfd369f..0000000
--- a/src/interfaces/vector2.hpp
+++ /dev/null
@@ -1,44 +0,0 @@
-#pragma once
-
-#include <memory>
-
-class IVector2
-{
-public:
- virtual ~IVector2() = default;
-
- /**
- * Returns the X coordinate.
- */
- [[nodiscard]] virtual unsigned int x() const = 0;
-
- /**
- * Sets the X coordinate.
- *
- * @param x A new X coordinate
- */
- virtual void x(unsigned int x) = 0;
-
- /**
- * Returns the Y coordinate.
- */
- [[nodiscard]] virtual unsigned int y() const = 0;
-
- /**
- * Sets the Y coordinate.
- *
- * @param Y A new Y coordinate
- */
- virtual void y(unsigned int y) = 0;
-
- virtual const IVector2 &operator+=(const IVector2 &vector2) = 0;
- virtual const IVector2 &operator-=(const IVector2 &vector2) = 0;
-};
-
-struct IVector2Options
-{
- unsigned int x;
- unsigned int y;
-};
-
-using IVector2Factory = std::shared_ptr<IVector2> (*)(const IVector2Options &options);