C 的 GLUT 键盘回调问题
我目前正在编写一个 Breakout 版本,作为 C 和 OpenGL 的快速学习体验。
我在移动桨时遇到一些问题。我设置了一个键盘回调,这样当按下左箭头时,它会从桨上的 x 值中减去 1,并在按下右箭头时将 x 值加 1。
考虑到这一点,当我按住任一键时,桨的移动速度非常慢。例如,我可以通过增加 x 值更改为 10 的量来更改此设置。当我执行此操作时,桨似乎在屏幕上结结巴巴,因为它一次跳跃 10 个。当然,它现在在屏幕上移动得更快,但看起来并不平滑。
我在 OSX 上使用 GLUT 来打开窗口。
有没有办法加快速度并保持它看起来顺利?
I'm currently coding a version of breakout as a quick learning experience of C and OpenGL.
Im having some issues with moving the paddle. I've set a keyboard callback so that when the left arrow is pressed, it subtracts 1 from the x value on the paddle, and adds 1 to the x value when pressing the right arrow.
With this in mind, the paddle moves incredibly slow when I hold either key. I can change this by increasing the amount the x value is changed to 10 for example. When I do this the paddle seems to stutter across the screen because it's jumping 10 at a time. It does of course move faster along the screen now but doesn't look smooth.
I'm using GLUT for windowing on OSX.
Is there a way of speeding this up and keeping it looking smooth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
游戏中常见的就是键盘阵列。因此,您还可以一次按下多个按钮。
你有一个数组来保存按键的状态(按下时设置 1,释放时设置 0)。您可以通过仅从数组而不是直接从输入获取信息来处理每一帧中的游戏。
A common thing in games is a keyboard array. Therefore you will be also able to press several buttons at a time.
You have an array where you keep state of keys (you put 1 when you get pressed, set 0 when released). And you process game in each frame by taking information just from array, not directly from input.
这是我的一个项目中的一些代码:
这本质上保留了每个键的状态数组,因此您每次都可以检查它们。那么您就不必依赖频繁传入的回调。
Here is some code from one of my projects:
This essentially keeps an array of the states of each key, so you can just check them each time. Then you don't have to depend on the callbacks coming in that frequently.