Browse Source

Migrate DiabloUI/art to SDLSurfaceUniquePtr

pull/1448/head
Gleb Mazovetskiy 5 years ago committed by Anders Jenbo
parent
commit
d966a1380e
  1. 14
      SourceX/DiabloUI/art.cpp
  2. 7
      SourceX/DiabloUI/art.h
  3. 8
      SourceX/DiabloUI/art_draw.cpp
  4. 17
      SourceX/DiabloUI/diabloui.cpp

14
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<BYTE *>(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<BYTE *>(artData), w, h, 8, w, SDL_PIXELFORMAT_INDEX8);
art->surface = ScaleSurfaceToOutput(SDLSurfaceUniquePtr{SDL_CreateRGBSurfaceWithFormatFrom(
const_cast<BYTE *>(artData), w, h, 8, w, SDL_PIXELFORMAT_INDEX8)});
art->logical_width = w;
art->frame_height = h / frames;
ScaleSurfaceToOutput(&art->surface);
}
} // namespace devilution

7
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;
}
};

8
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();
}

17
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;
}

Loading…
Cancel
Save