Browse Source

Port essential parts to use SDL abstraction for file access

pull/3185/head
Anders Jenbo 5 years ago
parent
commit
11e37e972a
  1. 21
      Source/DiabloUI/art.cpp
  2. 13
      Source/engine/load_file.hpp
  3. 14
      Source/engine/render/text_render.cpp

21
Source/DiabloUI/art.cpp

@ -5,6 +5,7 @@
#include <memory>
#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<std::uint32_t>(-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<BYTE[]> 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<uint8_t *>(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++) {

13
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 <typename T>

14
Source/engine/render/text_render.cpp

@ -127,15 +127,17 @@ std::array<uint8_t, 256> *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;

Loading…
Cancel
Save