如何使用gotox函数而不是clrscr [英] how to use gotoxy function instead of clrscr
问题描述
做第一个项目,它是tetris;
现在我正在做动画部分,但我有一个问题,清除屏幕,我试过:
void clrscr()
{
system(cls);
}
它工作,但它不断闪烁的屏幕,对于同一目的,c $ c> gotoxy 函数代替 clrscr
?
不幸的是屏幕I / O是系统依赖。正如你提到cls而不是清除,我想你正在使用Windows控制台:
-
你有一个函数
gotoxy()
,可以在一行之后,打印很多空格。这不是超级执行,但它是一种方法。此 SO问题提供gotoxy()
交替,因为它是一个非标准函数。 -
此微软支持推荐提供了一个更高效的替代方法,以清除Windows上的屏幕,使用 winapi控制台函数,例如
GetConsoleScreenBufferInfo()
,FillConsoleOutputCharacter c $ c>和
SetConsoleCursorPosition()
。
编辑:
我理解你使用基于字符的输出,当你写一个控制台应用程序,而不是一个功能齐全的win32图形应用程序。
然后你可以调整上面提供的代码,只清除控制台的一部分:
void console_clear_region(int x,int y,int dx,int dy,char clearwith = '')
{
HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //屏幕缓冲区信息
DWORD chars_written; // count successful output
GetConsoleScreenBufferInfo(hc,& csbi); // Get screen info& size
GetConsoleScreenBufferInfo(hc,& csbi); //获取当前文本显示属性
if(x + dx> csbi.dwSize.X)//验证最大宽度和高度
dx = csbi.dwSize.X - x; //并且如果必要则调整
if(y + dy> csbi.dwSize.Y)
dy = csbi.dwSize.Y-y;
for(int j = 0; j< dy; j ++){//为行$循环
COORD cursor = {x,y + j}; //开始填充
//使用char(默认为空白)填充行部分
FillConsoleOutputCharacter(hc,TCHAR(clearwith),
dx,cursor,& chars_written);
//相应地改变文本属性
FillConsoleOutputAttribute(hc,csbi.wAttributes,
dx,cursor,& chars_written);
}
COORD cursor = {x,y};
SetConsoleCursorPosition(hc,cursor); //设置新光标位置
}
编辑2:
此外,这里有两个cusor定位函数,您可以与标准cout输出结合使用:
void console_gotoxy(int x,int y)
{
HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
COORD cursor = {x,y};
SetConsoleCursorPosition(hc,cursor); //设置新的光标位置
}
void console_getxy(int& x,int& y)
{
HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //屏幕缓冲区信息
GetConsoleScreenBufferInfo(hc,& csbi); // Get screen info& size
x = csbi.dwCursorPosition.X;
y = csbi.dwCursorPosition.Y;
}
Doing first project and it's tetris; Now I'm doing the animation part, but I have a problem with clearing the screen, I've tried :
void clrscr()
{
system("cls");
}
It worked but it kept flashing the screen, is there a way using gotoxy
function instead of clrscr
on same purpose?
I'm using windows console system 32, on visual studio 2008.
system("cls")
executes a shell command to clear the screen. This is tremendously innefficient and definitievly not for game programming.
Unfortunately screen I/O is system dependent. As you refer to "cls" and not to "clear", I guess you're working with windows console:
If you have a function
gotoxy()
, it's possible to position on one line after the other and print a lot of spaces. It's not ultra performant, but it's an approach. This SO question providesgotoxy()
alternatves, as it's a non-standard function.This microsoft support recommendation provides a more performant alternative to clear the screen on Windows, using the winapi console functions such as
GetConsoleScreenBufferInfo()
,FillConsoleOutputCharacter()
andSetConsoleCursorPosition()
.
Edit:
I understand that you use character based output, as you write a console app and not a full featured win32 graphic app.
You can then adapt the code provided above, by clearing only a part of the console:
void console_clear_region (int x, int y, int dx, int dy, char clearwith = ' ')
{
HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE); // get console handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // screen buffer information
DWORD chars_written; // count successful output
GetConsoleScreenBufferInfo(hc, &csbi); // Get screen info & size
GetConsoleScreenBufferInfo(hc, &csbi); // Get current text display attributes
if (x + dx > csbi.dwSize.X) // verify maximum width and height
dx = csbi.dwSize.X - x; // and adjust if necessary
if (y + dy > csbi.dwSize.Y)
dy = csbi.dwSize.Y - y;
for (int j = 0; j < dy; j++) { // loop for the lines
COORD cursor = { x, y+j }; // start filling
// Fill the line part with a char (blank by default)
FillConsoleOutputCharacter(hc, TCHAR(clearwith),
dx, cursor, &chars_written);
// Change text attributes accordingly
FillConsoleOutputAttribute(hc, csbi.wAttributes,
dx, cursor, &chars_written);
}
COORD cursor = { x, y };
SetConsoleCursorPosition(hc, cursor); // set new cursor position
}
Edit 2:
And in addition, here two cusor positionning function that you can mix with standard cout output:
void console_gotoxy(int x, int y)
{
HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE); // get console handle
COORD cursor = { x, y };
SetConsoleCursorPosition(hc, cursor); // set new cursor position
}
void console_getxy(int& x, int& y)
{
HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE); // get console handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // screen buffer information
GetConsoleScreenBufferInfo(hc, &csbi); // Get screen info & size
x = csbi.dwCursorPosition.X;
y = csbi.dwCursorPosition.Y;
}
这篇关于如何使用gotox函数而不是clrscr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!