SDL: testintersections: check integer argument + no global done + get final tick before SDL shutdown

From 08d5235da0db49d680d67e0d4acde77546a34650 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 16 Mar 2023 15:38:26 +0100
Subject: [PATCH] testintersections: check integer argument + no global done +
 get final tick before SDL shutdown

---
 test/testintersections.c | 42 +++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/test/testintersections.c b/test/testintersections.c
index 516c6a5e5497..41b20ec1694c 100644
--- a/test/testintersections.c
+++ b/test/testintersections.c
@@ -40,7 +40,6 @@ static int current_color = 255;
 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
 
 static float mouse_begin_x = -1.0f, mouse_begin_y = -1.0f;
-static int done;
 
 static void DrawPoints(SDL_Renderer *renderer)
 {
@@ -206,14 +205,15 @@ DrawRectRectIntersections(SDL_Renderer *renderer)
     }
 }
 
-static void loop(void)
+static void loop(void *arg)
 {
     int i;
     SDL_Event event;
+    int *done = (int*)arg;
 
     /* Check for events */
     while (SDL_PollEvent(&event)) {
-        SDLTest_CommonEvent(state, &event, &done);
+        SDLTest_CommonEvent(state, &event, done);
         switch (event.type) {
         case SDL_EVENT_MOUSE_BUTTON_DOWN:
             mouse_begin_x = event.button.x;
@@ -274,7 +274,7 @@ static void loop(void)
         SDL_RenderPresent(renderer);
     }
 #ifdef __EMSCRIPTEN__
-    if (done) {
+    if (*done) {
         emscripten_cancel_main_loop();
     }
 #endif
@@ -285,18 +285,20 @@ int main(int argc, char *argv[])
     int i;
     Uint64 then, now;
     Uint32 frames;
-
-    /* Enable standard application logging */
-    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+    int done;
 
     /* Initialize parameters */
-    num_objects = NUM_OBJECTS;
+    num_objects = -1;  /* -1 means not initialized */
 
     /* Initialize test framework */
     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
     if (state == NULL) {
         return 1;
     }
+
+    /* Enable standard application logging */
+    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
     for (i = 1; i < argc;) {
         int consumed;
 
@@ -328,13 +330,16 @@ int main(int argc, char *argv[])
             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
                 cycle_alpha = SDL_TRUE;
                 consumed = 1;
-            } else if (SDL_isdigit(*argv[i])) {
-                num_objects = SDL_atoi(argv[i]);
-                consumed = 1;
+            } else if (num_objects < 0 && SDL_isdigit(*argv[i])) {
+                char *endptr = NULL;
+                num_objects = (int)SDL_strtol(argv[i], &endptr, 0);
+                if (endptr != argv[i] && *endptr == '\0' && num_objects >= 0) {
+                    consumed = 1;
+                }
             }
         }
         if (consumed < 0) {
-            static const char *options[] = { "[--blend none|blend|add|mod|mul]", "[--cyclecolor]", "[--cyclealpha]", NULL };
+            static const char *options[] = { "[--blend none|blend|add|mod|mul]", "[--cyclecolor]", "[--cyclealpha]", "[count]", NULL };
             SDLTest_CommonLogUsage(state, argv[0], options);
             return 1;
         }
@@ -344,6 +349,10 @@ int main(int argc, char *argv[])
         return 2;
     }
 
+    if (num_objects < 0) {
+        num_objects = NUM_OBJECTS;
+    }
+
     /* Create the windows and initialize the renderers */
     for (i = 0; i < state->num_windows; ++i) {
         SDL_Renderer *renderer = state->renderers[i];
@@ -360,18 +369,19 @@ int main(int argc, char *argv[])
     done = 0;
 
 #ifdef __EMSCRIPTEN__
-    emscripten_set_main_loop(loop, 0, 1);
+    emscripten_set_main_loop_arg(loop, &done, 0, 1);
 #else
     while (!done) {
         ++frames;
-        loop();
+        loop(&done);
     }
 #endif
 
-    SDLTest_CommonQuit(state);
-
     /* Print out some timing information */
     now = SDL_GetTicks();
+
+    SDLTest_CommonQuit(state);
+
     if (now > then) {
         double fps = ((double)frames * 1000) / (now - then);
         SDL_Log("%2.2f frames per second\n", fps);