From d2268856723716e044f229137abe6543168f2787 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Sun, 5 Sep 2021 11:00:18 +0300 Subject: [PATCH] Introduce SDLWrap::AllocPalette --- Source/storm/storm_svid.cpp | 13 ++++--------- Source/utils/sdl_ptrs.h | 14 ++++++++++++++ Source/utils/sdl_wrap.h | 9 +++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Source/storm/storm_svid.cpp b/Source/storm/storm_svid.cpp index b92129682..728f96c04 100644 --- a/Source/storm/storm_svid.cpp +++ b/Source/storm/storm_svid.cpp @@ -39,7 +39,7 @@ double SVidFrameLength; bool SVidLoop; smk SVidSMK; SDL_Color SVidPreviousPalette[256]; -SDL_Palette *SVidPalette; +SDLPaletteUniquePtr SVidPalette; SDLSurfaceUniquePtr SVidSurface; #ifndef DEVILUTIONX_STORM_FILE_WRAPPER_AVAILABLE @@ -229,11 +229,8 @@ bool SVidPlayBegin(const char *filename, int flags) SVidWidth, SDL_PIXELFORMAT_INDEX8); - SVidPalette = SDL_AllocPalette(256); - if (SVidPalette == nullptr) { - ErrSdl(); - } - if (SDLC_SetSurfaceColors(SVidSurface.get(), SVidPalette) <= -1) { + SVidPalette = SDLWrap::AllocPalette(256); + if (SDLC_SetSurfaceColors(SVidSurface.get(), SVidPalette.get()) <= -1) { ErrSdl(); } @@ -262,7 +259,7 @@ bool SVidPlayContinue() } memcpy(logical_palette, orig_palette, sizeof(logical_palette)); - if (SDLC_SetSurfaceAndPaletteColors(SVidSurface.get(), SVidPalette, colors, 0, 256) <= -1) { + if (SDLC_SetSurfaceAndPaletteColors(SVidSurface.get(), SVidPalette.get(), colors, 0, 256) <= -1) { Log("{}", SDL_GetError()); return false; } @@ -366,9 +363,7 @@ void SVidPlayEnd() SVidBuffer = nullptr; #endif - SDL_FreePalette(SVidPalette); SVidPalette = nullptr; - SVidSurface = nullptr; memcpy(orig_palette, SVidPreviousPalette, sizeof(orig_palette)); diff --git a/Source/utils/sdl_ptrs.h b/Source/utils/sdl_ptrs.h index e103385b2..9336399ee 100644 --- a/Source/utils/sdl_ptrs.h +++ b/Source/utils/sdl_ptrs.h @@ -7,6 +7,11 @@ #include #include +#ifdef USE_SDL1 +#include "utils/sdl2_to_1_2_backports.h" +#else +#include "utils/sdl2_backports.h" +#endif namespace devilution { @@ -44,6 +49,15 @@ struct SDLTextureDeleter { using SDLTextureUniquePtr = std::unique_ptr; #endif +struct SDLPaletteDeleter { + void operator()(SDL_Palette *palette) const + { + SDL_FreePalette(palette); + } +}; + +using SDLPaletteUniquePtr = std::unique_ptr; + /** * @brief Deletes the object using `SDL_free`. */ diff --git a/Source/utils/sdl_wrap.h b/Source/utils/sdl_wrap.h index fcf0bcf73..af3005567 100644 --- a/Source/utils/sdl_wrap.h +++ b/Source/utils/sdl_wrap.h @@ -76,6 +76,15 @@ inline SDLTextureUniquePtr CreateTexture(SDL_Renderer *renderer, Uint32 format, } #endif +inline SDLPaletteUniquePtr AllocPalette(int ncolors) +{ + SDLPaletteUniquePtr ret { SDL_AllocPalette(ncolors) }; + if (ret == nullptr) + ErrSdl(); + + return ret; +} + } //namespace SDLWrap } //namespace devilution