From 38f14606c78c119d452f302f17329455e29a9a6f Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 10 Mar 2022 19:12:31 +0100 Subject: refactor: rename game initializer & move input config --- src/engine/user/cursor.cpp | 39 ++++++++++++--------------------------- src/engine/user/cursor.hpp | 8 ++++---- 2 files changed, 16 insertions(+), 31 deletions(-) (limited to 'src/engine/user') diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp index 39fef23..663d56a 100644 --- a/src/engine/user/cursor.cpp +++ b/src/engine/user/cursor.cpp @@ -5,32 +5,17 @@ #include #include -CursorController::CursorController() : _position(_request_position()) {} +CursorController::CursorController() : _position({.x = 0, .y = 0}) {} void CursorController::move(const Vector2 &direction, const uint32_t &amount) noexcept { - auto format = direction_format_map.at(direction); + auto direction_format = direction_format_map.at(direction); - fmt::print(fmt::runtime(format.data()), fmt::arg("esc", ESC), + fmt::print(fmt::runtime(direction_format.data()), fmt::arg("esc", ESC), fmt::arg("amount", amount)); std::cout.flush(); - if (direction == Vector2::up()) - { - _position.set_y(_position.get_y() + static_cast(amount)); - } - else if (direction == Vector2::down()) - { - _position.set_y(_position.get_y() - static_cast(amount)); - } - else if (direction == Vector2::left()) - { - _position.set_x(_position.get_x() - static_cast(amount)); - } - else if (direction == Vector2::right()) - { - _position.set_x(_position.get_x() + static_cast(amount)); - } + _position = _position.to_direction(direction, static_cast(amount)); } void CursorController::move_to(const Vector2 &position) noexcept @@ -53,13 +38,7 @@ void CursorController::hide() std::cout.flush(); } -void CursorController::show() -{ - fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); - std::cout.flush(); -} - -Vector2 CursorController::_request_position() noexcept +void CursorController::ensure_position() noexcept { fmt::print(REQUEST_CURSOR_POSITION, fmt::arg("esc", ESC)); std::cout.flush(); @@ -69,5 +48,11 @@ Vector2 CursorController::_request_position() noexcept // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) scanf("\033[%u;%uR", &vector2_options.y, &vector2_options.x); - return Vector2(vector2_options); + _position = Vector2(vector2_options); +} + +void CursorController::show() +{ + fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); + std::cout.flush(); } diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp index 977bfdf..c591235 100644 --- a/src/engine/user/cursor.hpp +++ b/src/engine/user/cursor.hpp @@ -39,14 +39,14 @@ public: void move_to(const Vector2 &position) noexcept override; + [[nodiscard]] Vector2 where() const noexcept override; + + void ensure_position() noexcept override; + static void hide(); static void show(); - [[nodiscard]] Vector2 where() const noexcept override; - private: Vector2 _position; - - [[nodiscard]] static Vector2 _request_position() noexcept; }; -- cgit v1.2.3-18-g5258