#### Archived

This topic is now archived and is closed to further replies.

# Console Clear Screen

## Recommended Posts

Laroche    140
How do you clear the screen in Console in c++? Im using VS6

##### Share on other sites
SYSTEM("CLS");
or is it
system("cls");
lol anyways system tells the compiler to use like dos commands and in dos you type CLS to clear the screen

##### Share on other sites
Irrelevant    175
and i think to use system() you need stdlib.h or process.h

[edited by - Irrelevant on October 2, 2002 1:09:11 PM]

##### Share on other sites
Fruny    1658
MSDN Q99261 - HOWTO: Performing Clear Screen (CLS) in a Console Application.

Heh, look at the publication date

Anyway, I wouldn''t recommend system() for such a trivial task. It''s horribly inefficient.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

##### Share on other sites
ElCrazon    308

  void clrscr(){  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); }

should work, might be slow though...

##### Share on other sites
Laroche    140
hmm im asking because im fooling around at school making a sort of roguelike game, and I want it NOT to flicker as it redraws the characters. I had something like what elcrazon showed me, but it was too slow. Is system("cls") fast enough?

##### Share on other sites
Fruny    1658
quote:
Original post by Laroche
hmm im asking because im fooling around at school making a sort of roguelike game, and I want it NOT to flicker as it redraws the characters. I had something like what elcrazon showed me, but it was too slow. Is system("cls") fast enough?

Redraw only what needs to be redrawn, and write spaces to erase what needs to be erased. No cls.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

##### Share on other sites
Laroche    140
Im not quite sure how I could only redraw parts of the map? Its stored in a multi-dimensional array of CTiles, which basically just have a char representing the onscreen "sprite" and an int for the effect it has. Its all been wrapped into a CMap class with a function called "render", which basically looks like this:

  for (int y = 0; y < Height; ++y){for (int x = 0; x < Width; ++x){cout << pMap[x + y * Width].Sprite;}}

How would I write a function like render which would only update changes or something? Im a little lost, since im used to using graphics and not chars (im doing it for fun to fool around a bit)

##### Share on other sites
if you hack around, theres a way to get the DC and write to it (via blt). if i find the code, ill post it.

##### Share on other sites
Fruny    1658
Look at the console functions (starting with those referenced in the MSDN article/code snippet), there are functions that let you change the position of the console cursor (i.e. choose where to write). It also has functions that will actually do the output.

At that level, I''m not sure how will streams interact with them.

you might also search for "ANSI control sequences", though it will not work on Win2k & later consoles (works fine in terminals like telnet, if you''re doing a networked version of nethack (finally ))

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

##### Share on other sites
You should look up the function
WriteConsoleOutput
It is pretty fast, as it does a ton of things in a single call. Its good for writting stuff to specific areas of the screen, i suggest you wrap it up in a small gameconsole engine.

##### Share on other sites
_Stinger    122
I beleive there is a function in conio.h (C runtime) called clrscr() but I haven''t used that for ages, and I havent tried it in a console window.

##### Share on other sites
I''m new to C++, so this might be a bad idea, but why not just SetConsoleCursorPosition? You could inline it, then call it easily. Or is that too flickery?