Collecting the keypresses without the mouse events?

Hi!

How to collect all the pending keypresses and avoiding to
have to discard all the mouse events? This is needed
in any FPS game (DooM, Quake etc…) where the user uses
both the keyboard and the mouse at the same time.

EG:

while (SDL_PollEvent(&event))
    root_sdl_event_filter(&event);

with

void root_sdl_event_filter(const SDL_Event *event)
{
switch (event->type)
{
case SDL_KEYUP:
case SDL_KEYDOWN:
return(sdl_key_filter(event));
case SDL_MOUSEMOTION:
break;

}

return;

}

The above code makes the while() stuck as long as the user
keeps moving the mouse thus introducing a jerky movement in
the game - That’s one of the reasons why Icculus’ port of Duke
Nukem 3d has a Jerky mouse behavior.

So I tryed to use a

SDL_PumpEvents();
SDL_GetMouseState(…);

before doing a

while (SDL_PollEvent(&event))
root_sdl_event_filter(&event);

because only the mouse position at the time we probe
is actually needed for FPS games. But SDL_GetMouseState
doesn’t seem to remove the move mouse events at all, so
it’s pointless and the while(…) will remain very laggy.

Any idea?

Thanks!

Hello Matt,

Sunday, March 12, 2006, 9:36:47 PM, you wrote:

MK> Hi!

MK> How to collect all the pending keypresses and avoiding to
MK> have to discard all the mouse events? This is needed
MK> in any FPS game (DooM, Quake etc…) where the user uses
MK> both the keyboard and the mouse at the same time.

MK> The above code makes the while() stuck as long as the user
MK> keeps moving the mouse thus introducing a jerky movement in
MK> the game - That’s one of the reasons why Icculus’ port of Duke
MK> Nukem 3d has a Jerky mouse behavior.

MK> So I tryed to use a

MK> SDL_PumpEvents();
MK> SDL_GetMouseState(…);

MK> before doing a

MK> while (SDL_PollEvent(&event))
MK> root_sdl_event_filter(&event);

MK> because only the mouse position at the time we probe
MK> is actually needed for FPS games. But SDL_GetMouseState
MK> doesn’t seem to remove the move mouse events at all, so
MK> it’s pointless and the while(…) will remain very laggy.

I also had this issue. I solved it by telling SDL to ignore mouse
movement events:

SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);

Then I polled for mouse movement per frame with SDL_GetMouseState()

For an FPS you might be best reading key input this way too, since as
multiple keypresses will no doubt be needed. It would be far more
efficient to dump keystate into an array and check buttons in there
than process messages.–
Best regards,
Peter mailto:@Peter_Mulholland

Dear Peter,

I also had this issue. I solved it by telling SDL to ignore mouse
movement events:

SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);

Then I polled for mouse movement per frame with SDL_GetMouseState()
For an FPS you might be best reading key input this way too, since as
multiple keypresses will no doubt be needed. It would be far more
efficient to dump keystate into an array and check buttons in there
than process messages.

Thanks for your answer. It’s working great :slight_smile: That’s exactly the thing to do.
Apparently SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE) doesn’t discard the
events for “SDL_PumpEvents(); SDL_GetMouseState();” but hide them from
"SDL_PollEvent(&event)"

Regarding the keypresses, I like a solution as

while (i–) // to be called on a per frame basis
if (SDL_PollEvent(&event))
root_sdl_event_filter(&event);

limitating the time spent at probing the keyboard, with i==2 or 3 for exemple.
This is because the user may hit keys quickly during the game. Taking the
current keystate may not show all the keys.

Best regards,

mk