Browse Source

Remove `Art` and related functions

pull/5221/head
Gleb Mazovetskiy 4 years ago
parent
commit
9b65eca49b
  1. 2
      Source/CMakeLists.txt
  2. 83
      Source/DiabloUI/art.cpp
  3. 57
      Source/DiabloUI/art.h
  4. 60
      Source/DiabloUI/art_draw.cpp
  5. 14
      Source/DiabloUI/art_draw.h
  6. 2
      Source/DiabloUI/credits.cpp
  7. 1
      Source/DiabloUI/diabloui.cpp
  8. 1
      Source/DiabloUI/diabloui.h
  9. 1
      Source/DiabloUI/progress.cpp
  10. 1
      Source/DiabloUI/ui_item.h
  11. 1
      Source/cursor.cpp
  12. 6
      Source/engine.h
  13. 1
      Source/engine/render/text_render.cpp
  14. 58
      Source/utils/pcx.cpp
  15. 2
      Source/utils/pcx.hpp

2
Source/CMakeLists.txt

@ -67,8 +67,6 @@ set(libdevilutionx_SRCS
controls/modifier_hints.cpp
controls/plrctrls.cpp
DiabloUI/art.cpp
DiabloUI/art_draw.cpp
DiabloUI/button.cpp
DiabloUI/credits.cpp
DiabloUI/credits_lines.cpp

83
Source/DiabloUI/art.cpp

@ -1,83 +0,0 @@
#include "DiabloUI/art.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include "engine/assets.hpp"
#include "utils/display.h"
#include "utils/log.hpp"
#include "utils/pcx.hpp"
#include "utils/sdl_compat.h"
#include "utils/sdl_wrap.h"
namespace devilution {
namespace {
Uint32 GetPcxSdlPixelFormat(unsigned bpp)
{
switch (bpp) {
case 8: // NOLINT(readability-magic-numbers)
return SDL_PIXELFORMAT_INDEX8;
case 24: // NOLINT(readability-magic-numbers)
return SDL_PIXELFORMAT_RGB888;
case 32: // NOLINT(readability-magic-numbers)
return SDL_PIXELFORMAT_RGBA8888;
default:
return 0;
}
}
} // namespace
void LoadArt(const char *pszFile, Art *art, int frames, SDL_Color *pPalette, const std::array<uint8_t, 256> *colorMapping)
{
if (art == nullptr || art->surface != nullptr)
return;
art->frames = frames;
int width;
int height;
std::uint8_t bpp;
SDL_RWops *handle = OpenAsset(pszFile);
if (handle == nullptr) {
return;
}
if (!LoadPcxMeta(handle, width, height, bpp)) {
Log("LoadArt(\"{}\"): LoadPcxMeta failed with code {}", pszFile, SDL_GetError());
SDL_RWclose(handle);
return;
}
SDLSurfaceUniquePtr artSurface = SDLWrap::CreateRGBSurfaceWithFormat(SDL_SWSURFACE, width, height, bpp, GetPcxSdlPixelFormat(bpp));
if (!LoadPcxPixelsAndPalette(handle, width, height, bpp, static_cast<uint8_t *>(artSurface->pixels),
artSurface->pitch, pPalette)) {
Log("LoadArt(\"{}\"): LoadPcxPixelsAndPalette failed with code {}", pszFile, SDL_GetError());
SDL_RWclose(handle);
return;
}
SDL_RWclose(handle);
if (colorMapping != nullptr) {
for (int i = 0; i < artSurface->h * artSurface->pitch; i++) {
auto &pixel = static_cast<uint8_t *>(artSurface->pixels)[i];
pixel = (*colorMapping)[pixel];
}
}
art->logical_width = artSurface->w;
art->frame_height = height / frames;
art->surface = ScaleSurfaceToOutput(std::move(artSurface));
}
void LoadMaskedArt(const char *pszFile, Art *art, int frames, int mask, const std::array<uint8_t, 256> *colorMapping)
{
LoadArt(pszFile, art, frames, nullptr, colorMapping);
if (art->surface != nullptr)
SDLC_SetColorKey(art->surface.get(), mask);
}
} // namespace devilution

57
Source/DiabloUI/art.h

@ -1,57 +0,0 @@
#pragma once
#include <array>
#include <cstdint>
#include "utils/sdl_ptrs.h"
namespace devilution {
struct Art {
SDLSurfaceUniquePtr surface;
int frames;
int logical_width;
int frame_height;
unsigned int palette_version;
#ifndef USE_SDL1
SDLTextureUniquePtr texture;
#endif
Art()
{
surface = nullptr;
frames = 1;
logical_width = 0;
frame_height = 0; // logical frame height (before scaling)
palette_version = 0;
#ifndef USE_SDL1
texture = nullptr;
#endif
}
int w() const
{
return logical_width;
}
int h() const
{
return frame_height;
}
void Unload()
{
surface = nullptr;
#ifndef USE_SDL1
texture = nullptr;
#endif
}
};
void LoadArt(const char *pszFile, Art *art, int frames = 1, SDL_Color *pPalette = nullptr, const std::array<uint8_t, 256> *colorMapping = nullptr);
void LoadMaskedArt(const char *pszFile, Art *art, int frames = 1, int mask = 250, const std::array<uint8_t, 256> *colorMapping = nullptr);
} // namespace devilution

60
Source/DiabloUI/art_draw.cpp

@ -1,60 +0,0 @@
#include "DiabloUI/art_draw.h"
#include "DiabloUI/diabloui.h"
#include "engine/palette.h"
#include "utils/display.h"
#include "utils/sdl_compat.h"
namespace devilution {
void UpdatePalette(Art *art, const SDL_Surface *output)
{
if (art->surface->format->BitsPerPixel != 8)
return;
if (art->palette_version == pal_surface_palette_version)
return;
if (output == nullptr || output->format->BitsPerPixel != 8)
output = PalSurface;
if (SDLC_SetSurfaceColors(art->surface.get(), output->format->palette) <= -1)
ErrSdl();
art->palette_version = pal_surface_palette_version;
}
void DrawArt(Point screenPosition, Art *art, int nFrame, Uint16 srcW, Uint16 srcH)
{
if (art->surface == nullptr || screenPosition.y >= gnScreenHeight || screenPosition.x >= gnScreenWidth)
return;
SDL_Rect srcRect = MakeSdlRect(0, nFrame * art->h(), art->w(), art->h());
ScaleOutputRect(&srcRect);
if (srcW != 0 && srcW < srcRect.w)
srcRect.w = srcW;
if (srcH != 0 && srcH < srcRect.h)
srcRect.h = srcH;
if (screenPosition.x + srcRect.w <= 0 || screenPosition.y + srcRect.h <= 0)
return;
SDL_Rect dstRect = MakeSdlRect(screenPosition.x, screenPosition.y, srcRect.w, srcRect.h);
ScaleOutputRect(&dstRect);
UpdatePalette(art);
if (SDL_BlitSurface(art->surface.get(), &srcRect, DiabloUiSurface(), &dstRect) < 0)
ErrSdl();
}
int GetAnimationFrame(int frames, int fps)
{
int frame = (SDL_GetTicks() / fps) % frames;
return frame > frames ? 0 : frame;
}
} // namespace devilution

14
Source/DiabloUI/art_draw.h

@ -1,14 +0,0 @@
#pragma once
#include "DiabloUI/art.h"
#include "engine.h"
namespace devilution {
void UpdatePalette(Art *art, const SDL_Surface *output = nullptr);
void DrawArt(Point screenPosition, Art *art, int nFrame = 0, Uint16 srcW = 0, Uint16 srcH = 0);
int GetAnimationFrame(int frames, int fps = 60);
} // namespace devilution

2
Source/DiabloUI/credits.cpp

@ -2,8 +2,6 @@
#include <memory>
#include <vector>
#include "DiabloUI/art.h"
#include "DiabloUI/art_draw.h"
#include "DiabloUI/credits_lines.h"
#include "DiabloUI/diabloui.h"
#include "DiabloUI/support_lines.h"

1
Source/DiabloUI/diabloui.cpp

@ -3,7 +3,6 @@
#include <algorithm>
#include <string>
#include "DiabloUI/art_draw.h"
#include "DiabloUI/button.h"
#include "DiabloUI/dialogs.h"
#include "DiabloUI/scrollbar.h"

1
Source/DiabloUI/diabloui.h

@ -5,7 +5,6 @@
#include <cstddef>
#include <cstdint>
#include "DiabloUI/art.h"
#include "DiabloUI/ui_item.h"
#include "engine/clx_sprite.hpp"
#include "player.h"

1
Source/DiabloUI/progress.cpp

@ -1,6 +1,5 @@
#include <SDL.h>
#include "DiabloUI/art_draw.h"
#include "DiabloUI/button.h"
#include "DiabloUI/diabloui.h"
#include "control.h"

1
Source/DiabloUI/ui_item.h

@ -5,7 +5,6 @@
#include <string>
#include <vector>
#include "DiabloUI/art.h"
#include "DiabloUI/ui_flags.hpp"
#include "engine/clx_sprite.hpp"
#include "engine/render/text_render.hpp"

1
Source/cursor.cpp

@ -7,7 +7,6 @@
#include <fmt/format.h>
#include "DiabloUI/art.h"
#include "DiabloUI/diabloui.h"
#include "control.h"
#include "controls/plrctrls.h"

6
Source/engine.h

@ -119,4 +119,10 @@ Direction GetDirection(Point start, Point destination);
*/
int CalculateWidth2(int width);
inline int GetAnimationFrame(int frames, int fps = 60)
{
int frame = (SDL_GetTicks() / fps) % frames;
return frame > frames ? 0 : frame;
}
} // namespace devilution

1
Source/engine/render/text_render.cpp

@ -12,7 +12,6 @@
#include <fmt/compile.h>
#include "DiabloUI/art_draw.h"
#include "DiabloUI/diabloui.h"
#include "DiabloUI/ui_item.h"
#include "engine.h"

58
Source/utils/pcx.cpp

@ -26,62 +26,4 @@ bool LoadPcxMeta(SDL_RWops *handle, int &width, int &height, uint8_t &bpp)
return true;
}
bool LoadPcxPixelsAndPalette(SDL_RWops *handle, int width, int height, std::uint8_t bpp,
uint8_t *buffer, std::ptrdiff_t bufferPitch, SDL_Color *palette)
{
std::ptrdiff_t pixelDataSize = SDL_RWsize(handle);
if (pixelDataSize < 0) {
// Unable to determine size, or an error occurred.
return false;
}
// SDL_RWsize gives the total size of the file however we've already read the header from an earlier call to
// LoadPcxMeta, so we only need to read the remainder of the file.
const std::size_t readSize = pixelDataSize - PcxHeaderSize;
std::unique_ptr<uint8_t[]> fileBuffer { new uint8_t[readSize] };
if (SDL_RWread(handle, fileBuffer.get(), readSize, 1) == 0) {
return false;
}
const std::ptrdiff_t xSkip = bufferPitch - width;
const std::ptrdiff_t srcSkip = width % 2;
uint8_t *dataPtr = fileBuffer.get();
for (int j = 0; j < height; j++) {
for (int x = 0; x < width;) {
constexpr std::uint8_t PcxMaxSinglePixel = 0xBF;
const std::uint8_t byte = *dataPtr++;
if (byte <= PcxMaxSinglePixel) {
*buffer++ = byte;
++x;
continue;
}
constexpr std::uint8_t PcxRunLengthMask = 0x3F;
const std::uint8_t runLength = (byte & PcxRunLengthMask);
std::memset(buffer, *dataPtr++, runLength);
buffer += runLength;
x += runLength;
}
dataPtr += srcSkip;
buffer += xSkip;
}
if (palette != nullptr && bpp == 8) {
// The file has a 256 color palette that needs to be loaded.
[[maybe_unused]] constexpr unsigned PcxPaletteSeparator = 0x0C;
assert(*dataPtr == PcxPaletteSeparator); // sanity check the delimiter
++dataPtr;
auto *out = palette;
for (unsigned i = 0; i < NumPaletteColors; ++i) {
out->r = *dataPtr++;
out->g = *dataPtr++;
out->b = *dataPtr++;
#ifndef USE_SDL1
out->a = SDL_ALPHA_OPAQUE;
#endif
++out;
}
}
return true;
}
} // namespace devilution

2
Source/utils/pcx.hpp

@ -31,7 +31,5 @@ struct PCXHeader {
static constexpr size_t PcxHeaderSize = 128;
bool LoadPcxMeta(SDL_RWops *handle, int &width, int &height, uint8_t &bpp);
bool LoadPcxPixelsAndPalette(SDL_RWops *handle, int width, int height, std::uint8_t bpp,
uint8_t *buffer, std::ptrdiff_t bufferPitch, SDL_Color *palette);
} // namespace devilution

Loading…
Cancel
Save