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

This commit is contained in:
Sylvain
2025-10-20 09:00:27 +02:00
committed by Sam Lantinga
parent 792bde98c3
commit 128baec810
2 changed files with 35 additions and 0 deletions

View File

@@ -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;

View File

@@ -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);