Skip to content

Commit

Permalink
test: added SDLTest_CommonDefaultArgs()
Browse files Browse the repository at this point in the history
This is for test apps that don't need custom command line arguments; it lets
us reduce the boilerplate code a tiny bit.
  • Loading branch information
icculus committed May 19, 2019
1 parent 18d8309 commit 00e5eeb
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 65 deletions.
11 changes: 11 additions & 0 deletions include/SDL_test_common.h
Expand Up @@ -158,6 +158,17 @@ const char *SDLTest_CommonUsage(SDLTest_CommonState * state);
*/
SDL_bool SDLTest_CommonInit(SDLTest_CommonState * state);

/**
* \brief Easy argument handling when test app doesn't need any custom args.
*
* \param state The common state describing the test window to create.
* \param argc argc, as supplied to SDL_main
* \param argv argv, as supplied to SDL_main
*
* \returns False if app should quit, true otherwise.
*/
SDL_bool SDLTest_CommonDefaultArgs(SDLTest_CommonState * state, const int argc, char **argv);

/**
* \brief Common event handler for test windows.
*
Expand Down
15 changes: 15 additions & 0 deletions src/test/SDL_test_common.c
Expand Up @@ -489,6 +489,21 @@ SDLTest_CommonUsage(SDLTest_CommonState * state)
}
}

SDL_bool
SDLTest_CommonDefaultArgs(SDLTest_CommonState *state, const int argc, char **argv)
{
int i = 1;
while (i < argc) {
const int consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
return SDL_FALSE;
}
i += consumed;
}
return SDL_TRUE;
}

static void
SDLTest_PrintRendererFlag(char *text, size_t maxlen, Uint32 flag)
{
Expand Down
15 changes: 2 additions & 13 deletions test/testgesture.c
Expand Up @@ -268,9 +268,6 @@ loop(void)

int main(int argc, char* argv[])
{
int i;

/* !!! FIXME: there should be an SDLTest_CommonDefaultArgs() so apps don't need this. */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
Expand All @@ -281,16 +278,8 @@ int main(int argc, char* argv[])
state->window_h = HEIGHT;
state->skip_renderer = SDL_TRUE;

for (i = 1; i < argc;) {
const int consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
return 1;
}
i += consumed;
}

if (!SDLTest_CommonInit(state)) {
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
SDLTest_CommonQuit(state);
return 1;
}

Expand Down
14 changes: 3 additions & 11 deletions test/testrendercopyex.c
Expand Up @@ -174,18 +174,10 @@ main(int argc, char *argv[])
if (!state) {
return 1;
}
for (i = 1; i < argc;) {
int consumed;

consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
return 1;
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
quit(2);
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
SDLTest_CommonQuit(state);
return 1;
}

drawstates = SDL_stack_alloc(DrawState, state->num_windows);
Expand Down
14 changes: 3 additions & 11 deletions test/testscale.c
Expand Up @@ -164,18 +164,10 @@ main(int argc, char *argv[])
if (!state) {
return 1;
}
for (i = 1; i < argc;) {
int consumed;

consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
return 1;
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
quit(2);
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
SDLTest_CommonQuit(state);
return 1;
}

drawstates = SDL_stack_alloc(DrawState, state->num_windows);
Expand Down
20 changes: 4 additions & 16 deletions test/testvulkan.c
Expand Up @@ -1119,7 +1119,7 @@ static SDL_bool render(void)
int main(int argc, char *argv[])
{
int fsaa, accel;
int i, done;
int done;
SDL_DisplayMode mode;
SDL_Event event;
Uint32 then, now, frames;
Expand All @@ -1138,27 +1138,15 @@ int main(int argc, char *argv[])
{
return 1;
}
for(i = 1; i < argc;)
{
int consumed;

consumed = SDLTest_CommonArg(state, i);
if(consumed < 0)
{
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
quit(1);
}
i += consumed;
}

/* Set Vulkan parameters */
state->window_flags |= SDL_WINDOW_VULKAN;
state->num_windows = 1;
state->skip_renderer = 1;

if(!SDLTest_CommonInit(state))
{
quit(2);
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
SDLTest_CommonQuit(state);
return 1;
}

SDL_GetCurrentDisplayMode(0, &mode);
Expand Down
17 changes: 3 additions & 14 deletions test/testwm2.c
Expand Up @@ -129,21 +129,10 @@ main(int argc, char *argv[])
if (!state) {
return 1;
}
for (i = 1; i < argc;) {
int consumed;

consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
consumed = -1;
}
if (consumed < 0) {
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
quit(1);
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
quit(2);
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
SDLTest_CommonQuit(state);
return 1;
}

SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
Expand Down

0 comments on commit 00e5eeb

Please sign in to comment.