[SDL] Re: Question about ignoring mouse events

Román Gorojovsky rgorojovsky at gmail.com
Thu Nov 17 07:36:31 PST 2005


aaaah.  Now I see the difference.  It didn't hurt me because, since my
program isn't a game, I can react slower, i. e. I draw the mandelbrot
set, wait for zoom in/out and redraw, so I don't have moving objects
or anythig else.  And I don't react to mouse movements.

But clearly the while (1) { while (SDL_Poll) } } version is better,
more realiable and flexible.  Thanks for the good advice!!



On 11/16/05, Bill Kendrick <nbs at sonic.net> wrote:
> 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!
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
>


--
Román


More information about the SDL mailing list