Browse Source

Move SFile helpers to storm.h

pull/1310/head
Gleb Mazovetskiy 5 years ago committed by Anders Jenbo
parent
commit
5814838d26
  1. 21
      3rdParty/Storm/Source/storm.h
  2. 29
      SourceX/storm/storm.cpp
  3. 12
      SourceX/storm_sdl_rw.cpp

21
3rdParty/Storm/Source/storm.h vendored

@ -1,5 +1,6 @@
#pragma once
#include <cstdint>
#include <limits>
#include <string>
namespace dvl {
@ -351,6 +352,26 @@ BOOL SFileEnableDirectAccess(BOOL enable);
#if defined(__GNUC__) || defined(__cplusplus)
}
// Additions to Storm API:
// Sets the file's 64-bit seek position.
inline std::uint64_t SFileSetFilePointer(HANDLE hFile, std::int64_t offset, int whence)
{
int high = static_cast<std::uint64_t>(offset) >> 32;
int low = static_cast<int>(offset);
low = SFileSetFilePointer(hFile, low, &high, whence);
return (static_cast<std::uint64_t>(high) << 32) | low;
}
// Returns the current 64-bit file seek position.
inline std::uint64_t SFileGetFilePointer(HANDLE hFile)
{
// We use `SFileSetFilePointer` with offset 0 to get the current position
// because there is no `SFileGetFilePointer`.
return SFileSetFilePointer(hFile, 0, DVL_FILE_CURRENT);
}
#endif
} // namespace dvl

29
SourceX/storm/storm.cpp

@ -127,23 +127,6 @@ BOOL SFileOpenFile(const char *filename, HANDLE *phFile)
return result;
}
static std::uint64_t GetFilePointer(HANDLE hFile)
{
// We use `SFileSetFilePointer` with offset 0 to get the current position
// because there is no `SFileGetFilePointer`.
int high = 0;
const std::uint32_t low = SFileSetFilePointer(hFile, 0, &high, DVL_FILE_CURRENT);
return (static_cast<std::uint64_t>(high) << 32) | low;
}
static std::uint64_t SetFilePointer(HANDLE hFile, std::int64_t offset, int whence)
{
int high = static_cast<std::uint64_t>(offset) >> 32;
int low = static_cast<int>(offset);
low = SFileSetFilePointer(hFile, low, &high, whence);
return (static_cast<std::uint64_t>(high) << 32) | low;
}
BOOL SBmpLoadImage(const char *pszFileName, SDL_Color *pPalette, BYTE *pBuffer, DWORD dwBuffersize, DWORD *pdwWidth, DWORD *dwHeight, DWORD *pdwBpp)
{
HANDLE hFile;
@ -213,8 +196,8 @@ BOOL SBmpLoadImage(const char *pszFileName, SDL_Color *pPalette, BYTE *pBuffer,
SFileSetFilePointer(hFile, 0, NULL, DVL_FILE_END);
fileBuffer = NULL;
} else {
const auto pos = GetFilePointer(hFile);
size = SetFilePointer(hFile, 0, DVL_FILE_END) - SetFilePointer(hFile, pos, DVL_FILE_BEGIN);
const auto pos = SFileGetFilePointer(hFile);
size = SFileSetFilePointer(hFile, 0, DVL_FILE_END) - SFileSetFilePointer(hFile, pos, DVL_FILE_BEGIN);
fileBuffer = (BYTE *)malloc(size);
}
@ -247,8 +230,12 @@ BOOL SBmpLoadImage(const char *pszFileName, SDL_Color *pPalette, BYTE *pBuffer,
}
if (pPalette && pcxhdr.BitsPerPixel == 8) {
SFileSetFilePointer(hFile, -768, NULL, DVL_FILE_CURRENT);
SFileReadFile(hFile, paldata, 768, 0, 0);
const auto pos = SFileSetFilePointer(hFile, -768, DVL_FILE_CURRENT);
if (pos == static_cast<std::uint64_t>(-1)) {
SDL_Log("SFileSetFilePointer error: %ud", (unsigned int)SErrGetLastError());
}
SFileReadFile(hFile, paldata, 768, 0, NULL);
for (int i = 0; i < 256; i++) {
pPalette[i].r = paldata[i][0];
pPalette[i].g = paldata[i][1];

12
SourceX/storm_sdl_rw.cpp

@ -46,17 +46,11 @@ static int SFileRw_seek(struct SDL_RWops *context, int offset, int whence)
default:
return -1;
}
int high = static_cast<std::uint64_t>(offset) >> 32;
int low = static_cast<int>(offset);
low = SFileSetFilePointer(SFileRw_GetHandle(context), low, &high, swhence);
if (low == -1) {
const std::uint64_t pos = SFileSetFilePointer(SFileRw_GetHandle(context), offset, swhence);
if (pos == static_cast<std::uint64_t>(-1)) {
SDL_Log("SFileRw_seek error: %ud", (unsigned int)SErrGetLastError());
}
#ifndef USE_SDL1
return (static_cast<std::uint64_t>(high) << 32) | low;
#else
return low;
#endif
return pos;
}
#ifndef USE_SDL1

Loading…
Cancel
Save