Jump to content
  • Advertisement
Sign in to follow this  

SDL event continously fired.

This topic is 3322 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

Hi, I just switched from GLUT to SDL and I'm having trouble handling events. It seems that when an event is fired, it's fired continuously until another event is fired. For instance, when I tap a key on my keyboard, the event gets fired, and I handle it, but after I've handled it, the event gets fired again, and again, and again, etc. etc. It keeps on getting fired until another event gets fired. Here is my loop:
int counter = 0;
Sdl.SDL_Event evt;
while (quitFlag == false)
{ 
    Sdl.SDL_PollEvent(out evt);
    switch (evt.type)
    {
        case Sdl.SDL_KEYDOWN:
            if ((evt.key.keysym.sym == (int)Sdl.SDLK_ESCAPE))
            {
                quitFlag = true;
            }
            foo++;
            System.Diagnostics.Debug.WriteLine("Pressed: " + counter.ToString()); // This gets executed around 100 times on a single key press.
            break;
    }
    Sdl.SDL_PumpEvents(); // I tried commenting this out and it doesn't help.
    try
    {
        MyRenderFunction();
    }
    catch (Exception ex) 
    {
        Log.WriteToLog("***Error: "+ex.ToString());
    }
}
}

I don't know what to do at this point. All my events are doing this, not just a key press. Any idea what's going on? thanks, -alex-

Share this post


Link to post
Share on other sites
Advertisement
SDL_PollEvent() only fills out the event structure you send it if there's an event waiting in the queue. If there is, it fills out the event struct, removes the event from the queue, and returns 1. If there isn't (that is, if the event queue is empty), it returns 0.

Since you're ignoring the return value, you're just processing the same event over and over again (until a new event arives in the queue, that is). That's why it seems like the event is firing repeatedly.

The 'canonical' SDL event loop processes all pending events in the queue every time through the main loop, typically using a 'while' loop. It typically looks something like this:
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(event)) {
// Process event...
}
}
Does that help?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!