aboutsummaryrefslogtreecommitdiff
path: root/src/engine/graphics/component_renderer.cpp
diff options
context:
space:
mode:
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();
+}