aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-23 20:16:07 +0200
committerHampusM <hampus@hampusmat.com>2022-06-23 20:16:07 +0200
commitafa22dc6450a7f2341c432604d5e50735d6830c7 (patch)
treeb2d3b9a27f8403b1b1117ae7dfef951b14b3c013 /src
parent0f9d874b6af4698234be96de1fddad15aa882a1d (diff)
perf: reduce cout flushing
Diffstat (limited to 'src')
-rw-r--r--src/engine/engine.hpp2
-rw-r--r--src/engine/graphics/component_renderer.cpp9
-rw-r--r--src/engine/user/cursor.cpp44
-rw-r--r--src/engine/user/cursor.hpp11
-rw-r--r--src/game/game.cpp30
-rw-r--r--src/interfaces/cursor.hpp15
-rw-r--r--src/interfaces/statusline.hpp1
7 files changed, 67 insertions, 45 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;
diff --git a/src/game/game.cpp b/src/game/game.cpp
index fd53841..055fcd6 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -275,13 +275,12 @@ void Game::_on_normal_mode_update() noexcept
_erase_entire_line();
- std::cout << ":";
- std::cout.flush();
+ std::cout.put(':');
_cursor_controller->update_position(
_cursor_controller->where() + Vector2::right());
- _cursor_controller->set_cursor_style(_command_mode_cursor_style);
+ _cursor_controller->set_cursor_style(_command_mode_cursor_style, true);
_command_mode_input = "";
@@ -466,7 +465,7 @@ void Game::_on_command_mode_update() noexcept
_command_mode_input.erase(input_pos_x - 1U, 1U);
- _cursor_controller->move(Vector2::left(), 1U);
+ _cursor_controller->move(Vector2::left(), 1U, true);
std::cout.put(' ');
std::cout.flush();
@@ -474,15 +473,14 @@ void Game::_on_command_mode_update() noexcept
_cursor_controller->update_position(
_cursor_controller->where() + Vector2::right());
- _cursor_controller->move(Vector2::left(), 1U);
+ _cursor_controller->move(Vector2::left(), 1U, true);
_erase_line_from_cursor();
- std::cout << _command_mode_input.substr(input_pos_x - 1U);
- std::cout.flush();
+ fmt::print("{}", _command_mode_input.substr(input_pos_x - 1U));
_cursor_controller->move_to(position);
- _cursor_controller->move(Vector2::left(), 1U);
+ _cursor_controller->move(Vector2::left(), 1U, true);
break;
}
@@ -490,7 +488,7 @@ void Game::_on_command_mode_update() noexcept
case keycodes::LEFT_ARROW:
if (_cursor_controller->where().get_x() >= 2)
{
- _cursor_controller->move(Vector2::left(), 1U);
+ _cursor_controller->move(Vector2::left(), 1U, true);
}
break;
@@ -498,7 +496,7 @@ void Game::_on_command_mode_update() noexcept
if (static_cast<uint32_t>(_cursor_controller->where().get_x()) - 1U <
_command_mode_input.size())
{
- _cursor_controller->move(Vector2::right(), 1U);
+ _cursor_controller->move(Vector2::right(), 1U, true);
}
break;
@@ -518,16 +516,15 @@ void Game::_on_command_mode_update() noexcept
_erase_line_from_cursor();
- std::cout << pressed_key << _command_mode_input.substr(input_pos_x + 1U);
- std::cout.flush();
+ fmt::print("{}{}", pressed_key, _command_mode_input.substr(input_pos_x + 1));
- _cursor_controller->move_to(position + Vector2::right());
+ _cursor_controller->move_to(position + Vector2::right(), true);
break;
}
_command_mode_input += pressed_key;
- std::cout << pressed_key;
+ std::cout.put(pressed_key);
std::cout.flush();
_cursor_controller->update_position(
@@ -626,14 +623,13 @@ void Game::_set_space(
{
const auto prev_position = _cursor_controller->where();
- _cursor_controller->move_to(position);
+ _cursor_controller->move_to(position, true);
std::cout.put(character);
- std::cout.flush();
matrix->set(position, character);
- _cursor_controller->move_to(prev_position);
+ _cursor_controller->move_to(prev_position, true);
}
void Game::_erase_entire_line() noexcept
diff --git a/src/interfaces/cursor.hpp b/src/interfaces/cursor.hpp
index b09f06f..35c3047 100644
--- a/src/interfaces/cursor.hpp
+++ b/src/interfaces/cursor.hpp
@@ -22,10 +22,12 @@ class ICursorController
public:
virtual ~ICursorController() noexcept = default;
- virtual void move(const Vector2 &direction, const uint32_t &amount) noexcept = 0;
+ virtual void move(
+ const Vector2 &direction,
+ const uint32_t &amount,
+ bool flush_cout = false) noexcept = 0;
- // NOLINTNEXTLINE(google-default-arguments)
- virtual void move_to(const Vector2 &position) noexcept = 0;
+ virtual void move_to(const Vector2 &position, bool flush_cout = false) noexcept = 0;
[[nodiscard]] virtual auto where() const noexcept -> Vector2 = 0;
@@ -38,11 +40,12 @@ public:
*/
virtual void update_position(const Vector2 &position) noexcept = 0;
- virtual void hide() noexcept = 0;
+ virtual void hide(bool flush_cout = false) noexcept = 0;
- virtual void show() noexcept = 0;
+ virtual void show(bool flush_cout = false) noexcept = 0;
- virtual void set_cursor_style(CursorStyle cursor_style) noexcept = 0;
+ virtual void
+ set_cursor_style(CursorStyle cursor_style, bool flush_cout = false) noexcept = 0;
virtual void set_bounds(const Bounds &bounds) noexcept = 0;
};
diff --git a/src/interfaces/statusline.hpp b/src/interfaces/statusline.hpp
index 0634d2e..ff6e1a9 100644
--- a/src/interfaces/statusline.hpp
+++ b/src/interfaces/statusline.hpp
@@ -27,7 +27,6 @@ enum StatusLineSection
class IStatusLine : public IComponent
{
public:
- // NOLINTNEXTLINE(google-default-arguments)
virtual void set_section_status(
StatusLineSection section,
const std::string_view &status,