What you have described here and in previous posts is not the recommended way of working with events. The traditional event loop is not usually the biggest CPU draw for a game, so optimizing it usually provides limited benefits for most developers. (On a 1GHz computer you have over 16 million cycles available in 1/60th of a second. Processing even a thousand events in the queue is still just a drop in the bucket.)
However, I think it is an interesting exercise/experiment. Do you have code that you are willing to share, or some minimal working example of what you have in mind?
SDL_FlushEvents(min, max) is generally used in order to remove a certain type-range of events from the queue. The event type values are somewhat loosely arranged by a category hierarchy. (See SDL/include/SDL_events.h). For instance mouse based events are 0x400 through 0x403. You could remove all mouse related events in the current queue by calling SDL_FlushEvents(SDL_MOUSEMOTION, SDL_MOUSEWHEEL);
SDL_FlushEvent(SDL_MOUSEMOTION)
will actually call SDL_FlushEvents(SDL_MOUSEMOTION, SDL_MOUSEMOTION)
, so while the wiki suggests using SDL_FlushEvent() for single event types, it is one less function call to use SDL_FlushEvents() by default.
It would indeed save a couple dozen CPU cycles per event by removing the ignored events from the queue instead of calling SDL_PollEvent(&event) just to ignore said event. On the other hand, if your program doesn’t use the mouse much, then you are adding a function call to each frame which may increase the CPU load on an otherwise idle program.
It would be easier to help further if we can see your code to work on.
Edit: Here’s how I imagine it might look.
#include <SDL2/SDL.h>
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window * win = SDL_CreateWindow("title", 10, 10, 1000, 800, SDL_WINDOW_SHOWN);
SDL_Renderer * screen = SDL_CreateRenderer(win, -1, 0);
SDL_Rect box = {0,0,32, 32};
bool run = true;
while(run)
{
while(SDL_WaitEvent(NULL) < 0)
{
// There are some cases where the Kernel can timeout SDL_WaitEvent,
// but those will cause it to return as a negative number,
// so we just tell the program to return to a waiting state.
}
SDL_FlushEvents(SDL_MOUSEMOTION, SDL_MOUSEMOTION);
SDL_Event ev;
while(SDL_PollEvent(&ev))
{
switch(ev.type)
{
case SDL_MOUSEBUTTONDOWN:
box.x = ev.button.x;
box.y = ev.button.y;
break;
case SDL_QUIT:
run = false;
break;
}
}
SDL_SetRenderDrawColor(screen, 50, 10, 10, 255);
SDL_RenderClear(screen);
SDL_SetRenderDrawColor(screen, 10, 200, 10, 255);
SDL_RenderFillRect(screen, &box);
SDL_RenderPresent(screen);
}
SDL_Quit();
}
There are some events that the source code describes as “spammy” see this exerpt from the SDL_LogEvent function:
/* sensor/mouse/finger motion are spammy, ignore these if they aren't demanded. */
if ( (SDL_EventLoggingVerbosity < 2) &&
( (event->type == SDL_MOUSEMOTION) ||
(event->type == SDL_FINGERMOTION) ||
(event->type == SDL_CONTROLLERTOUCHPADMOTION) ||
(event->type == SDL_CONTROLLERSENSORUPDATE) ||
(event->type == SDL_SENSORUPDATE) ) ) {
return;
}
Note to SDL3 Devs: It would be nice if SDL_FlushEvents() returned either a confirmation bool that an event was removed or a count of the number of events removed. Maybe something that SDL3 could implement?