Don't run SDL_IterateMainCallbacks() if the init call returns an exit code

This fixes a crash in testsprite if you pass invalid command line parameters.
main
Sam Lantinga 2023-11-03 21:34:04 -07:00
parent dad1a84be4
commit ad5264e54f
1 changed files with 28 additions and 28 deletions

View File

@ -38,41 +38,41 @@ static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name
int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
{
int rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
if (rc == 0) {
SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0;
Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0;
while ((rc = SDL_IterateMainCallbacks()) == 0) {
// !!! FIXME: this can be made more complicated if we decide to
// !!! FIXME: optionally hand off callback responsibility to the
// !!! FIXME: video subsystem (for example, if Wayland has a
// !!! FIXME: protocol to drive an animation loop, maybe we hand
// !!! FIXME: off to them here if/when the video subsystem becomes
// !!! FIXME: initialized).
while ((rc = SDL_IterateMainCallbacks()) == 0) {
// !!! FIXME: this can be made more complicated if we decide to
// !!! FIXME: optionally hand off callback responsibility to the
// !!! FIXME: video subsystem (for example, if Wayland has a
// !!! FIXME: protocol to drive an animation loop, maybe we hand
// !!! FIXME: off to them here if/when the video subsystem becomes
// !!! FIXME: initialized).
// !!! FIXME: maybe respect this hint even if there _is_ a window.
// if there's no window, try to run at about 60fps (or whatever rate
// the hint requested). This makes this not eat all the CPU in
// simple things like loopwave. If there's a window, we run as fast
// as possible, which means we'll clamp to vsync in common cases,
// and won't be restrained to vsync if the app is doing a benchmark
// or doesn't want to be, based on how they've set up that window.
if ((callback_rate_increment == 0) || SDL_HasWindows()) {
next_iteration = 0; // just clear the timer and run at the pace the video subsystem allows.
} else {
const Uint64 now = SDL_GetTicksNS();
if (next_iteration > now) { // Running faster than the limit, sleep a little.
SDL_DelayNS(next_iteration - now);
// !!! FIXME: maybe respect this hint even if there _is_ a window.
// if there's no window, try to run at about 60fps (or whatever rate
// the hint requested). This makes this not eat all the CPU in
// simple things like loopwave. If there's a window, we run as fast
// as possible, which means we'll clamp to vsync in common cases,
// and won't be restrained to vsync if the app is doing a benchmark
// or doesn't want to be, based on how they've set up that window.
if ((callback_rate_increment == 0) || SDL_HasWindows()) {
next_iteration = 0; // just clear the timer and run at the pace the video subsystem allows.
} else {
next_iteration = now; // running behind (or just lost the window)...reset the timer.
const Uint64 now = SDL_GetTicksNS();
if (next_iteration > now) { // Running faster than the limit, sleep a little.
SDL_DelayNS(next_iteration - now);
} else {
next_iteration = now; // running behind (or just lost the window)...reset the timer.
}
next_iteration += callback_rate_increment;
}
next_iteration += callback_rate_increment;
}
SDL_DelHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
}
SDL_DelHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
SDL_QuitMainCallbacks();
return (rc < 0) ? 1 : 0;