From 11e37e972a171bd104227167aed1c3affd994337 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Wed, 20 Oct 2021 13:36:54 +0200 Subject: [PATCH] Port essential parts to use SDL abstraction for file access --- Source/DiabloUI/art.cpp | 21 +++++++++++---------- Source/engine/load_file.hpp | 13 +++++++------ Source/engine/render/text_render.cpp | 14 ++++++++------ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Source/DiabloUI/art.cpp b/Source/DiabloUI/art.cpp index 5b36aed4b..d8782651b 100644 --- a/Source/DiabloUI/art.cpp +++ b/Source/DiabloUI/art.cpp @@ -5,6 +5,7 @@ #include #include "storm/storm.h" +#include "storm/storm_sdl_rw.h" #include "utils/display.h" #include "utils/log.hpp" #include "utils/sdl_compat.h" @@ -16,10 +17,10 @@ constexpr size_t PcxHeaderSize = 128; constexpr unsigned NumPaletteColors = 256; constexpr unsigned PcxPaletteSize = 1 + NumPaletteColors * 3; -bool LoadPcxMeta(HANDLE handle, int &width, int &height, std::uint8_t &bpp) +bool LoadPcxMeta(SDL_RWops *handle, int &width, int &height, std::uint8_t &bpp) { PCXHeader pcxhdr; - if (!SFileReadFileThreadSafe(handle, &pcxhdr, PcxHeaderSize)) { + if (!SDL_RWread(handle, &pcxhdr, PcxHeaderSize, 1)) { return false; } width = SDL_SwapLE16(pcxhdr.Xmax) - SDL_SwapLE16(pcxhdr.Xmin) + 1; @@ -28,11 +29,11 @@ bool LoadPcxMeta(HANDLE handle, int &width, int &height, std::uint8_t &bpp) return true; } -bool LoadPcxPixelsAndPalette(HANDLE handle, int width, int height, std::uint8_t bpp, +bool LoadPcxPixelsAndPalette(SDL_RWops *handle, int width, int height, std::uint8_t bpp, BYTE *buffer, std::size_t bufferPitch, SDL_Color *palette) { const bool has256ColorPalette = palette != nullptr && bpp == 8; - std::uint32_t pixelDataSize = SFileGetFileSize(handle); + std::uint32_t pixelDataSize = SDL_RWsize(handle); if (pixelDataSize == static_cast(-1)) { return false; } @@ -41,7 +42,7 @@ bool LoadPcxPixelsAndPalette(HANDLE handle, int width, int height, std::uint8_t // We read 1 extra byte because it delimits the palette. const size_t readSize = pixelDataSize + (has256ColorPalette ? PcxPaletteSize : 0); std::unique_ptr fileBuffer { new BYTE[readSize] }; - if (!SFileReadFileThreadSafe(handle, fileBuffer.get(), readSize)) { + if (!SDL_RWread(handle, fileBuffer.get(), readSize, 1)) { return false; } const unsigned xSkip = bufferPitch - width; @@ -108,17 +109,17 @@ void LoadArt(const char *pszFile, Art *art, int frames, SDL_Color *pPalette, con art->frames = frames; - HANDLE handle; int width; int height; std::uint8_t bpp; - if (!SFileOpenFile(pszFile, &handle)) { + SDL_RWops *handle = SFileOpenRw(pszFile); + if (handle == nullptr) { return; } if (!LoadPcxMeta(handle, width, height, bpp)) { Log("LoadArt(\"{}\"): LoadPcxMeta failed with code {}", pszFile, SErrGetLastError()); - SFileCloseFileThreadSafe(handle); + SDL_RWclose(handle); return; } @@ -126,10 +127,10 @@ void LoadArt(const char *pszFile, Art *art, int frames, SDL_Color *pPalette, con if (!LoadPcxPixelsAndPalette(handle, width, height, bpp, static_cast(artSurface->pixels), artSurface->pitch, pPalette)) { Log("LoadArt(\"{}\"): LoadPcxPixelsAndPalette failed with code {}", pszFile, SErrGetLastError()); - SFileCloseFileThreadSafe(handle); + SDL_RWclose(handle); return; } - SFileCloseFileThreadSafe(handle); + SDL_RWclose(handle); if (colorMapping != nullptr) { for (int i = 0; i < artSurface->h * artSurface->pitch; i++) { diff --git a/Source/engine/load_file.hpp b/Source/engine/load_file.hpp index 8b9254a7d..918e72fdb 100644 --- a/Source/engine/load_file.hpp +++ b/Source/engine/load_file.hpp @@ -8,6 +8,7 @@ #include "appfat.h" #include "diablo.h" #include "storm/storm.h" +#include "storm/storm_sdl_rw.h" #include "utils/stdcompat/cstddef.hpp" namespace devilution { @@ -16,8 +17,8 @@ class SFile { public: explicit SFile(const char *path) { - if (!SFileOpenFile(path, &handle_)) { - handle_ = nullptr; + handle_ = SFileOpenRw(path); + if (handle_ == nullptr) { if (!gbQuietMode) { const std::uint32_t code = SErrGetLastError(); if (code == STORM_ERROR_FILE_NOT_FOUND) { @@ -32,7 +33,7 @@ public: ~SFile() { if (handle_ != nullptr) - SFileCloseFileThreadSafe(handle_); + SDL_RWclose(handle_); } [[nodiscard]] bool Ok() const @@ -42,16 +43,16 @@ public: [[nodiscard]] std::size_t Size() const { - return SFileGetFileSize(handle_); + return SDL_RWsize(handle_); } bool Read(void *buffer, std::size_t len) const { - return SFileReadFileThreadSafe(handle_, buffer, len); + return SDL_RWread(handle_, buffer, len, 1); } private: - HANDLE handle_; + SDL_RWops *handle_; }; template diff --git a/Source/engine/render/text_render.cpp b/Source/engine/render/text_render.cpp index 0c19ddaa4..488808604 100644 --- a/Source/engine/render/text_render.cpp +++ b/Source/engine/render/text_render.cpp @@ -127,15 +127,17 @@ std::array *LoadFontKerning(GameFontTables size, uint16_t row) auto *kerning = &FontKerns[fontId]; - HANDLE handle; if (IsFullWidth(row)) { kerning->fill(FontFullwidth[size]); - } else if (SFileOpenFile(path, &handle)) { - SFileReadFileThreadSafe(handle, kerning, 256); - SFileCloseFileThreadSafe(handle); } else { - LogError("Missing font kerning: {}", path); - kerning->fill(FontFullwidth[size]); + SDL_RWops *handle = SFileOpenRw(path); + if (handle != nullptr) { + SDL_RWread(handle, kerning, 256, 1); + SDL_RWclose(handle); + } else { + LogError("Missing font kerning: {}", path); + kerning->fill(FontFullwidth[size]); + } } return kerning;