diff options
author | HampusM <hampus@hampusmat.com> | 2022-03-06 13:16:05 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:54 +0200 |
commit | 0e40bc7ce8c3b3be083002f88c3317d65f6570ad (patch) | |
tree | 2fcc470a0f1ce1d51ff26c53c8a9a890b3f31b3b /src/engine | |
parent | f4d812a5b9131e65bb55db7211dc68fc453792df (diff) |
refactor: make vector2 & bounds data classes
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/data/bounds.cpp (renamed from src/engine/graphics/bounds.cpp) | 18 | ||||
-rw-r--r-- | src/engine/data/bounds.hpp | 42 | ||||
-rw-r--r-- | src/engine/data/vector2.cpp | 39 | ||||
-rw-r--r-- | src/engine/data/vector2.hpp | 52 | ||||
-rw-r--r-- | src/engine/graphics/bounds.hpp | 28 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.cpp | 6 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.hpp | 11 | ||||
-rw-r--r-- | src/engine/graphics/vector2.cpp | 39 | ||||
-rw-r--r-- | src/engine/graphics/vector2.hpp | 48 | ||||
-rw-r--r-- | src/engine/user/cursor.cpp | 13 | ||||
-rw-r--r-- | src/engine/user/cursor.hpp | 15 |
11 files changed, 161 insertions, 150 deletions
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" |