Unable to get Linux SDL_QUIT event to work

(Compiling gcc under Linux) Below, is an attempt to loop until a SDL_QUIT event occurs. However, the program immediately drops out of the while loop without an event. What have I done wrong?

// +-------------------------------------+
// |               main.c                |
// |           poll SDL_QUIT             |
// |   by Jan Zumwalt   ver 2020-08-01   |
// +-------------------------------------+

#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>

_Bool QUIT=0;
SDL_Event pEvent;

int main(int argc, char* argv[])
{
       
   if (SDL_Init(SDL_INIT_EVERYTHING)) // attempt to initialize graphics system
   {
      while (!QUIT) 
      {
         while (SDL_PollEvent(&pEvent)) /* handle events here */
         {   
            if (pEvent.type == SDL_QUIT) 
            {
               QUIT = 1;
            }
         }
          /* do some other stuff here -- draw your app, etc. */
      } 
   }
   printf("Pause for 5 seconds\n");
   SDL_Delay(5000);  // show messages 5sec
   
   SDL_Quit();  // clean up sdl2 resources before exiting
   return 0;    // return success
}

You need to create a window with SDL_CreateWindow() after calling SDL_Init, or there won’t be anything to get events about.

1 Like

According to the documentation you can initialise the events subsystem (SDL_INIT_EVENTS) without initialising the video subsystem (SDL_INIT_VIDEO) so I would have expected SDL_PollEvent() to work properly without a window being created.

He was saying that the while (SDL_PollEvent()) loop was dropping right through…I think? If not (and if the SDL_Init condition didn’t fail), this would be a bug in SDL.

Technically on some platforms you can still get an SDL_QUIT when the user hits CTRL-C, or see an audio device get hotplugged, but most events are only available when a window is present to generate them.

1 Like

The documentation for SDL_Init() says it returns zero on success and negative in case of error. Should be as easy as changing your if (SDL_Init(...)) to if (SDL_Init(..) == 0).