[SDL] Re: SDL_PollEvent, Mouse events and Linux
Hemant Muthiyan
hemant_gamer-sdl at yahoo.co.in
Wed Feb 16 04:47:30 PST 2005
Olof Bjarnason <olof.bjarnason <at> gmail.com> writes:
>
> On Wed, 16 Feb 2005 09:21:02 +0000 (UTC), Hemant Muthiyan
> <hemant_gamer-sdl <at> yahoo.co.in> wrote:
> > Hello,
> > Here is a simple program for getting mouse events.
> >
> > #include <iostream>
> > #include "SDL.h"
> >
> > int main(int argc, char **argv)
> > {
> > SDL_Event event;
> > SDL_Surface *screen;
> >
> > /* start SDL video */
> > if(SDL_Init(SDL_INIT_VIDEO) < 0)
> > {
> > printf("SDL_Init: %s\n",SDL_GetError());
> > exit(1);
> > }
> > atexit(SDL_Quit); /* remember to quit SDL */
> >
> > /* open the screen */
> > if(!(screen=SDL_SetVideoMode(800,600,0,0)))
> > {
> > printf("SDL_SetVideoMode: %s\n",SDL_GetError());
> > return 1;
> > }
> >
> > while(SDL_PollEvent(&event) >= 0)
> > {
> > switch(event.type)
> > {
> > case SDL_QUIT:
> > exit(0);
> > break;
> >
> > case SDL_MOUSEBUTTONUP:
> > case SDL_MOUSEBUTTONDOWN:
> > std::cout << "Mouse event" << std::endl;
> > break;
> > }
> > }
> >
> > return 0;
> > }
> >
> > It compiles and runs successfully.
> >
> > My problem is, when i click mouse once, means generating 1 mouse up and 1
mouse
> > down event. But SDL generates more than 1 mouse UP and Down events ie. more
> > than 1 mouse clicks.
> Cut-and-paste from SDL-doc-wiki on topic SDL_PollEvent():
>
> Description
> Polls for currently pending events.
> If event is not NULL, the next event is removed from the queue and
> stored in that area.
Which area?
When the event is removed from queue, then PollEvent must not find the same
event again.
so in case of single click, there will be only two events.
SDL_PollEvent will remove both events SDL_MOUSEBUTTONUP and SDL_MOUSEBUTTONDOWN
one after another.
But this is not the case.
The output i got is
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Mouse event
Where as the output must be
Mouse event
Mouse event
>
> Return Value
> Returns 1 if there are any pending events, or 0 if there are none available.
>
> .. So what you are doing is running the switch() whether or not
> PollEvent() says there are any event waiting or not..
>
> /Olof
>
> >
> > Now if i replace SDL_PollEvent with SDL_WaitEvent, the result is as per
> > expectation, SDL generates only one mouse up and mouse down event.
> >
> > Why the behavious is different in above case?
> >
> > I can not use SDL_WaitEvent, because rest of the game logic waits for the
event.
> >
> > Thx
> > Hemu
> >
> > _______________________________________________
> > SDL mailing list
> > SDL <at> libsdl.org
> > http://www.libsdl.org/mailman/listinfo/sdl
> >
>
More information about the SDL
mailing list