虽然普通的 ttf 渲染函数采用 const char* 作为文本,但它们将渲染 TTF_RenderUNICODE_Solid() 函数采用 const Uint*。
我在处理 ASCII 字符时使用此结构从 ttf 表面创建纹理:
Text = TTF_RenderText_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
.
当我想使用 unicode 时,我尝试了这个:
Text = TTF_RenderUNICODE_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
.
因为 title.c_str() 是 const char* 并且函数需要 const Uint16 我无法创建纹理。
这是我传递标题的方式:
MenuButtons[0] = new CreateButton("TEXT");
void CreateButton(string title)
{
Text = TTF_RenderText_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
//or
Text = TTF_RenderUNICODE_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
}
问题:如何将我的字符串转换为 Uint16?
最佳答案
我见过 TTF_RenderText_Solid
的两个版本。一种支持utf-8
,一种支持latin1
。当您的版本支持 utf-8
时,您只需要一个字符串,其中文本以这种格式编码。 utf-8
和 latin-1
使用简单的 char
作为存储单元,因此您需要在文档中查找它才能知道。假设您的版本支持 latin1
,那么它涵盖的字符比您预期的 ascii
字符范围多。
然而,那仍然不是你想要的。因此,当您想要使用 TTF_RenderUNICODE_Solid
时,您的文本制作者必须提供 UTF-16
字符。因此,您需要知道 title
中的内容来自哪里以及它是如何编码的。
对于快速示例,您可以尝试静态文本(使用 c++11 编译器):
const Uint16 text[]=u"Hello World! \u20AC";
关于c++ - TTF_RenderUNICODE_Solid() 函数 Uint16 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32413086/