SDL_PollEvent, Mouse events and Linux

Hello,
Here is a simple program for getting mouse events.

#include
#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.

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

Hello,
Here is a simple program for getting mouse events.

#include
#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.

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…

/OlofOn Wed, 16 Feb 2005 09:21:02 +0000 (UTC), Hemant Muthiyan <hemant_gamer-sdl at yahoo.co.in> wrote:

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

'lo Hemant,On 16/02/2005, Hemant Muthiyan, you wrote:

while(SDL_PollEvent(&event) >= 0)

Remove the “>= 0”.

Regards

Please remove “.ARGL.invalid” from my email when replying.
Incoming HTML mails are automatically deleted.

Olof Bjarnason <olof.bjarnason gmail.com> writes:

Hello,
Here is a simple program for getting mouse events.

#include
#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.> On Wed, 16 Feb 2005 09:21:02 +0000 (UTC), Hemant Muthiyan <hemant_gamer-sdl yahoo.co.in> wrote:

Thx
Hemu


SDL mailing list
SDL libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Olof Bjarnason <olof.bjarnason gmail.com> writes:

Hello,
Here is a simple program for getting mouse events.

#include
#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?

Your actually quoting the SDL-doc-wiki now, not me, but I can see what
is confusing: the word “area” in this context means “the memory
area/structure the pointer points to”.
Anyway, your problem is, as another poster already mentioned, that
your are doing a >=0 test of the result from SDL_PollEvent instead of
a “non+zero”-test (which mean you can leave the comparison out
altogether in the syntax of C).

What is happening in your code is that you are executing the
switch()-statement all the time, even if there are no event waiting on
the queue. Then your event-structure-variable is left unchanged (I
guess) by PollEvent, so you keep doing the same case in the switch(),
which is the case of the last event received which ought to be a
mouse-up event.

/OlofOn Wed, 16 Feb 2005 12:47:30 +0000 (UTC), Hemant Muthiyan <hemant_gamer-sdl at yahoo.co.in> wrote:

On Wed, 16 Feb 2005 09:21:02 +0000 (UTC), Hemant Muthiyan <hemant_gamer-sdl yahoo.co.in> wrote:

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 libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi,

Selon Hemant Muthiyan <hemant_gamer-sdl at yahoo.co.in>:

Olof Bjarnason <olof.bjarnason gmail.com> writes:

If event is not NULL, the next event is removed from the queue and
stored in that area.

Which area?

“event”.

When the event is removed from queue, then PollEvent must not find the same
event again.

It does not, but you are reading event.type even when SDL_PollEvent() tell you
there is no event to handle (ie. even when SDL_PollEvent() returns 0).

Your code should be :
8<--------8<--------8<--------8<--------8<--------
for(;:wink: /* Infinite loop /
{
while(SDL_PollEvent(&event))
{
/
Enter here only when SDL_PollEvent() returns
non-zero.*/
switch(event.type)
{
case SDL_QUIT:
exit(0);
break;

                    case SDL_MOUSEBUTTONUP:
                    case SDL_MOUSEBUTTONDOWN:
                            std::cout << "Mouse event" << std::endl;
                            break;
                    }
            }
    }

8<--------8<--------8<--------8<--------8<--------

Regards,

Xavier

oops…
Thanks a lot Xavier.
Silly mistakes happens some time.

Thanks once again.
Hemu.