Win32 C ++控制台清除屏幕而不闪烁 [英] Win32 C++ console clearing screen without blinking

查看:1086
本文介绍了Win32 C ++控制台清除屏幕而不闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过一些控制台游戏,屏幕刷新/清除自己没有恼人的眨眼。我尝试了很多解决方案,这里是我到现在:

I've seen some console games where the screen refreshes/clears itself without the annoying blinking. I've tried numerous solutions, here's what I got as of now:

while(true)
{
    if(screenChanged) //if something needs to be drawn on new position
    {
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    } 

    ///printf all the time graphics on their right position with SetConsoleCursorPosition

    Sleep(33.3f);
}  

仍然,我得到一些最小的闪烁。任何人都有任何想法?

Still, I'm getting some minimal blinking. Anyone have any ideas?

推荐答案

发生这种情况的原因是显示在刷新控制台屏幕时间绘制它。通常这会发生得很快,你永远不会看到它,但有一段时间,你在适当的时间,你遇到闪烁。

The reason this is happening is because the display refreshes between the time you clear the console screen and actually draw to it. Usually this can happen so fast that you never see it but once in a while you do it at the right time and you experience flickering.

一个伟大的选择是创建一个屏幕缓冲区与控制台屏幕具有相同的大小和宽度,执行所有的文本输出和更新,然后使用 WriteConsoleOutput 。请确保您考虑到屏幕缓冲区必须保存文本和属性信息,格式与控制台相同。

One great option is to create an offscreen buffer the same size and width as the console screen, do all of your text output and updating there, then send the entire buffer to the console screen using WriteConsoleOutput. Make sure you take into account that the screen buffer has to hold both text and attribute information, the same format as the console.

BOOL WINAPI WriteConsoleOutput(
  _In_     HANDLE hConsoleOutput,
  _In_     const CHAR_INFO *lpBuffer,
  _In_     COORD dwBufferSize,
  _In_     COORD dwBufferCoord,
  _Inout_  PSMALL_RECT lpWriteRegion
);

这篇关于Win32 C ++控制台清除屏幕而不闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆