From 3dbf3b68c484704aafcda9fd05ae88a0337956ef Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 19 Mar 2022 13:28:29 +0100 Subject: feat: add inserting cells --- src/engine/data/bounds.cpp | 28 +++++++++++++++++++--------- src/engine/data/bounds.hpp | 8 +++++--- src/engine/data/vector2.cpp | 10 ++++++++++ src/engine/data/vector2.hpp | 1 + src/engine/engine.cpp | 31 ++++++++++++++++++++++++++++--- src/engine/graphics/scene.cpp | 8 +++++++- src/engine/graphics/scene.hpp | 6 ++++-- src/interfaces/scene.hpp | 3 +++ 8 files changed, 77 insertions(+), 18 deletions(-) diff --git a/src/engine/data/bounds.cpp b/src/engine/data/bounds.cpp index fdf8c6b..1a8c4d0 100644 --- a/src/engine/data/bounds.cpp +++ b/src/engine/data/bounds.cpp @@ -50,26 +50,36 @@ CoordsValidation Bounds::validate_coords(const Vector2 &coords) const noexcept return CoordsValidation::VALID; } -const Bounds &Bounds::operator*=(const Bounds &bounds) noexcept +const Bounds &Bounds::operator*=(const Bounds &rhs) noexcept { - _width *= bounds._width; - _height *= bounds._height; + _width *= rhs._width; + _height *= rhs._height; return *this; } -const Bounds &Bounds::operator+=(const Bounds &bounds) noexcept +const Bounds &Bounds::operator+=(const Bounds &rhs) noexcept { - _width += bounds._width; - _height += bounds._height; + _width += rhs._width; + _height += rhs._height; return *this; } -const Bounds &Bounds::operator-=(const Bounds &bounds) noexcept +const Bounds &Bounds::operator-=(const Bounds &rhs) noexcept { - _width -= bounds._width; - _height -= bounds._height; + _width -= rhs._width; + _height -= rhs._height; return *this; } + +Bounds Bounds::operator-(const Bounds &rhs) const noexcept +{ + auto new_bounds = Bounds(*this); + + new_bounds._width -= rhs._width; + new_bounds._height -= rhs._height; + + return new_bounds; +} diff --git a/src/engine/data/bounds.hpp b/src/engine/data/bounds.hpp index 4a29158..ccea2c3 100644 --- a/src/engine/data/bounds.hpp +++ b/src/engine/data/bounds.hpp @@ -36,9 +36,11 @@ public: [[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; + const Bounds &operator*=(const Bounds &rhs) noexcept; + const Bounds &operator+=(const Bounds &rhs) noexcept; + const Bounds &operator-=(const Bounds &rhs) noexcept; + + Bounds operator-(const Bounds &rhs) const noexcept; private: Value _width = 0U; diff --git a/src/engine/data/vector2.cpp b/src/engine/data/vector2.cpp index dba745f..ba155b5 100644 --- a/src/engine/data/vector2.cpp +++ b/src/engine/data/vector2.cpp @@ -54,6 +54,16 @@ Vector2 Vector2::operator+(const Vector2 &vector2) const noexcept return new_vector2; } +Vector2 Vector2::operator-(const Vector2 &vector2) const noexcept +{ + auto new_vector2 = Vector2(*this); + + new_vector2._x -= vector2._x; + new_vector2._y -= vector2._y; + + return new_vector2; +} + Vector2 Vector2::operator*(const Vector2 &vector2) const noexcept { auto new_vector2 = Vector2(*this); diff --git a/src/engine/data/vector2.hpp b/src/engine/data/vector2.hpp index 868394a..60cecf3 100644 --- a/src/engine/data/vector2.hpp +++ b/src/engine/data/vector2.hpp @@ -37,6 +37,7 @@ public: const Vector2 &operator-=(const Vector2 &vector2) noexcept; Vector2 operator+(const Vector2 &vector2) const noexcept; + Vector2 operator-(const Vector2 &vector2) const noexcept; Vector2 operator*(const Vector2 &vector2) const noexcept; bool operator==(const Vector2 &vector2) const noexcept; diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 2fc94fe..afa5924 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -2,11 +2,10 @@ #include "input_actions.hpp" #include "strings.hpp" - #include "util/function.hpp" -#include #include +#include #include CLIGameEngine::CLIGameEngine(IGameFactory game_factory, ISceneFactory scene_factory, @@ -41,14 +40,40 @@ void CLIGameEngine::start() noexcept fmt::arg("y", center_position.get_y()))); std::atexit(normalize_lambda( - [scene, this]() + [this, scene]() { scene->leave(); _input_handler->leave_raw_mode(); + + for (auto row : *scene->get_matrix()) + { + for (auto &col : row) + { + fmt::print("{}", col); + } + + fmt::print("\n"); + } + + std::cout.flush(); })); const std::unordered_map input_config = { {'q', InputActions::exit_success}, + {'i', + [this, scene]() + { + const auto position = _cursor_controller->where(); + + std::cout.put('x'); + std::cout.flush(); + + _cursor_controller->move_to(position); + + auto matrix = scene->get_matrix(); + + matrix->set(position - Vector2({.x = 0U, .y = 1U}), "#"); + }}, {'k', InputActions::move_cursor(Vector2::up(), _cursor_controller, scene, _window)}, {'j', diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index 660e3ca..476b966 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -12,10 +12,11 @@ Scene::Scene(IMatrixFactory matrix_factory, std::shared_ptr cursor_controller, std::shared_ptr window) noexcept : _is_shown(false), - _matrix_factory(matrix_factory), + _matrix(matrix_factory(window->size() - Bounds({.width = 0U, .height = 1U}))), _cursor_controller(std::move(cursor_controller)), _window(std::move(window)) { + _matrix->fill(" "); } void Scene::enter() noexcept @@ -62,3 +63,8 @@ void Scene::write_status(const std::string_view &str) noexcept _cursor_controller->move_to(previous_position); } + +const std::shared_ptr> &Scene::get_matrix() const noexcept +{ + return _matrix; +} diff --git a/src/engine/graphics/scene.hpp b/src/engine/graphics/scene.hpp index 22a96a4..1f1c51c 100644 --- a/src/engine/graphics/scene.hpp +++ b/src/engine/graphics/scene.hpp @@ -29,11 +29,13 @@ public: void write_status(const std::string_view &str) noexcept override; + [[nodiscard]] const std::shared_ptr> & + get_matrix() const noexcept override; + private: bool _is_shown; - IMatrixFactory _matrix_factory; - + std::shared_ptr> _matrix; std::shared_ptr _cursor_controller; std::shared_ptr _window; }; diff --git a/src/interfaces/scene.hpp b/src/interfaces/scene.hpp index 47df5fa..0443d41 100644 --- a/src/interfaces/scene.hpp +++ b/src/interfaces/scene.hpp @@ -16,6 +16,9 @@ public: virtual void leave() noexcept = 0; virtual void write_status(const std::string_view &str) noexcept = 0; + + [[nodiscard]] virtual const std::shared_ptr> & + get_matrix() const noexcept = 0; }; using ISceneFactory = std::shared_ptr (*)( -- cgit v1.2.3-18-g5258