C 的 GLUT 键盘回调问题

发布于 2024-10-26 16:15:53 字数 310 浏览 1 评论 0原文

我目前正在编写一个 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 技术交流群。

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

发布评论

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

评论(2

原来分手还会想你 2024-11-02 16:15:53

游戏中常见的就是键盘阵列。因此,您还可以一次按下多个按钮。

你有一个数组来保存按键的状态(按下时设置 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.

绮烟 2024-11-02 16:15:53

这是我的一个项目中的一些代码:

bool keyDown[256];

...

//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {  
    keyDown[key] = true;
}

void handleKeyUp(unsigned char key, int x, int y){
    keyDown[key] = false;
}

这本质上保留了每个键的状态数组,因此您每次都可以检查它们。那么您就不必依赖频繁传入的回调。

Here is some code from one of my projects:

bool keyDown[256];

...

//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {  
    keyDown[key] = true;
}

void handleKeyUp(unsigned char key, int x, int y){
    keyDown[key] = false;
}

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.

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