C++ screen keeps flickering

I have this code but every time the screen is cleared to redraw the bar it flickers. How would I go about fixing this?

clear function (didn't make this)
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
28
29
30
31
32
33
HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 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 draw 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
std::string attack(int speed, bool hit) {

	HANDLE  hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, 15);

	std::string attack_type;
	int target_x;
	bool fighting = 1;

	int capacity = 35;
	int timer_x = 0;

	while (fighting) {
		cls();
		std::cout << "[";

		timer_x += 1;
		for (unsigned int i = 0; i < capacity; ++i) {
			if (i < timer_x) {
				SetConsoleTextAttribute(hConsole, 2);
				std::cout << ':';
			} else {
				if (i <= capacity - 16) { // Hit
					SetConsoleTextAttribute(hConsole, 3);
					std::cout << '#';
				}
				else if ((i >= capacity - 15) && (i <= capacity - 10)) { // Critical
					SetConsoleTextAttribute(hConsole, 4);
					std::cout << '#';
				}
				else if ((i >= capacity - 9)) { // Miss
					SetConsoleTextAttribute(hConsole, 15);
					std::cout << '#';
				}
			}
		}
		SetConsoleTextAttribute(hConsole, 15);
		std::cout << "]" << std::endl;

		if (GetAsyncKeyState(VK_SPACE)) {
			timer_x += 1;
			hit = 1;
			fighting = 0;
		}

		if (hit) {
			SetConsoleTextAttribute(hConsole, 15);
			if ((timer_x >= 20) && (timer_x <= 25)) {
				SetConsoleTextAttribute(hConsole, 15);
				std::cout << "Critical" << std::endl;
				attack_type = "Critical";
			}
			else if (timer_x >= 26) {
				SetConsoleTextAttribute(hConsole, 15);
				std::cout << "Miss" << std::endl;
				attack_type = "Miss";
			}
			else if (timer_x <= 19) {
				SetConsoleTextAttribute(hConsole, 15);
				std::cout << "Hit" << std::endl;
				attack_type = "Hit";
			}
		}

		Sleep(speed);
	}

	return attack_type;
}
Clearing the screen on every cycle is asking for flicker. Don't do that.

Alternately, use double buffering. Set an active screen and draw to the inactive screen. Swap to refresh. Google "msdn SetConsoleActiveScreenBuffer" to find the documentation.

Hope this helps.
Registered users can post here. Sign in or register to post.