Search code examples
mouseeventsdlsdl-2

Why does this code not clear mouse events from the SDL Events Poll?


I found some of this code from other StackOverflow questions, but basically in my code I run an SDL_Delay and I don't want any mouse events to be registered during this delay, but they still are so I run this code:

SDL_FlushEvents(SDL_USEREVENT, SDL_LASTEVENT);
SDL_PollEvent( &event );
event.type = SDL_USEREVENT;
event.button.x = 0;
event.button.y = 0;

But even after this code after the next SDL_PollEvent(&event), the mouse up events are registered. How do I fix this and stop these mouse events from being registered?


Solution

  • SDL_FlushEvents only clears events that are currently in the SDL queue. Events are put into queue by SDL_PumpEvents call (called internally by SDL_PollEvent). In addition, in clears events in specified type range. As written in question, it clears only events of type "user event" (every type with value higher than SDL_USEREVENT is available as user event type). To clear all events, you want to clear types [0, MAX], or special aliases SDL_FIRSTEVENT (0) and SDL_LASTEVENT (maximum value event type can take).

    To sum it up:

    SDL_PumpEvents();
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
    

    If required, you can check actual values of event types in SDL_event.h file. If you only want to clear mouse events, it would be e.g. SDL_FlushEvents(SDL_MOUSEMOTION, SDL_MOUSEMOTION+0x50).

    The other way to do the same is to process entire event queue until it is empty - e.g.

    while(SDL_PollEvent(NULL)) {}