SDL_PeepEvents()

I’m trying to remove only keyboard events from the event queue. I think I want to use SDL_PeepEvents(), but from the docs I can’t really tell how to use it properly.

int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action, Uint32 mask);

I’m pretty certain that passing SDL_EVENTMASK(SDL_KeyboardEvent) for ‘mask’ will return the keyboard events, but… Specifically, how is ‘numevents’ used. Do I have to know the number of keyboard events I want beforehand?

Any help would be appreciated,
Nick

Also if an admin could delete the other copy of this? I sent it previously using the wrong account.

I’m trying to remove only keyboard events from the event queue.? I
think I want to use SDL_PeepEvents(), but from the docs I can’t really
tell how to use it properly.
?
int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction
action, Uint32 mask);
?
I’m pretty certain that passing SDL_EVENTMASK(SDL_KeyboardEvent) for
’mask’ will return the keyboard events, but…? Specifically, how is
’numevents’ used.? Do I have to know the number of keyboard events I
want beforehand?

numevents is just the size of the array of SDL_Event structures pointed
to by *events. The simplest usage would be

SDL_Event    inEvent;
SDL_PeepEvents(&inEvent, 1, SDL_GETEVENT, 

SDL_EVENTMASK(SDL_KeyboardEvent));
//do something with inEvent. call the previous line again to get the
next event.

However - if your rationale for getting only keyboard events is that
you’re only interested in keyboard events, you still can’t afford to
ignore the others. Other kinds of events will build up, and eventually
fill the queue. The queue can only hold so much, and when it’s full all
new events will just get ignored!

For this reason, You would be best served by a loop that handles all
events. You can chose to ignore all but keyboard events, but you must
remove them all. Like:

SDL_Event inEvent;
while(SDL_PollEvent(&inEvent)){
	if (inEvent.type == SDL_KEYUP || inEvent.type == SDL_KEYDOWN){
		//Nick Campbell's time to shine!
	} else {
		continue;  // ignore
	}
}

This loop will run until there are no events left, at which point
SDL_PollEvent will return 0 and the rest of your code will run. Call
this loop again when you’re ready for your next batch of events. This
works well in most circumstances (especially if you only anticipate
keyboard input)

For a better explanation of SDL’s event system and a solution if you
anticipate massive numbers of events (network programming for example),
check out Bob Pendleton’s article at
http://gameprogrammer.com/fastevents/fastevents1.html because bob is in
fact the man. (not the man that keeps us down. just a great
programmer-man). If you decide to use Bob’s FastEvent method, the code
above can be made awesome by replacing ‘SDL_PollEvent’ with
’FE_PollEvent’

Hope that helps
phip (also not the man that keeps you down)On Dec 12, 2003, at 4:41 PM, Nick Campbell wrote:

Hmm…with the keyboard stuff I was hoping to avoid the single place for
events, ie the big switch statement that is a catch-all. I wanted my
keyboard class to be reponsible for retrieving its own events. I suppose I
could just PumpEvents and get the keyboard state, but I didn’t want to loop
through an array of keys when I could just get the list of keys that were
pressed using events.

Nick> ----- Original Message -----

From: phip@spymac.com (John Philip)
To:
Sent: Friday, December 12, 2003 7:28 PM
Subject: Re: [SDL] SDL_PeepEvents()

On Dec 12, 2003, at 4:41 PM, Nick Campbell wrote:

I’m trying to remove only keyboard events from the event queue. I
think I want to use SDL_PeepEvents(), but from the docs I can’t really
tell how to use it properly.

int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction
action, Uint32 mask);

I’m pretty certain that passing SDL_EVENTMASK(SDL_KeyboardEvent) for
’mask’ will return the keyboard events, but… Specifically, how is
’numevents’ used. Do I have to know the number of keyboard events I
want beforehand?

numevents is just the size of the array of SDL_Event structures pointed
to by *events. The simplest usage would be

SDL_Event inEvent;
SDL_PeepEvents(&inEvent, 1, SDL_GETEVENT,
SDL_EVENTMASK(SDL_KeyboardEvent));
//do something with inEvent. call the previous line again to get the
next event.

However - if your rationale for getting only keyboard events is that
you’re only interested in keyboard events, you still can’t afford to
ignore the others. Other kinds of events will build up, and eventually
fill the queue. The queue can only hold so much, and when it’s full all
new events will just get ignored!

For this reason, You would be best served by a loop that handles all
events. You can chose to ignore all but keyboard events, but you must
remove them all. Like:

SDL_Event inEvent;
while(SDL_PollEvent(&inEvent)){
if (inEvent.type == SDL_KEYUP || inEvent.type == SDL_KEYDOWN){
//Nick Campbell’s time to shine!
} else {
continue; // ignore
}
}

This loop will run until there are no events left, at which point
SDL_PollEvent will return 0 and the rest of your code will run. Call
this loop again when you’re ready for your next batch of events. This
works well in most circumstances (especially if you only anticipate
keyboard input)

For a better explanation of SDL’s event system and a solution if you
anticipate massive numbers of events (network programming for example),
check out Bob Pendleton’s article at
http://gameprogrammer.com/fastevents/fastevents1.html because bob is in
fact the man. (not the man that keeps us down. just a great
programmer-man). If you decide to use Bob’s FastEvent method, the code
above can be made awesome by replacing ‘SDL_PollEvent’ with
’FE_PollEvent’

Hope that helps
phip (also not the man that keeps you down)


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

Then use the first snippet i sent, with a loop, within your keyboard
class:

SDL_Event inEvent;
while(0 < SDL_PeepEvents(&inEvent, 1, SDL_GETEVENT,
SDL_EVENTMASK(SDL_KeyboardEvent)) ){
//do something with inEvent, which is a keyboard event
}

This will execute the loop body for every valid keyboard event
SDL_PeepEvents fetches.
Hoping that is helpful-like
phipOn Dec 13, 2003, at 6:58 AM, Nick Campbell wrote:

Hmm…with the keyboard stuff I was hoping to avoid the single place
for
events, ie the big switch statement that is a catch-all. I wanted my
keyboard class to be reponsible for retrieving its own events. I
suppose I
could just PumpEvents and get the keyboard state, but I didn’t want to
loop
through an array of keys when I could just get the list of keys that
were
pressed using events.

Nick

Nick Campbell wrote:

Hmm…with the keyboard stuff I was hoping to avoid the single place for
events, ie the big switch statement that is a catch-all. I wanted my
keyboard class to be reponsible for retrieving its own events. I suppose I
could just PumpEvents and get the keyboard state, but I didn’t want to loop
through an array of keys when I could just get the list of keys that were
pressed using events.

Nick

btw, I noticed in the source that peep events doesn’t have any
optimization for looking up a specific category of events. It just loops
through all the events. If you split up your event system into multiple
classes each one using peep events then it seems to me you will have a
lot of unnecessary looping going on. It would be more efficient to
centralize this with a poll events and move each event to the proper class.

Tyler Nowicki

Nick Campbell wrote:

Hmm…with the keyboard stuff I was hoping to avoid the single place
for events, ie the big switch statement that is a catch-all. I
wanted my keyboard class to be reponsible for retrieving its own
events.

I think that’s a bad idea, because by retrieving different types of events
in different places in your code you lose the ordering of events. That
doesn’t mean you have to have a big switch statement. You could, for
example, dispatch events to event handlers based on type, or have a chain of
event handlers which either handle a event or pass it along to the next
hanlder in the chain.–
Rainer Deyke - rainerd at eldwood.com - http://eldwood.com