0

虽然普通的 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?

4

1 回答 1

1

我看过两个版本的TTF_RenderText_Solid。一个支持utf-8,一个支持latin1。当您的版本支持时utf-8,您只需要一个字符串,其中文本以这种格式编码。utf-8latin-1使用简单char的存储单元,因此您需要在文档中查找以了解这一点。让我们假设您的版本支持latin1然后它涵盖的字符比您预期的ascii字符范围多。

但是,那仍然不是您想要的。所以当你想使用TTF_RenderUNICODE_Solid你的文本制作者时必须提供UTF-16字符。所以你需要知道内容来自哪里title以及它是如何编码的。

对于一个快速示例,您可以尝试静态文本(使用 c++11 编译器):

const Uint16 text[]=u"Hello World! \u20AC";

这也应该有所帮助: 每个软件开发人员绝对、肯定必须了解 Unicode 和字符集的绝对最小值(没有借口!)

于 2015-09-05T12:56:19.767 回答