Frage

I'm trying to develop a robust game input system with SDL 2.0.1. I want there to be no input lag at all.

I used to do this with SDL_GetKeyboardState(), but I switched over to using SDL_Event and SDL_PollEvent() to have a uniform interface for Keyboard, Mouse and Joystick input (everything done with events).

This works fine, but if I want a continuous input when the key is held down (e.g. to move a character), there is a slight delay before SDL notices that the key is being repeated.

In SDL 1.2, it was possible to set this delay using a function call. Now it is no longer (as far as I'm aware).

How can I remove this delay? Should I switch back to reading the Keyboard state directly?

For reference, here is how I'm currently getting input...

SDL_Event sdlEvent;
while (running)
{
    SDL_PollEvent(&sdlEvent);
    switch (sdlEvent.type)
    {
    case SDL_QUIT:
        running = false;
        break;
    case SDL_KEYDOWN:
        printf("Key down!\n");
        break;
    default:
        break;
    }
}

The application prints "Key down!", then nothing for a small time (about a second), and then repeatedly prints until the key is released.

So how do I get rid of this delay?

War es hilfreich?

Lösung

Since you're not getting an SDL_KEYUP you can just have a bool that's true until you get a SDL_KEYUP

SDL_Event sdlEvent;
while (running)
{
    bool keyDown = false;
    while ( SDL_PollEvent(&sdlEvent) )
    {
        switch (sdlEvent.type)
        {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_KEYDOWN:
                keyDown = true;
                break;
            case SDL_KEUP:
                keyDown = false;
                break;
            default:
                break;
        }
    }
    if ( keyDown )
        printf("Key down!\n");
}

Of course you'll need something to store all keys, like an array. ( Or even better; use C++ with std::map) Then you can use the SDL_Keycode ( event.key.keysym.sym ) as key.

Andere Tipps

First, you should Poll your event into a loop, else you will just get one event per frame so

SDL_Event sdlEvent;
while (running)
{
    while(SDL_PollEvent(&sdlEvent))
    {
        switch (sdlEvent.type)
        {
        case SDL_QUIT:
            running = false;
            break;
        case SDL_KEYDOWN:
            printf("Key down!\n");
            break;
        default:
            break;
        }
    }
}

And, to handle your Key Delay, should create a struct with the key pressed, and the timestamp when the key down was launch. You can put those struct into a vector and then remove them when key up is catched for this key. And to handle key delay just iterate your key down, and check the difference between the current timestamp and the key down inital timestamp.

As is noted here, the correct procedure to make this work cross-platform is using SDL keystates instead of events:

const Uint8* keystates = SDL_GetKeyboardState(NULL);

if(keystates[SDL_SCANCODE_DOWN])
{
    printf("Key down!\n");    
}

In this way you bypass any OS-based keyboard delays. A full tutorial: http://lazyfoo.net/tutorials/SDL/18_key_states/index.php

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top