[SDL] Re: Question about ignoring mouse events

Bill Kendrick nbs at sonic.net
Wed Nov 16 10:44:56 PST 2005


On Wed, Nov 16, 2005 at 03:10:31PM -0300, Román Gorojovsky wrote:
> ok, but is there an important difference between the following?
> 
>   while (1)
>   {
>       if (SDL_PollEvent(&event))
>       {
>             /* process events */
>             case SDL_QUIT:
>                    exit(0);
>       }
>   }
> 
> vs
> 
>   while (1)
>   {
>       while (SDL_PollEvent(&event))
>       {
>             /* process events */
>       }
>   }

Yes, in the first case, you only handle ONE event per loop.
("Loop" here being the outer "while(1)" loop).

Move the mouse for a few seconds and the queue _quickly_ become backed up
with mouse motion events that you haven't gotten around to handling.


In the latter case, you suck the queue dry whenever you get the chance,
keeping your program up-to-speed with what the user and OS might be doing
(wiggling the mouse, hitting a bunch of keys, firing timers, etc.)


> Should I handle the SDL_QUIT case differently?

Well, really what >I< do is:

  done = 0;  /* FALSE */

  do
  {
    while (SDL_PollEvent(&event))
    {
      if (event.type == SDL_QUIT)
        done = 1;  /* TRUE */
      else if (event.type ...)   /* keypress, mouse motion, timers, etc. */
        ...
    }

    /* Do my once-a-loop stuff (e.g., draw enemies, move enemies, etc.) */

    /* SDL_Delay() can be useful here; see numerous mailinglist threads... */
  }
  while (!done);

  /* Clean up... free memory, etc. etc. */

  SDL_Quit();


You can have events also cause "done" to become 1 (TRUE), such as
the user hitting [Escape] at the main menu, or pressing [Alt]+[F4],
or pulling down a menu in your super-duper SDL-based GUI and selecting
"Exit" and then clicking "Yes" to "But I'm such a cool game! Are you SURE?".

:)

-bill!




More information about the SDL mailing list