flickers in console (clear screen)

Good morning/evening!

I've tested Duoas code in clearing the screen but the flicker is still there.
http://www.cplusplus.com/forum/articles/10515/page1.html#msg49080

Also system("cls") has a flicker. //since "system" is bad/ugly/evil according to some.

So the solution is to make a code with fastest time in clearing screen to avoid flicker am I right?

my question is: Is there another built-in function (excluding conio.h library for cls, clrscr) in C++ for clearing the screen without flicker? If there's none so obviously I must make my own function for clearing the screen right?
Thanks.
What flicker are you referring to? I'm guessing the fact that you scrolling the screen rather than doing animation like console work.

if you want to change how the console operates there's as easy fix, move to GUI programming, or use ncurses linux, or pcurses for windows.
[...]pcurses for windows.

You mean pdcurses.
Excuse me, what do you mean according to some? It's never good; it's just usable if you're messing with some code thing.
And I honestly doubt that the flicker is a result of clearing the screen unless you're clearing it that fast.
Use curses if you're worried about advanced console manipulation.
There shouldn't be any flicker for a default Windows Console.

Have you done anything to it or modified it in any way?
For example, I usually use Marko Bozikovic's Console ( http://sourceforge.net/projects/console/ ) which flickers quite a bit (as it tries to keep up with the hidden Windows Console it uses).

The code I posted in the Clear the screen article clears the console window using exactly the same code as does the built-in cmd.exe "cls" function. The Windows event subsystem queues the modification events and usually updates them all at the same time, but it is occasionally possible that an update event occurs in the middle of the modifications.


I would be careful about how obnoxious you get about system() too. I didn't personally decide to start an anti-system() agenda. I simply learned from those who actually know better than I, and so now I spread the knowledge so that you who read here may learn as well.

Anyone who advocates you use system() to do stuff like clear the screen simply doesn't care about you, your job, or your company's assets and reputation. If I were an employer, anyone who deliberately disabled my company's software security by taking shortcuts with system() would find themselves in the unemployment line (and potentially worse -- depending how much damage is done before the error is discovered).

JSYK
Thanks for the reply.

The flickers that I am referring to is when I output a text then clear the screen then output again a text. the characters blink.

I have here some functions/lines in my code:

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
int main(){
....

}

void gameInstruct()
{
	cout<<"\n\n\n\tPress arrow key to change the location of the box."<<endl;
	cout<<"\tPress page up key to make a clockwise rotation."<<endl;
	cout<<"\tPress page down key to make a counterclockwise rotation.\n\n\n\n"<<endl;
}

void printOutput(int passArray[3][3], int m, int n, int positi, int positj)
{
    ....
}

void invalidInput(int numDisplay[3][3], int ipos, int jpos)
{
	int invali, invalj;
	system("cls");//<-- I only use this to see if my code is working
                                      //what I want is to make a function similar to this that clears the
                                      // screen fast to remove the flicker. 
	gameInstruct();
                ....
}

void rotateNumber(int numPass[3][3], int posi, int posj)
{
      ....
      	choice = _getch();
	choiceToInt = (int)choice;
	switch(choiceToInt){
		case 72:
			system("cls"); //<--- another one
			gameInstruct();
			posi = (posi + 1)%2;
			for(i = 0; i < 3; i++){
				for(j = 0; j < 3; j++){
					printOutput(numPass, i, j, posi, posj);
				}
				cout<<endl;
			}		
			break;
               ....
}


To be specific when I press a character in the keyboard, the "box" move or the number swithces position or outputs an invalid input then repeat again until the correct position of number is attained.

when I try debugging code with curses...
my Visual Studio 2008 said that I don't have a library of that.
and also someone told me that I could remove flickers without using pdcurses.
So that is my goal for now.

Now I know system() really sucks (from my point of view).

Thanks. I really appreciate your fast reply.


That's because curses is not a standard library. Curses is an external library for console manipulation. Look it up on wikipedia. The windows version is pdcurses, linux is ncurses.
If you are using an old 16-bit DOS/Win 3.x compiler (like Borland C/C++ 4.0 or the like) then that is probably the source of the flickering.

If you want to use pdcurses on Windows, visit here:
http://www.cplusplus.com/forum/windows/15935/#msg79025

You'll notice that requires MinGW -- the Windows port of the GCC. For other compilers, you'll have to download the sources and compile it yourself. Go here:
http://pdcurses.sourceforge.net/
(I'm not so sure you can do that on an old compiler. Typically, however, those old compilers provided routines to do that for you.)

Here's an example curses program:
http://www.cplusplus.com/forum/general/497/page1.html#msg1734

Good luck!
I solved my problem! Thanks to all.

It's as simple as this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <window.h>
void clearScreen()
{
    HANDLE hOut;
    COORD Position;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

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


Hehe, by the way my program is not finished yet. I have to make a box. Goodluck to me!

Thanks.
But... that doesn't clear the screen -- it only positions the cursor "home".

Are you re-drawing the contents of a screen that don't change from clear to clear? That would definitely cause a flicker -- but it is not MS's fault (or mine!).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
while (true)
  {
  cout << "Select your option:\n"
          " 1 Eat paint\n"
          " 2 Drive recklessly"
          " 3 Run with scissors"
          " 4 Feed the elephants"
          " 5 Give up"
          "> "
       << flush;
  int option;
  if (cin >> option)
    {
    if (option == 5) break;
    }
  else
    {
    cin.clear();
    cin.ignore( 10000, '\n' );
    }

  ClearScreen();
  }
This will definitely flicker, because it was built to do that. Redrawing the whole screen takes time.

Hope this helps.
 
" 6 Tease the alligators with a stick"
Yeah it doesn't clear the screen but I modify it to this one:

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
71
72

bool rotateNumber(int numPass[3][3], int posi, int posj)
{
   ....
   choice = _getch();
   choiceToInt = (int)choice;
   switch(choiceToInt){
       case 72:
    	   clearScreen();
	   gameInstruct();
	   clearBox();
	   clearScreen();
	   cout<<"\n\n\n\n\n\n\n\n\n\n";
	   posi = (posi + 1)%2;
	   printOutput(numPass, posi, posj);
	   break;
      case 80:
	   clearScreen();
	   gameInstruct();
	   clearBox();
	   clearScreen();
	   cout<<"\n\n\n\n\n\n\n\n\n\n";
	   posi = (posi + 1)%2;
	   printOutput(numPass, posi, posj);
	   break;
        ....
}
				
void gameInstruct()
{
	cout<<"\n\n\n\tPress arrow key to change the location of the box."<<endl;
	cout<<"\tPress page up key to make a clockwise rotation."<<endl;
	cout<<"\tPress page down key to make a counterclockwise rotation."<<endl;
	cout<<"\tPress ESC key to quit.\n\n\n"<<endl;
}

void invalidInput(int numDisplay[3][3], int ipos, int jpos)
{
	clearScreen();
	gameInstruct();
	printOutput(numDisplay, ipos, jpos);
	cout<<"\n\t\t\t\tInvalid Input!";
}

void clearScreen()
{
    HANDLE hOut;
    COORD Position;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

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

void clearBox()
{
    HANDLE hOut;
    COORD Position;
    DWORD Written;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    Position.X = 0;
    Position.Y = 10;
    FillConsoleOutputCharacter(hOut,' ', 1000000, Position, &Written);

    SetConsoleCursorPosition(hOut, Position);
}



So basically the clearBox() function clears the screen at (0,10) and so on... since at (0,0) to (0,9) the console output same text at same location.

Well thats how I do it. It works! the flicker was removed.

FillConsoleOutputCharacter(hOut,' ', 1000000, Position, &Written);
This line actually "clears" (put space to remove the characters printed.

Correct me if I'm wrong. Thanks. I'm still learning C++.
Topic archived. No new replies allowed.