clear screen??

How do i clear the screen of the program because i want to make a text game??
If you are using visual studio 2008 or 2005 and have the platform SDK installed with it and configured correctly. (You can use other IDE's with the platform SDK as long as you have it set up correctly)

So if you have that installed and #include <windows.h>

You can use:

system("cls");

or something like this - by bliting an array to the screen which is a little hard to learn but a very a good method as printf() and std::cout take a lot of time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define WINDOW_WIDTH_SIZE 79
#define WINDOW_HEIGHT_SIZE 45

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

CHAR_INFO     offscreenBuffer[WINDOW_HEIGHT_SIZE][WINDOW_WIDTH_SIZE];

// ---

COORD offscreenBufferBuff;
offscreenBufferBuff.X = WINDOW_WIDTH_SIZE;
offscreenBufferBuff.Y = WINDOW_HEIGHT_SIZE;
      
COORD dest;
dest.X = dest.Y = 0;
      
SMALL_RECT  rt;
rt.Top = 0;
rt.Bottom = WINDOW_HEIGHT_SIZE;
rt.Left = 0;
rt.Right = WINDOW_WIDTH_SIZE;
      
WriteConsoleOutput(hOut, &offscreenBuffer[0][0], offscreenBufferBuff, dest, &rt);


Another method is reseting the consoles cursor to the begining of the screen so you have no flickering:

1
2
COORD CursorPosition = {0, 0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);


This last method I know is direct from microsofts MSDN site - which is a better way to clear the screen than "system("cls"); :
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
void ClearScreen()
{
	COORD coordScreen = {0, 0};    // home for the cursor 
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD dwConSize;

	// Get the number of character cells in the current buffer. 
	if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
	{
		return;
	}

	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

	// Fill the entire screen with blanks.
	if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten))
	{
		return;
	}

	// Get the current text attribute.
	if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
	{
		 return;
	}

	// Set the buffer's attributes accordingly.
	if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
	{
		return;
	}

	// Put the cursor at its home coordinates.
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
}


Hope some of this helps.

Myth.
Last edited on
Topic archived. No new replies allowed.