From 5d54f8d32210db48a70d5d761c481432fe5c5118 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 4 Aug 2021 20:18:12 +0300 Subject: [PATCH] Introduce SDLWrap::CreateRGBSurface --- Source/DiabloUI/ttf_render_wrapped.cpp | 5 ++--- Source/dx.cpp | 13 ++++++------ Source/utils/display.cpp | 15 +++++++------- Source/utils/sdl_wrap.h | 28 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 Source/utils/sdl_wrap.h diff --git a/Source/DiabloUI/ttf_render_wrapped.cpp b/Source/DiabloUI/ttf_render_wrapped.cpp index cb7b706ed..3f0a5c344 100644 --- a/Source/DiabloUI/ttf_render_wrapped.cpp +++ b/Source/DiabloUI/ttf_render_wrapped.cpp @@ -14,6 +14,7 @@ #include "utils/log.hpp" #include "utils/sdl_compat.h" +#include "utils/sdl_wrap.h" namespace devilution { @@ -107,9 +108,7 @@ SDLSurfaceUniquePtr RenderUTF8_Solid_Wrapped(TTF_Font *font, const char *text, S return SDLSurfaceUniquePtr{ TTF_RenderText_Solid(font, text, fg) }; /* Create the target surface */ - SDLSurfaceUniquePtr textbuf { SDL_CreateRGBSurface(SDL_SWSURFACE, (strLines.size() > 1) ? wrapLength : width, height * strLines.size() + (lineSpace * (strLines.size() - 1)), 8, 0, 0, 0, 0) }; - if (textbuf == nullptr) - return {}; + auto textbuf = SDLWrap::CreateRGBSurface(SDL_SWSURFACE, (strLines.size() > 1) ? wrapLength : width, height * strLines.size() + (lineSpace * (strLines.size() - 1)), 8, 0, 0, 0, 0); /* Fill the palette with the foreground color */ SDL_Palette *palette = textbuf->format->palette; diff --git a/Source/dx.cpp b/Source/dx.cpp index ff2b3eff4..f814d2794 100644 --- a/Source/dx.cpp +++ b/Source/dx.cpp @@ -13,6 +13,7 @@ #include "utils/display.h" #include "utils/log.hpp" #include "utils/sdl_mutex.h" +#include "utils/sdl_wrap.h" #ifdef __3DS__ #include <3ds.h> @@ -279,18 +280,16 @@ void Blit(SDL_Surface *src, SDL_Rect *srcRect, SDL_Rect *dstRect) // If the surface has a color key, we must stretch first and can then call BlitSurface. if (SDL_HasColorKey(src)) { - SDL_Surface *stretched = SDL_CreateRGBSurface(SDL_SWSURFACE, dstRect->w, dstRect->h, src->format->BitsPerPixel, + SDLSurfaceUniquePtr stretched = SDLWrap::CreateRGBSurface(SDL_SWSURFACE, dstRect->w, dstRect->h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->BitsPerPixel, src->format->Amask); - SDL_SetColorKey(stretched, SDL_SRCCOLORKEY, src->format->colorkey); + SDL_SetColorKey(stretched.get(), SDL_SRCCOLORKEY, src->format->colorkey); if (src->format->palette != NULL) - SDL_SetPalette(stretched, SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors); + SDL_SetPalette(stretched.get(), SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors); SDL_Rect stretched_rect = { 0, 0, dstRect->w, dstRect->h }; - if (SDL_SoftStretch(src, srcRect, stretched, &stretched_rect) < 0 - || SDL_BlitSurface(stretched, &stretched_rect, dst, dstRect) < 0) { - SDL_FreeSurface(stretched); + if (SDL_SoftStretch(src, srcRect, stretched.get(), &stretched_rect) < 0 + || SDL_BlitSurface(stretched.get(), &stretched_rect, dst, dstRect) < 0) { ErrSdl(); } - SDL_FreeSurface(stretched); return; } diff --git a/Source/utils/display.cpp b/Source/utils/display.cpp index 436d85dd4..cb421c9cd 100644 --- a/Source/utils/display.cpp +++ b/Source/utils/display.cpp @@ -17,6 +17,7 @@ #include "controls/game_controls.h" #include "options.h" #include "utils/log.hpp" +#include "utils/sdl_wrap.h" #ifdef USE_SDL1 #ifndef SDL1_VIDEO_MODE_BPP @@ -290,22 +291,20 @@ void ScaleOutputRect(SDL_Rect *rect) #ifdef USE_SDL1 namespace { -SDL_Surface *CreateScaledSurface(SDL_Surface *src) +SDLSurfaceUniquePtr CreateScaledSurface(SDL_Surface *src) { SDL_Rect stretched_rect = { 0, 0, static_cast(src->w), static_cast(src->h) }; ScaleOutputRect(&stretched_rect); - SDL_Surface *stretched = SDL_CreateRGBSurface( + SDLSurfaceUniquePtr stretched = SDLWrap::CreateRGBSurface( SDL_SWSURFACE, stretched_rect.w, stretched_rect.h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask); if (SDL_HasColorKey(src)) { - SDL_SetColorKey(stretched, SDL_SRCCOLORKEY, src->format->colorkey); + SDL_SetColorKey(stretched.get(), SDL_SRCCOLORKEY, src->format->colorkey); if (src->format->palette != NULL) - SDL_SetPalette(stretched, SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors); + SDL_SetPalette(stretched.get(), SDL_LOGPAL, src->format->palette->colors, 0, src->format->palette->ncolors); } - if (SDL_SoftStretch((src), NULL, stretched, &stretched_rect) < 0) { - SDL_FreeSurface(stretched); + if (SDL_SoftStretch((src), NULL, stretched.get(), &stretched_rect) < 0) ErrSdl(); - } return stretched; } @@ -316,7 +315,7 @@ SDLSurfaceUniquePtr ScaleSurfaceToOutput(SDLSurfaceUniquePtr surface) { #ifdef USE_SDL1 if (OutputRequiresScaling()) - return SDLSurfaceUniquePtr { CreateScaledSurface(surface.get()) }; + return CreateScaledSurface(surface.get()); #endif return surface; } diff --git a/Source/utils/sdl_wrap.h b/Source/utils/sdl_wrap.h new file mode 100644 index 000000000..8f4ff462b --- /dev/null +++ b/Source/utils/sdl_wrap.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#ifdef USE_SDL1 +#include "utils/sdl2_to_1_2_backports.h" +#else +#include "utils/sdl2_backports.h" +#endif + +#include "appfat.h" +#include "utils/sdl_ptrs.h" + +namespace devilution { + +namespace SDLWrap { + +inline SDLSurfaceUniquePtr CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) +{ + SDLSurfaceUniquePtr ret { SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask) }; + if (ret == nullptr) + ErrSdl(); + + return ret; +} + +} //namespace SDLWrap + +} //namespace devilution