From 78d620981879065fdb9964d9750d200f008257a1 Mon Sep 17 00:00:00 2001 From: user Date: Sat, 12 Jul 2025 09:12:08 -0400 Subject: [PATCH] only move cursor when needed --- src/Aquarium.cpp | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Aquarium.cpp b/src/Aquarium.cpp index a6cc3eb..46536f7 100644 --- a/src/Aquarium.cpp +++ b/src/Aquarium.cpp @@ -280,35 +280,38 @@ void Aquarium::initColorLookup() { void Aquarium::renderToScreen() { static std::string output; output.clear(); - output.reserve(height * width * 20); // Reserve space for efficiency + output.reserve(height * width * 20); + + int cursor_y = -1, cursor_x = -1; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { const Cell &newCell = currentFrame[y][x]; Cell &oldCell = previousFrame[y][x]; - if (newCell != oldCell) { - oldCell = newCell; + if (newCell == oldCell) + continue; - // Move cursor to position + oldCell = newCell; + + // Move cursor only when needed + if (cursor_y != y || cursor_x != x) { output += ANSI::moveTo(y, x); - - // Reset cell - output += ANSI::RESET_BLACK_BG; - - // Set color and attributes - if (newCell.bold) { - output += ANSI::BOLD; - } - output += colorLookup[static_cast(newCell.colorChar)]; - - // Add the character - output += newCell.ch; + cursor_y = y; + cursor_x = x; } + + // Apply cell formatting and character + output += ANSI::RESET_BLACK_BG; + if (newCell.bold) + output += ANSI::BOLD; + output += colorLookup[static_cast(newCell.colorChar)]; + output += newCell.ch; + + ++cursor_x; } } - // Output everything at once if (!output.empty()) { std::cout << output << std::flush; }