test: use SDL_test to parse arguments of tests

This commit is contained in:
Anonymous Maarten
2025-11-17 13:24:16 +01:00
committed by Anonymous Maarten
parent 14470755b7
commit 3ba9bdfc01
5 changed files with 114 additions and 44 deletions

View File

@@ -13,6 +13,7 @@
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
#include "testutils.h" #include "testutils.h"
@@ -36,6 +37,7 @@ typedef struct
float padding; float padding;
} MSDFShaderUniforms; } MSDFShaderUniforms;
static SDLTest_CommonState *state = NULL;
static SDL_Window *window = NULL; static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL; static SDL_Renderer *renderer = NULL;
static SDL_Texture *font_texture = NULL; static SDL_Texture *font_texture = NULL;
@@ -244,6 +246,14 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
SDL_SetAppMetadata(description, "1.0", "com.example.testgpurender_msdf"); SDL_SetAppMetadata(description, "1.0", "com.example.testgpurender_msdf");
state = SDLTest_CommonCreateState(argv, 0);
if (!state) {
return SDL_APP_FAILURE;
}
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
return SDL_APP_FAILURE;
}
if (!SDL_Init(SDL_INIT_VIDEO)) { if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
@@ -314,5 +324,7 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result)
{ {
/* SDL will clean up the window/renderer for us. */ /* SDL will clean up the window/renderer for us. */
QuitGPURenderState(); QuitGPURenderState();
SDL_Quit();
SDLTest_CommonDestroyState(state);
} }

View File

@@ -12,7 +12,7 @@
/* Simple program: Move N sprites around on the screen as fast as possible */ /* Simple program: Move N sprites around on the screen as fast as possible */
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_test_memory.h> #include <SDL3/SDL_test.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#ifdef SDL_PLATFORM_EMSCRIPTEN #ifdef SDL_PLATFORM_EMSCRIPTEN
@@ -456,18 +456,32 @@ int main(int argc, char *argv[])
{ {
SDL_Window *window = NULL; SDL_Window *window = NULL;
int i, return_code = -1; int i, return_code = -1;
SDLTest_CommonState *state;
SDLTest_TrackAllocations(); state = SDLTest_CommonCreateState(argv, 0);
if (!state) {
goto quit;
}
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ) {
if (SDL_strcmp(argv[1], "--renderer") == 0 && argv[i + 1]) { int consumed = SDLTest_CommonArg(state, i);
++i; SDL_Log("consumed=%d", consumed);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, argv[i]); if (consumed == 0) {
} else { if (SDL_strcmp(argv[i], "--renderer") == 0 && argv[i + 1]) {
SDL_Log("Usage: %s [--renderer driver]", argv[0]); SDL_SetHint(SDL_HINT_RENDER_DRIVER, argv[i + 1]);
consumed = 2;
}
}
if (consumed <= 0) {
static const char *options[] = {
"[--renderer RENDERER]",
NULL,
};
SDLTest_CommonLogUsage(state, argv[0], options);
return_code = 1; return_code = 1;
goto quit; goto quit;
} }
i += consumed;
} }
if (!SDL_CreateWindowAndRenderer("testpalette", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) { if (!SDL_CreateWindowAndRenderer("testpalette", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
@@ -498,6 +512,6 @@ quit:
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
SDLTest_LogAllocations(); SDLTest_CommonDestroyState(state);
return return_code; return return_code;
} }

View File

@@ -11,6 +11,7 @@
*/ */
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
#define SQUARE_SIZE 100.0f #define SQUARE_SIZE 100.0f
@@ -54,9 +55,19 @@ int main(int argc, char *argv[])
SDL_Renderer *renderer = NULL; SDL_Renderer *renderer = NULL;
bool done = false; bool done = false;
SDL_Event event; SDL_Event event;
SDLTest_CommonState *state;
int return_code = 1; int return_code = 1;
state = SDLTest_CommonCreateState(argv, 0);
if (!state) {
return 1;
}
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
goto quit;
}
window = SDL_CreateWindow("SDL Software Renderer Transparent Test", 800, 600, SDL_WINDOW_TRANSPARENT | SDL_WINDOW_RESIZABLE); window = SDL_CreateWindow("SDL Software Renderer Transparent Test", 800, 600, SDL_WINDOW_TRANSPARENT | SDL_WINDOW_RESIZABLE);
if (!window) { if (!window) {
SDL_Log("Couldn't create transparent window: %s", SDL_GetError()); SDL_Log("Couldn't create transparent window: %s", SDL_GetError());
@@ -106,5 +117,6 @@ quit:
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
SDLTest_CommonDestroyState(state);
return return_code; return return_code;
} }

View File

@@ -11,6 +11,7 @@
*/ */
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
static void tryOpenURL(const char *url) static void tryOpenURL(const char *url)
{ {
@@ -25,19 +26,38 @@ static void tryOpenURL(const char *url)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i; int i;
SDLTest_CommonState *state;
state = SDLTest_CommonCreateState(argv, 0);
if (!SDL_Init(SDL_INIT_VIDEO)) { if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("SDL_Init failed: %s", SDL_GetError()); SDL_Log("SDL_Init failed: %s", SDL_GetError());
return 1; return 1;
} }
if (argc > 1) { /* Parse commandline */
for (i = 1; i < argc; i++) { for (i = 1; i < argc;) {
tryOpenURL(argv[i]); int consumed;
consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
if (argv[i][0] != '-') {
tryOpenURL(argv[i]);
consumed = 1;
}
} }
} else { if (consumed <= 0) {
tryOpenURL("https://libsdl.org/"); static const char *options[] = {
"[URL [...]]",
NULL,
};
SDLTest_CommonLogUsage(state, argv[0], options);
return SDL_APP_FAILURE;
}
i += consumed;
} }
SDL_Quit(); SDL_Quit();
SDLTest_CommonDestroyState(state);
return 0; return 0;
} }

View File

@@ -11,6 +11,7 @@
*/ */
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_test.h>
#include <wayland-client.h> #include <wayland-client.h>
#include <xdg-shell-client-protocol.h> #include <xdg-shell-client-protocol.h>
@@ -113,7 +114,7 @@ static int InitSprites(void)
} }
/* Encapsulated in a struct to silence shadow variable warnings */ /* Encapsulated in a struct to silence shadow variable warnings */
static struct _state static struct
{ {
/* These are owned by SDL and must not be destroyed! */ /* These are owned by SDL and must not be destroyed! */
struct wl_display *wl_display; struct wl_display *wl_display;
@@ -124,11 +125,11 @@ static struct _state
struct xdg_wm_base *xdg_wm_base; struct xdg_wm_base *xdg_wm_base;
struct xdg_surface *xdg_surface; struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel; struct xdg_toplevel *xdg_toplevel;
} state; } test_wl_state;
static void xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) static void xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
{ {
xdg_surface_ack_configure(state.xdg_surface, serial); xdg_surface_ack_configure(test_wl_state.xdg_surface, serial);
} }
static const struct xdg_surface_listener xdg_surface_listener = { static const struct xdg_surface_listener xdg_surface_listener = {
@@ -164,7 +165,7 @@ static const struct xdg_toplevel_listener xdg_toplevel_listener = {
static void xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) static void xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial)
{ {
xdg_wm_base_pong(state.xdg_wm_base, serial); xdg_wm_base_pong(test_wl_state.xdg_wm_base, serial);
} }
static const struct xdg_wm_base_listener xdg_wm_base_listener = { static const struct xdg_wm_base_listener xdg_wm_base_listener = {
@@ -174,8 +175,8 @@ static const struct xdg_wm_base_listener xdg_wm_base_listener = {
static void registry_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) static void registry_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version)
{ {
if (SDL_strcmp(interface, xdg_wm_base_interface.name) == 0) { if (SDL_strcmp(interface, xdg_wm_base_interface.name) == 0) {
state.xdg_wm_base = wl_registry_bind(state.wl_registry, name, &xdg_wm_base_interface, 1); test_wl_state.xdg_wm_base = wl_registry_bind(test_wl_state.wl_registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(state.xdg_wm_base, &xdg_wm_base_listener, NULL); xdg_wm_base_add_listener(test_wl_state.xdg_wm_base, &xdg_wm_base_listener, NULL);
} }
} }
@@ -193,9 +194,19 @@ int main(int argc, char **argv)
{ {
int ret = -1; int ret = -1;
SDL_PropertiesID props; SDL_PropertiesID props;
SDLTest_CommonState *state;
state = SDLTest_CommonCreateState(argv, 0);
if (!state) {
return 1;
}
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
goto exit;
}
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
return -1; goto exit;
} }
if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") != 0) { if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") != 0) {
@@ -227,29 +238,29 @@ int main(int argc, char **argv)
} }
/* Get the display object and use it to create a registry object, which will enumerate the xdg_wm_base protocol. */ /* Get the display object and use it to create a registry object, which will enumerate the xdg_wm_base protocol. */
state.wl_display = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL); test_wl_state.wl_display = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL);
state.wl_registry = wl_display_get_registry(state.wl_display); test_wl_state.wl_registry = wl_display_get_registry(test_wl_state.wl_display);
wl_registry_add_listener(state.wl_registry, &wl_registry_listener, NULL); wl_registry_add_listener(test_wl_state.wl_registry, &wl_registry_listener, NULL);
/* Roundtrip to enumerate registry objects. */ /* Roundtrip to enumerate registry objects. */
wl_display_roundtrip(state.wl_display); wl_display_roundtrip(test_wl_state.wl_display);
if (!state.xdg_wm_base) { if (!test_wl_state.xdg_wm_base) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "'xdg_wm_base' protocol not found!"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "'xdg_wm_base' protocol not found!");
goto exit; goto exit;
} }
/* Get the wl_surface object from the SDL_Window, and create a toplevel window with it. */ /* Get the wl_surface object from the SDL_Window, and create a toplevel window with it. */
state.wl_surface = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL); test_wl_state.wl_surface = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL);
/* Create the xdg_surface from the wl_surface. */ /* Create the xdg_surface from the wl_surface. */
state.xdg_surface = xdg_wm_base_get_xdg_surface(state.xdg_wm_base, state.wl_surface); test_wl_state.xdg_surface = xdg_wm_base_get_xdg_surface(test_wl_state.xdg_wm_base, test_wl_state.wl_surface);
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, NULL); xdg_surface_add_listener(test_wl_state.xdg_surface, &xdg_surface_listener, NULL);
/* Create the xdg_toplevel from the xdg_surface. */ /* Create the xdg_toplevel from the xdg_surface. */
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface); test_wl_state.xdg_toplevel = xdg_surface_get_toplevel(test_wl_state.xdg_surface);
xdg_toplevel_add_listener(state.xdg_toplevel, &xdg_toplevel_listener, NULL); xdg_toplevel_add_listener(test_wl_state.xdg_toplevel, &xdg_toplevel_listener, NULL);
xdg_toplevel_set_title(state.xdg_toplevel, SDL_GetWindowTitle(window)); xdg_toplevel_set_title(test_wl_state.xdg_toplevel, SDL_GetWindowTitle(window));
/* Initialize the sprites. */ /* Initialize the sprites. */
if (InitSprites() < 0) { if (InitSprites() < 0) {
@@ -294,21 +305,21 @@ int main(int argc, char **argv)
exit: exit:
/* The display and surface handles obtained from SDL are owned by SDL and must *NOT* be destroyed here! */ /* The display and surface handles obtained from SDL are owned by SDL and must *NOT* be destroyed here! */
if (state.xdg_toplevel) { if (test_wl_state.xdg_toplevel) {
xdg_toplevel_destroy(state.xdg_toplevel); xdg_toplevel_destroy(test_wl_state.xdg_toplevel);
state.xdg_toplevel = NULL; test_wl_state.xdg_toplevel = NULL;
} }
if (state.xdg_surface) { if (test_wl_state.xdg_surface) {
xdg_surface_destroy(state.xdg_surface); xdg_surface_destroy(test_wl_state.xdg_surface);
state.xdg_surface = NULL; test_wl_state.xdg_surface = NULL;
} }
if (state.xdg_wm_base) { if (test_wl_state.xdg_wm_base) {
xdg_wm_base_destroy(state.xdg_wm_base); xdg_wm_base_destroy(test_wl_state.xdg_wm_base);
state.xdg_wm_base = NULL; test_wl_state.xdg_wm_base = NULL;
} }
if (state.wl_registry) { if (test_wl_state.wl_registry) {
wl_registry_destroy(state.wl_registry); wl_registry_destroy(test_wl_state.wl_registry);
state.wl_registry = NULL; test_wl_state.wl_registry = NULL;
} }
/* Destroy the SDL resources */ /* Destroy the SDL resources */
@@ -326,5 +337,6 @@ exit:
} }
SDL_Quit(); SDL_Quit();
SDLTest_CommonDestroyState(state);
return ret; return ret;
} }