c++ - TTF_RenderUNICODE_Solid() 函数 Uint16 参数?

标签 c++ sdl sdl-ttf

虽然普通的 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-8latin-1 使用简单的 char 作为存储单元,因此您需要在文档中查找它才能知道。假设您的版本支持 latin1,那么它涵盖的字符比您预期的 ascii 字符范围多。

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

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

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

这也应该有帮助: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

关于c++ - TTF_RenderUNICODE_Solid() 函数 Uint16 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32413086/

相关文章:

c++ - 为什么 CLion 向我发出警告?

c++ - 将 Eigen::MatrixXd 矩阵转换为 cgal 容器类型

在 for 循环期间控制台在 C 中崩溃

c++ - 应用程序在编译错误后执行时关闭,但在调试时它工作正常!

c++ - SDL_TTF 绘制垃圾

c++ - 如何为 dynamic_cast 指定 “all parameterized types” 或 “all argument lists”?

c++ - 这是变量声明的简写吗?

c++ - 无法将 'bool solid' 设置为仅某些图 block

c++ - SDL 在屏幕上显示分数

c++ - TTF_RenderUTF8_Shaded 无效读取