From cc9ea97e0fa079a17c9179faea6f308852d6adbd Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Tue, 24 Aug 2021 05:03:50 +0300 Subject: [PATCH] Introduce SDLWrap::CreateTexture --- Source/dx.cpp | 12 +++++++----- Source/storm/storm_svid.cpp | 12 ++---------- Source/utils/display.cpp | 5 +---- Source/utils/display.h | 4 +++- Source/utils/sdl_ptrs.h | 11 +++++++++++ Source/utils/sdl_wrap.h | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 20 deletions(-) diff --git a/Source/dx.cpp b/Source/dx.cpp index f814d2794..3819fef76 100644 --- a/Source/dx.cpp +++ b/Source/dx.cpp @@ -23,7 +23,9 @@ namespace devilution { int refreshDelay; SDL_Renderer *renderer; -SDL_Texture *texture; +#ifndef USE_SDL1 +SDLTextureUniquePtr texture; +#endif /** Currently active palette */ SDL_Palette *Palette; @@ -101,7 +103,7 @@ void CreatePrimarySurface() int height = 0; SDL_RenderGetLogicalSize(renderer, &width, &height); Uint32 format; - if (SDL_QueryTexture(texture, &format, nullptr, nullptr, nullptr) < 0) + if (SDL_QueryTexture(texture.get(), &format, nullptr, nullptr, nullptr) < 0) ErrSdl(); renderer_texture_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, SDL_BITSPERPIXEL(format), format); } @@ -207,7 +209,7 @@ void dx_cleanup() SDL_FreePalette(Palette); SDL_FreeSurface(renderer_texture_surface); #ifndef USE_SDL1 - SDL_DestroyTexture(texture); + texture = nullptr; SDL_DestroyRenderer(renderer); #endif SDL_DestroyWindow(ghMainWnd); @@ -315,7 +317,7 @@ void RenderPresent() #ifndef USE_SDL1 if (renderer != nullptr) { - if (SDL_UpdateTexture(texture, nullptr, surface->pixels, surface->pitch) <= -1) { //pitch is 2560 + if (SDL_UpdateTexture(texture.get(), nullptr, surface->pixels, surface->pitch) <= -1) { //pitch is 2560 ErrSdl(); } @@ -327,7 +329,7 @@ void RenderPresent() if (SDL_RenderClear(renderer) <= -1) { ErrSdl(); } - if (SDL_RenderCopy(renderer, texture, nullptr, nullptr) <= -1) { + if (SDL_RenderCopy(renderer, texture.get(), nullptr, nullptr) <= -1) { ErrSdl(); } SDL_RenderPresent(renderer); diff --git a/Source/storm/storm_svid.cpp b/Source/storm/storm_svid.cpp index 200a8077a..b92129682 100644 --- a/Source/storm/storm_svid.cpp +++ b/Source/storm/storm_svid.cpp @@ -210,11 +210,7 @@ bool SVidPlayBegin(const char *filename, int flags) smk_info_video(SVidSMK, &SVidWidth, &SVidHeight, nullptr); #ifndef USE_SDL1 if (renderer != nullptr) { - SDL_DestroyTexture(texture); - texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, SVidWidth, SVidHeight); - if (texture == nullptr) { - ErrSdl(); - } + texture = SDLWrap::CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, SVidWidth, SVidHeight); if (SDL_RenderSetLogicalSize(renderer, SVidWidth, SVidHeight) <= -1) { ErrSdl(); } @@ -378,11 +374,7 @@ void SVidPlayEnd() memcpy(orig_palette, SVidPreviousPalette, sizeof(orig_palette)); #ifndef USE_SDL1 if (renderer != nullptr) { - SDL_DestroyTexture(texture); - texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, gnScreenWidth, gnScreenHeight); - if (texture == nullptr) { - ErrSdl(); - } + texture = SDLWrap::CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, gnScreenWidth, gnScreenHeight); if (renderer != nullptr && SDL_RenderSetLogicalSize(renderer, gnScreenWidth, gnScreenHeight) <= -1) { ErrSdl(); } diff --git a/Source/utils/display.cpp b/Source/utils/display.cpp index cb421c9cd..468062ec8 100644 --- a/Source/utils/display.cpp +++ b/Source/utils/display.cpp @@ -230,10 +230,7 @@ bool SpawnWindow(const char *lpWindowName) ErrSdl(); } - texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, width, height); - if (texture == nullptr) { - ErrSdl(); - } + texture = SDLWrap::CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, width, height); if (sgOptions.Graphics.bIntegerScaling && SDL_RenderSetIntegerScale(renderer, SDL_TRUE) < 0) { ErrSdl(); diff --git a/Source/utils/display.h b/Source/utils/display.h index ea8247e38..b1c2aa348 100644 --- a/Source/utils/display.h +++ b/Source/utils/display.h @@ -18,7 +18,9 @@ namespace devilution { extern int refreshDelay; // Screen refresh rate in nanoseconds extern SDL_Window *window; extern SDL_Renderer *renderer; -extern SDL_Texture *texture; +#ifndef USE_SDL1 +extern SDLTextureUniquePtr texture; +#endif extern SDL_Palette *Palette; extern SDL_Surface *pal_surface; diff --git a/Source/utils/sdl_ptrs.h b/Source/utils/sdl_ptrs.h index 18cc1a53b..e103385b2 100644 --- a/Source/utils/sdl_ptrs.h +++ b/Source/utils/sdl_ptrs.h @@ -33,6 +33,17 @@ struct SDLCursorDeleter { using SDLCursorUniquePtr = std::unique_ptr; #endif +#ifndef USE_SDL1 +struct SDLTextureDeleter { + void operator()(SDL_Texture *texture) const + { + SDL_DestroyTexture(texture); + } +}; + +using SDLTextureUniquePtr = std::unique_ptr; +#endif + /** * @brief Deletes the object using `SDL_free`. */ diff --git a/Source/utils/sdl_wrap.h b/Source/utils/sdl_wrap.h index 7c16861bc..fcf0bcf73 100644 --- a/Source/utils/sdl_wrap.h +++ b/Source/utils/sdl_wrap.h @@ -41,6 +41,41 @@ inline SDLSurfaceUniquePtr CreateRGBSurfaceWithFormatFrom(void *pixels, int widt return ret; } +#ifndef USE_SDL1 +inline SDLSurfaceUniquePtr ConvertSurface(SDL_Surface *src, const SDL_PixelFormat *fmt, Uint32 flags) +#else +inline SDLSurfaceUniquePtr ConvertSurface(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags) +#endif +{ + SDLSurfaceUniquePtr ret { SDL_ConvertSurface(src, fmt, flags) }; + if (ret == nullptr) + ErrSdl(); + + return ret; +} + +#ifndef USE_SDL1 +inline SDLSurfaceUniquePtr ConvertSurfaceFormat(SDL_Surface *src, Uint32 pixel_format, Uint32 flags) +{ + SDLSurfaceUniquePtr ret { SDL_ConvertSurfaceFormat(src, pixel_format, flags) }; + if (ret == nullptr) + ErrSdl(); + + return ret; +} +#endif + +#ifndef USE_SDL1 +inline SDLTextureUniquePtr CreateTexture(SDL_Renderer *renderer, Uint32 format, int access, int w, int h) +{ + SDLTextureUniquePtr ret { SDL_CreateTexture(renderer, format, access, w, h) }; + if (ret == nullptr) + ErrSdl(); + + return ret; +} +#endif + } //namespace SDLWrap } //namespace devilution