游戏中的 Unicode 输入处理
我有一个游戏,要求我允许玩家通过网络互相聊天。一切都很好,除了玩家可以输入 Unicode 输入的部分。
所以,这个问题可以分为两部分:
当玩家打字时,我如何捕获输入?我之前已经通过游戏输入处理(轮询)做到了这一点,但是,它不是与 Windows 窗体之类的响应速度一样。
在我将输入捕获到字符串中后,如何使用 TrueType 字体输出它?我问这个的原因是因为通常,我会在游戏开始时从所有字体构建位图字体游戏中使用的文字。但使用 unicode 输入时,需要近 10k 个字符,这在游戏开始时是不可能构建的。
PS 我的目标输入语言更具体为中文、韩语和日语。
I have a game that requires me to allow players to chat with each other via network. All is well, except the part where players can type in Unicode input.
So, the question can be split into two parts:
When players type, how do I capture input? I have done this before via the game input handling (polling), however, it is not as responsive as something like Windows Forms.
After I capture input into a string, how do I output it using TrueType Fonts? The reason I ask this is because usually, I would build bitmap fonts at the start of the game from the all the text used in the game. But with unicode input, there are nearly 10k characters that are needed, which is quite impossible to build at the start of the game.
P.S. My target input languages are more specific to Chinese, Korean and Japanese.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于输入
用于渲染
如果所需的字体大小相当小,比如说 16px,您实际上可以将其全部渲染为单个纹理,您可以在该尺寸的 1024x1024 纹理上容纳至少 4096 个字形,当您将它们紧密打包时可以容纳更多字形(请参阅 fontgen 例如代码)。这对于普通聊天来说应该足够了,但不足以容纳 TTF 文件的所有字形。
如果您不想使用更大的纹理尺寸,则必须根据需要生成字体,为此只需像往常一样创建纹理,然后使用 glTexSubImage2D 将新字形上传到纹理。
另一种选择是不将纹理用于字形,而是用于文本本身。这样你就可以绕过字形生成产生的所有麻烦。但对于非静态可编辑文本来说,这可能不是一个好主意。
For Input
unicode
member of SDL_keysym to get the unicodeFor Rendering
If the needed font size is reasonably small, say 16px, you actually could just render it all to a single texture, you can fit a minimum of 4096 glyphs on 1024x1024 texture at that size, a bit more when you pack them tightly (see fontgen for example code). That should be enough for common chat, but not enough to fit all the glyphs of a TTF file.
If you don't want to use a larger texture size you have to generate the fonts on demand, to do that just create the Texture's as usual and then use glTexSubImage2D to upload new glyphs to the texture.
Another alternative is to not use textures for glyphs, but for the text itself. That way you bypass all the trouble that glyph generation produces. But its probably not such a good idea for non-static editable text.
我猜这取决于你使用什么。我对 SDL 不熟悉。在 Linux 上,您可以使用标准 X 函数和事件循环,它运行良好(例如在 Quake 中使用;因此它应该具有足够的反应性)。
您应该看看 FreeType2 库。它允许您加载 TrueType 字体,并检索任何字符的字形(图像)。
我有同样的问题。我想带有 MRU(最近使用的)字符的缓存管理器可以解决这个问题。不过,我比简单的静态位图更复杂一些。
That depends on what you use I guess. I'm not familiar with SDL. On Linux you can use standard X functions and event loop, it works well (used in Quake for example; so it should be reactive enough).
You should have a look at FreeType2 library. It lets you load TrueType fonts, and retrieve the glyph (the image) of any character.
I have the same problem. I guess a cache manager with MRU (most recently used) characters would do the trick. I bit more complicated than simple static bitmap though.
我自己没有做过游戏开发,所以我对那里的工作原理只有一个模糊的想法,但这里是我的 2 美分:
不要在程序开始时缓存所有字形。相反,当您必须显示聊天字符串时,请将整个字符串即时渲染为某个新纹理。将此纹理保留在内存中,直到不太可能再次需要它为止(例如,在聊天窗口关闭之后)。也许您可以在更新时重新渲染整个聊天窗口 - 那么您只需担心一个纹理。
I've done no game development myself, so I have just a vague idea of how things work there, but here are my 2 cents:
Don't cache all the glyphs at the start of the program. Instead, when you have to display a chat string, render the whole string on-the-fly to some new texture. Keep this texture in memory until a time when it is unlikely that it will be needed again (say, after the chat window is closed). Perhaps you can re-render the whole chat window when it gets updated - then you would only have one texture to worry about.
就显示而言,我对 本教程,音译为C++。
对于字体,GNU Unifont 具有完整的BMP 字形覆盖,以方便的 TTF 形式提供。
As far as display goes, I've had very good luck with the caching system described in this tutorial, transliterated to C++.
For fonts, GNU Unifont has full BMP glyph coverage, available in convenient TTF form.
下面是一些代码,展示了如何使用 SDL 捕获键盘输入。
首先,您需要通过调用 EventPoll 从 SDL 查询按键输入。
您可以在准备好处理输入时执行此操作,或者定期执行此操作
固定间隔并将按键和键盘状态存储在内部表中。
以下是描述使用 OpenGL 渲染文本的方法的文档链接:http://www.opengl.org/resources/features/fontsurvey/" rel="nofollow noreferrer">http://www.opengl.org opengl.org/resources/features/fontsurvey/
您可能想要做的是捕获键盘输入并使用您预加载的正确字体即时渲染它。
Here is some code showing how to capture keyboard input with SDL.
First of all you need to query key input from SDL by calling EventPoll.
You can do that whenever you are ready to process input, or regularly in
a fixed interval and store keys and keyboard status in internal tables.
Here is a link to a document describing methods to render text with OpenGL: http://www.opengl.org/resources/features/fontsurvey/
What you may want to do is to capture keyboard input and render it on the fly using the proper font(s) you have preloaded.