在没有 SDL 或 GLUT 的情况下检测按键的最佳方法?

发布于 2024-12-09 20:45:45 字数 209 浏览 1 评论 0原文

我最近通过 C++ 使用 OpenGL 和 GLUT 创建了一个小型应用程序。 一切都工作得很好,然后我实现了一个具有帧速率等的适当的游戏循环,这使很多事情变得更好,但它似乎也打破了常规的过剩键盘检测(无论如何,这对于游戏键盘输入来说似乎从来都不是很好) - 所以现在我正在我的应用程序中寻找另一种键盘输入方法。

有人可以建议任何其他(最好是相对简单的)方法来检测应用程序中的关键输入吗?

I've recently created a small application using OpenGL and GLUT via C++.
Everything was working great, and then I implemented a proper game loop with framerate and the like, and this made a lot of things better, however it also seemed to break the regular GLUT keyboard detection (which never seemed great for game keyboard input anyway) - so now I'm looking for another method of keyboard input in my application.

Could anyone suggest any other (preferably relatively easy) methods of detecting key inputs in an application?

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

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

发布评论

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

评论(3

蒗幽 2024-12-16 20:45:45

所以现在我正在我的应用程序中寻找一种合适的键盘输入方法。

不,您正在考虑不再使用 GLUT。

至少在 Windows 上,您获取输入的能力与您的窗口直接相关。如果您没有某种窗口,则您没有输入(控制台本身就是一个窗口)。更重要的是,如果 GLUT 拥有您的窗口,那么 GLUT 决定如何处理该窗口的输入。

因此,如果您使用 GLUT,则必须使用 GLUT 的输入系统。如果 GLUT 的输入系统不足以满足您的需求,那么是时候抛弃 GLUT 寻找更好的选择了。毕竟,GLUT 是用于演示,而不是严肃的应用程序。

因此,您需要替代 GLUT 为您提供的所有功能。您也许可以使用 GLFW,但这可能不足以满足您的游戏需求。因此,SDLSFMLAllegro 5 都是可能的。他们有使用 OpenGL 生成窗口的方法,而且都是跨平台的。它们都拥有比 GLUT 更有效、更灵活的输入系统。

so now I'm looking for a decent method of keyboard input in my application.

No, you're looking at not using GLUT anymore.

On Windows at least, your ability to get input is directly tied to your window. If you don't have a window of some kind, you don't have input (the console is itself a window). And more importantly, if GLUT owns your window, then GLUT decides how the input from that window gets handled.

Therefore, if you're using GLUT, you have to use GLUT's input system. And if GLUT's input system isn't good enough for your needs, then it's time to ditch GLUT for greener pastures. After all, GLUT is intended for demos, not serious applications.

So you need a replacement for all the stuff GLUT does for you. You might be able to get away with GLFW, but that might not be sufficient for your game. As such, SDL, SFML, and Allegro 5 are all possibilities. They have ways of spawning windows that use OpenGL, and they are all cross-platform. All of them has more effective and flexible input systems than GLUT.

謌踐踏愛綪 2024-12-16 20:45:45

没有简单的方法。如果您想要从操作系统输入信息,则需要跟踪 Windows 消息,而如果您使用 GLUT,则不会发生这种情况,因为它会为您创建窗口。 GLUT 的另一个坏处是它并不真正跟踪按下的键,而是跟踪输入的字符,因此按 num1 和 num1+shift 会生成 2 个不同的东西,并且对于每个键盘布局,这些都会不同。如果您想进行正确的键盘处理,请使用 SDL 或更好的 SFML(谷歌搜索 - 简单而快速的多媒体库)。这些允许您跟踪按键。或者,如果您不想为此使用任何库,则需要自己创建和处理窗口,并且除非您为每个平台制作该代码,否则它不会是可移植的。即使您不想要可移植性,使用 win32 编程也是一件痛苦的事情,您肯定不想这样做。

GLUT 根本就不是为游戏而设计的,仅此而已,我的建议是使用 SFML。

更新:
如果您真的想搞乱 win32(Windows 操作系统的情况),假设您知道如何创建和处理窗口,则 Windows 发送到窗口的消息是 WM_KEYDOWN,并且按下的键标识符存储为 wParam。您可以在此处找到所有可能的值虚拟键码

There isn't easy way. You need to track windows messages if you want input from OS and that's not going to happen if you use GLUT because it creates the window for you. Also another bad thing about GLUT is that it doesn't really track pressed keys, but characters entered, so pressing num1 and num1+shift generates 2 different things and for every keyboard layout these will be different. If you want to do proper keyboard handling, use either SDL or better SFML (google for it - simple and fast multimedia library). These allows you to track keypresses. Or if you don't want to use any library for that, you need to create and handle window for yourself and it won't be portable unless you make that code for every platform. Even if you don't want portable, programming with win32 is pain in the ass and you surely don't want to do that.

GLUT is simply not made for games, there's nothing more to it, and my advice is to use SFML.

Update:
In case you really want to mess with win32 (OS Windows case), assuming that you know how to create and handle your window, the message Windows sends to window is WM_KEYDOWN and pressed key identifier is stored as wParam. Here you can find all possible values virtual keycodes.

八巷 2024-12-16 20:45:45

OIS。食人魔人有一些文档

There's OIS. The Ogre people have some documentation.

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