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.
--- a/include/SDL_timer.h Sun Oct 20 20:41:30 2013 -0700
+++ b/include/SDL_timer.h Sun Oct 20 20:42:55 2013 -0700
@@ -45,6 +45,17 @@
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);
--- a/src/audio/arts/SDL_artsaudio.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/audio/arts/SDL_artsaudio.c Sun Oct 20 20:42:55 2013 -0700
@@ -220,7 +220,7 @@
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;
}
--- a/src/audio/bsd/SDL_bsdaudio.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/audio/bsd/SDL_bsdaudio.c Sun Oct 20 20:42:55 2013 -0700
@@ -125,9 +125,7 @@
/* 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);
}
--- a/src/audio/esd/SDL_esdaudio.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/audio/esd/SDL_esdaudio.c Sun Oct 20 20:42:55 2013 -0700
@@ -135,8 +135,7 @@
}
/* 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);
}
--- a/src/audio/paudio/SDL_paudio.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/audio/paudio/SDL_paudio.c Sun Oct 20 20:42:55 2013 -0700
@@ -133,9 +133,7 @@
/* 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);
}
--- a/src/events/SDL_events.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/events/SDL_events.c Sun Oct 20 20:42:55 2013 -0700
@@ -443,7 +443,7 @@
/* 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;
}
--- a/src/haptic/windows/SDL_syshaptic.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/haptic/windows/SDL_syshaptic.c Sun Oct 20 20:42:55 2013 -0700
@@ -1567,7 +1567,7 @@
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);
--- a/src/power/uikit/SDL_syspower.m Sun Oct 20 20:41:30 2013 -0700
+++ b/src/power/uikit/SDL_syspower.m Sun Oct 20 20:42:55 2013 -0700
@@ -38,11 +38,7 @@
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];
--- a/src/thread/pthread/SDL_syssem.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/thread/pthread/SDL_syssem.c Sun Oct 20 20:42:55 2013 -0700
@@ -156,7 +156,7 @@
#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);
--- a/src/video/cocoa/SDL_cocoaevents.m Sun Oct 20 20:41:30 2013 -0700
+++ b/src/video/cocoa/SDL_cocoaevents.m Sun Oct 20 20:42:55 2013 -0700
@@ -271,7 +271,7 @@
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;
}
--- a/src/video/x11/SDL_x11events.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/video/x11/SDL_x11events.c Sun Oct 20 20:42:55 2013 -0700
@@ -909,7 +909,7 @@
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 @@
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
--- a/src/video/x11/SDL_x11window.c Sun Oct 20 20:41:30 2013 -0700
+++ b/src/video/x11/SDL_x11window.c Sun Oct 20 20:42:55 2013 -0700
@@ -66,7 +66,7 @@
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;
}
}