Refactor color IO

This commit is contained in:
Alexander Batalov 2023-02-19 11:40:48 +03:00
parent 7a64aff818
commit 3edbe3f311
3 changed files with 32 additions and 57 deletions

View File

@ -10,9 +10,9 @@
namespace fallout {
static int colorOpen(const char* filePath, int flags);
static int colorRead(int fd, void* buffer, size_t size);
static int colorClose(int fd);
static void* colorOpen(const char* filePath);
static int colorRead(void* handle, void* buffer, size_t size);
static int colorClose(void* handle);
static void* defaultMalloc(size_t size);
static void* defaultRealloc(void* ptr, size_t size);
static void defaultFree(void* ptr);
@ -105,17 +105,17 @@ static ColorCloseFunc* closeFunc;
static ColorOpenFunc* openFunc;
// 0x4BFDC0
static int colorOpen(const char* filePath, int flags)
static void* colorOpen(const char* filePath)
{
if (openFunc != NULL) {
return openFunc(filePath, flags);
return openFunc(filePath);
}
return -1;
return nullptr;
}
// 0x4BFDD8
static int colorRead(int fd, void* buffer, size_t size)
static int colorRead(void* fd, void* buffer, size_t size)
{
if (readFunc != NULL) {
return readFunc(fd, buffer, size);
@ -125,7 +125,7 @@ static int colorRead(int fd, void* buffer, size_t size)
}
// 0x4BFDF0
static int colorClose(int fd)
static int colorClose(void* fd)
{
if (closeFunc != NULL) {
return closeFunc(fd);
@ -440,8 +440,8 @@ bool loadColorTable(const char* path)
}
// NOTE: Uninline.
int fd = colorOpen(path, 0x200);
if (fd == -1) {
void* handle = colorOpen(path);
if (handle == nullptr) {
errorStr = _aColor_cColorTa;
return false;
}
@ -452,13 +452,13 @@ bool loadColorTable(const char* path)
unsigned char b;
// NOTE: Uninline.
colorRead(fd, &r, sizeof(r));
colorRead(handle, &r, sizeof(r));
// NOTE: Uninline.
colorRead(fd, &g, sizeof(g));
colorRead(handle, &g, sizeof(g));
// NOTE: Uninline.
colorRead(fd, &b, sizeof(b));
colorRead(handle, &b, sizeof(b));
if (r <= 0x3F && g <= 0x3F && b <= 0x3F) {
mappedColor[index] = 1;
@ -475,23 +475,21 @@ bool loadColorTable(const char* path)
}
// NOTE: Uninline.
colorRead(fd, colorTable, 0x8000);
colorRead(handle, colorTable, 0x8000);
unsigned int type;
// NOTE: Uninline.
colorRead(fd, &type, sizeof(type));
colorRead(handle, &type, sizeof(type));
// NOTE: The value is "NEWC". Original code uses cmp opcode, not stricmp,
// or comparing characters one-by-one.
if (type == 0x4E455743) {
if (type == 'NEWC') {
// NOTE: Uninline.
colorRead(fd, intensityColorTable, 0x10000);
colorRead(handle, intensityColorTable, 0x10000);
// NOTE: Uninline.
colorRead(fd, colorMixAddTable, 0x10000);
colorRead(handle, colorMixAddTable, 0x10000);
// NOTE: Uninline.
colorRead(fd, colorMixMulTable, 0x10000);
colorRead(handle, colorMixMulTable, 0x10000);
} else {
setIntensityTables();
@ -503,7 +501,7 @@ bool loadColorTable(const char* path)
rebuildColorBlendTables();
// NOTE: Uninline.
colorClose(fd);
colorClose(handle);
return true;
}

View File

@ -14,9 +14,9 @@ typedef unsigned char ColorIndex;
typedef const char*(ColorNameMangleFunc)(const char*);
typedef void(fade_bk_func)();
typedef int(ColorOpenFunc)(const char* path, int mode);
typedef int(ColorReadFunc)(int fd, void* buffer, size_t size);
typedef int(ColorCloseFunc)(int fd);
typedef void*(ColorOpenFunc)(const char* path);
typedef int(ColorReadFunc)(void* fd, void* buffer, size_t size);
typedef int(ColorCloseFunc)(void* fd);
typedef void*(ColorMallocFunc)(size_t size);
typedef void*(ColorReallocFunc)(void* ptr, size_t size);
typedef void(ColorFreeFunc)(void* ptr);

View File

@ -3,7 +3,6 @@
#include <algorithm>
#include "game/palette.h"
#include "pointer_registry.h"
#include "plib/color/color.h"
#include "plib/db/db.h"
#include "plib/gnw/button.h"
@ -22,9 +21,9 @@ namespace fallout {
static void win_free(int win);
static void win_clip(Window* window, RectPtr* rectListNodePtr, unsigned char* a3);
static void refresh_all(Rect* rect, unsigned char* a2);
static int colorOpen(const char* path, int flags);
static int colorRead(int fd, void* buf, size_t count);
static int colorClose(int fd);
static void* colorOpen(const char* path);
static int colorRead(void* handle, void* buf, size_t count);
static int colorClose(void* handle);
// 0x53A22C
static bool GNW95_already_running = false;
@ -1334,43 +1333,21 @@ void win_set_trans_b2b(int id, WindowBlitProc* trans_b2b)
}
// 0x4C422C
static int colorOpen(const char* path, int flags)
static void* colorOpen(const char* path)
{
char mode[4];
memset(mode, 0, sizeof(mode));
if ((flags & 0x01) != 0) {
mode[0] = 'w';
} else if ((flags & 0x10) != 0) {
mode[0] = 'a';
} else {
mode[0] = 'r';
}
if ((flags & 0x100) != 0) {
mode[1] = 't';
} else if ((flags & 0x200) != 0) {
mode[1] = 'b';
}
DB_FILE* stream = db_fopen(path, mode);
if (stream != NULL) {
return ptrToInt(stream);
}
return -1;
return db_fopen(path, "rb");
}
// 0x4C4298
static int colorRead(int fd, void* buf, size_t count)
static int colorRead(void* handle, void* buf, size_t count)
{
return db_fread(buf, 1, count, (DB_FILE*)intToPtr(fd));
return db_fread(buf, 1, count, reinterpret_cast<DB_FILE*>(handle));
}
// 0x4C42A0
static int colorClose(int fd)
static int colorClose(void* handle)
{
return db_fclose((DB_FILE*)intToPtr(fd));
return db_fclose(reinterpret_cast<DB_FILE*>(handle));
}
// 0x4C42B8