How is ESC[2J and CLS different programmatically, and how would I use the Windows API to implement it in C?

  ansi-escape, c++, windows, windows-console

With Windows 10 now supporting VT100 escape sequences, I’ve noticed that the sequence ESC[2J and CLS are functionally the same, but CLS flickers quite a bit while ESC[2J does not. Here’s 2 batch files as a simple example:

For ESC[2J

@ECHO OFF
FOR /F %%A in ('ECHO prompt $E^| cmd') DO SET "ESC=%%A"
FOR /L %%Q in () DO (
    ECHO %ESC%[2J%ESC%[1;1HHi
)

For CLS

@ECHO OFF
FOR /F %%A in ('ECHO prompt $E^| cmd') DO SET "ESC=%%A"
FOR /L %%Q in () DO (
    CLS
    ECHO %ESC%[1;1HHi
)

The CLS one flickers significantly more. Why is that? Is there a difference in how they are implemented functionally? The reason I am asking is because I’m using the Windows Console API in C to create a small game. I’m using this function to clear the text each loop:

void clearScreen(HANDLE handle, enum ClearType type) {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (!GetConsoleScreenBufferInfo(handle, &csbi)) {
        printf("Unable to read Console Buffer Info!");
        exit(0);
    }
    DWORD charwritten;
    FillConsoleOutputCharacter(handle, ' ', csbi.dwSize.X * csbi.dwSize.Y, (COORD) {0, 0}, &charwritten);
}

But in a loop the result is the same as using CLS : lots of flicker. My question is, is there another way to clear the screen similar to ESC[2J (without flicker)? What other console function can I use?

Thanks in advance,

LardPies

Source: Windows Questions

LEAVE A COMMENT