aboutsummaryrefslogtreecommitdiff
path: root/src/engine/graphics/component_renderer.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-07 19:45:17 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:57:01 +0200
commit8805b1fe27344e8086cebabf869b7a02d2376f05 (patch)
treea90a2e2dda1bcb98fb4de5cd983138e5441c2222 /src/engine/graphics/component_renderer.cpp
parentf778317bae709f397345a2d5e04e23864c6391b3 (diff)
refactor: decouple statusline from scene & cursor controller
Might be slightly slower than previously though...
Diffstat (limited to 'src/engine/graphics/component_renderer.cpp')
-rw-r--r--src/engine/graphics/component_renderer.cpp55
1 files changed, 55 insertions, 0 deletions
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();
+}