以下代码在Windows XP中按预期工作,但在Windows 10中图像开始闪烁。我如何使它在Windows 10中工作?控制台图像在窗口中闪烁10
#include <windows.h>
#include <ctime>
#include <vector>
#define xMax 180
#define yMax 45
#define Fps 250
class dbconsole
{
private:
int width, height, FPS, delay;
HANDLE h0, h1;
std::vector<CHAR_INFO> chiBuffer;
bool curBuffer;
int drawingTimer;
void preparebuffer(HANDLE &h)
{
CONSOLE_CURSOR_INFO cursor = {false, 1};
SMALL_RECT windowRectangle = {0,0,width-1,height-1};
h = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL);
SetConsoleCursorInfo(h, &cursor);
SetConsoleScreenBufferSize (h, {width,height});
SetConsoleWindowInfo(h,true,&windowRectangle);
}
public:
dbconsole(int Width, int Height, int fps)
{
chiBuffer.reserve(Width*Height);
width = Width;
height = Height;
FPS = fps;
preparebuffer(h0);
preparebuffer(h1);
curBuffer = 0;
drawingTimer = clock();
for (int i = 0; i < xMax; i++) for (int j = 0; j < yMax; j++) chiBuffer[i+width*j] = {'t',16};
}
void depict()
{
SMALL_RECT srctWriteRect;
srctWriteRect.Top = 0;
srctWriteRect.Left = 0;
srctWriteRect.Bottom = height-1;
srctWriteRect.Right = width-1;
if ((clock()-drawingTimer)*FPS>CLOCKS_PER_SEC)
{
if (curBuffer)
{
WriteConsoleOutput(h0, &chiBuffer[0], {width,height}, {0,0}, &srctWriteRect);
SetConsoleActiveScreenBuffer(h0);
}
else
{
WriteConsoleOutput(h1, &chiBuffer[0], {width,height}, {0,0}, &srctWriteRect);
SetConsoleActiveScreenBuffer(h1);
}
curBuffer=!curBuffer;
drawingTimer = clock();
}
}
};
int main(void)
{
dbconsole myConsole = dbconsole(xMax,yMax,Fps);
while (true) myConsole.depict();
}
我想要的程序,以显示黑色字母蓝色背景上的“T”,但没有闪烁,并与双缓冲
我记得在Windows 10之前很久都有类似的问题......我不确定,但我认为这是XP时代。对我来说,解决方案是为当前屏幕缓冲区写一个更新程序,而不是频繁切换缓冲区,但是我没有去250 fps那里...... – grek40
切换到60 fps并没有帮助。 此外,此绘图适用于Windows 8. 我想使用这些控制台来显示一些信息,我可能需要在瞬间重新绘制每个分区,因此双缓冲似乎是必要的。 – Vladgor
ofcourse ... 60fps无论如何可能是您的显示器刷新率。您应该下降到1fps,以便评估单个刷新的效果。只要你能看到任何效果,它就会以更快的更新闪烁。 – grek40