diff options
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/components/statusline.cpp | 99 | ||||
-rw-r--r-- | src/engine/components/statusline.hpp | 45 | ||||
-rw-r--r-- | src/engine/engine.cpp | 15 | ||||
-rw-r--r-- | src/engine/engine.hpp | 4 | ||||
-rw-r--r-- | src/engine/graphics/component_renderer.cpp | 55 | ||||
-rw-r--r-- | src/engine/graphics/component_renderer.hpp | 23 | ||||
-rw-r--r-- | src/engine/graphics/scene.cpp | 15 | ||||
-rw-r--r-- | src/engine/graphics/scene.hpp | 14 | ||||
-rw-r--r-- | src/engine/graphics/statusline.cpp | 112 | ||||
-rw-r--r-- | src/engine/graphics/statusline.hpp | 48 |
10 files changed, 268 insertions, 162 deletions
diff --git a/src/engine/components/statusline.cpp b/src/engine/components/statusline.cpp new file mode 100644 index 0000000..ae0f3fa --- /dev/null +++ b/src/engine/components/statusline.cpp @@ -0,0 +1,99 @@ +#include "statusline.hpp" + +#include "engine/data/vector2.hpp" +#include "util/color.hpp" + +#include <fmt/color.h> + +#include <utility> + +StatusLine::StatusLine(std::shared_ptr<ComponentMatrix> component_matrix) noexcept + : _component_matrix(std::move(component_matrix)), _need_render(false) +{ + _component_matrix->fill(' '); +} + +auto StatusLine::get() const noexcept -> const std::shared_ptr<ComponentMatrix> & +{ + return _component_matrix; +} + +auto StatusLine::get_need_render() const noexcept -> bool +{ + return _need_render; +} + +void StatusLine::set_need_render(bool need_render) noexcept +{ + _need_render = need_render; +} + +auto StatusLine::get_foreground_color() const noexcept -> uint32_t +{ + return static_cast<uint32_t>(fmt::color::white); +} + +auto StatusLine::get_background_color() const noexcept -> uint32_t +{ + return STATUSLINE_COLOR; +} + +void StatusLine::set_status( + StatusLineSection section, + const std::string_view &status, + int32_t start) noexcept +{ + _clear_section(section, start); + + auto section_start = _get_section_start_x(section); + + auto pos = Vector2({.x = section_start + start, .y = 0}); + + for (const auto &character : status) + { + _component_matrix->set(pos, character); + + pos += Vector2::right(); + } + + set_need_render(true); +} + +void StatusLine::set_section_length(StatusLineSection section, int32_t length) noexcept +{ + _section_lengths[section] = length; +} + +auto StatusLine::_get_section_start_x(StatusLineSection section) const noexcept -> int32_t +{ + int32_t section_start = 0; + + auto section_index = static_cast<int32_t>(section); + + while (section_index > 0) + { + const auto prev_section = static_cast<StatusLineSection>(section_index - 1); + + section_start += static_cast<int32_t>(_section_lengths.at(prev_section)); + + section_index--; + } + + return section_start; +} + +void StatusLine::_clear_section(StatusLineSection section, int32_t start) noexcept +{ + auto section_start = _get_section_start_x(section); + + auto pos = Vector2({.x = section_start + start, .y = 0}); + + auto section_length = _section_lengths[section]; + + for (auto index = 0; index < section_length - start; index++) + { + _component_matrix->set(pos, ' '); + + pos += Vector2::right(); + } +} diff --git a/src/engine/components/statusline.hpp b/src/engine/components/statusline.hpp new file mode 100644 index 0000000..3fdb8d3 --- /dev/null +++ b/src/engine/components/statusline.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include "interfaces/matrix.hpp" +#include "interfaces/statusline.hpp" + +#include "engine/data/vector2.hpp" + +#include <memory> +#include <string_view> +#include <unordered_map> + +constexpr uint32_t STATUSLINE_COLOR = 0x1A1A1AU; + +class StatusLine : public IStatusLine +{ +public: + explicit StatusLine(std::shared_ptr<ComponentMatrix> component_matrix) noexcept; + + auto get() const noexcept -> const std::shared_ptr<ComponentMatrix> & override; + + [[nodiscard]] auto get_need_render() const noexcept -> bool override; + + void set_need_render(bool need_render) noexcept override; + + auto get_foreground_color() const noexcept -> uint32_t override; + + auto get_background_color() const noexcept -> uint32_t override; + + void set_status( + StatusLineSection section, + const std::string_view &status, + int32_t start) noexcept override; + + void set_section_length(StatusLineSection section, int32_t length) noexcept override; + +private: + std::shared_ptr<ComponentMatrix> _component_matrix; + std::unordered_map<StatusLineSection, int32_t> _section_lengths; + bool _need_render; + + [[nodiscard]] auto _get_section_start_x(StatusLineSection section) const noexcept + -> int32_t; + + void _clear_section(StatusLineSection section, int32_t start) noexcept; +}; diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 4775fe4..f947a45 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -9,10 +9,12 @@ CLIGameEngine::CLIGameEngine( IGameFactory game_factory, ISceneFactory scene_factory, + IComponentRendererFactory component_renderer_factory, std::shared_ptr<IUserInputObserver> user_input_observer, std::shared_ptr<ICursorController> cursor_controller) noexcept : _game_factory(std::move(game_factory)), _scene_factory(std::move(scene_factory)), + _component_renderer_factory(std::move(component_renderer_factory)), _user_input_observer(std::move(user_input_observer)), _cursor_controller(std::move(cursor_controller)) { @@ -22,6 +24,8 @@ void CLIGameEngine::start() noexcept { std::shared_ptr<IScene> scene = _scene_factory(_cursor_controller); + auto component_renderer = _component_renderer_factory(_cursor_controller); + scene->enter(); _cursor_controller->set_bounds(scene->size()); @@ -65,8 +69,17 @@ void CLIGameEngine::start() noexcept game->on_update(); - last_update_time = std::chrono::system_clock::now(); + for (auto [component, position] : scene->get_components()) + { + if (component->get_need_render()) + { + component_renderer->render(component, position); + component->set_need_render(false); + } + } _user_input_observer->clear_currently_pressed(); + + last_update_time = std::chrono::system_clock::now(); } } diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 1562bbb..a274ed4 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -1,5 +1,6 @@ #pragma once +#include "interfaces/component_renderer.hpp" #include "interfaces/cursor.hpp" #include "interfaces/engine.hpp" #include "interfaces/game.hpp" @@ -19,6 +20,7 @@ class CLIGameEngine : public ICLIGameEngine, CLIGameEngine, IGameFactory, ISceneFactory, + IComponentRendererFactory, IUserInputObserver, ICursorController> { @@ -26,6 +28,7 @@ public: CLIGameEngine( IGameFactory game_factory, ISceneFactory scene_factory, + IComponentRendererFactory component_renderer_factory, std::shared_ptr<IUserInputObserver> user_input_observer, std::shared_ptr<ICursorController> cursor_controller) noexcept; @@ -34,6 +37,7 @@ public: private: IGameFactory _game_factory; ISceneFactory _scene_factory; + IComponentRendererFactory _component_renderer_factory; std::shared_ptr<IUserInputObserver> _user_input_observer; std::shared_ptr<ICursorController> _cursor_controller; diff --git a/src/engine/graphics/component_renderer.cpp b/src/engine/graphics/component_renderer.cpp new file mode 100644 index 0000000..1ab4c08 --- /dev/null +++ b/src/engine/graphics/component_renderer.cpp @@ -0,0 +1,55 @@ +#include "component_renderer.hpp" + +#include "engine/escape.hpp" +#include "util/color.hpp" + +#include <fmt/core.h> + +#include <iostream> +#include <utility> + +ComponentRenderer::ComponentRenderer( + std::shared_ptr<ICursorController> cursor_controller) noexcept + : _cursor_controller(std::move(cursor_controller)) +{ +} + +void ComponentRenderer::render( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept +{ + const auto previous_pos = _cursor_controller->where(); + + _cursor_controller->hide(); + _cursor_controller->move_to(position); + + const auto component_matrix = component->get(); + + const auto foreground_color = component->get_foreground_color(); + const auto background_color = component->get_background_color(); + + fmt::print( + "{}{}", + get_background_esc_seq(background_color), + get_foreground_esc_seq(foreground_color)); + + for (const auto &row : *component_matrix) + { + for (const auto &col : row) + { + std::cout.put(col); + } + + const auto current_pos = _cursor_controller->where(); + + _cursor_controller->move_to( + Vector2({.x = previous_pos.get_x(), .y = current_pos.get_y() - 1})); + } + + fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); + + std::cout.flush(); + + _cursor_controller->move_to(previous_pos); + _cursor_controller->show(); +} diff --git a/src/engine/graphics/component_renderer.hpp b/src/engine/graphics/component_renderer.hpp new file mode 100644 index 0000000..4f53e07 --- /dev/null +++ b/src/engine/graphics/component_renderer.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "interfaces/component.hpp" +#include "interfaces/component_renderer.hpp" +#include "interfaces/cursor.hpp" + +#include "engine/data/vector2.hpp" + +#include <memory> + +class ComponentRenderer : public IComponentRenderer +{ +public: + explicit ComponentRenderer( + std::shared_ptr<ICursorController> cursor_controller) noexcept; + + void render( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept override; + +private: + std::shared_ptr<ICursorController> _cursor_controller; +}; diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index 9f561f0..e0f4da6 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -8,7 +8,6 @@ #include <iostream> #include <sys/ioctl.h> -#include <utility> Scene::Scene( IMatrixFactory<MatrixElement> matrix_factory, @@ -84,3 +83,17 @@ auto Scene::get_matrix() const noexcept -> const std::shared_ptr<IMatrix<MatrixE { return _matrix; } + +void Scene::register_component( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept +{ + _components.emplace_back(std::make_pair(component, position)); +} + +auto Scene::get_components() const noexcept + -> std::vector<std::pair<std::shared_ptr<IComponent>, Vector2>> +{ + return _components; +} + diff --git a/src/engine/graphics/scene.hpp b/src/engine/graphics/scene.hpp index 60b541a..c2b11e8 100644 --- a/src/engine/graphics/scene.hpp +++ b/src/engine/graphics/scene.hpp @@ -1,14 +1,19 @@ #pragma once +#include "interfaces/component.hpp" #include "interfaces/cursor.hpp" #include "interfaces/matrix.hpp" #include "interfaces/scene.hpp" +#include "engine/data/vector2.hpp" + #include <fmt/core.h> #include <memory> #include <string_view> #include <termios.h> +#include <utility> +#include <vector> constexpr fmt::string_view ENABLE_ALT_BUFFER = "{esc}[?1049h"; constexpr fmt::string_view DISABLE_ALT_BUFFER = "{esc}[?1049l"; @@ -29,10 +34,19 @@ public: [[nodiscard]] auto get_matrix() const noexcept -> const std::shared_ptr<IMatrix<MatrixElement>> & override; + void register_component( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept override; + + [[nodiscard]] auto get_components() const noexcept + -> std::vector<std::pair<std::shared_ptr<IComponent>, Vector2>> override; + private: std::shared_ptr<IMatrix<MatrixElement>> _matrix; std::shared_ptr<ICursorController> _cursor_controller; bool _is_shown; std::shared_ptr<termios> _original_termios = nullptr; + + std::vector<std::pair<std::shared_ptr<IComponent>, Vector2>> _components; }; diff --git a/src/engine/graphics/statusline.cpp b/src/engine/graphics/statusline.cpp deleted file mode 100644 index bc83993..0000000 --- a/src/engine/graphics/statusline.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#include "statusline.hpp" - -#include "engine/escape.hpp" -#include "util/color.hpp" - -#include <string> -#include <utility> - -StatusLine::StatusLine( - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IScene> scene) noexcept - : _cursor_controller(std::move(cursor_controller)), _scene(std::move(scene)) -{ -} - -void StatusLine::initialize_background() noexcept -{ - const auto previous_position = _move_to_statusline(0); - - auto background_color = get_background_esc_seq(STATUSBAR_COLOR); - - fmt::print("{}{}", background_color, std::string(_scene->size().get_width(), ' ')); - fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); - - _move_back(previous_position); -} - -void StatusLine::set_status( - const StatusLineSection §ion, - const std::string_view &status, - std::size_t start) noexcept -{ - _clear_section(section, start); - - auto section_start = _get_section_start_x(section); - - const auto previous_position = - _move_to_statusline(section_start + static_cast<int32_t>(start)); - - auto background_color = get_background_esc_seq(STATUSBAR_COLOR); - - auto section_length = _sections_lengths[section]; - - fmt::print("{}{}", background_color, status); - fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); - - _move_back(previous_position); -} - -void StatusLine::set_section_length( - const StatusLineSection §ion, - uint32_t length) noexcept -{ - _sections_lengths[section] = length; -} - -auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2 -{ - const auto previous_position = _cursor_controller->where(); - - _cursor_controller->hide(); - - _cursor_controller->move_to(Vector2({.x = x, .y = 0})); - - return previous_position; -} - -void StatusLine::_move_back(Vector2 previous_position) noexcept -{ - _cursor_controller->move_to(previous_position); - _cursor_controller->show(); -} - -auto StatusLine::_get_section_start_x(const StatusLineSection §ion) const noexcept - -> int32_t -{ - int32_t section_start = 0; - - auto section_index = static_cast<int32_t>(section); - - while (section_index > 0) - { - section_start += static_cast<int32_t>( - _sections_lengths.at(StatusLineSection(section_index - 1))); - - section_index--; - } - - return section_start; -} - -void StatusLine::_clear_section( - const StatusLineSection §ion, - std::size_t start) noexcept -{ - auto section_start = _get_section_start_x(section); - - auto start_int32 = static_cast<int32_t>(start); - - const auto previous_position = _move_to_statusline(section_start + start_int32); - - auto background_color = get_background_esc_seq(STATUSBAR_COLOR); - - auto section_length = _sections_lengths[section]; - - auto start_uint32 = static_cast<uint32_t>(start); - - fmt::print("{}{}", background_color, std::string(section_length - start_uint32, ' ')); - fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); - - _move_back(previous_position); -} diff --git a/src/engine/graphics/statusline.hpp b/src/engine/graphics/statusline.hpp deleted file mode 100644 index 6f0a4c2..0000000 --- a/src/engine/graphics/statusline.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include "interfaces/cursor.hpp" -#include "interfaces/scene.hpp" -#include "interfaces/statusline.hpp" - -#include "engine/data/vector2.hpp" - -#include <fmt/core.h> - -#include <memory> -#include <string_view> -#include <unordered_map> - -constexpr uint32_t STATUSBAR_COLOR = 0x1A1A1AU; - -class StatusLine : public IStatusLine -{ -public: - StatusLine( - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IScene> scene) noexcept; - - void initialize_background() noexcept override; - - void set_status( - const StatusLineSection §ion, - const std::string_view &status, - std::size_t start) noexcept override; - - void set_section_length(const StatusLineSection §ion, uint32_t length) noexcept - override; - -private: - std::unordered_map<StatusLineSection, uint32_t> _sections_lengths; - - std::shared_ptr<ICursorController> _cursor_controller; - std::shared_ptr<IScene> _scene; - - auto _move_to_statusline(int32_t x) noexcept -> Vector2; - - void _move_back(Vector2 previous_position) noexcept; - - [[nodiscard]] auto - _get_section_start_x(const StatusLineSection §ion) const noexcept -> int32_t; - - void _clear_section(const StatusLineSection §ion, std::size_t start) noexcept; -}; |