Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL event continously fired.

Started by grayaii Apr 16, 2009 at 8:55 PM 1 replies 900+ views
Original Post
grayaii
grayaii
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-
Zakwayda
Zakwayda
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?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.