Windows 游戏:UTF-8、UTF-16、DirectX 和 Lua

发布于 2024-08-23 12:05:02 字数 1385 浏览 5 评论 0原文

我正在为 Windows 开发一个游戏用于学习目的(我正在学习 DirectX)。我希望它有 UTF 支持。

阅读这个问题我了解到Windows使用wchar_t,即UTF-16 。我希望我的游戏能够支持 Lua 脚本,而 Lua 不太喜欢 Unicode。它只是将字符串视为“字节流”;这对于 UTF-8 来说足够有效,但 UTF-16 几乎无法使用。

长话短说:windows 需要 UTF-16,lua 需要 UTF-8。

所以我想,让我们将 UTF-8 与普通的 char*string 一起使用! .length() 会很混乱,但谁在乎呢?然而它不起作用:

const char test_utf8[] = { 111, 108, 0xc3, 0xa9, 0 }; // UTF-8 for olè
mFont->DrawTextA(0, test_utf8, -1, &R, DT_NOCLIP, BLACK);
    /* DrawText is a Direct3d function to, well, draw text.
     * It's like MessageBox: it is a define to either DrawTextA
     * or DrawTextW, depending if unicode is defined or not. Here
     * we will use DrawTextA, since we are passing a normal char*. */

这会打印 olé。换句话说,它似乎没有使用 UTF-8,而是使用 ISO-8859-1

那么,我能做什么呢?我可以想到以下几点:

  1. 放弃UTF的想法;使用 ISO-8859-1 并感到高兴(这就是魔兽世界所做的,至少对于 enUS 版本)
  2. 将每个帧的每个字符串从 UTF-8 转换为 UTF-16(我担心性能问题,不过,考虑到它会对每个字符串每秒执行 60 次以上,而且时间复杂度为 O(N),我很确定它会相当慢)
  3. 对于每个 lua 字符串,保留一个 UTF-16 副本;巨大的内存浪费,非常难以实现(当 UTF-16 字符串在 Lua 中更改时保持最新,等等)

I'm developing a game for windows for learning purposes (I'm learning DirectX). I would like it to have UTF support.

Reading this question I learned that windows uses wchar_t, which is UTF-16. I want my game to have Lua scripting support, and Lua doesn't really like Unicode much.. It simply treats strings as a "stream of bytes"; this works well enough for UTF-8, but UTF-16 would be virtually impossible to use.

Long story short: windows wants UTF-16, lua wants UTF-8.

So I thought, let's just use UTF-8 with normal char* and string! .length() will be messed up but who cares? However it doesn't work:

const char test_utf8[] = { 111, 108, 0xc3, 0xa9, 0 }; // UTF-8 for olè
mFont->DrawTextA(0, test_utf8, -1, &R, DT_NOCLIP, BLACK);
    /* DrawText is a Direct3d function to, well, draw text.
     * It's like MessageBox: it is a define to either DrawTextA
     * or DrawTextW, depending if unicode is defined or not. Here
     * we will use DrawTextA, since we are passing a normal char*. */

This prints olé. In other words it doesn't appear to use UTF-8 but rather ISO-8859-1.

So, what can I do? I can think of the following:

  1. Abandon the idea of UTF; use ISO-8859-1 and be happy (this is what World of Warcraft does, at least for the enUS version)
  2. Convert every single string at every single frame from UTF-8 to UTF-16 (I'm worried about performance issues, though, considering it will do this 60+ times a second for each string and it's O(N) I'm pretty sure it will be fairly slow)
  3. For each lua string keep an UTF-16 copy; huge waste of memory, very difficult to implement (keeping the UTF-16 strings up to date when they change in Lua, etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追星践月 2024-08-30 12:05:02

它也不使用 8859-1,而是使用系统的本地代码页。您可以通过自己转换字符串来转换为 UTF16 并使用 DrawText()。如果您的类库没有任何支持,那么您可以使用 MultiByteToWideChar()

It doesn't use 8859-1 either, it uses your system's local code page. You can convert to UTF16 and use DrawText() by converting the string yourself. If your class library doesn't have any support then you can use MultiByteToWideChar().

一杯敬自由 2024-08-30 12:05:02

如果 WoW 不使用 DirectX 文本绘制方法,我不会感到震惊。拥有自己的自定义文本绘制解决方案可以让您在编码支持方面更加灵活。这并不太难。

I wouldn't be shocked is WoW doesn't use DirectX text draw methods. Having your own custom text draw solution gives you a lot more flexibility in your support for encodings. It isn't too hard.

丶视觉 2024-08-30 12:05:02

í•œêµì–´/ì¡°ì„ ë§。我在《星际争霸》中经常看到这种情况,因为它没有对 Unicode 提供适当的支持。

打好仗!使用 UTF-8。每帧转换为 UTF-16(除非文档中提到有更好的方法,我懒得看)。不要担心这里的性能,直到它成为问题!

한국어/ì¡°ì„ ë§. I see this all the time in StarCraft because it doesn't have proper support for Unicode.

Fight the good fight! Use UTF-8. Convert to UTF-16 every frame (unless there's a better way to do it mentioned in the docs which I'm too lazy to look at). Don't worry about performance here until it becomes a problem!

丢了幸福的猪 2024-08-30 12:05:02

您可以让 lua 缓存到 UTF-16 的转换

utf16 = setmetatable ( {} , { __index = function ( t , k , v )
        local utf16str = my_conversion_func_to_utf16 ( v )
        rawset ( t , k , utf16str )
        return utf16str
    end } )

,然后让您的所有函数仅采用 utf16 字符串类型(可能是 lua 字符串或某种用户数据(可能是您的 wchar_t 数组)),

如果你不明白...

You can get lua to cache your conversions to UTF-16

utf16 = setmetatable ( {} , { __index = function ( t , k , v )
        local utf16str = my_conversion_func_to_utf16 ( v )
        rawset ( t , k , utf16str )
        return utf16str
    end } )

then just have all your functions only take the utf16 string types (which could be a lua string or some sort of userdata (which could be your wchar_t array))

I can help more if you don't understand...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文