如何防止每个键都被发送到 GLUT 中的 KeyboardUpFunc?

发布于 2024-12-10 07:02:34 字数 993 浏览 1 评论 0原文

我对 OpenGL 相当陌生,为了好玩,我正在编写一个简单的 2D 游戏。然而,我遇到了一个我很难解决的问题。

似乎每当调用我的 KeyboardUpFunc 时,不仅实际出现的键会发送到该函数,而且当前按下的每个键也会发送到该函数。

我使用一个简单的键缓冲区来管理键, keyUp 将键标记​​为 up 并且仅在此函数中调用。 keyDown 在我的keyboardFunc 中被调用。 isDown 返回一个布尔值,表示该键是否被按下。以这段代码为例:

#include <iostream>
...

void keyboardUp(unsigned char key, int x, int y)
{
    keys.keyUp(key);
    if (keys.isDown('s') == false)
    {
        std::cout << "It's resetting s as well!" << std::endl;
    }
    // reset acceleration here, for each key
    if ( (key == 'w') || (key == 's') )
    {
        yStep = 0.1;
    }
    if ( (key == 'a') || (key == 'd') )
    {
        xStep = 0.1;
    }

    std::cout << key << " is now up." << std::endl;
}

如果你运行这段代码,如果你按住 S 和 D,然后松开 D 键,你会注意到 S 也被标记为 up,因为这是 keyUp 被调用的唯一位置。

假设我的 keyBuffer 代码工作正常(确实如此,但如果您希望我发布它,请告诉我......),有什么办法可以解决这个问题吗?如果您按住一个键,然后按下另一个键,应用程序会返回到您刚刚按住原始键时正在执行的操作吗?而不是将两者都标记为已启动?或者这对于 GLUT 来说不可行吗?

I'm fairly new to OpenGL, and I am writing a simple game in 2D, for fun. However, I ran into an issue I am having a hard time wrapping my head around.

It seems that whenever my keyboardUpFunc is called, that not only the key that has actually come up sent to the function, but every single key currently being pressed as well.

I'm using a simple key buffer to manage the keys, keyUp marks the key as up and is only called in this function. keyDown is called in my keyboardFunc. isDown returns a boolean value of whether or not the key is pressed. Take this code for example:

#include <iostream>
...

void keyboardUp(unsigned char key, int x, int y)
{
    keys.keyUp(key);
    if (keys.isDown('s') == false)
    {
        std::cout << "It's resetting s as well!" << std::endl;
    }
    // reset acceleration here, for each key
    if ( (key == 'w') || (key == 's') )
    {
        yStep = 0.1;
    }
    if ( (key == 'a') || (key == 'd') )
    {
        xStep = 0.1;
    }

    std::cout << key << " is now up." << std::endl;
}

If you run this code, if you for example, hold S and D, then release the D key, you will note that S has been marked as up too, since this is the only location keyUp is being called.

Assuming my keyBuffer code is working correctly (and it is, but let me know if you want me to post it...), is there any way to get around this? Where if you were holding a key, and then pressed another key, the application would go back to what you were doing when you were just holding the original key? Instead of marking both as up? Or is this not feasible with GLUT?

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

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

发布评论

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

评论(2

相思碎 2024-12-17 07:02:34

不太清楚出了什么问题..但是你到底在哪里/如何调用这个函数?直接在主游戏循环中,或者您是否在“更新”函数中检查某些条件。我问这个问题是因为您需要在每次运行无限循环时检查输入,并且如果您使用布尔值来确定某个键是否按下,则实际上应该在执行相应操作后重置它。无论如何,只是我的2分钱。

Not very clear what is going wrong.. But where/how exactly are you calling this function ?? Directly in the Main Game loop, or are you checking certain conditions in an 'update' function. I ask because you need to check for input every run of the infinite loop, and if you are using a boolean to determine if a key is down, you should essentially reset it after its corresponding action has been performed. Anyway, just my 2 cents.

忘年祭陌 2024-12-17 07:02:34

我更改了键盘缓冲区的实现,上面描述的内容现在可以工作了。不同之处在于,之前我使用向量来堆积按下的键,而现在,我使用固定大小的布尔值数组。

显然,密钥缓冲区的向量实现在 GLUT 情况下无法正常工作。

I changed my implementation of the keyboard buffer, and what I was describing above now works. The difference is that before I was using a vector to pile on the keys that were being pressed, whereas now, I am using a fixed-size array of boolean values.

Apparently, a vector implementation of a key buffer won't work properly with GLUT.

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