From 45e3ae285c63d91dcac426cffdd9d9648e48399b Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 8 Jun 2022 16:29:16 +0200 Subject: refactor: move components to game folder --- src/CMakeLists.txt | 2 +- src/bootstrap.cpp | 2 +- src/engine/components/statusline.cpp | 119 ----------------------------------- src/engine/components/statusline.hpp | 48 -------------- src/game/components/statusline.cpp | 119 +++++++++++++++++++++++++++++++++++ src/game/components/statusline.hpp | 48 ++++++++++++++ 6 files changed, 169 insertions(+), 169 deletions(-) delete mode 100644 src/engine/components/statusline.cpp delete mode 100644 src/engine/components/statusline.hpp create mode 100644 src/game/components/statusline.cpp create mode 100644 src/game/components/statusline.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4b470a6..52c5679 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ file(GLOB SOURCES game/game.cpp game/status_manager.cpp game/generation_tracker.cpp + game/components/statusline.cpp engine/engine.cpp engine/main.cpp engine/data/vector2.cpp @@ -21,7 +22,6 @@ file(GLOB SOURCES engine/graphics/scene.cpp engine/graphics/string_matrix.cpp engine/graphics/component_renderer.cpp - engine/components/statusline.cpp engine/user/input.cpp engine/user/cursor.cpp) diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 1231937..3554183 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -15,7 +15,6 @@ #include "interfaces/statusline.hpp" // Implementations -#include "engine/components/statusline.hpp" #include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include "engine/engine.hpp" @@ -25,6 +24,7 @@ #include "engine/user/cursor.hpp" #include "engine/user/input.hpp" #include "game/cell_helper.hpp" +#include "game/components/statusline.hpp" #include "game/game.hpp" #include "game/generation_tracker.hpp" #include "game/status_manager.hpp" diff --git a/src/engine/components/statusline.cpp b/src/engine/components/statusline.cpp deleted file mode 100644 index 5b59a53..0000000 --- a/src/engine/components/statusline.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "statusline.hpp" - -#include "engine/data/bounds.hpp" -#include "engine/data/vector2.hpp" -#include "util/color.hpp" - -#include - -#include - -StatusLine::StatusLine(std::shared_ptr component_matrix) noexcept - : _component_matrix(std::move(component_matrix)), _need_render(false) -{ - _component_matrix->fill(' '); -} - -auto StatusLine::get() const noexcept -> const std::shared_ptr & -{ - 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(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}); - - const auto column_cnt = static_cast(_component_matrix->get_column_cnt()); - - const auto section_length = _section_lengths[section]; - - const auto status_len = static_cast(status.length()); - - _matrix_write_string( - pos, - status_len <= section_length - ? status - : status.substr(0U, static_cast(section_length))); - - set_need_render(true); -} - -void StatusLine::set_section_length(StatusLineSection section, int32_t length) noexcept -{ - _section_lengths[section] = length; -} - -void StatusLine::_matrix_write_string( - const Vector2 &position, - const std::string_view &str) noexcept -{ - auto working_pos = position; - - for (const auto &character : str) - { - _component_matrix->set(working_pos, character); - - working_pos += Vector2::right(); - } -} - -auto StatusLine::_get_section_start_x(StatusLineSection section) const noexcept -> int32_t -{ - int32_t section_start = 0; - - auto section_index = static_cast(section); - - while (section_index > 0) - { - const auto prev_section = static_cast(section_index - 1); - - section_start += static_cast(_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 deleted file mode 100644 index 8c1969b..0000000 --- a/src/engine/components/statusline.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include "interfaces/matrix.hpp" -#include "interfaces/statusline.hpp" - -#include "engine/data/vector2.hpp" - -#include -#include -#include - -constexpr uint32_t STATUSLINE_COLOR = 0x1A1A1AU; - -class StatusLine : public IStatusLine -{ -public: - explicit StatusLine(std::shared_ptr component_matrix) noexcept; - - auto get() const noexcept -> const std::shared_ptr & 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 _component_matrix; - std::unordered_map _section_lengths; - bool _need_render; - - void - _matrix_write_string(const Vector2 &position, const std::string_view &str) noexcept; - - [[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/game/components/statusline.cpp b/src/game/components/statusline.cpp new file mode 100644 index 0000000..5b59a53 --- /dev/null +++ b/src/game/components/statusline.cpp @@ -0,0 +1,119 @@ +#include "statusline.hpp" + +#include "engine/data/bounds.hpp" +#include "engine/data/vector2.hpp" +#include "util/color.hpp" + +#include + +#include + +StatusLine::StatusLine(std::shared_ptr component_matrix) noexcept + : _component_matrix(std::move(component_matrix)), _need_render(false) +{ + _component_matrix->fill(' '); +} + +auto StatusLine::get() const noexcept -> const std::shared_ptr & +{ + 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(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}); + + const auto column_cnt = static_cast(_component_matrix->get_column_cnt()); + + const auto section_length = _section_lengths[section]; + + const auto status_len = static_cast(status.length()); + + _matrix_write_string( + pos, + status_len <= section_length + ? status + : status.substr(0U, static_cast(section_length))); + + set_need_render(true); +} + +void StatusLine::set_section_length(StatusLineSection section, int32_t length) noexcept +{ + _section_lengths[section] = length; +} + +void StatusLine::_matrix_write_string( + const Vector2 &position, + const std::string_view &str) noexcept +{ + auto working_pos = position; + + for (const auto &character : str) + { + _component_matrix->set(working_pos, character); + + working_pos += Vector2::right(); + } +} + +auto StatusLine::_get_section_start_x(StatusLineSection section) const noexcept -> int32_t +{ + int32_t section_start = 0; + + auto section_index = static_cast(section); + + while (section_index > 0) + { + const auto prev_section = static_cast(section_index - 1); + + section_start += static_cast(_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/game/components/statusline.hpp b/src/game/components/statusline.hpp new file mode 100644 index 0000000..8c1969b --- /dev/null +++ b/src/game/components/statusline.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include "interfaces/matrix.hpp" +#include "interfaces/statusline.hpp" + +#include "engine/data/vector2.hpp" + +#include +#include +#include + +constexpr uint32_t STATUSLINE_COLOR = 0x1A1A1AU; + +class StatusLine : public IStatusLine +{ +public: + explicit StatusLine(std::shared_ptr component_matrix) noexcept; + + auto get() const noexcept -> const std::shared_ptr & 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 _component_matrix; + std::unordered_map _section_lengths; + bool _need_render; + + void + _matrix_write_string(const Vector2 &position, const std::string_view &str) noexcept; + + [[nodiscard]] auto _get_section_start_x(StatusLineSection section) const noexcept + -> int32_t; + + void _clear_section(StatusLineSection section, int32_t start) noexcept; +}; -- cgit v1.2.3-18-g5258