C++ 为什么在控制台中执行双缓冲时会出现随机符号?
我试图在控制台中使用双缓冲。我已经编写了这段代码,它可以编译,但是在创建的控制台中,最下面的几行都是随机字符。更改字符信息电流[宽度*高度]代码> 进入C++ 为什么在控制台中执行双缓冲时会出现随机符号?,c++,windows,doublebuffered,double-buffering,C++,Windows,Doublebuffered,Double Buffering,我试图在控制台中使用双缓冲。我已经编写了这段代码,它可以编译,但是在创建的控制台中,最下面的几行都是随机字符。更改字符信息电流[宽度*高度] 进入 CHAR_INFO cur[宽度*高度+200]有帮助,但我不明白为什么宽度*高度的记忆不够用 #include <windows.h> #include <ctime> #define xMax 80 #define yMax 25 #define fPS 250 #define Delay 60 class dbcon
CHAR_INFO cur[宽度*高度+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();
}
}
#包括
#包括
#定义xmax80
#定义yMax 25
#定义fPS 250
#定义延迟60
类dbconsole
{
私人:
整数宽度、高度、FPS、延迟;
手柄h0、h1;
字符信息*缓冲区;
布尔·库布弗;
内画计时器;
公众:
dbconsole(整数宽度、整数高度、整数fps)
{
字符信息电流[宽度*高度];
宽度=宽度;
高度=高度;
FPS=FPS;
制备缓冲液(h0);
制备缓冲液(h1);
chiBuffer=cur;
柯布费=0;
drawingTimer=时钟();
}
真空准备缓冲(手柄和h)
{
控制台\光标\信息光标;
cursor.bVisible=false;
cursor.dwSize=1;
h=CreateConsolesScreenbuffer(
一般的读,一般的写,
文件共享读取文件共享写入,
无效的
控制台\文本模式\缓冲区,
无效);
SetConsoleCursorInfo(h和光标);
}
无效像素(整数x,整数y,字符信息输入)
{
chiBuffer[x+宽度*y]=输入;
}
void descript()
{
小直srctwriter;
srctWriteRect.Top=0;
srctWriteRect.Left=0;
srctWriteRect.Bottom=yMax-1;
srctWriteRect.Right=xMax-1;
如果((时钟()-drawingTimer)*FPS>时钟每秒)
{
if(courbuffer)
{
WriteConsoleOutput(h0,chiBuffer,{xMax,yMax},{0,0},&srctWriteRect);
设置控制台活动屏幕缓冲区(h0);
}
其他的
{
WriteConsoleOutput(h1,chiBuffer,{xMax,yMax},{0,0},&srctWrite);
设置控制台活动屏幕缓冲区(h1);
}
柯布费=!柯布费;
drawingTimer=时钟();
}
}
};
内部主(空)
{
dbconsole myConsole=dbconsole(xMax、yMax、fPS);
SetConsoleTitle(“使用箭头键控制字符”);
长时间移动计时器=时钟();
int x=0,y=0;
while(true)
{
对于(inti=0;i每秒时钟数)
{
if(GetAsyncKeyState(VK_RIGHT))
{
movetimer=clock();
if(x0)x--;
}
if(GetAsyncKeyState(VK_向下))
{
movetimer=clock();
if(y0)y--;
}
if(GetAsyncKeyState(VK_ESCAPE))返回0;
}
myConsole.putpixel(x,y,{1,15 | 16});
myConsole.descript();
}
}
我认为这个问题是由于chiBuffer对应的一些内存没有为它保留,但我不明白为什么。那么,问题出在哪里呢?这里:
dbconsole(int Width, int Height, int fps)
{
CHAR_INFO cur[Width*Height];
....
chiBuffer = cur;
cur
是一个局部变量,一旦构造函数离开,它就不再存在。此时,chiBuffer
不再是有效指针,使用它会导致未定义的行为
一个简单的解决方案是将chiBuffer
Astd::vector
:
// also: #include <vector> at the top
std::vector<CHAR_INFO> chiBuffer;
这需要的唯一附加更改是
// v----------v--- here
WriteConsoleOutput(h0, &chiBuffer[0], {xMax,yMax}, {0,0}, &srctWriteRect);
从向量中提取C函数WriteConsoleOutput
可以理解的指针。这是因为std::vector
保证它在内存中连续存储其元素(如数组)。这里:
dbconsole(int Width, int Height, int fps)
{
CHAR_INFO cur[Width*Height];
....
chiBuffer = cur;
cur
是一个局部变量,一旦构造函数离开,它就不再存在。此时,chiBuffer
不再是有效指针,使用它会导致未定义的行为
一个简单的解决方案是将chiBuffer
Astd::vector
:
// also: #include <vector> at the top
std::vector<CHAR_INFO> chiBuffer;
这需要的唯一附加更改是
// v----------v--- here
WriteConsoleOutput(h0, &chiBuffer[0], {xMax,yMax}, {0,0}, &srctWriteRect);
从向量中提取C函数WriteConsoleOutput
可以理解的指针。这是因为std::vector
保证它在内存中连续存储其元素(如数组)。这里:
dbconsole(int Width, int Height, int fps)
{
CHAR_INFO cur[Width*Height];
....
chiBuffer = cur;
cur
是一个局部变量,一旦构造函数离开,它就不再存在。此时,chiBuffer
不再是有效指针,使用它会导致未定义的行为
一个简单的解决方案是将chiBuffer
Astd::vector
:
// also: #include <vector> at the top
std::vector<CHAR_INFO> chiBuffer;
这需要的唯一附加更改是
// v----------v--- here
WriteConsoleOutput(h0, &chiBuffer[0], {xMax,yMax}, {0,0}, &srctWriteRect);
从向量中提取C函数WriteConsoleOutput
可以理解的指针。这是因为std::vector
保证它在内存中连续存储其元素(如数组)。这里:
dbconsole(int Width, int Height, int fps)
{
CHAR_INFO cur[Width*Height];
....
chiBuffer = cur;
cur
是一个局部变量,一旦构造函数离开,它就不再存在。此时,chiBuffer
不再是有效指针,使用它会导致未定义的行为
一个简单的解决方案是将chiBuffer
Astd::vector
:
// also: #include <vector> at the top
std::vector<CHAR_INFO> chiBuffer;
这需要的唯一附加更改是
// v----------v--- here
WriteConsoleOutput(h0, &chiBuffer[0], {xMax,yMax}, {0,0}, &srctWriteRect);
从向量中提取C函数WriteConsoleOutput
可以理解的指针。这是因为std::vector
保证它在内存中连续存储其元素(如数组)。为什么不使用std::vector
为什么不使用std::vector
为什么不使用std::vector
为什么不使用u