Added a macro SDL_TICKS_PASSED() to correctly compare two 32-bit tick values. Went through the code and used the macro and fixed a couple places that were using incorrect timestamp comparisons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
diff --git a/include/SDL_timer.h b/include/SDL_timer.h
index e065cf4..28ab415 100644
--- a/include/SDL_timer.h
+++ b/include/SDL_timer.h
@@ -45,6 +45,17 @@ extern "C" {
extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
/**
+ * \brief Compare SDL ticks values, and return true if A has passed B
+ *
+ * e.g. if you want to wait 100 ms, you could do this:
+ * Uint32 timeout = SDL_GetTicks() + 100;
+ * while (!SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
+ * ... do work until timeout has elapsed
+ * }
+ */
+#define SDL_TICKS_PASSED(A, B) ((Sint32)((B) - (A)) <= 0)
+
+/**
* \brief Get the current value of the high resolution counter
*/
extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
diff --git a/src/audio/arts/SDL_artsaudio.c b/src/audio/arts/SDL_artsaudio.c
index bd80643..7e835c1 100644
--- a/src/audio/arts/SDL_artsaudio.c
+++ b/src/audio/arts/SDL_artsaudio.c
@@ -220,7 +220,7 @@ static int
ARTS_Suspend(void)
{
const Uint32 abortms = SDL_GetTicks() + 3000; /* give up after 3 secs */
- while ( (!SDL_NAME(arts_suspended)()) && (SDL_GetTicks() < abortms) ) {
+ while ( (!SDL_NAME(arts_suspended)()) && !SDL_TICKS_PASSED(SDL_GetTicks(), abortms) ) {
if ( SDL_NAME(arts_suspend)() ) {
break;
}
diff --git a/src/audio/bsd/SDL_bsdaudio.c b/src/audio/bsd/SDL_bsdaudio.c
index ad51dc3..6694683 100644
--- a/src/audio/bsd/SDL_bsdaudio.c
+++ b/src/audio/bsd/SDL_bsdaudio.c
@@ -125,9 +125,7 @@ BSDAUDIO_WaitDevice(_THIS)
/* Use timer for general audio synchronization */
Sint32 ticks;
- ticks =
- ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) -
- FUDGE_TICKS;
+ ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
if (ticks > 0) {
SDL_Delay(ticks);
}
diff --git a/src/audio/esd/SDL_esdaudio.c b/src/audio/esd/SDL_esdaudio.c
index c630565..8bba640 100644
--- a/src/audio/esd/SDL_esdaudio.c
+++ b/src/audio/esd/SDL_esdaudio.c
@@ -135,8 +135,7 @@ ESD_WaitDevice(_THIS)
}
/* Use timer for general audio synchronization */
- ticks =
- ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
+ ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
if (ticks > 0) {
SDL_Delay(ticks);
}
diff --git a/src/audio/paudio/SDL_paudio.c b/src/audio/paudio/SDL_paudio.c
index 6584ddd..8a248af 100644
--- a/src/audio/paudio/SDL_paudio.c
+++ b/src/audio/paudio/SDL_paudio.c
@@ -133,9 +133,7 @@ PAUDIO_WaitDevice(_THIS)
/* Use timer for general audio synchronization */
Sint32 ticks;
- ticks =
- ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) -
- FUDGE_TICKS;
+ ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
if (ticks > 0) {
SDL_Delay(ticks);
}
diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 9213dae..53ba23f 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -443,7 +443,7 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
/* Polling and no events, just return */
return 0;
}
- if (timeout > 0 && ((int) (SDL_GetTicks() - expiration) >= 0)) {
+ if (timeout > 0 && SDL_TICKS_PASSED(SDL_GetTicks(), expiration)) {
/* Timeout expired and no events */
return 0;
}
diff --git a/src/haptic/windows/SDL_syshaptic.c b/src/haptic/windows/SDL_syshaptic.c
index 68447a9..d7edf0f 100644
--- a/src/haptic/windows/SDL_syshaptic.c
+++ b/src/haptic/windows/SDL_syshaptic.c
@@ -1567,7 +1567,7 @@ SDL_RunXInputHaptic(void *arg)
SDL_LockMutex(hwdata->mutex);
/* If we're currently running and need to stop... */
if (hwdata->stopTicks) {
- if ((hwdata->stopTicks != SDL_HAPTIC_INFINITY) && (hwdata->stopTicks < SDL_GetTicks())) {
+ if ((hwdata->stopTicks != SDL_HAPTIC_INFINITY) && SDL_TIMESTAMP_PASSED(SDL_GetTicks(), hwdata->stopTicks)) {
XINPUT_VIBRATION vibration = { 0, 0 };
hwdata->stopTicks = 0;
XINPUTSETSTATE(hwdata->userid, &vibration);
diff --git a/src/power/uikit/SDL_syspower.m b/src/power/uikit/SDL_syspower.m
index 3364da5..f870ea2 100644
--- a/src/power/uikit/SDL_syspower.m
+++ b/src/power/uikit/SDL_syspower.m
@@ -38,11 +38,7 @@ void
SDL_UIKit_UpdateBatteryMonitoring(void)
{
if (SDL_UIKitLastPowerInfoQuery) {
- const Uint32 prev = SDL_UIKitLastPowerInfoQuery;
- const UInt32 now = SDL_GetTicks();
- const UInt32 ticks = now - prev;
- /* if timer wrapped (now < prev), shut down, too. */
- if ((now < prev) || (ticks >= BATTERY_MONITORING_TIMEOUT)) {
+ if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) {
UIDevice *uidev = [UIDevice currentDevice];
SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
[uidev setBatteryMonitoringEnabled:NO];
diff --git a/src/thread/pthread/SDL_syssem.c b/src/thread/pthread/SDL_syssem.c
index b10f9b0..e1613fc 100644
--- a/src/thread/pthread/SDL_syssem.c
+++ b/src/thread/pthread/SDL_syssem.c
@@ -156,7 +156,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
#else
end = SDL_GetTicks() + timeout;
while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
- if ((Sint32)(SDL_GetTicks() - end) >= 0) {
+ if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
break;
}
SDL_Delay(1);
diff --git a/src/video/cocoa/SDL_cocoaevents.m b/src/video/cocoa/SDL_cocoaevents.m
index 1dc0ca6..e6b292b 100644
--- a/src/video/cocoa/SDL_cocoaevents.m
+++ b/src/video/cocoa/SDL_cocoaevents.m
@@ -271,7 +271,7 @@ Cocoa_PumpEvents(_THIS)
SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
Uint32 now = SDL_GetTicks();
if (!data->screensaver_activity ||
- (int)(now-data->screensaver_activity) >= 30000) {
+ SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
UpdateSystemActivity(UsrActivity);
data->screensaver_activity = now;
}
diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index df5a04e..ba5eca3 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -909,7 +909,7 @@ X11_HandleFocusChanges(_THIS)
SDL_WindowData *data = videodata->windowlist[i];
if (data && data->pending_focus != PENDING_FOCUS_NONE) {
Uint32 now = SDL_GetTicks();
- if ( (int)(data->pending_focus_time-now) <= 0 ) {
+ if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
if ( data->pending_focus == PENDING_FOCUS_IN ) {
X11_DispatchFocusIn(data);
} else {
@@ -963,7 +963,7 @@ X11_PumpEvents(_THIS)
if (_this->suspend_screensaver) {
Uint32 now = SDL_GetTicks();
if (!data->screensaver_activity ||
- (int) (now - data->screensaver_activity) >= 30000) {
+ SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
X11_XResetScreenSaver(data->display);
#if SDL_USE_LIBDBUS
diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c
index 120b803..62a7e73 100644
--- a/src/video/x11/SDL_x11window.c
+++ b/src/video/x11/SDL_x11window.c
@@ -66,7 +66,7 @@ X11_XIfEventTimeout(Display *display, XEvent *event_return, Bool (*predicate)(),
Uint32 start = SDL_GetTicks();
while (!X11_XCheckIfEvent(display, event_return, predicate, arg)) {
- if ((SDL_GetTicks() - start) >= timeoutMS) {
+ if (SDL_TICKS_PASSED(SDL_GetTicks(), start + timeoutMS)) {
return False;
}
}