Jump to content
  • Advertisement
Sign in to follow this  
robv

SDL User event flood

This topic is 3900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Not quite sure where I'm going wrong, but I have a simple message pump kind of like this: while (1) { SDL_PollEvent(&event); switch(event.type) { case MOVE_LEFT: printf("left\n"); break; default: break; } } When I send a user event I receive about 20 notifications. ie: "left" I send the user event like this: SDL_Event e; e.type = MOVE_LEFT; // custom defined SDL_PushEvent(&e); Any ideas where I'm going wrong or is there more information you need than that? It looks like the switch finds and handles the user event a few times before it is actually removed from the queue?

Share this post


Link to post
Share on other sites
Advertisement
You need to put the event loop in a conditional. SDL_PollEvent returns 0 when no event is present.

Your code directly translated

while(1)
{
if(SDL_PollEvent(&event))
{
switch(event.type) {
case MOVE_LEFT:
printf("left\n");
break;
default:
break;
}
}
}




Standard SDL event loop (allows background processing). This will be more responsive than the above.

while(1)
{
while(SDL_PollEvent(&event))
{
switch(event.type) {
case MOVE_LEFT:
printf("left\n");
break;
default:
break;
}
}
}




Input driven program (no background processing). Might be used in a game like civilisation, stuff doesn't happen until the user clicks somewhere.

while(SDL_WaitEvent(&event))
{
switch(event.type) {
case MOVE_LEFT:
printf("left\n");
break;
default:
break;
}
}




Which you choose depends on the needs of your application.

Share this post


Link to post
Share on other sites
Awesome, works great. Thanks so much.
Now that I think about it, it's so obvious, but when I was looking at it I just had no idea :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!