SDL 2.0密钥重复和延迟 [英] SDL 2.0 Key repeat and delay

查看:124
本文介绍了SDL 2.0密钥重复和延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类似Pong的游戏中,SDL 2.0键盘输入存在问题.当我命令通过按左箭头向左移动时,它会由SDL_PollEvents()处理,并且如果按下此键一次,则会正确响应.但是,如果我一直按下该键,则在连续移动之前会出现短暂的延迟(只要Windows按键重复延迟).

I'm having a problem with SDL 2.0 keyboard input in pong-like game. When I order to move to the left by pressing left arrow, it is processed by SDL_PollEvents() and responds correctly if the key was pressed once. However, if I keep the key pressed, I get a short delay (as long as Windows key repeat delay) before moving continuously.

以下是函数处理事件:

void Event::PlayerEvent (Player &player)
{
    while (SDL_PollEvent (&mainEvent))
    {
        switch (mainEvent.type)
        {
            case SDL_KEYDOWN :
                switch (mainEvent.key.keysym.sym)
                {
                    case SDLK_ESCAPE :
                        gameRunning = false;
                        break;
                    case SDLK_LEFT :
                        player.moving = player.left;
                        break;
                    case SDLK_RIGHT :
                        player.moving = player.right;
                }
                break;
            case SDL_QUIT :
                gameRunning = false;
        }
    }
}

毕竟,我设法通过致电来解决此问题 程序的开头是SystemParametersInfo(SPI_SETKEYBOARDDELAY,0、0、0),结尾是SystemParametersInfo(SPI_SETKEYBOARDDELAY,1、0、0),以返回到标准键重复延迟.

After all, I managed to fix this issue by calling SystemParametersInfo (SPI_SETKEYBOARDDELAY, 0, 0, 0) at the start of the program and SystemParametersInfo (SPI_SETKEYBOARDDELAY, 1, 0, 0) at the end, to return to standard key repeat delay.

推荐答案

对于游戏的移动,通常不使用事件,而使用状态.

For game movement, you would typically not use events, but rather use states.

尝试在事件循环之外使用SDL_GetKeyboardState():

Try using SDL_GetKeyboardState() outside of the event loop:

const Uint8* keystates = SDL_GetKeyboardState(NULL);

...

if(keystates[SDL_SCANCODE_LEFT])
    player.moving = player.left;
else if(keystates[SDL_SCANCODE_RIGHT])
    player.moving = player.right;

这篇关于SDL 2.0密钥重复和延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆