From f2a7328037b872275af561f83668861d8960e03a Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Fri, 3 Apr 2020 23:13:34 -0700 Subject: [PATCH] Backport atexit consolidation --- sc2/src/libs/graphics/sdl/sdl1_common.c | 12 ++++-- sc2/src/libs/graphics/sdl/sdl2_common.c | 12 +++++- sc2/src/libs/graphics/sdl/sdl_common.c | 55 ++++++++----------------- 3 files changed, 38 insertions(+), 41 deletions(-) diff --git a/sc2/src/libs/graphics/sdl/sdl1_common.c b/sc2/src/libs/graphics/sdl/sdl1_common.c index 7791e8ab3..b3de02a30 100644 --- a/sc2/src/libs/graphics/sdl/sdl1_common.c +++ b/sc2/src/libs/graphics/sdl/sdl1_common.c @@ -36,6 +36,8 @@ #if SDL_MAJOR_VERSION == 1 +static void TFB_PreQuit (void); + void TFB_PreInit (void) { @@ -62,6 +64,12 @@ TFB_PreInit (void) } } +static void +TFB_PreQuit (void) +{ + SDL_Quit (); +} + int TFB_ReInitGraphics (int driver, int flags, int width, int height) { @@ -229,9 +237,7 @@ Create_Screen (SDL_Surface *templat, int w, int h) int SDL1_ReInit_Screen (SDL_Surface **screen, SDL_Surface *templat, int w, int h) { - if (*screen) { - SDL_FreeSurface (*screen); - } + UnInit_Screen (screen); *screen = Create_Screen (templat, w, h); return *screen == 0 ? -1 : 0; diff --git a/sc2/src/libs/graphics/sdl/sdl2_common.c b/sc2/src/libs/graphics/sdl/sdl2_common.c index 11e4a9c9b..3eaf7af80 100644 --- a/sc2/src/libs/graphics/sdl/sdl2_common.c +++ b/sc2/src/libs/graphics/sdl/sdl2_common.c @@ -36,6 +36,8 @@ #if SDL_MAJOR_VERSION > 1 +static void TFB_PreQuit (void); + void TFB_PreInit (void) { @@ -62,6 +64,14 @@ TFB_PreInit (void) log_add (log_Fatal, "Could not initialize SDL: %s.", SDL_GetError ()); exit (EXIT_FAILURE); } + + atexit (TFB_PreQuit); +} + +static void +TFB_PreQuit (void) +{ + SDL_Quit (); } int @@ -90,7 +100,7 @@ TFB_ReInitGraphics (int driver, int flags, int width, int height) return result; } -void +bool TFB_SetGamma (float gamma) { log_add (log_Warning, "Custom gamma correction is not available in the SDL2 engine."); diff --git a/sc2/src/libs/graphics/sdl/sdl_common.c b/sc2/src/libs/graphics/sdl/sdl_common.c index bfec9b63e..79f165da9 100644 --- a/sc2/src/libs/graphics/sdl/sdl_common.c +++ b/sc2/src/libs/graphics/sdl/sdl_common.c @@ -50,42 +50,6 @@ TFB_GRAPHICS_BACKEND *graphics_backend = NULL; volatile int QuitPosted = 0; volatile int GameActive = 1; // Track the SDL_ACTIVEEVENT state SDL_APPACTIVE -static void TFB_PreQuit (void); - -void -TFB_PreInit (void) -{ - log_add (log_Info, "Initializing base SDL functionality."); - log_add (log_Info, "Using SDL version %d.%d.%d (compiled with " - "%d.%d.%d)", SDL_Linked_Version ()->major, - SDL_Linked_Version ()->minor, SDL_Linked_Version ()->patch, - SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL); -#if 0 - if (SDL_Linked_Version ()->major != SDL_MAJOR_VERSION || - SDL_Linked_Version ()->minor != SDL_MINOR_VERSION || - SDL_Linked_Version ()->patch != SDL_PATCHLEVEL) { - log_add (log_Warning, "The used SDL library is not the same version " - "as the one used to compile The Ur-Quan Masters with! " - "If you experience any crashes, this would be an excellent " - "suspect."); - } -#endif - - if ((SDL_Init (SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) == -1)) - { - log_add (log_Fatal, "Could not initialize SDL: %s.", SDL_GetError ()); - exit (EXIT_FAILURE); - } - - atexit (TFB_PreQuit); -} - -static void -TFB_PreQuit (void) -{ - SDL_Quit (); -} - int TFB_ReInitGraphics (int driver, int flags, int width, int height) { @@ -137,9 +101,15 @@ TFB_ReInitGraphics (int driver, int flags, int width, int height) int TFB_InitGraphics (int driver, int flags, int width, int height) { - int result; + int i, result; char caption[200]; + /* Null out screen pointers for the first time */ + for (i = 0; i < TFB_GFX_NUMSCREENS; i++) + { + SDL_Screens[i] = NULL; + } + GfxFlags = flags; if (driver == TFB_GFXDRIVER_SDL_OPENGL) @@ -373,3 +343,14 @@ TFB_HasColorKey (SDL_Surface *surface) Uint32 key; return TFB_GetColorKey (surface, &key) == 0; } + +void +UnInit_Screen (SDL_Surface **screen) +{ + if (*screen == NULL) { + return; + } + + SDL_FreeSurface (*screen); + *screen = NULL; +}