我正在尝试制作一个简单的控制台游戏,并且正在制作一种控制台图形引擎,该引擎绘制带有地图和一些文本的屏幕。由于某种原因,引擎会将奇怪的字符写入控制台,而不是要写入的字符。
此引擎采用两个2d向量来描述控制台中要使用的字符和颜色。我使用WriteConsole()
写入控制台,并使用SetConsoleTextAttribute()
更改绘制时的文本颜色。出于某种原因,当我尝试打印文本时,它会写一些真正奇怪的字符,这些字符与要打印的字符无关。颜色工作正常。我的字符存储为TCHAR
,颜色存储为int
。
我实际绘制屏幕的功能:
void update()
{
for (int y = 0; y < SCREEN_HEIGHT; y++) //loop through all of the tiles
{
for (int x = 0; x < SCREEN_WIDTH; x++)
{
if (screen.at(y).at(x) != buffer.at(y).at(x) && screenColors.at(y).at(x) != bufferColors.at(y).at(x)) //only draw the tile if it has changed
{
pos.X = x; //set coords of cursor to tile to be drawn
pos.Y = y;
SetConsoleTextAttribute(hStdOut, bufferColors.at(y).at(x)); //set the text color
SetConsoleCursorPosition(hStdOut, pos); //move the cursor to the tile to be drawn
WriteConsole(hStdOut, &(buffer.at(y).at(x)), 1, dw, NULL); //actually draw the tile
screen.at(y).at(x) = buffer.at(y).at(x); //update 2d screen vector (used to read what is on the screen for other reasons)
}
}
}
SetConsoleCursorPosition(hStdOut, restPos); //move the cursor away, so it doesn't look ugly
}
我写缓冲区矢量的函数:
void draw2dVector(int x, int y, vector<vector<TCHAR>> draw, vector<vector<int>> colors)
{
for (unsigned int drawY = 0; drawY < draw.size(); drawY++)
{
for (unsigned int drawX = 0; drawX < draw.front().size(); drawX++)
{
buffer.at(y + drawY).at(x + drawX) = colors.at(drawY).at(drawX); // <- I found the problem. I am writing color ints to the buffer.
bufferColors.at(y + drawY).at(x + drawX) = colors.at(drawY).at(drawX);
}
}
}
我将string
转换为vector<TCHAR>
的功能。
vector<TCHAR> stringToTCHARvector(string str, int strLen) //convert a TCHAR string to a TCHAR vector
{
vector<TCHAR> result;
for (int i = 0; i < strLen; i++) //loop through the characters in the string
{
result.push_back(str[i]); //add the character to the TCHAR vector
}
return result;
}
我希望输出看起来像这样:
###....### Inventory:
####....## Gold Piece
#..##...##
#......###
###.@..###
##########
但是相反,它给了我这个:
êêêêêêêêêê *insert strange characters here, because stack overflow doesn't
êêêêêwwêêê show them*
êêêêwwwwww
êêêêwwwwww
êêêêêwwwww
êêêêêwwwww
更新:
事实证明,问题在于我正在将ints
从我的颜色矢量写入我的TCHAR
缓冲区。我已经解决了这个问题。谢谢您的帮助。