From 0e40bc7ce8c3b3be083002f88c3317d65f6570ad Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 6 Mar 2022 13:16:05 +0100 Subject: refactor: make vector2 & bounds data classes --- src/CMakeLists.txt | 4 +-- src/bootstrap.cpp | 21 ++--------- src/engine/data/bounds.cpp | 65 +++++++++++++++++++++++++++++++++++ src/engine/data/bounds.hpp | 42 ++++++++++++++++++++++ src/engine/data/vector2.cpp | 39 +++++++++++++++++++++ src/engine/data/vector2.hpp | 52 ++++++++++++++++++++++++++++ src/engine/graphics/bounds.cpp | 65 ----------------------------------- src/engine/graphics/bounds.hpp | 28 --------------- src/engine/graphics/string_matrix.cpp | 6 ++-- src/engine/graphics/string_matrix.hpp | 11 +++--- src/engine/graphics/vector2.cpp | 39 --------------------- src/engine/graphics/vector2.hpp | 48 -------------------------- src/engine/user/cursor.cpp | 13 +++---- src/engine/user/cursor.hpp | 15 ++++---- src/interfaces/bounds.hpp | 41 ---------------------- src/interfaces/matrix.hpp | 10 +++--- src/interfaces/vector2.hpp | 44 ------------------------ 17 files changed, 227 insertions(+), 316 deletions(-) create mode 100644 src/engine/data/bounds.cpp create mode 100644 src/engine/data/bounds.hpp create mode 100644 src/engine/data/vector2.cpp create mode 100644 src/engine/data/vector2.hpp delete mode 100644 src/engine/graphics/bounds.cpp delete mode 100644 src/engine/graphics/bounds.hpp delete mode 100644 src/engine/graphics/vector2.cpp delete mode 100644 src/engine/graphics/vector2.hpp delete mode 100644 src/interfaces/bounds.hpp delete mode 100644 src/interfaces/vector2.hpp 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(std::make_unique())); }); - container.bind().to_factory( - [](const IVector2Options &options) - { - return std::dynamic_pointer_cast( - std::make_shared(options)); - }); - container.bind>().to_factory( - [](const IBounds &bounds) + [](const Bounds &bounds) { return std::dynamic_pointer_cast>( std::make_shared(bounds)); }); - container.bind().to_factory( - [](const IBoundsOptions &options) - { - return std::dynamic_pointer_cast(std::make_shared(options)); - }); - return container; } diff --git a/src/engine/data/bounds.cpp b/src/engine/data/bounds.cpp new file mode 100644 index 0000000..cd08a02 --- /dev/null +++ b/src/engine/data/bounds.cpp @@ -0,0 +1,65 @@ +#include "bounds.hpp" + +Bounds::Bounds(const BoundsOptions &options) + : _width(options.width), _height(options.height) +{ +} + +uint32_t Bounds::width() const noexcept +{ + return _width; +} + +void Bounds::width(uint32_t width) noexcept +{ + _width = width; +} + +uint32_t Bounds::height() const noexcept +{ + return _height; +} + +void Bounds::height(uint32_t height) noexcept +{ + _height = height; +} + +CoordsValidation Bounds::validate_coords(const Vector2 &coords) const noexcept +{ + if (coords.x() >= _width) + { + return CoordsValidation::X_HIGH; + } + + if (coords.y() >= _height) + { + return CoordsValidation::Y_HIGH; + } + + return CoordsValidation::VALID; +} + +const Bounds &Bounds::operator*=(const Bounds &bounds) noexcept +{ + _width *= bounds.width(); + _height *= bounds.height(); + + return *this; +} + +const Bounds &Bounds::operator+=(const Bounds &bounds) noexcept +{ + _width += bounds.width(); + _height += bounds.height(); + + return *this; +} + +const Bounds &Bounds::operator-=(const Bounds &bounds) noexcept +{ + _width -= bounds.width(); + _height -= bounds.height(); + + return *this; +} 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 + +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 + +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.cpp b/src/engine/graphics/bounds.cpp deleted file mode 100644 index 8cb83eb..0000000 --- a/src/engine/graphics/bounds.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "bounds.hpp" - -Bounds::Bounds(const IBoundsOptions &options) - : _width(options.width), _height(options.height) -{ -} - -unsigned int Bounds::width() const -{ - return _width; -} - -void Bounds::width(unsigned int width) -{ - _width = width; -} - -unsigned int Bounds::height() const -{ - return _height; -} - -void Bounds::height(unsigned int height) -{ - _height = height; -} - -CoordsValidation Bounds::validate_coords(const IVector2 &coords) const -{ - if (coords.x() >= _width) - { - return CoordsValidation::X_HIGH; - } - - if (coords.y() >= _height) - { - return CoordsValidation::Y_HIGH; - } - - return CoordsValidation::VALID; -} - -const IBounds &Bounds::operator*=(const IBounds &bounds) -{ - _width *= bounds.width(); - _height *= bounds.height(); - - return *this; -} - -const IBounds &Bounds::operator+=(const IBounds &bounds) -{ - _width += bounds.width(); - _height += bounds.height(); - - return *this; -} - -const IBounds &Bounds::operator-=(const IBounds &bounds) -{ - _width -= bounds.width(); - _height -= bounds.height(); - - return *this; -} 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 #include @@ -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 - -/** - * 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 #include -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 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 @@ -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 +class CursorController : public AutoWirable { public: - explicit CursorController(IVector2Factory vector2_factory); + CursorController() = default; template 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 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 - -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 (*)(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 @@ -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 -using IMatrixFactory = std::shared_ptr> (*)(const IBounds &bounds); +using IMatrixFactory = std::shared_ptr> (*)(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 - -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 (*)(const IVector2Options &options); -- cgit v1.2.3-18-g5258