For this code I am trying to implement double buffering so that it does not blink when updating the std::cout
on my console window in windows 10. What is the best way to implement this into my current code? I am looking at some Microsoft documentation but I can't figure out a way to merge it so to speak?
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
homeCoords.X = 0;
homeCoords.Y = 0;
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition(hStdOut, homeCoords);
}
The basic idea is to call CreateConsoleScreenBuffer
to create an off-screen buffer. Then clear/fill it as needed by passing the handle to that screen buffer when you do your calls to FillConsoleOutputCharacter
, FillConsoleOutputAttribute
, etc. When it's ready for the user to view, call SetConsoleActiveScreenBuffer
to make it the active buffer for the console.
Note that in most cases you won't want to create a new screen buffer every time you clear the screen--rather you'll probably want to create two screen buffers when you start your program, and alternate between the two as you write and display output.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.