diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-23 20:16:07 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-23 20:16:07 +0200 |
commit | afa22dc6450a7f2341c432604d5e50735d6830c7 (patch) | |
tree | b2d3b9a27f8403b1b1117ae7dfef951b14b3c013 /src/engine | |
parent | 0f9d874b6af4698234be96de1fddad15aa882a1d (diff) |
perf: reduce cout flushing
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/engine.hpp | 2 | ||||
-rw-r--r-- | src/engine/graphics/component_renderer.cpp | 9 | ||||
-rw-r--r-- | src/engine/user/cursor.cpp | 44 | ||||
-rw-r--r-- | src/engine/user/cursor.hpp | 11 |
4 files changed, 45 insertions, 21 deletions
diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 28eb8fc..65d1a80 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -12,7 +12,7 @@ #include <memory> #include <unordered_map> -constexpr auto MIN_TIME_SINCE_LAST_UPDATE_MILLIS = 20; +constexpr auto MIN_TIME_SINCE_LAST_UPDATE_MILLIS = 15; class CLIGameEngine : public ICLIGameEngine, public yacppdic::AutoWirable< diff --git a/src/engine/graphics/component_renderer.cpp b/src/engine/graphics/component_renderer.cpp index eb024bc..effe153 100644 --- a/src/engine/graphics/component_renderer.cpp +++ b/src/engine/graphics/component_renderer.cpp @@ -21,7 +21,7 @@ void ComponentRenderer::render( const auto previous_pos = _cursor_controller->where(); _cursor_controller->hide(); - _cursor_controller->move_to(position); + _cursor_controller->move_to(position, true); _use_component_colors(component); @@ -61,15 +61,14 @@ void ComponentRenderer::render( const auto current_pos = _cursor_controller->where(); _cursor_controller->move_to( - Vector2({.x = previous_pos.get_x(), .y = current_pos.get_y() - 1})); + Vector2({.x = previous_pos.get_x(), .y = current_pos.get_y() - 1}), + true); } fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); - std::cout.flush(); - _cursor_controller->move_to(previous_pos); - _cursor_controller->show(); + _cursor_controller->show(true); } void ComponentRenderer::_use_component_colors( 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<Vector2::Value>(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<int>(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; |