需要使用 TTF 渲染文本在 C++ 中打印出一个全局变量。所以它会像这样显示:
“杀戮总数:”此处可变
明白了,但它会将文本推到左边
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, "Humans killed: " + totalKilled, foregroundColor, backgroundColor);
最佳答案
"Humans killed: " + totalKilled
这是指针运算。它不转换totalKilled
到 std::string
, 将其连接到 "Humans killed: "
, 并将结果转换为以 null 结尾的字符串。
试试这个:
#include <sstream>
#include <string>
template< typename T >
std::string ToString( const T& var )
{
std::ostringstream oss;
oss << var;
return var.str();
}
...
SDL_Surface* textSurface = TTF_RenderText_Shaded
(
font,
( std::string( "Humans killed: " ) + ToString( totalKilled ) ).c_str(),
foregroundColor,
backgroundColor
);
如果你愿意使用 Boost,你可以使用 lexical_cast<>
而不是 ToString()
.
关于c++ - 在渲染 SDL_TTF 文本 C++ 时显示变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22763881/