接受 Glut 的键盘输入

发布于 2024-11-19 18:11:17 字数 892 浏览 9 评论 0原文

我正在使用 Freeglut 来设计一些基本的游戏。但是,我在键盘输入方面遇到了一些问题。

在我之前制作的一些游戏中,我做了这样的事情:(伪代码)

class Key {

bool pressed;
void press() {pressed = true;}
void release() {pressed = false;}
bool isPressed() {return pressed;}

}

Key wkey, skey;

handleKeypress(unsigned char key, int xf, int yf) { //This is the function that glutKeyBoardFunc gets
    switch(key) {
    case 'w':
        wkey.press();
    case 's':
        skey.press();
    }
}

handleKeypress(unsigned char key, int xf, int yf) { //This is the function that glutKeyBoardUpFunc gets
    switch(key) {
    case 'w':
        wkey.release();
    case 's':
        skey.release();
    }
}

当我想检查是否按下了某个键时,我检查了wkey.isPressed()。然而,这引起了问题。例如,Esc 键应该暂停游戏,然后从暂停屏幕按 Esc 键应该将用户带到主菜单。但是,直接按 Esc 会将用户带到主菜单,因为用户在主循环的该周期中没有释放退出键。

为了避免此类问题,使用 Freeglut 进行(和使用)键盘输入的最佳方法是什么?

编辑:顺便说一下,这是C++

I'm using Freeglut to design some basic games. However, I have been having some issues with keyboard input.

In some previous games I made, I had did something like this: (pseudocode)

class Key {

bool pressed;
void press() {pressed = true;}
void release() {pressed = false;}
bool isPressed() {return pressed;}

}

Key wkey, skey;

handleKeypress(unsigned char key, int xf, int yf) { //This is the function that glutKeyBoardFunc gets
    switch(key) {
    case 'w':
        wkey.press();
    case 's':
        skey.press();
    }
}

handleKeypress(unsigned char key, int xf, int yf) { //This is the function that glutKeyBoardUpFunc gets
    switch(key) {
    case 'w':
        wkey.release();
    case 's':
        skey.release();
    }
}

When I wanted to check whether or not a key was pressed, I checked wkey.isPressed(). However, this caused issues. For example, the Esc key was supposed to pause the game, and then pressing Esc from the pause screen was supposed to take the user to the main menu. However, pressing Esc directly took the user to the main menu because the user didn't release the escape key in that tick of the main loop.

To avoid this kind of issue, what is the best way to take (and use) keyboard input with Freeglut?

EDIT: By the way, this is C++

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

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

发布评论

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

评论(1

和我恋爱吧 2024-11-26 18:11:17

为每一帧保存先前的关键状态和当前的关键状态怎么样?这样您就可以测试之前的按键状态是否为 KEY UP,而新的按键状态是否为 KEY DOWN。这表明该键已被按下。您可以将其用于“按键按下”功能 - 只需测试一下先前的按键状态是否为 KEY DOWN 并且新的按键状态是否也是 KEY DOWN。 “按键向上”功能将测试先前的按键状态是否为按键向下并且新的按键状态是否为按键向上。

How about you save a previous key state and a current key state for each frame? This way you can test if the previous key state was KEY UP and the new key state is KEY DOWN. This indicates that the key has been pressed. You can adopt this for a "key pressed" function - just test to see if the previous key state was KEY DOWN and the new key state is also KEY DOWN. A "key up" function would test if the previous key state was KEY DOWN and the new key state is KEY UP.

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