From f396e6ec8fa9c8eabfd68025c6ec633026c360be Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Sat, 24 Aug 2019 00:05:28 -0700 Subject: [PATCH] Additional orthogonalization and defensive coding around colorkeys and alpha mods --- sc2/src/libs/graphics/sdl/sdl1_common.c | 26 +++++++++++++++ sc2/src/libs/graphics/sdl/sdl2_common.c | 43 +++++++++++++++++++++++-- sc2/src/libs/graphics/sdl/sdl_common.c | 7 ++++ sc2/src/libs/graphics/sdl/sdl_common.h | 2 ++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/sc2/src/libs/graphics/sdl/sdl1_common.c b/sc2/src/libs/graphics/sdl/sdl1_common.c index 68be53419..651931342 100644 --- a/sc2/src/libs/graphics/sdl/sdl1_common.c +++ b/sc2/src/libs/graphics/sdl/sdl1_common.c @@ -400,6 +400,16 @@ TFB_SetGamma (float gamma) } } +int +TFB_HasSurfaceAlphaMod (SDL_Surface *surface) +{ + if (!surface) + { + return 0; + } + return (surface->flags & SDL_SRCALPHA) ? 1 : 0; +} + int TFB_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha) { @@ -421,12 +431,20 @@ TFB_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha) int TFB_SetSurfaceAlphaMod (SDL_Surface *surface, Uint8 alpha) { + if (!surface) + { + return -1; + } return SDL_SetAlpha (surface, SDL_SRCALPHA, alpha); } int TFB_DisableSurfaceAlphaMod (SDL_Surface *surface) { + if (!surface) + { + return -1; + } return SDL_SetAlpha (surface, 0, 255); } @@ -445,12 +463,20 @@ TFB_GetColorKey (SDL_Surface *surface, Uint32 *key) int TFB_SetColorKey (SDL_Surface *surface, Uint32 key) { + if (!surface) + { + return -1; + } return SDL_SetColorKey (surface, SDL_SRCCOLORKEY, key); } int TFB_DisableColorKey (SDL_Surface *surface) { + if (!surface) + { + return -1; + } return SDL_SetColorKey (surface, 0, 0); } #endif diff --git a/sc2/src/libs/graphics/sdl/sdl2_common.c b/sc2/src/libs/graphics/sdl/sdl2_common.c index d34d01568..272cdc2e9 100644 --- a/sc2/src/libs/graphics/sdl/sdl2_common.c +++ b/sc2/src/libs/graphics/sdl/sdl2_common.c @@ -366,6 +366,21 @@ TFB_SetGamma (float gamma) log_add (log_Warning, "Custom gamma correction is not available in the SDL2 engine."); } +int +TFB_HasSurfaceAlphaMod (SDL_Surface *surface) +{ + SDL_BlendMode blend_mode; + if (!surface) + { + return 0; + } + if (SDL_GetSurfaceBlendMode (surface, &blend_mode) != 0) + { + return 0; + } + return blend_mode == SDL_BLENDMODE_BLEND; +} + int TFB_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha) { @@ -376,7 +391,10 @@ TFB_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha) } if (SDL_GetSurfaceBlendMode (surface, &blend_mode) == 0) { - return SDL_GetSurfaceAlphaMod (surface, alpha); + if (blend_mode == SDL_BLENDMODE_BLEND) + { + return SDL_GetSurfaceAlphaMod (surface, alpha); + } } *alpha = 255; return 0; @@ -385,7 +403,12 @@ TFB_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha) int TFB_SetSurfaceAlphaMod (SDL_Surface *surface, Uint8 alpha) { - int result = SDL_SetSurfaceBlendMode (surface, SDL_BLENDMODE_BLEND); + int result; + if (!surface) + { + return -1; + } + result = SDL_SetSurfaceBlendMode (surface, SDL_BLENDMODE_BLEND); if (result == 0) { result = SDL_SetSurfaceAlphaMod (surface, alpha); @@ -396,6 +419,10 @@ TFB_SetSurfaceAlphaMod (SDL_Surface *surface, Uint8 alpha) int TFB_DisableSurfaceAlphaMod (SDL_Surface *surface) { + if (!surface) + { + return -1; + } SDL_SetSurfaceAlphaMod (surface, 255); return SDL_SetSurfaceBlendMode (surface, SDL_BLENDMODE_NONE); } @@ -403,18 +430,30 @@ TFB_DisableSurfaceAlphaMod (SDL_Surface *surface) int TFB_GetColorKey (SDL_Surface *surface, Uint32 *key) { + if (!surface || !key) + { + return -1; + } return SDL_GetColorKey (surface, key); } int TFB_SetColorKey (SDL_Surface *surface, Uint32 key) { + if (!surface) + { + return -1; + } return SDL_SetColorKey (surface, SDL_TRUE, key); } int TFB_DisableColorKey (SDL_Surface *surface) { + if (!surface) + { + return -1; + } return SDL_SetColorKey (surface, SDL_FALSE, 0); } diff --git a/sc2/src/libs/graphics/sdl/sdl_common.c b/sc2/src/libs/graphics/sdl/sdl_common.c index 05436a2a4..5b6976e29 100644 --- a/sc2/src/libs/graphics/sdl/sdl_common.c +++ b/sc2/src/libs/graphics/sdl/sdl_common.c @@ -243,3 +243,10 @@ TFB_UploadTransitionScreen (void) { graphics_backend->uploadTransitionScreen (); } + +int +TFB_HasColorKey (SDL_Surface *surface) +{ + Uint32 key; + return TFB_GetColorKey (surface, &key) != 0; +} diff --git a/sc2/src/libs/graphics/sdl/sdl_common.h b/sc2/src/libs/graphics/sdl/sdl_common.h index fc9d4968b..f125dbd8f 100644 --- a/sc2/src/libs/graphics/sdl/sdl_common.h +++ b/sc2/src/libs/graphics/sdl/sdl_common.h @@ -45,9 +45,11 @@ extern SDL_Surface *SDL_Screens[TFB_GFX_NUMSCREENS]; extern SDL_Surface *format_conv_surf; SDL_Surface* TFB_DisplayFormatAlpha (SDL_Surface *surface); +int TFB_HasSurfaceAlphaMod (SDL_Surface *surface); int TFB_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha); int TFB_SetSurfaceAlphaMod (SDL_Surface *surface, Uint8 alpha); int TFB_DisableSurfaceAlphaMod (SDL_Surface *surface); +int TFB_HasColorKey (SDL_Surface *surface); int TFB_GetColorKey (SDL_Surface *surface, Uint32 *key); int TFB_SetColorKey (SDL_Surface *surface, Uint32 key); int TFB_DisableColorKey (SDL_Surface *surface);