Sign in to follow this  
Lode

SDL: Slow response to mouse scrollwheel and window close button

Recommended Posts

My programs using SDL respond slowly to the scrollwheel of the mouse and the close button of a window. Both scrollwheel and window close button happen to be checked inside the same function called every frame, so probably I'm doing something wrong. I think the SDL_Delay(5) may have to do with it. But, how can you limit the consumed CPU time like the SDL_Delay(5) does and at the same time get all messages that happened during that delay as well as during the other calculations of the frame? Here's the code of the function that's called every frame and handles that:
bool frame(bool quit_if_esc, bool delay) //delay makes CPU have some free time, use once per frame to avoid 100% usage of a CPU core
{
  if(delay) SDL_Delay(5); //so it consumes less processing power

  int done = 0;
  if(!SDL_PollEvent(&event)) return 1;
  inkeys.readKeys(); // ------> this uses "SDL_GetKeyState"
  if(quit_if_esc && inkeys[SDLK_ESCAPE]) done = 1;
  if(event.type == SDL_QUIT) done = 1;
  
  globalMouseWheelUp = globalMouseWheelDown = false;
  if(event.type == SDL_MOUSEBUTTONDOWN)
  {
    if(event.button.button == SDL_BUTTON_WHEELUP) globalMouseWheelUp = true;
    if(event.button.button == SDL_BUTTON_WHEELDOWN) globalMouseWheelDown = true;
  }
  
  return !done;
}

It's the SDL_QUIT qnd the SDL_MOUSEBUTTONDOWN events that are coming slow. It's really like: you press the close button of the window, and the game keeps running for 3 seconds (still rendering every frame, responding to keyboard and regular mouse buttons, ...), then only quits. Same for the scrollwheel: if you scroll, the action happend a few seconds later, but during those seconds the game runs normally and responds to keyboard and left/right mouse button (which are detected in a different way than the above code).

Share this post


Link to post
Share on other sites
You should clear the event queue by looping over it. Otherwise, if there were 10 events in the queue (programs that use the mouse tend to get lots of events), it will be 10 frames in the future that you process events occurring now. Most SDL event loops are written like this:

bool frame(bool quit_if_esc, bool delay)
{
if(delay) SDL_Delay(5); //so it consumes less processing power

globalMouseWheelUp = globalMouseWheelDown = false;

while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT) return false;


if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_WHEELUP) globalMouseWheelUp = true;
if(event.button.button == SDL_BUTTON_WHEELDOWN) globalMouseWheelDown = true;
}
}

inkeys.readKeys(); // ------> this uses "SDL_GetKeyState"
if(quit_if_esc && inkeys[SDLK_ESCAPE]) return false;

return true;
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this