diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index b5bdb3705..9338407b9 100644 --- a/Source/CMakeLists.txt +++ b/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 diff --git a/Source/DiabloUI/art.cpp b/Source/DiabloUI/art.cpp deleted file mode 100644 index a9f3bf9f7..000000000 --- a/Source/DiabloUI/art.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "DiabloUI/art.h" - -#include -#include -#include - -#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 *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(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(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 *colorMapping) -{ - LoadArt(pszFile, art, frames, nullptr, colorMapping); - if (art->surface != nullptr) - SDLC_SetColorKey(art->surface.get(), mask); -} - -} // namespace devilution diff --git a/Source/DiabloUI/art.h b/Source/DiabloUI/art.h deleted file mode 100644 index 3827a69f8..000000000 --- a/Source/DiabloUI/art.h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include -#include - -#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 *colorMapping = nullptr); -void LoadMaskedArt(const char *pszFile, Art *art, int frames = 1, int mask = 250, const std::array *colorMapping = nullptr); - -} // namespace devilution diff --git a/Source/DiabloUI/art_draw.cpp b/Source/DiabloUI/art_draw.cpp deleted file mode 100644 index 7b2a9dc37..000000000 --- a/Source/DiabloUI/art_draw.cpp +++ /dev/null @@ -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 diff --git a/Source/DiabloUI/art_draw.h b/Source/DiabloUI/art_draw.h deleted file mode 100644 index 0602563b9..000000000 --- a/Source/DiabloUI/art_draw.h +++ /dev/null @@ -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 diff --git a/Source/DiabloUI/credits.cpp b/Source/DiabloUI/credits.cpp index ff018e522..74c8143f0 100644 --- a/Source/DiabloUI/credits.cpp +++ b/Source/DiabloUI/credits.cpp @@ -2,8 +2,6 @@ #include #include -#include "DiabloUI/art.h" -#include "DiabloUI/art_draw.h" #include "DiabloUI/credits_lines.h" #include "DiabloUI/diabloui.h" #include "DiabloUI/support_lines.h" diff --git a/Source/DiabloUI/diabloui.cpp b/Source/DiabloUI/diabloui.cpp index 37a5f48c0..069118e70 100644 --- a/Source/DiabloUI/diabloui.cpp +++ b/Source/DiabloUI/diabloui.cpp @@ -3,7 +3,6 @@ #include #include -#include "DiabloUI/art_draw.h" #include "DiabloUI/button.h" #include "DiabloUI/dialogs.h" #include "DiabloUI/scrollbar.h" diff --git a/Source/DiabloUI/diabloui.h b/Source/DiabloUI/diabloui.h index eca247a83..b467a8e91 100644 --- a/Source/DiabloUI/diabloui.h +++ b/Source/DiabloUI/diabloui.h @@ -5,7 +5,6 @@ #include #include -#include "DiabloUI/art.h" #include "DiabloUI/ui_item.h" #include "engine/clx_sprite.hpp" #include "player.h" diff --git a/Source/DiabloUI/progress.cpp b/Source/DiabloUI/progress.cpp index d6c8a8def..9e1e50b56 100644 --- a/Source/DiabloUI/progress.cpp +++ b/Source/DiabloUI/progress.cpp @@ -1,6 +1,5 @@ #include -#include "DiabloUI/art_draw.h" #include "DiabloUI/button.h" #include "DiabloUI/diabloui.h" #include "control.h" diff --git a/Source/DiabloUI/ui_item.h b/Source/DiabloUI/ui_item.h index d49cab33b..771209161 100644 --- a/Source/DiabloUI/ui_item.h +++ b/Source/DiabloUI/ui_item.h @@ -5,7 +5,6 @@ #include #include -#include "DiabloUI/art.h" #include "DiabloUI/ui_flags.hpp" #include "engine/clx_sprite.hpp" #include "engine/render/text_render.hpp" diff --git a/Source/cursor.cpp b/Source/cursor.cpp index 8d024f0f5..7f911750f 100644 --- a/Source/cursor.cpp +++ b/Source/cursor.cpp @@ -7,7 +7,6 @@ #include -#include "DiabloUI/art.h" #include "DiabloUI/diabloui.h" #include "control.h" #include "controls/plrctrls.h" diff --git a/Source/engine.h b/Source/engine.h index 50bb08035..cf464bf57 100644 --- a/Source/engine.h +++ b/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 diff --git a/Source/engine/render/text_render.cpp b/Source/engine/render/text_render.cpp index 9ceb290f4..280d09136 100644 --- a/Source/engine/render/text_render.cpp +++ b/Source/engine/render/text_render.cpp @@ -12,7 +12,6 @@ #include -#include "DiabloUI/art_draw.h" #include "DiabloUI/diabloui.h" #include "DiabloUI/ui_item.h" #include "engine.h" diff --git a/Source/utils/pcx.cpp b/Source/utils/pcx.cpp index 993f26466..ed572a70f 100644 --- a/Source/utils/pcx.cpp +++ b/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 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 diff --git a/Source/utils/pcx.hpp b/Source/utils/pcx.hpp index 0d7eae89f..3e9157006 100644 --- a/Source/utils/pcx.hpp +++ b/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