diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-22 17:05:00 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:59 +0200 |
commit | 7de921836587cdc359c2c4b84ed6446ada16c008 (patch) | |
tree | c4ec20b4769817c41ce7d939956da297bf787597 | |
parent | 723ea6535b4c4e5605e5592137a898d6ffa458c1 (diff) |
refactor: remove window class
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/bootstrap.cpp | 17 | ||||
-rw-r--r-- | src/commands/move_cursor.cpp | 8 | ||||
-rw-r--r-- | src/commands/move_cursor.hpp | 6 | ||||
-rw-r--r-- | src/engine/engine.cpp | 10 | ||||
-rw-r--r-- | src/engine/engine.hpp | 8 | ||||
-rw-r--r-- | src/engine/graphics/matrix.hpp | 1 | ||||
-rw-r--r-- | src/engine/graphics/scene.cpp | 20 | ||||
-rw-r--r-- | src/engine/graphics/scene.hpp | 8 | ||||
-rw-r--r-- | src/engine/graphics/statusline.cpp | 12 | ||||
-rw-r--r-- | src/engine/graphics/statusline.hpp | 6 | ||||
-rw-r--r-- | src/engine/graphics/window.cpp | 13 | ||||
-rw-r--r-- | src/engine/graphics/window.hpp | 15 | ||||
-rw-r--r-- | src/game/game.cpp | 19 | ||||
-rw-r--r-- | src/game/game.hpp | 3 | ||||
-rw-r--r-- | src/interfaces/game.hpp | 8 | ||||
-rw-r--r-- | src/interfaces/scene.hpp | 9 | ||||
-rw-r--r-- | src/interfaces/statusline.hpp | 4 | ||||
-rw-r--r-- | src/interfaces/window.hpp | 12 |
19 files changed, 64 insertions, 116 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 32118e2..6fd044f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,7 +21,6 @@ file(GLOB SOURCES engine/data/vector2.cpp engine/data/bounds.cpp engine/graphics/scene.cpp - engine/graphics/window.cpp engine/graphics/string_matrix.cpp engine/graphics/statusline.cpp engine/user/input.cpp diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index b11f199..2e687ed 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -13,7 +13,6 @@ #include "interfaces/status_manager.hpp" #include "interfaces/statusline.hpp" #include "interfaces/statusline_subscriber_adapter.hpp" -#include "interfaces/window.hpp" // Implementations #include "argument_parser.hpp" @@ -23,7 +22,6 @@ #include "engine/graphics/matrix.hpp" #include "engine/graphics/scene.hpp" #include "engine/graphics/statusline.hpp" -#include "engine/graphics/window.hpp" #include "engine/user/cursor.hpp" #include "engine/user/input.hpp" #include "game/game.hpp" @@ -49,17 +47,15 @@ auto bootstrap() noexcept -> yacppdic::Container container.bind<IInputHandler>().to<InputHandler>(); container.bind<ICursorController>().to<CursorController>(); container.bind<ICLIGameEngine>().to<CLIGameEngine>(); - container.bind<IWindow>().to<Window>(); container.bind<ISeedGenerator>().to<SeedGenerator>(); container.bind<IGameFactory>().to_factory( [&container]( - const std::shared_ptr<IWindow> &window, const std::shared_ptr<IScene> &scene, const std::shared_ptr<ICursorController> &cursor_controller) { std::shared_ptr<IStatusLine> statusline = - container.get<IStatusLineFactory>()(cursor_controller, window); + container.get<IStatusLineFactory>()(cursor_controller, scene); std::shared_ptr<IStatusManager> status_manager = container.get<IStatusManagerFactory>()(statusline); @@ -71,7 +67,6 @@ auto bootstrap() noexcept -> yacppdic::Container container.get<IStatusLineSubscriberAdapterFactory<Vector2>>(); return std::make_unique<Game>( - window, scene, cursor_controller, statusline, @@ -93,20 +88,18 @@ auto bootstrap() noexcept -> yacppdic::Container }); container.bind<ISceneFactory>().to_factory( - [&container]( - const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IWindow> &window) + [&container](const std::shared_ptr<ICursorController> &cursor_controller) { auto matrix_factory = container.get<IMatrixFactory<std::string_view>>(); - return std::make_unique<Scene>(matrix_factory, cursor_controller, window); + return std::make_unique<Scene>(matrix_factory, cursor_controller); }); container.bind<IStatusLineFactory>().to_factory( [](const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IWindow> &window) + const std::shared_ptr<IScene> &scene) { - return std::make_unique<StatusLine>(cursor_controller, window); + return std::make_unique<StatusLine>(cursor_controller, scene); }); container.bind<IStatusManagerFactory>().to_factory( diff --git a/src/commands/move_cursor.cpp b/src/commands/move_cursor.cpp index 12a247d..6bd8eda 100644 --- a/src/commands/move_cursor.cpp +++ b/src/commands/move_cursor.cpp @@ -3,8 +3,8 @@ MoveCursorCommand::MoveCursorCommand( const Vector2 &direction, const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IWindow> &window) noexcept - : _direction(direction), _cursor_controller(cursor_controller), _window(window) + const std::shared_ptr<IScene> &scene) noexcept + : _direction(direction), _cursor_controller(cursor_controller), _scene(scene) { } @@ -16,9 +16,9 @@ void MoveCursorCommand::execute() noexcept const auto new_position = _cursor_controller->where().to_direction(_direction, amount); - const auto window_size = _window->size(); + const auto scene_size = _scene->size(); - if (window_size.validate_coords(new_position) != CoordsValidation::VALID) + if (scene_size.validate_coords(new_position) != CoordsValidation::VALID) { return; } diff --git a/src/commands/move_cursor.hpp b/src/commands/move_cursor.hpp index aab228c..dc08c64 100644 --- a/src/commands/move_cursor.hpp +++ b/src/commands/move_cursor.hpp @@ -2,7 +2,7 @@ #include "interfaces/command.hpp" #include "interfaces/cursor.hpp" -#include "interfaces/window.hpp" +#include "interfaces/scene.hpp" #include "engine/data/vector2.hpp" @@ -14,12 +14,12 @@ public: MoveCursorCommand( const Vector2 &direction, const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IWindow> &window) noexcept; + const std::shared_ptr<IScene> &scene) noexcept; void execute() noexcept override; private: Vector2 _direction; const std::shared_ptr<ICursorController> &_cursor_controller; - const std::shared_ptr<IWindow> &_window; + const std::shared_ptr<IScene> &_scene; }; diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 8b55d88..fda1fc2 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -9,24 +9,22 @@ CLIGameEngine::CLIGameEngine( IGameFactory game_factory, ISceneFactory scene_factory, std::shared_ptr<IInputHandler> input_handler, - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IWindow> window) noexcept + std::shared_ptr<ICursorController> cursor_controller) noexcept : _game_factory(std::move(game_factory)), _scene_factory(std::move(scene_factory)), _input_handler(std::move(input_handler)), - _cursor_controller(std::move(cursor_controller)), - _window(std::move(window)) + _cursor_controller(std::move(cursor_controller)) { } void CLIGameEngine::start() noexcept { - std::shared_ptr<IScene> scene = _scene_factory(_cursor_controller, _window); + std::shared_ptr<IScene> scene = _scene_factory(_cursor_controller); scene->enter(); _input_handler->enter_raw_mode(); - auto game = _game_factory(_window, scene, _cursor_controller); + auto game = _game_factory(scene, _cursor_controller); game->on_start(); diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 5073553..7b322b5 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -6,7 +6,6 @@ #include "interfaces/game.hpp" #include "interfaces/input.hpp" #include "interfaces/scene.hpp" -#include "interfaces/window.hpp" #include <yacppdic/auto_wirable.hpp> @@ -20,16 +19,14 @@ class CLIGameEngine : public ICLIGameEngine, IGameFactory, ISceneFactory, IInputHandler, - ICursorController, - IWindow> + ICursorController> { public: CLIGameEngine( IGameFactory game_factory, ISceneFactory scene_factory, std::shared_ptr<IInputHandler> input_handler, - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IWindow> window) noexcept; + std::shared_ptr<ICursorController> cursor_controller) noexcept; void start() noexcept override; @@ -39,7 +36,6 @@ private: std::shared_ptr<IInputHandler> _input_handler; std::shared_ptr<ICursorController> _cursor_controller; - std::shared_ptr<IWindow> _window; void _configure_input( const std::unordered_map<char, std::shared_ptr<ICommand>> &input_config) noexcept; diff --git a/src/engine/graphics/matrix.hpp b/src/engine/graphics/matrix.hpp index f71018b..5a7d893 100644 --- a/src/engine/graphics/matrix.hpp +++ b/src/engine/graphics/matrix.hpp @@ -7,6 +7,7 @@ #include "engine/matrix_iterator.hpp" #include <gsl/pointers> + #include <memory> template <typename Element> diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index 73c1292..52613d8 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -5,17 +5,17 @@ #include <fmt/color.h> #include <fmt/core.h> + #include <iostream> +#include <sys/ioctl.h> #include <utility> Scene::Scene( IMatrixFactory<std::string_view> matrix_factory, - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IWindow> window) noexcept + std::shared_ptr<ICursorController> cursor_controller) noexcept : _is_shown(false), - _matrix(matrix_factory(window->size() - Bounds({.width = 0U, .height = 1U}))), - _cursor_controller(std::move(cursor_controller)), - _window(std::move(window)) + _matrix(matrix_factory(size() - Bounds({.width = 0U, .height = 1U}))), + _cursor_controller(std::move(cursor_controller)) { _matrix->fill(" "); } @@ -46,6 +46,16 @@ void Scene::leave() noexcept _is_shown = false; } +auto Scene::size() const noexcept -> Bounds +{ + winsize window_size = {}; + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + ioctl(0, TIOCGWINSZ, &window_size); + + return Bounds({window_size.ws_col, window_size.ws_row}); +} + auto Scene::get_matrix() const noexcept -> const std::shared_ptr<IMatrix<std::string_view>> & { diff --git a/src/engine/graphics/scene.hpp b/src/engine/graphics/scene.hpp index 5e74725..268f8dc 100644 --- a/src/engine/graphics/scene.hpp +++ b/src/engine/graphics/scene.hpp @@ -3,9 +3,9 @@ #include "interfaces/cursor.hpp" #include "interfaces/matrix.hpp" #include "interfaces/scene.hpp" -#include "interfaces/window.hpp" #include <fmt/core.h> + #include <memory> #include <string_view> @@ -17,13 +17,14 @@ class Scene : public IScene public: explicit Scene( IMatrixFactory<std::string_view> matrix_factory, - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IWindow> window) noexcept; + std::shared_ptr<ICursorController> cursor_controller) noexcept; void enter() noexcept override; void leave() noexcept override; + [[nodiscard]] auto size() const noexcept -> Bounds override; + [[nodiscard]] auto get_matrix() const noexcept -> const std::shared_ptr<IMatrix<std::string_view>> & override; @@ -32,5 +33,4 @@ private: std::shared_ptr<IMatrix<std::string_view>> _matrix; std::shared_ptr<ICursorController> _cursor_controller; - std::shared_ptr<IWindow> _window; }; diff --git a/src/engine/graphics/statusline.cpp b/src/engine/graphics/statusline.cpp index 3968fae..52edd8f 100644 --- a/src/engine/graphics/statusline.cpp +++ b/src/engine/graphics/statusline.cpp @@ -8,8 +8,8 @@ StatusLine::StatusLine( std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IWindow> window) noexcept - : _cursor_controller(std::move(cursor_controller)), _window(std::move(window)) + std::shared_ptr<IScene> scene) noexcept + : _cursor_controller(std::move(cursor_controller)), _scene(std::move(scene)) { } @@ -19,7 +19,7 @@ void StatusLine::initialize_background() noexcept auto background_color = get_background_esc_seq(STATUSBAR_COLOR); - fmt::print("{}{}", background_color, std::string(_window->size().get_width(), ' ')); + fmt::print("{}{}", background_color, std::string(_scene->size().get_width(), ' ')); fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); _move_back(previous_position); @@ -58,13 +58,13 @@ auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2 { const auto previous_position = _cursor_controller->where(); - const auto window_size = _window->size(); + const auto scene_size = _scene->size(); _cursor_controller->hide(); - auto window_height = static_cast<Vector2::Value>(window_size.get_height()); + auto scene_height = static_cast<Vector2::Value>(scene_size.get_height()); - _cursor_controller->move_to(Vector2({.x = x, .y = window_height}), true); + _cursor_controller->move_to(Vector2({.x = x, .y = scene_height}), true); return previous_position; } diff --git a/src/engine/graphics/statusline.hpp b/src/engine/graphics/statusline.hpp index add8e02..6f0a4c2 100644 --- a/src/engine/graphics/statusline.hpp +++ b/src/engine/graphics/statusline.hpp @@ -1,8 +1,8 @@ #pragma once #include "interfaces/cursor.hpp" +#include "interfaces/scene.hpp" #include "interfaces/statusline.hpp" -#include "interfaces/window.hpp" #include "engine/data/vector2.hpp" @@ -19,7 +19,7 @@ class StatusLine : public IStatusLine public: StatusLine( std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IWindow> window) noexcept; + std::shared_ptr<IScene> scene) noexcept; void initialize_background() noexcept override; @@ -35,7 +35,7 @@ private: std::unordered_map<StatusLineSection, uint32_t> _sections_lengths; std::shared_ptr<ICursorController> _cursor_controller; - std::shared_ptr<IWindow> _window; + std::shared_ptr<IScene> _scene; auto _move_to_statusline(int32_t x) noexcept -> Vector2; diff --git a/src/engine/graphics/window.cpp b/src/engine/graphics/window.cpp deleted file mode 100644 index bb33402..0000000 --- a/src/engine/graphics/window.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "window.hpp" - -#include <sys/ioctl.h> - -auto Window::size() const noexcept -> Bounds -{ - winsize window_size = {}; - - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) - ioctl(0, TIOCGWINSZ, &window_size); - - return Bounds({window_size.ws_col, window_size.ws_row}); -} diff --git a/src/engine/graphics/window.hpp b/src/engine/graphics/window.hpp deleted file mode 100644 index 9284e5e..0000000 --- a/src/engine/graphics/window.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "interfaces/window.hpp" - -#include "engine/data/bounds.hpp" - -#include <yacppdic/auto_wirable.hpp> - -class Window : public IWindow, public yacppdic::AutoWirable<IWindow, Window> -{ -public: - Window() noexcept = default; - - [[nodiscard]] auto size() const noexcept -> Bounds override; -}; diff --git a/src/game/game.cpp b/src/game/game.cpp index 4201b7b..d642f7c 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -6,11 +6,11 @@ #include "commands/toggle_pause.hpp" #include <fmt/core.h> + #include <iostream> #include <utility> Game::Game( - std::shared_ptr<IWindow> window, std::shared_ptr<IScene> scene, std::shared_ptr<ICursorController> cursor_controller, std::shared_ptr<IStatusLine> statusline, @@ -18,8 +18,7 @@ Game::Game( std::shared_ptr<IStatusManager> status_manager, IStatusLineSubscriberAdapterFactory<Vector2> vector2_statusline_subscriber_adapter_factory) noexcept - : _window(std::move(window)), - _scene(std::move(scene)), + : _scene(std::move(scene)), _cursor_controller(std::move(cursor_controller)), _statusline(std::move(statusline)), _generation_tracker(std::move(generation_tracker)), @@ -48,11 +47,11 @@ void Game::on_start() noexcept CursorEvent::POSITION_CHANGE, vector2_statusline_subscriber_adapter); - const auto window_size = _window->size(); + const auto scene_size = _scene->size(); const auto center_position = Vector2( - {.x = static_cast<Vector2::Value>(window_size.get_width()) / 2, - .y = static_cast<Vector2::Value>(window_size.get_height()) / 2}); + {.x = static_cast<Vector2::Value>(scene_size.get_width()) / 2, + .y = static_cast<Vector2::Value>(scene_size.get_height()) / 2}); _cursor_controller->move_to(center_position); @@ -96,20 +95,20 @@ auto Game::get_input_config() const noexcept {'i', std::make_shared<InsertCellCommand>(_cursor_controller, _scene)}, {'p', std::make_shared<TogglePauseCommand>(_generation_tracker, _status_manager)}, {'k', - std::make_shared<MoveCursorCommand>(Vector2::up(), _cursor_controller, _window)}, + std::make_shared<MoveCursorCommand>(Vector2::up(), _cursor_controller, _scene)}, {'j', std::make_shared<MoveCursorCommand>( Vector2::down(), _cursor_controller, - _window)}, + _scene)}, {'h', std::make_shared<MoveCursorCommand>( Vector2::left(), _cursor_controller, - _window)}, + _scene)}, {'l', std::make_shared<MoveCursorCommand>( Vector2::right(), _cursor_controller, - _window)}}; + _scene)}}; } diff --git a/src/game/game.hpp b/src/game/game.hpp index fac9061..c765312 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -7,7 +7,6 @@ #include "interfaces/status_manager.hpp" #include "interfaces/statusline.hpp" #include "interfaces/statusline_subscriber_adapter.hpp" -#include "interfaces/window.hpp" #include <memory> @@ -15,7 +14,6 @@ class Game : public IGame { public: Game( - std::shared_ptr<IWindow> window, std::shared_ptr<IScene> scene, std::shared_ptr<ICursorController> cursor_controller, std::shared_ptr<IStatusLine> statusline, @@ -34,7 +32,6 @@ public: -> std::unordered_map<char, std::shared_ptr<ICommand>> override; private: - std::shared_ptr<IWindow> _window; std::shared_ptr<IScene> _scene; std::shared_ptr<ICursorController> _cursor_controller; std::shared_ptr<IStatusLine> _statusline; diff --git a/src/interfaces/game.hpp b/src/interfaces/game.hpp index e4df51c..a54782a 100644 --- a/src/interfaces/game.hpp +++ b/src/interfaces/game.hpp @@ -3,7 +3,6 @@ #include "interfaces/command.hpp" #include "interfaces/cursor.hpp" #include "interfaces/scene.hpp" -#include "interfaces/window.hpp" #include <yacppdic/factory.hpp> @@ -26,13 +25,6 @@ public: -> std::unordered_map<char, std::shared_ptr<ICommand>> = 0; }; -/* -using IGameFactory = std::unique_ptr<IGame> (*)( - const std::shared_ptr<IWindow> &window, const std::shared_ptr<IScene> &scene, - const std::shared_ptr<ICursorController> &cursor_controller); - */ - using IGameFactory = yacppdic::Factory<std::unique_ptr<IGame>( - const std::shared_ptr<IWindow> &window, const std::shared_ptr<IScene> &scene, const std::shared_ptr<ICursorController> &cursor_controller)>; diff --git a/src/interfaces/scene.hpp b/src/interfaces/scene.hpp index 01be07d..08615a9 100644 --- a/src/interfaces/scene.hpp +++ b/src/interfaces/scene.hpp @@ -2,11 +2,13 @@ #include "interfaces/cursor.hpp" #include "interfaces/matrix.hpp" -#include "interfaces/window.hpp" + +#include "engine/data/bounds.hpp" #include <yacppdic/factory.hpp> #include <memory> +#include <string_view> // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class IScene @@ -18,10 +20,11 @@ public: virtual void leave() noexcept = 0; + [[nodiscard]] virtual auto size() const noexcept -> Bounds = 0; + [[nodiscard]] virtual auto get_matrix() const noexcept -> const std::shared_ptr<IMatrix<std::string_view>> & = 0; }; using ISceneFactory = yacppdic::Factory<std::unique_ptr<IScene>( - const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IWindow> &window)>; + const std::shared_ptr<ICursorController> &cursor_controller)>; diff --git a/src/interfaces/statusline.hpp b/src/interfaces/statusline.hpp index ca2a617..2e97ed6 100644 --- a/src/interfaces/statusline.hpp +++ b/src/interfaces/statusline.hpp @@ -1,7 +1,7 @@ #pragma once #include "interfaces/cursor.hpp" -#include "interfaces/window.hpp" +#include "interfaces/scene.hpp" #include <yacppdic/factory.hpp> @@ -36,4 +36,4 @@ public: using IStatusLineFactory = yacppdic::Factory<std::unique_ptr<IStatusLine>( const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IWindow> &window)>; + const std::shared_ptr<IScene> &scene)>; diff --git a/src/interfaces/window.hpp b/src/interfaces/window.hpp deleted file mode 100644 index 6fc56e9..0000000 --- a/src/interfaces/window.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "engine/data/bounds.hpp" - -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class IWindow -{ -public: - virtual ~IWindow() noexcept = default; - - [[nodiscard]] virtual auto size() const noexcept -> Bounds = 0; -}; |