c++ - 如何使用 gotoxy 函数代替 clrscr

标签 c++ windows console tetris cls

做第一个项目是俄罗斯方 block ; 现在我正在做动画部分,但我在清除屏幕时遇到问题,我试过了:

void clrscr() 
{ 
  system("cls"); 
}

它有效,但它一直在闪烁屏幕,有没有办法使用 gotoxy 函数代替 clrscr 达到相同的目的?

我在 Visual Studio 2008 上使用 Windows 控制台系统 32。

最佳答案

system("cls") 执行一个 shell 命令来清除屏幕。这是极其低效的,而且绝对不适合游戏编程。

不幸的是,屏幕 I/O 依赖于系统。当您提到“cls”而不是“清除”时,我猜您正在使用 Windows 控制台:

  • 如果您有函数 gotoxy(),则可以一行接一行地定位并打印大量空格。它不是超高性能,但它是一种方法。这SO question提供 gotoxy() 替代方案,因为它是一个非标准函数。

  • microsoft support recommendation使用 winapi console functions 提供了一个更高效的替代方法来清除 Windows 上的屏幕例如 GetConsoleScreenBufferInfo()FillConsoleOutputCharacter()SetConsoleCursorPosition()

编辑:

我知道您使用基于字符的输出,因为您编写的是控制台应用程序而不是功能齐全的 win32 图形应用程序。

然后您可以通过仅清除控制台的一部分来调整上面提供的代码:

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
}

编辑2:

此外,这里还有两个可以与标准 cout 输出混合的光标定位函数:

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;
}  

关于c++ - 如何使用 gotoxy 函数代替 clrscr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29721523/

相关文章:

c++ - 生成 0 到 n 范围内的随机数,其中 n 可以 > RAND_MAX

c++ - 在 O(1) 中为 LinkedStack 编写 pop() 方法

windows - 让 Node 在 cygwin 中工作

windows - 如何通过 premake5 将全局属性添加到生成的 Visual Studio 项目和解决方案中?

c++ - vector<vector<double>> 是制作矩阵类的好方法吗?

c++ - 3ds Max 如何在网格类中存储法线?

c++ - 如何从一个进程向多个其他进程发出信号?

javascript - 在 Jest 中使某些测试保持沉默

iphone - objective-c iPhone : can we view console log on device

c++ - 控制台应用程序中的回显关闭问题 (Linux)