Portable way to clear a screen in C++

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Portable way to clear a screen in C++

Post by Durandal »

This is pissing me off. I've been searching forever, and I haven't found a cross-platform method for clearing the screen using C++. My C++ teacher gave us system ("cls"), since apparently no other compilers exist in the scope of this class.

I could use system ("clear") for for *nix boxes, but I'd rather just have one set of code for the trivial programs I'm writing. Anyone know of a way to clear the screen that will work under VS.Net and gcc or any compliant C compiler?
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
User avatar
Pu-239
Sith Marauder
Posts: 4727
Joined: 2002-10-21 08:44am
Location: Fake Virginia

Post by Pu-239 »

I don't think there are any, since I haven't found it anywhere in the standard C library. Can't you use #defines, #ifdef, #else, and #endif, and/or make a wrapper function?

ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer


George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
User avatar
Sarevok
The Fearless One
Posts: 10681
Joined: 2002-12-24 07:29am
Location: The Covenants last and final line of defense

Post by Sarevok »

PU has a good point there. The best way would be to use a macro.
I have to tell you something everything I wrote above is a lie.
User avatar
Slartibartfast
Emperor's Hand
Posts: 6730
Joined: 2002-09-10 05:35pm
Location: Where The Sea Meets The Sky
Contact:

Post by Slartibartfast »

Using the system() is probably the worst way to go about it. The computer effectively has to run a new process just to clear your screen... in DOS it means loading a COMMAND.COM in memory, executing "cls", then terminating COMMAND.COM.

The best way is to directly write in screen memory, and yes, that's pretty much machine-specific. There are a few tutorials on the 'net. Some consoles may not even HAVE a clear function (specifically dumb terminals I suppose), and on those you just have to do a page break or something - that is, writing 25 or so line feeds.

Maybe you can try putchar(12) or putchar(14) or sumthin.

To tell the truth, if you're trying to do it in a portable way, there is no real reason to clear the screen, just as there should be no reason to use extended ASCII characters, or colored chars, etc... like if you're trying to make some kind of DOS console function (like "arj x file.arj")
Image
User avatar
Pu-239
Sith Marauder
Posts: 4727
Joined: 2002-10-21 08:44am
Location: Fake Virginia

Post by Pu-239 »

The problem is that different characters are written to different consoles, such as a VT or an Xterm, so you'll have to account for that too if you don't use clear. It's better on *nix just to call functions from your curses library. Don't know much about DOS.


Funny how I made a JNI class that called a native library that simply ran system(cls) for cls() that was called every 20ms- horribly inefficient, but easy.


For ncurses, you do :

Code: Select all

#include <curses.h>
#include <term.h>
                                                                                                                                              
int main(void){
    setupterm((char *)0, 1, (int *)0);
    putp(clear_screen);
    endwin();
    return 0;
}
Note the above was derieved from reading the various ncurses man pages, which are a bitch(possibily resulting in bad coding).
Last edited by Pu-239 on 2004-03-11 07:54pm, edited 1 time in total.

ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer


George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
User avatar
Jawawithagun
Jedi Master
Posts: 1141
Joined: 2002-10-10 07:05pm
Location: Terra Secunda

Post by Jawawithagun »

the cheapest and easiest way to do it usually is just printing 25 carriage returns, scrolling the onscreen text up 25 lines (and therefore clearing the screen in a 80x25 console window as it is common under WinDos)
"I said two shot to the head, not three." (Anonymous wiretap, Dallas, TX, 11/25/63)

Only one way to make a ferret let go of your nose - stick a fag up its arse!

there is no god - there is no devil - there is no heaven - there is no hell
live with it
- Lazarus Long
User avatar
Pu-239
Sith Marauder
Posts: 4727
Joined: 2002-10-21 08:44am
Location: Fake Virginia

Post by Pu-239 »

Except that *nix consoles aren't always a puny 25 lines like Windoze... :lol: mine's 160x64 on VT and 143x69 in maximized terminal emulator (yes, I know you can change the # of lines on a cmd console, but I don't think you can change columns).

ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer


George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
User avatar
Pu-239
Sith Marauder
Posts: 4727
Joined: 2002-10-21 08:44am
Location: Fake Virginia

Post by Pu-239 »

Also, on *nix, the ncurses method is cheaper, since all it does is print a couple characters to trigger a clearscreen rather than attempting to scroll down or overwrite stuff w/ blanks.

ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer


George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post by Durandal »

Jawawithagun wrote:the cheapest and easiest way to do it usually is just printing 25 carriage returns, scrolling the onscreen text up 25 lines (and therefore clearing the screen in a 80x25 console window as it is common under WinDos)
*nix consoles can be a lot larger than 80x24.
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
User avatar
Uraniun235
Emperor's Hand
Posts: 13772
Joined: 2002-09-12 12:47am
Location: OREGON
Contact:

Post by Uraniun235 »

Clearly the answer is to put in 500 carriage returns. :P
User avatar
Mad
Jedi Council Member
Posts: 1923
Joined: 2002-07-04 01:32am
Location: North Carolina, USA
Contact:

Post by Mad »

The man page for clear:
The clear utility clears the terminal screen if this is possible. It looks in the environment for the terminal type, if this is not already specified by the term operand, and then looks up the terminfo database to figure out how to clear the screen.
Essentially, that's what you'd have to do to do it properly for *nix. To take a shortcut, you'd probably just take the codes used for the most common term types (xterm, vt100, and ansi, I think are the 3 most common).

For MS-DOS/Windows console, I know there used to be a control character you could send that would clear the screen. Dunno if it became unsupported in newer incarnations, though. Try outputting char values between from 1-31 and see what you get.
Later...
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post by Durandal »

I just settled on this ...

Code: Select all

void clearScreen ()
{
	#if defined (WIN_32)
		system ("cls");
	#else
		system ("clear");
	#endif
}
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
User avatar
Pu-239
Sith Marauder
Posts: 4727
Joined: 2002-10-21 08:44am
Location: Fake Virginia

Post by Pu-239 »

Mad wrote:The man page for clear:
The clear utility clears the terminal screen if this is possible. It looks in the environment for the terminal type, if this is not already specified by the term operand, and then looks up the terminfo database to figure out how to clear the screen.
Essentially, that's what you'd have to do to do it properly for *nix. To take a shortcut, you'd probably just take the codes used for the most common term types (xterm, vt100, and ansi, I think are the 3 most common).

For MS-DOS/Windows console, I know there used to be a control character you could send that would clear the screen. Dunno if it became unsupported in newer incarnations, though. Try outputting char values between from 1-31 and see what you get.

Well that's what this did:

Code: Select all

#include <curses.h>
#include <term.h>
                                                                                                                                             
int main(void){
    setupterm((char *)0, 1, (int *)0);
    putp(clear_screen);
    endwin();
    return 0;
}
putp just reads what you want to send it and prints out the necessary character from the terminfo database. No need to figure out how to query the TI database manually.

ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer


George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
Post Reply