为什么当我在控制台中进行双重缓冲时会出现随机符号
Why do random symbols appear when I do double buffering in console?
我正在尝试在控制台中使用双重缓冲。我已经编写了这段代码,它可以编译,但在创建的控制台中,最下方的几行填充了随机字符。更改CHAR_INFO cur[Width*Height];
到 CHAR_INFO cur[Width*Height+200];
有帮助,但我不明白为什么宽度*高度的记忆不足以满足它。
#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);
从 C 函数WriteConsoleOutput
可以理解的向量中提取指针。这是有效的std::vector
因为它保证将其元素连续存储在内存中(如数组)。
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 检查TCHAR数组输入是否为带符号整数C++
- 如何使用原子指针执行双缓冲
- 用符号版本替换对函数的所有调用
- 未解析的外部符号_MsiLocateComponentW@12.
- 将无符号char*缓冲区分配给字符串
- 为什么当我在控制台中进行双重缓冲时会出现随机符号