only move cursor when needed
This commit is contained in:
@@ -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) {
|
||||
if (newCell == oldCell)
|
||||
continue;
|
||||
|
||||
oldCell = newCell;
|
||||
|
||||
// Move cursor to position
|
||||
// Move cursor only when needed
|
||||
if (cursor_y != y || cursor_x != x) {
|
||||
output += ANSI::moveTo(y, x);
|
||||
cursor_y = y;
|
||||
cursor_x = x;
|
||||
}
|
||||
|
||||
// Reset cell
|
||||
// Apply cell formatting and character
|
||||
output += ANSI::RESET_BLACK_BG;
|
||||
|
||||
// Set color and attributes
|
||||
if (newCell.bold) {
|
||||
if (newCell.bold)
|
||||
output += ANSI::BOLD;
|
||||
}
|
||||
output += colorLookup[static_cast<unsigned char>(newCell.colorChar)];
|
||||
|
||||
// Add the character
|
||||
output += newCell.ch;
|
||||
}
|
||||
|
||||
++cursor_x;
|
||||
}
|
||||
}
|
||||
|
||||
// Output everything at once
|
||||
if (!output.empty()) {
|
||||
std::cout << output << std::flush;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user