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