I have been working on a Maze project where users try to complete a maze. But since I don't want to re-output the maze every time the user makes a move, I tried using ANSI cursor movement code only to change a portion of the outputted maze.
The problem I encountered was that when I scrolled through the console or if the console scrolled itself (to display text at the bottom), the ANSI code cursor movement coordinates changes.
Try it out yourself:
#include <iostream>
int main()
{
for (int i = 0; i < 35; i++) // Whatever number that is greater than your console's maximum line number
{
for (int j = 0; j < 35; j++)
std::cout << 1; // Initialize: everything is '1'
std::cout << std::endl;
}
std::cout << "\033[2;3H"; // Move the output cursor to (2, 3)
std::cout << 2; // Change the number to '2'
}
Output:
11111111111111111111111111111111111
11111111111111111111111111111111111
11111111111111111111111111111111111
11111111111111111111111111111111111 // Comment: *
112 1111111111111111111111111111111
D:\VSProject\Project1\x64\Debug\Project1.exe exited with code 0.
Press any key to close this window . . .
11111111111111111111111111111111111
... // A lot of "11...1"s ommitted
*: Since the text was too long for the console to display, the console automatically scrolled down to here, and then the coordinate's origin changed to the new top-left corner, not the original one.
The expected output:
11111111111111111111111111111111111
11211111111111111111111111111111111
11111111111111111111111111111111111
... // A lot of "11...1"s ommitted
How can I prevent the ANSI code coordinates from changing? I still want to scroll through the console. Any help is welcome.
P.S. I am using MSWindows so any solution doesn't need to be cross-platform.
You can use the SetConsoleCursorPosition function.
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = { 2, 3 };
SetConsoleCursorPosition(hStdOut, coord);
Additionally, here's another suggestion for you:
Combine all the content you want to output into a single string and output it all at once. By doing this, re-outputting the maze every time might become an acceptable solution. Remember that the overhead of outputting a large amount of content at once may even be less than outputting single characters twice. (This is related to the refresh frequency of the output buffer; when outputting to the console, the buffer is refreshed immediately after each output.)
P.S. You can also change the font size of the console or prevent it from wrapping text automatically.