Fixed bug 2764 - Timer is not rescheduled with the returned interval
authorSam Lantinga <slouken@libsdl.org>
Sun, 13 Aug 2017 21:48:40 -0700
changeset 1127837f4d29e155d
parent 11277 a2792ce0ec8c
child 11279 b9b53f45bec6
Fixed bug 2764 - Timer is not rescheduled with the returned interval

afwlehmann

Sorry for re-opening, but it turns out that the current interval is indeed not updated. I've just checked the source code of the 2.0.3 release again:

163 if (current->canceled) {
164 interval = 0;
165 } else {
166 interval = current->callback(current->interval, current->param);
167 }
168
169 if (interval > 0) {
170 /* Reschedule this timer */
171 current->interval = interval; // <-- this line is missing
172 current->scheduled = tick + interval;
173 SDL_AddTimerInternal(data, current);
174 } else {

According to the documentation: "The callback function is passed the current timer interval and the user supplied parameter from the SDL_AddTimer() call and returns the next timer interval. If the returned value from the callback is 0, the timer is canceled."

If I understand the text correctly, then the current interval should in fact be updated according to the returned value. Otherwise there would be a discrepancy between the next time for which the timer is actually re-scheduled and the value that's passed to the callback once the timer fires again.

This could be fixed by adding line #171.
src/timer/SDL_timer.c
     1.1 --- a/src/timer/SDL_timer.c	Sun Aug 13 21:18:59 2017 -0700
     1.2 +++ b/src/timer/SDL_timer.c	Sun Aug 13 21:48:40 2017 -0700
     1.3 @@ -168,6 +168,7 @@
     1.4  
     1.5              if (interval > 0) {
     1.6                  /* Reschedule this timer */
     1.7 +                current->interval = interval;
     1.8                  current->scheduled = tick + interval;
     1.9                  SDL_AddTimerInternal(data, current);
    1.10              } else {