c++ - 为什么我在控制台做双缓冲时会出现随机符号?

标签 c++ windows doublebuffered double-buffering

我正在尝试在控制台中使用双缓冲。我已经编写了这段代码,它可以编译,但是在创建的控制台中,最下面的几行充满了随机字符。更改CHAR_INFO cur[Width*Height]; 进入 CHAR_INFO cur[Width*Height+200]; 有帮助,但我不明白为什么 Width*Height 的内存不够用。

#include <windows.h>
#include <ctime>

#define xMax 80
#define yMax 25
#define fPS 250
#define Delay 60

class dbconsole
{
private:
    int width, height, FPS, delay;
    HANDLE h0, h1;
    CHAR_INFO *chiBuffer;
    bool curBuffer;
    int drawingTimer;
public:
    dbconsole(int Width, int Height, int fps)
    {
        CHAR_INFO cur[Width*Height];
        width = Width;
        height = Height;
        FPS = fps;
        preparebuffer(h0);
        preparebuffer(h1);
        chiBuffer = cur;
        curBuffer = 0;
        drawingTimer = clock();
    }
    void preparebuffer(HANDLE &h)
    {
        CONSOLE_CURSOR_INFO cursor;
        cursor.bVisible = false;
        cursor.dwSize = 1;
        h = CreateConsoleScreenBuffer(
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                NULL,
                CONSOLE_TEXTMODE_BUFFER,
                NULL);
        SetConsoleCursorInfo(h, &cursor);
    }
    void putpixel(int x, int y, CHAR_INFO input)
    {
        chiBuffer[x+width*y]=input;
    }
    void depict()
    {
        SMALL_RECT srctWriteRect;
        srctWriteRect.Top = 0;
        srctWriteRect.Left = 0;
        srctWriteRect.Bottom = yMax-1;
        srctWriteRect.Right = xMax-1;
        if ((clock()-drawingTimer)*FPS>CLOCKS_PER_SEC)
        {
            if (curBuffer)
            {
                WriteConsoleOutput(h0, chiBuffer, {xMax,yMax}, {0,0}, &srctWriteRect);
                SetConsoleActiveScreenBuffer(h0);
            }
            else
            {
                WriteConsoleOutput(h1, chiBuffer, {xMax,yMax}, {0,0}, &srctWriteRect);
                SetConsoleActiveScreenBuffer(h1);
            }
            curBuffer=!curBuffer;
            drawingTimer = clock();
        }
    }
};

int main(void)
{
    dbconsole myConsole = dbconsole(xMax,yMax,fPS);
    SetConsoleTitle("Use arrow keys to control character");
    long long movetimer = clock();
    int x = 0, y = 0;
    while (true)
    {
        for (int i = 0; i < xMax; i++) for (int j = 0; j < yMax; j++) myConsole.putpixel(i,j, {' ',16});
        if ((clock()-movetimer)*Delay>CLOCKS_PER_SEC)
        {
            if (GetAsyncKeyState(VK_RIGHT))
            {
                movetimer = clock();
                if (x < xMax-1) x++;
            }
            if (GetAsyncKeyState(VK_LEFT))
            {
                movetimer = clock();
                if (x > 0) x--;
            }
            if (GetAsyncKeyState(VK_DOWN))
            {
                movetimer = clock();
                if (y < yMax-1) y++;
            }
            if (GetAsyncKeyState(VK_UP))
            {
                movetimer = clock();
                if (y > 0) y--;
            }
            if (GetAsyncKeyState(VK_ESCAPE)) return 0;
        }
        myConsole.putpixel(x,y,{1,15|16});
        myConsole.depict();
    }
}

我认为问题是由于chiBuffer对应的一些内存没有为它保留,但我不明白为什么。那么,问题是什么?

最佳答案

这里:

dbconsole(int Width, int Height, int fps)
{
    CHAR_INFO cur[Width*Height];
    ....
    chiBuffer = cur;

cur是一个局部变量,一旦离开构造函数,它就不存在了。此时,chiBuffer 不再是一个有效的指针,任何对它的使用都会导致未定义的行为。

一个简单的解决方案是使 chiBuffer 成为 std::vector:

// also: #include <vector> at the top
std::vector<CHAR_INFO> chiBuffer;

并像这样在构造函数中初始化它:

dbconsole(int Width, int Height, int fps)
  : chiBuffer(Width * Height)
{
  // cur no longer necessary.

唯一需要的额外改变是

//                     v----------v--- here
WriteConsoleOutput(h0, &chiBuffer[0], {xMax,yMax}, {0,0}, &srctWriteRect);

从 vector 中提取 C 函数 WriteConsoleOutput 可以理解的指针。这是可行的,因为 std::vector 保证它在内存中连续存储其元素(如数组)。

关于c++ - 为什么我在控制台做双缓冲时会出现随机符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27965064/

相关文章:

java - JFrame 中的 BufferStrategy 与 DIY 双缓冲

c++ - 通过使用字符串调用新构造函数来创建新对象

c# - 在 C# 中检测系统从 sleep 中唤醒的事件

java - 使用 JWindow 进行双缓冲

c# - 在不闪烁的情况下在双缓冲控件上重新绘制图像时出现问题

windows - 如何在 Windows 7 上停止 rabbitmq 服务器

c++ - friend 功能不访问另一个 friend 类的私有(private)成员

c++ - 如何用用户的输入填充二维数组?

c++ - 如何将指针值转换为枚举?

java - 设置 Emacs 23.4、CEDET 1.1 和 SemanticDB 以在 Windows 上使用 GNU Global