我正在尝试使用 raylib 显示一个包含西里尔字符的字符串。所以我加载带有代码点的字体,如下所示:
int codepoints[512] = { 0 };
for (int i = 0; i < 95; i++) codepoints[i] = 32 + i;
for (int i = 0; i < 255; i++) codepoints[96 + i] = 0x400 + i;
Font font = LoadFontEx("arial.ttf", 32, codepoints, 512);
如果我在屏幕上绘制
font.texture
,我可以在屏幕上看到 ASCII 和西里尔字符。但是,我无法使 DrawTextEx
渲染这些字符,但 DrawTextCodepoint
按我的预期工作。例如:
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextEx(font, "Ё", (Vector2) { 10, 70 }, 32, 0, BLACK); // draws a ? symbol
DrawTextEx(font, "\u0401", (Vector2) { 10, 40 }, 32, 0, BLACK); // draws a ? symbol
DrawTextCodepoint(font, 0x0401, (Vector2) { 10, 10 }, 32, BLACK); // draws Ё, as expected
EndDrawing();
我应该再花几分钟在谷歌上搜索答案。我正在使用 Visual Studio 2022,在查看了 this response 以获得更普遍的问题后,我突然意识到我应该将我的字符串文字显式标记为使用
u8
前缀编码的 UTF-8。所以,这将完美地工作:
DrawTextEx(font, u8"Ё", (Vector2) { 10, 70 }, 32, 0, BLACK); // draws Ё, as expected
DrawTextEx(font, u8"\u0401", (Vector2) { 10, 40 }, 32, 0, BLACK); // draws Ё, as expected too
非常感谢。我搜索了很长时间,但没有一个例子起作用)