SDL2: lag with SDL_GetTicks


#1

Hello Everyone,

During the development of my project, i have noticed some “lags” when using SDL_GetTicks().
At a repeated interval of 3 seconds, there is a lag of 40 ms.
Here after is a simple code triggering the observed lag:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sdl.h>

int main(int argc, char argv[])
{
SDL_Window
* myWindow = NULL;
SDL_Event myEvent;
SDL_bool keepOnTesting = SDL_TRUE;
Uint32 lastTick = 0, temp = 0, lagTime = 0;

SDL_Init(SDL_INIT_VIDEO);

myWindow = SDL_CreateWindow(“Lag test”, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 600, 400, SDL_WINDOW_RESIZABLE);

while (keepOnTesting)
{
SDL_PollEvent(&myEvent);

 switch(myEvent.type)
   {
   case SDL_QUIT:
       keepOnTesting = SDL_FALSE;
      break;
  }
  temp = SDL_GetTicks();
  if ((temp - lastTick) > 1)
  {
      SDL_Log("Lag time: %d, Lag interval:%d, Lag value: %d",temp,temp-lagTime,temp-lastTick);
      lagTime = temp;
  }
  lastTick = temp;

}
return EXIT_SUCCESS;
}

The result of SDL_Log can be seen in the uploaded screen capture

Is it possible to get rid of this lag?

lag
Thanks.


#2

Do you mean that the lag is introduced via SDL_GetTicks() or that there is some lag in general?

Compiling your code and running it on my machines (both win10 and linux) does not result in the same. I assume you are on windows - are you sure your AV software/debugger or something else running on your machine isn’t causing this? Window resizes or movement can also be a cause.


#3

Try SDL 2.0.8 to see if the situation improves, if so then try this patch for 2.0.9:

Apparently in 2.0.9 joystick querying can cause a hiccup on some setups every 3 seconds. I was getting 100ms hiccups without this patch.


#4

Hello mkalte and dafu, thank you for having taken the time trying to help me.

To answer the questions of mkalte, I’m working on W10. I’ve closed the AV software but it did not help.
The problem is no related to the SDL_GetTicks() function in itself, but the SDL_GetTicks() function allowed me to highlight the problem.

I’ve modified my code to remove the SDL_PollEvent function (just looping and closing the window after 20s, not monitoring any event in the system) and when I did that, the lag disappeared. I did also observed the same lag problem with the SDL_WaitEvent() function.

I think now that I’m experiencing the bug reported by dafu as following the answer of dafu, i have disabled all joystick events (a total of 7 different events ranging from SDL_JOYAXISMOTION to SDL_JOYDEVICEREMOVED) with the SDL_EventState(event type, SDL_IGNORE) function and it solved the lag problem.

As I don’t plan using the Joystick in my application, I don’t need reverting back to SDL 2.0.8 nor I need applying the given patch.

Your answers helped me to solve my problem. I’m very grateful for that and I thank both of you a lot.