简体   繁体   中英

Not recognizing keyboard input

whenever I press a key nothing happens, there are no errors either, rather odd this is.

Here is the init function:

bool Game::init(int resx, int resy, bool fullscreen)
{
    tm = new TextureManager();
    int flags = 0;
    if(fullscreen)
    {
        flags = SDL_WINDOW_FULLSCREEN;
    }

    if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
    {
        g_pWindow = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, resx, resy, flags);

        if(g_pWindow != 0)
        {
            g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);

            if(g_pRenderer != 0)
            {
                running = true;

                tm->load("/Users/WilsonKoder/C++P/projects/Pong/Pong/Images/bg.png", "bg", g_pRenderer);
                tm->load("/Users/WilsonKoder/C++P/projects/Pong/Pong/Images/paddle.png", "player1", g_pRenderer);
                tm->load("/Users/WilsonKoder/C++P/projects/Pong/Pong/Images/paddle.png", "player2", g_pRenderer);
                tm->load("/Users/WilsonKoder/C++P/projects/Pong/Pong/Images/ball.png", "ball", g_pRenderer);

                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    return false;
}

Here is my event handler:

void Game::handleEvents()
{
    SDL_Event event;

    switch (SDL_PollEvent(&event)) {
        case SDL_KEYDOWN:
            p1MoveUp = true;
            break;
        case SDL_KEYUP:
            p1MoveUp = false;
            break;

        default:
            break;

    }
}

Here is my update function:

void Game::update()
{
    if (p1MoveUp)
    {
        std::cout << "asfasfasf" << std::endl; //for debug purposes :)
        p1YPos -= 5;
    }
}

and last but not least, here is main.cpp (excluding includes...)

Game *g_game = 0;

int main(int argc, const char * argv[])
{
    g_game = new Game();
    g_game->init(800, 600, false);

    while(g_game->isRunning())
    {
        g_game->handleEvents();
        g_game->update();
        g_game->render();
    }

    g_game->clean();
    SDL_Quit();

    return 0;
}

You are using the SDL_PollEvent incorrectly. The function does not return the type of event, but rather 1 if there are still items in the event queue, or 0 if the queue is empty.

So you'd want to poll events as long as there are things in the queue and THEN do a switch over the event.type as such:

void Game::handleEvents()
{
    SDL_Event event;

    // Poll events until the queue is empty...
    while(SDL_PollEvent(&event)) {

        // ...then check the event.
        switch (event.type) {
            case SDL_KEYDOWN:
                p1MoveUp = true;
                break;
            case SDL_KEYUP:
                p1MoveUp = false;
                break;

            default:
                break;
        }    
    }
}

EDIT : Note that currently ALL key down and up events of each key on the keyboard would cause the p1MoveUp flag to switch. Within your SDL_KEYDOWN and SDL_KEYUP you normally would also check which key has been pressed and act accordingly. You can do this like this:

if(event.key.keysym.sym == SDLK_UP) ...

This would check if the keycode for the "Up Cursor" has been pressed or not. There is a difference between a key scancode and a keycode , for which I would advise you to check the latter as shown above. You can find all these SDL defined keycodes here (look at the rightmost column): https://wiki.libsdl.org/SDL_Keycode

Please note that the event handler can grow quite rapidly if you are checking for different keys and different key events, so I'd suggest that you implement an extra class or other means of encapsulation once you are comfortable with the event mechanism of SDL.

SDL_PollEvent returns true or false. https://wiki.libsdl.org/SDL_PollEvent

If SDL_PollEvent returns true, then 'event' is set. Then do your switch on event.type instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM