Win32 C ++ console cleaning screen without blinking

I saw several console games where the screen is updated / cleared without annoying blinking. I tried many solutions, here is what I got now:

while(true)
{
    if(screenChanged) //if something needs to be drawn on new position
    {
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    } 

    ///printf all the time graphics on their right position with SetConsoleCursorPosition

    Sleep(33.3f);
}  

However, I blink a little. Does anyone have any ideas?

+5
source share
3 answers

The reason this happens is because the display is updated between the times when you clear the console screen and actually draw it. This can usually happen so fast that you never see it, but from time to time you do it at the right time, and you experience flickering.

- , , , , WriteConsoleOutput. , , , , , .

BOOL WINAPI WriteConsoleOutput(
  _In_     HANDLE hConsoleOutput,
  _In_     const CHAR_INFO *lpBuffer,
  _In_     COORD dwBufferSize,
  _In_     COORD dwBufferCoord,
  _Inout_  PSMALL_RECT lpWriteRegion
);
+4

. APC CreateConsoleScreenBuffer SetConsoleActiveScreenBuffer, , , :) , , : http://msdn.microsoft.com/en-us/library/windows/desktop/ms685032%28v=vs.85%29.aspx

+3

-2

All Articles