Clearscreen flicker...

Im trying to make a roguelike game, and everytime you move it needs to clear the screen then reprint it, but I didnt use system(cls), I used this as a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void cls()
{
 HANDLE                     hOut;
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 DWORD                      count;
 DWORD                      cellCount;
 COORD                      homeCoords = {0, 0};
 hOut = GetStdHandle( STD_OUTPUT_HANDLE );
 if (hOut == INVALID_HANDLE_VALUE) return;
 if (!GetConsoleScreenBufferInfo( hOut, &csbi )) return;
 cellCount = csbi.dwSize.X *csbi.dwSize.Y;
 if (!FillConsoleOutputCharacter(
 hOut,
 (TCHAR) ' ',
 cellCount,
 homeCoords,
 &count
 )) return;
 if (!FillConsoleOutputAttribute(
 hOut,
 csbi.wAttributes,
 cellCount,
 homeCoords,
 &count
 )) return;
 SetConsoleCursorPosition( hOut, homeCoords );
}

And it worked, the only problem is the screen flickered because it took to long for it to clear it... So i tried this function:
1
2
3
4
5
6
7
8
9
10
11
void clearScreen()
{
    HANDLE hOut;
    COORD Position;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    Position.X = 0;
    Position.Y = 0;
    SetConsoleCursorPosition(hOut, Position);
}

This was almost what I wanted... It doesnt flicker at all, but it doesnt remove everything on the screen, only replaces it basically... So anything new text doesnt cover shows up around the new text...Here are the pictures:
First code: http://img683.imageshack.us/img683/2803/flicker.jpg
Second code: http://img686.imageshack.us/img686/5001/nonflicker.jpg
So how can I edit the second one to remove everything like the first one while not flickering?
The easiest way to get rid of the flickering is to erase the old text which is not covered by the new text by printing blanks on that position. Otherwise you have to create a second buffer to write in. If writing is done you can swap it with the screen buffer.

http://msdn.microsoft.com/en-us/library/ms682088(VS.85).aspx
I solved the problem now, thanks for the help...
This is a common question that comes from a misunderstanding of how things work.

If you clear the screen, then redraw it, of course there will be a flicker.

To avoid flicker, only redraw the parts of the screen that change.


The other option you can employ under Windows, is to create a second screen buffer. Update the invisible buffer. When done, swap the visible buffer.

Hope this helps.
@Duoas,
If you only update parts of the screen at a time, then don't you get tearing?

That's why you would use double-buffering like you said.

Or better yet, use decatruple-buffering.
No. http://en.wikipedia.org/wiki/Screen_tearing

By updating only those parts that change, you reduce update time and visual artifacts are reduced to the stuff that is changing anyway... This is the way that ncurses does it.

More than two buffers is also a waste. You only need two: one that is visible while you draw on the other. The only visual artifact is in the swap. If done properly, it is nearly instantaneous. If not done properly, then you get tearing. For video games, you only need to update when drawing is done, so the possibility of tearing is eliminated.
Topic archived. No new replies allowed.