Browse Source

Updated SDL_TimerID to use the same type as other IDs in SDL

Sam Lantinga 1 year ago
parent
commit
d6a41f8f31
3 changed files with 8 additions and 12 deletions
  1. 1 1
      include/SDL3/SDL_timer.h
  2. 5 9
      src/timer/SDL_timer.c
  3. 2 2
      test/testautomation_timer.c

+ 1 - 1
include/SDL3/SDL_timer.h

@@ -137,7 +137,7 @@ typedef Uint32 (SDLCALL *SDL_TimerCallback)(Uint32 interval, void *param);
 /**
  * Definition of the timer ID type.
  */
-typedef int SDL_TimerID;
+typedef Uint32 SDL_TimerID;
 
 /**
  * Call a callback function at a future time.

+ 5 - 9
src/timer/SDL_timer.c

@@ -29,7 +29,7 @@
 
 typedef struct SDL_Timer
 {
-    int timerID;
+    SDL_TimerID timerID;
     SDL_TimerCallback callback;
     void *param;
     Uint64 interval;
@@ -40,7 +40,7 @@ typedef struct SDL_Timer
 
 typedef struct SDL_TimerMap
 {
-    int timerID;
+    SDL_TimerID timerID;
     SDL_Timer *timer;
     struct SDL_TimerMap *next;
 } SDL_TimerMap;
@@ -50,7 +50,6 @@ typedef struct
 {
     /* Data used by the main thread */
     SDL_Thread *thread;
-    SDL_AtomicInt nextID;
     SDL_TimerMap *timermap;
     SDL_Mutex *timermap_lock;
 
@@ -227,8 +226,6 @@ int SDL_InitTimers(void)
             SDL_QuitTimers();
             return -1;
         }
-
-        SDL_AtomicSet(&data->nextID, 1);
     }
     return 0;
 }
@@ -300,7 +297,7 @@ SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *para
             return 0;
         }
     }
-    timer->timerID = SDL_AtomicIncRef(&data->nextID);
+    timer->timerID = SDL_GetNextObjectID();
     timer->callback = callback;
     timer->param = param;
     timer->interval = SDL_MS_TO_NS(interval);
@@ -370,7 +367,7 @@ SDL_bool SDL_RemoveTimer(SDL_TimerID id)
 
 typedef struct SDL_TimerMap
 {
-    int timerID;
+    SDL_TimerID timerID;
     int timeoutID;
     Uint32 interval;
     SDL_TimerCallback callback;
@@ -380,7 +377,6 @@ typedef struct SDL_TimerMap
 
 typedef struct
 {
-    int nextID;
     SDL_TimerMap *timermap;
 } SDL_TimerData;
 
@@ -423,7 +419,7 @@ SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *para
     if (!entry) {
         return 0;
     }
-    entry->timerID = ++data->nextID;
+    entry->timerID = SDL_GetNextObjectID();
     entry->callback = callback;
     entry->param = param;
     entry->interval = interval;

+ 2 - 2
test/testautomation_timer.c

@@ -131,7 +131,7 @@ static int timer_addRemoveTimer(void *arg)
     /* Set timer with a long delay */
     id = SDL_AddTimer(10000, timerTestCallback, NULL);
     SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
-    SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
+    SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, id);
 
     /* Remove timer again and check that callback was not called */
     result = SDL_RemoveTimer(id);
@@ -153,7 +153,7 @@ static int timer_addRemoveTimer(void *arg)
     /* Set timer with a short delay */
     id = SDL_AddTimer(10, timerTestCallback, (void *)&param);
     SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
-    SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
+    SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, id);
 
     /* Wait to let timer trigger callback */
     SDL_Delay(100);