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

This commit is contained in:
Anonymous Maarten 2023-03-16 15:38:26 +01:00 committed by Anonymous Maarten
parent 6e2851878f
commit 08d5235da0
1 changed files with 26 additions and 16 deletions

View File

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