Browse Source

Add option "--quit-after-ms N" to automatically quit application after N ms

Sylvain 1 month ago
parent
commit
128baec810
2 changed files with 35 additions and 0 deletions
  1. 4 0
      include/SDL3/SDL_test_common.h
  2. 31 0
      src/test/SDL_test_common.c

+ 4 - 0
include/SDL3/SDL_test_common.h

@@ -153,6 +153,10 @@ typedef struct
     SDL_Rect confine;
     bool hide_cursor;
 
+    /* Misc. */
+    int quit_after_ms_interval;
+    SDL_TimerID quit_after_ms_timer;
+
     /* Options info */
     SDLTest_ArgumentParser common_argparser;
     SDLTest_ArgumentParser video_argparser;

+ 31 - 0
src/test/SDL_test_common.c

@@ -63,6 +63,7 @@ static const char *video_usage[] = {
     "[--minimize]",
     "[--mouse-focus]",
     "[--noframe]",
+    "[--quit-after-ms N]",
     "[--refresh R]",
     "[--renderer driver]",
     "[--resizable]",
@@ -86,6 +87,16 @@ static const char *audio_usage[] = {
     NULL
 };
 
+// tests can quit after N ms
+static Uint32 SDLCALL quit_after_ms_cb(void *userdata, SDL_TimerID timerID, Uint32 interval)
+{
+    SDL_Event event;
+    event.type = SDL_EVENT_QUIT;
+    event.common.timestamp = 0;
+    SDL_PushEvent(&event);
+    return 0;
+}
+
 static void SDL_snprintfcat(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 {
     size_t length = SDL_strlen(text);
@@ -509,6 +520,17 @@ static int SDLCALL SDLTest_CommonStateParseVideoArguments(void *data, char **arg
         state->window_flags |= SDL_WINDOW_BORDERLESS;
         return 1;
     }
+    if (SDL_strcasecmp(argv[index], "--quit-after-ms") == 0) {
+        ++index;
+        if (!argv[index]) {
+            return -1;
+        }
+        state->quit_after_ms_interval = SDL_atoi(argv[index]);
+        if (state->quit_after_ms_interval <= 0) {
+            return -1;
+        }
+        return 2;
+    }
     if (SDL_strcasecmp(argv[index], "--resizable") == 0) {
         state->window_flags |= SDL_WINDOW_RESIZABLE;
         return 1;
@@ -1525,6 +1547,11 @@ bool SDLTest_CommonInit(SDLTest_CommonState *state)
 
     SDL_InitSubSystem(state->flags);
 
+
+    if (state->quit_after_ms_interval) {
+        state->quit_after_ms_timer = SDL_AddTimer(state->quit_after_ms_interval, quit_after_ms_cb, NULL);
+    }
+
     return true;
 }
 
@@ -2725,6 +2752,10 @@ void SDLTest_CommonQuit(SDLTest_CommonState *state)
             }
             SDL_free(state->windows);
         }
+
+        if (state->quit_after_ms_timer) {
+            SDL_RemoveTimer(state->quit_after_ms_timer);
+        }
     }
     SDL_Quit();
     SDLTest_CommonDestroyState(state);