Jump to content
  • Advertisement
Sign in to follow this  
mxrss

Clearing the screen natively?

This topic is 4999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just have a question how one would clear the output from a console application in nativly. I use the system call function in the windows.h header file but i would like to know how to just 'clear it' so i can port this to linux. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
There is no portable way to clear the screen
unless you do something like

#ifdef WINDOWS
...
#else
...


if you know what i mean...

Share this post


Link to post
Share on other sites
A quick search turned up these:

"pritnf("\033[2J");"


// clrscr.cpp
//
// adapted from
// Vincent Fatica
// vefatica@syr.edu
// Syracuse University Mathematics
// http://barnyard.syr.edu/~vefatica/
//
// by Shannon Bauman
// August 9, 1998
//
// clear entire console screen buffer

#include <windows.h>
#include <conio.h>// don't know if it's really necessary
//I can run it without it, though...

int clrscr()
{
HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hndl, &csbi);
DWORD written;
DWORD N = csbi.dwSize.X * csbi.dwCursorPosition.Y +
csbi.dwCursorPosition.X + 1;
COORD curhome = {0,0};

FillConsoleOutputCharacter(hndl, ' ', N, curhome, &written);
csbi.srWindow.Bottom -= csbi.srWindow.Top;
csbi.srWindow.Top = 0;
SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
SetConsoleCursorPosition(hndl, curhome);

return 0;
}



Maybe this too?

void cls()
{
_asm {
mov AH,15
int 10h
mov AH,0
int 10h
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Xiachunyi
A quick search turned up these:

"printf("\033[2J");"

*** Source Snippet Removed ***


The print("\033[2J"]); requires ANSI.SYS or similar compatible support to work, I believe. (Loaded in CONFIG.SYS.)

The source snippet I think is from MSDN and works without ANSI.SYS because it literally writes spaces over the entire screen.

Another ANSI.SYS option from http://home.fhtw-berlin.de/~junghans/cref/EXAMPLES/clrscr.c

/*************************************************************************
*
* Purpose: Clear the screen with VT100 escape codes. This can be done
* with conio.h on PCs - non standard code. Or curses.h, bit of
* a fag...
* Author: M.J. Leslie
* Date: 22-Jun-94
*
************************************************************************/


void clrscr(void);

main()
{
clrscr();
}

void clrscr(void)
{
char a[80];
printf("\033[2J"); /* Clear the entire screen. */
printf("\033[0;0f"); /* Move cursor to the top left hand corner */
}



Many libraries include their own version of clrscr() defined in conio.h. You might check to see if whatever environment you're using has it predefined. (Most folks gravitate towards a cls() command.)

I'm pretty sure GCC has a clrscr() function, as well as Turbo C++ v4.5 and Borland C++ 4.5/5.0/5.5. I've never tried it under any of the Visual Studio products - I tend to use them to create graphical Win32 applications.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!