diff --git a/SourceX/DiabloUI/art.cpp b/SourceX/DiabloUI/art.cpp index 62622d78d..97760f52f 100644 --- a/SourceX/DiabloUI/art.cpp +++ b/SourceX/DiabloUI/art.cpp @@ -32,38 +32,34 @@ void LoadArt(const char *pszFile, Art *art, int frames, SDL_Color *pPalette) format = 0; break; } - SDL_Surface *art_surface = SDL_CreateRGBSurfaceWithFormat(SDL_SWSURFACE, width, height, bpp, format); + SDLSurfaceUniquePtr art_surface { SDL_CreateRGBSurfaceWithFormat(SDL_SWSURFACE, width, height, bpp, format) }; if (!SBmpLoadImage(pszFile, pPalette, static_cast(art_surface->pixels), art_surface->pitch * art_surface->format->BytesPerPixel * height, 0, 0, 0)) { SDL_Log("Failed to load image"); - SDL_FreeSurface(art_surface); - art->surface = NULL; return; } - art->surface = art_surface; art->logical_width = art_surface->w; art->frame_height = height / frames; - ScaleSurfaceToOutput(&art->surface); + art->surface = ScaleSurfaceToOutput(std::move(art_surface)); } void LoadMaskedArt(const char *pszFile, Art *art, int frames, int mask) { LoadArt(pszFile, art, frames); if (art->surface != NULL) - SDLC_SetColorKey(art->surface, mask); + SDLC_SetColorKey(art->surface.get(), mask); } void LoadArt(Art *art, const BYTE *artData, int w, int h, int frames) { art->frames = frames; - art->surface = SDL_CreateRGBSurfaceWithFormatFrom( - const_cast(artData), w, h, 8, w, SDL_PIXELFORMAT_INDEX8); + art->surface = ScaleSurfaceToOutput(SDLSurfaceUniquePtr{SDL_CreateRGBSurfaceWithFormatFrom( + const_cast(artData), w, h, 8, w, SDL_PIXELFORMAT_INDEX8)}); art->logical_width = w; art->frame_height = h / frames; - ScaleSurfaceToOutput(&art->surface); } } // namespace devilution diff --git a/SourceX/DiabloUI/art.h b/SourceX/DiabloUI/art.h index bdb44200f..98193b316 100644 --- a/SourceX/DiabloUI/art.h +++ b/SourceX/DiabloUI/art.h @@ -1,11 +1,13 @@ #pragma once #include "all.h" +#include "../sdl_ptrs.h" +#include "../3rdParty/Storm/Source/storm.h" namespace devilution { struct Art { - SDL_Surface *surface; + SDLSurfaceUniquePtr surface; int frames; int logical_width; int frame_height; @@ -32,8 +34,7 @@ struct Art { void Unload() { - SDL_FreeSurface(surface); - surface = NULL; + surface = nullptr; } }; diff --git a/SourceX/DiabloUI/art_draw.cpp b/SourceX/DiabloUI/art_draw.cpp index 80498b21c..ca57f70dd 100644 --- a/SourceX/DiabloUI/art_draw.cpp +++ b/SourceX/DiabloUI/art_draw.cpp @@ -26,12 +26,12 @@ void DrawArt(Sint16 screenX, Sint16 screenY, Art *art, int nFrame, Uint16 srcW, ScaleOutputRect(&dst_rect); if (art->surface->format->BitsPerPixel == 8 && art->palette_version != pal_surface_palette_version) { - if (SDLC_SetSurfaceColors(art->surface, pal_surface->format->palette) <= -1) + if (SDLC_SetSurfaceColors(art->surface.get(), pal_surface->format->palette) <= -1) ErrSdl(); art->palette_version = pal_surface_palette_version; } - if (SDL_BlitSurface(art->surface, &src_rect, DiabloUiSurface(), &dst_rect) < 0) + if (SDL_BlitSurface(art->surface.get(), &src_rect, DiabloUiSurface(), &dst_rect) < 0) ErrSdl(); } @@ -57,12 +57,12 @@ void DrawArt(CelOutputBuffer out, Sint16 screenX, Sint16 screenY, Art *art, int dst_rect.h = src_rect.h; if (art->surface->format->BitsPerPixel == 8 && art->palette_version != pal_surface_palette_version) { - if (SDLC_SetSurfaceColors(art->surface, out.surface->format->palette) <= -1) + if (SDLC_SetSurfaceColors(art->surface.get(), out.surface->format->palette) <= -1) ErrSdl(); art->palette_version = pal_surface_palette_version; } - if (SDL_BlitSurface(art->surface, &src_rect, out.surface, &dst_rect) < 0) + if (SDL_BlitSurface(art->surface.get(), &src_rect, out.surface, &dst_rect) < 0) ErrSdl(); } diff --git a/SourceX/DiabloUI/diabloui.cpp b/SourceX/DiabloUI/diabloui.cpp index 07cef65d3..21a3c1daf 100644 --- a/SourceX/DiabloUI/diabloui.cpp +++ b/SourceX/DiabloUI/diabloui.cpp @@ -14,6 +14,7 @@ #include "DiabloUI/scrollbar.h" #include "DiabloUI/text_draw.h" #include "display.h" +#include "sdl_ptrs.h" #include "stubs.h" #include "utf8.h" @@ -470,24 +471,22 @@ void LoadHeros() } SDL_Rect src_rect = makeRect(0, offset, ArtHero.w(), portraitHeight); SDL_Rect dst_rect = makeRect(0, i * portraitHeight, ArtHero.w(), portraitHeight); - SDL_BlitSurface(ArtHero.surface, &src_rect, heros, &dst_rect); + SDL_BlitSurface(ArtHero.surface.get(), &src_rect, heros, &dst_rect); } - Art ArtPortrait; for (int i = 0; i <= NUM_CLASSES; i++) { + Art portrait; char portraitPath[18]; sprintf(portraitPath, "ui_art\\hero%i.pcx", i); - LoadArt(portraitPath, &ArtPortrait); - if (ArtPortrait.surface == nullptr) + LoadArt(portraitPath, &portrait); + if (portrait.surface == nullptr) continue; - SDL_Rect dst_rect = makeRect(0, i * portraitHeight, ArtPortrait.w(), portraitHeight); - SDL_BlitSurface(ArtPortrait.surface, nullptr, heros, &dst_rect); - ArtPortrait.Unload(); + SDL_Rect dst_rect = makeRect(0, i * portraitHeight, portrait.w(), portraitHeight); + SDL_BlitSurface(portrait.surface.get(), nullptr, heros, &dst_rect); } - ArtHero.Unload(); - ArtHero.surface = heros; + ArtHero.surface = SDLSurfaceUniquePtr { heros }; ArtHero.frame_height = portraitHeight; ArtHero.frames = NUM_CLASSES; }