From e9063c578f4cd84589fed0b94ef05f75e976a226 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Tue, 24 Aug 2021 02:57:02 +0300 Subject: [PATCH] Introduce SDLWrap::CreateRGBSurfaceWithFormatFrom --- Source/storm/storm_svid.cpp | 22 +++++++++------------- Source/utils/sdl_wrap.h | 9 +++++++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Source/storm/storm_svid.cpp b/Source/storm/storm_svid.cpp index 2296c6608..200a8077a 100644 --- a/Source/storm/storm_svid.cpp +++ b/Source/storm/storm_svid.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #ifndef NOSOUND @@ -22,6 +21,7 @@ #include "utils/display.h" #include "utils/log.hpp" #include "utils/sdl_compat.h" +#include "utils/sdl_wrap.h" #include "utils/stdcompat/optional.hpp" namespace devilution { @@ -40,7 +40,7 @@ bool SVidLoop; smk SVidSMK; SDL_Color SVidPreviousPalette[256]; SDL_Palette *SVidPalette; -SDL_Surface *SVidSurface; +SDLSurfaceUniquePtr SVidSurface; #ifndef DEVILUTIONX_STORM_FILE_WRAPPER_AVAILABLE std::unique_ptr SVidBuffer; @@ -225,22 +225,19 @@ bool SVidPlayBegin(const char *filename, int flags) std::memcpy(SVidPreviousPalette, orig_palette, sizeof(SVidPreviousPalette)); // Copy frame to buffer - SVidSurface = SDL_CreateRGBSurfaceWithFormatFrom( + SVidSurface = SDLWrap::CreateRGBSurfaceWithFormatFrom( (unsigned char *)smk_get_video(SVidSMK), SVidWidth, SVidHeight, 8, SVidWidth, SDL_PIXELFORMAT_INDEX8); - if (SVidSurface == nullptr) { - ErrSdl(); - } SVidPalette = SDL_AllocPalette(256); if (SVidPalette == nullptr) { ErrSdl(); } - if (SDLC_SetSurfaceColors(SVidSurface, SVidPalette) <= -1) { + if (SDLC_SetSurfaceColors(SVidSurface.get(), SVidPalette) <= -1) { ErrSdl(); } @@ -269,7 +266,7 @@ bool SVidPlayContinue() } memcpy(logical_palette, orig_palette, sizeof(logical_palette)); - if (SDLC_SetSurfaceAndPaletteColors(SVidSurface, SVidPalette, colors, 0, 256) <= -1) { + if (SDLC_SetSurfaceAndPaletteColors(SVidSurface.get(), SVidPalette, colors, 0, 256) <= -1) { Log("{}", SDL_GetError()); return false; } @@ -297,7 +294,7 @@ bool SVidPlayContinue() #ifndef USE_SDL1 if (renderer != nullptr) { - if (SDL_BlitSurface(SVidSurface, nullptr, GetOutputSurface(), nullptr) <= -1) { + if (SDL_BlitSurface(SVidSurface.get(), nullptr, GetOutputSurface(), nullptr) <= -1) { Log("{}", SDL_GetError()); return false; } @@ -329,16 +326,16 @@ bool SVidPlayContinue() if (isIndexedOutputFormat || outputSurface->w == static_cast(SVidWidth) || outputSurface->h == static_cast(SVidHeight)) { - if (SDL_BlitSurface(SVidSurface, nullptr, outputSurface, &outputRect) <= -1) { + if (SDL_BlitSurface(SVidSurface.get(), nullptr, outputSurface, &outputRect) <= -1) { ErrSdl(); } } else { // The source surface is always 8-bit, and the output surface is never 8-bit in this branch. // We must convert to the output format before calling SDL_BlitScaled. #ifdef USE_SDL1 - SDLSurfaceUniquePtr converted { SDL_ConvertSurface(SVidSurface, ghMainWnd->format, 0) }; + SDLSurfaceUniquePtr converted { SDL_ConvertSurface(SVidSurface.get(), ghMainWnd->format, 0) }; #else - SDLSurfaceUniquePtr converted { SDL_ConvertSurfaceFormat(SVidSurface, wndFormat, 0) }; + SDLSurfaceUniquePtr converted { SDL_ConvertSurfaceFormat(SVidSurface.get(), wndFormat, 0) }; #endif if (SDL_BlitScaled(converted.get(), nullptr, outputSurface, &outputRect) <= -1) { Log("{}", SDL_GetError()); @@ -376,7 +373,6 @@ void SVidPlayEnd() SDL_FreePalette(SVidPalette); SVidPalette = nullptr; - SDL_FreeSurface(SVidSurface); SVidSurface = nullptr; memcpy(orig_palette, SVidPreviousPalette, sizeof(orig_palette)); diff --git a/Source/utils/sdl_wrap.h b/Source/utils/sdl_wrap.h index de209ed39..7c16861bc 100644 --- a/Source/utils/sdl_wrap.h +++ b/Source/utils/sdl_wrap.h @@ -32,6 +32,15 @@ inline SDLSurfaceUniquePtr CreateRGBSurfaceWithFormat(Uint32 flags, int width, i return ret; } +inline SDLSurfaceUniquePtr CreateRGBSurfaceWithFormatFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 format) +{ + SDLSurfaceUniquePtr ret { SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, depth, pitch, format) }; + if (ret == nullptr) + ErrSdl(); + + return ret; +} + } //namespace SDLWrap } //namespace devilution