From 8805b1fe27344e8086cebabf869b7a02d2376f05 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 7 Jun 2022 19:45:17 +0200 Subject: refactor: decouple statusline from scene & cursor controller Might be slightly slower than previously though... --- src/engine/graphics/component_renderer.cpp | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/engine/graphics/component_renderer.cpp (limited to 'src/engine/graphics/component_renderer.cpp') 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 + +#include +#include + +ComponentRenderer::ComponentRenderer( + std::shared_ptr cursor_controller) noexcept + : _cursor_controller(std::move(cursor_controller)) +{ +} + +void ComponentRenderer::render( + const std::shared_ptr &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(); +} -- cgit v1.2.3-18-g5258