SDL_PollEvent() loop is not executing. Are no events being queued?

710 views Asked by At

I'm currently using C with the gcc compiler to mess around with SDL2. I've managed to get an image to load and close automatically after 2 seconds, but now I want to wait until the user presses the X button at the top, or for the user to press the ESC key.

However, after several print statements showing that everything is working BUT my SDL_PollEvent() loop, I'm wondering if any events are being queued at all. When I run my program, my image is displayed on the screen and the program waits for my input. I furiously smash any key on my keyboard for a response and nothing happens. What should happen is a message saying "You pressed something!"

Then I try to click the X at the top of the window but nothing happens. The only way I can exit out of my program is if I press CTRL+C in the terminal.

Here is a piece of my code for the event loop:

/* Handle events on queue */
while(SDL_PollEvent(&e) != 0)
{
    /* This print statement does not execute */
    printf("Handling events!\n");

    /* User quits */
    switch(e.type)
    {
        case SDL_KEYDOWN:
            switch(e.key.keysym.sym)
            {
                case SDLK_ESCAPE:
                    printf("Escape pressed!\n");
                    quit = true;
                    break;

                default: printf("You pressed something!");
            }

        case SDL_QUIT:
            quit = true;
            break;

        default: printf("Print anything!\n");
    }
}
2

There are 2 answers

1
Plaidypus On BEST ANSWER

I appreciate the suggestions so far but the actual reason why nothing was working is because my clean up function was called close(). I ended up changing it to some arbitrary name and that fixed my problem!

0
Albert Vaca Cintora On

Make sure you are calling the SDL_PollEvent loop inside the game loop. SDL_PollEvent won't block waiting for events, so you need to keep calling it every frame until there are some events to process.

Apart from that, your code looks fine.