From afa22dc6450a7f2341c432604d5e50735d6830c7 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 23 Jun 2022 20:16:07 +0200 Subject: perf: reduce cout flushing --- src/engine/user/cursor.cpp | 44 ++++++++++++++++++++++++++++++++++---------- src/engine/user/cursor.hpp | 11 ++++++----- 2 files changed, 40 insertions(+), 15 deletions(-) (limited to 'src/engine/user') diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp index 963531f..b38cee0 100644 --- a/src/engine/user/cursor.cpp +++ b/src/engine/user/cursor.cpp @@ -10,7 +10,10 @@ CursorController::CursorController() noexcept { } -void CursorController::move(const Vector2 &direction, const uint32_t &amount) noexcept +void CursorController::move( + const Vector2 &direction, + const uint32_t &amount, + bool flush_cout) noexcept { auto direction_format = direction_format_map.at(direction); @@ -18,12 +21,16 @@ void CursorController::move(const Vector2 &direction, const uint32_t &amount) no fmt::runtime(direction_format.data()), fmt::arg("esc", ESC), fmt::arg("amount", amount)); - std::cout.flush(); + + if (flush_cout) + { + std::cout.flush(); + } _position = _position.to_direction(direction, static_cast(amount)); } -void CursorController::move_to(const Vector2 &position) noexcept +void CursorController::move_to(const Vector2 &position, bool flush_cout) noexcept { const auto inverted_pos = _invert_position_y(position); @@ -32,7 +39,11 @@ void CursorController::move_to(const Vector2 &position) noexcept fmt::arg("esc", ESC), fmt::arg("row", inverted_pos.get_y()), fmt::arg("column", inverted_pos.get_x() + 1)); - std::cout.flush(); + + if (flush_cout) + { + std::cout.flush(); + } _position = position; } @@ -60,26 +71,39 @@ void CursorController::update_position(const Vector2 &position) noexcept _position = position; } -void CursorController::hide() noexcept +void CursorController::hide(bool flush_cout) noexcept { fmt::print(CURSOR_INVISIBLE, fmt::arg("esc", ESC)); - std::cout.flush(); + + if (flush_cout) + { + std::cout.flush(); + } } -void CursorController::show() noexcept +void CursorController::show(bool flush_cout) noexcept { fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); - std::cout.flush(); + + if (flush_cout) + { + std::cout.flush(); + } } -void CursorController::set_cursor_style(CursorStyle cursor_style) noexcept +void CursorController::set_cursor_style( + CursorStyle cursor_style, + bool flush_cout) noexcept { fmt::print( SET_CURSOR_STYLE, fmt::arg("esc", ESC), fmt::arg("style", static_cast(cursor_style))); - std::cout.flush(); + if (flush_cout) + { + std::cout.flush(); + } } void CursorController::set_bounds(const Bounds &bounds) noexcept diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp index ace47ee..16d37e3 100644 --- a/src/engine/user/cursor.hpp +++ b/src/engine/user/cursor.hpp @@ -39,9 +39,10 @@ class CursorController : public ICursorController, public: CursorController() noexcept; - void move(const Vector2 &direction, const uint32_t &amount) noexcept override; + void move(const Vector2 &direction, const uint32_t &amount, bool flush_cout) noexcept + override; - void move_to(const Vector2 &position) noexcept override; + void move_to(const Vector2 &position, bool flush_cout) noexcept override; [[nodiscard]] auto where() const noexcept -> Vector2 override; @@ -49,11 +50,11 @@ public: void update_position(const Vector2 &position) noexcept override; - void hide() noexcept override; + void hide(bool flush_cout) noexcept override; - void show() noexcept override; + void show(bool flush_cout) noexcept override; - void set_cursor_style(CursorStyle cursor_style) noexcept override; + void set_cursor_style(CursorStyle cursor_style, bool flush_cout) noexcept override; void set_bounds(const Bounds &bounds) noexcept override; -- cgit v1.2.3-18-g5258