Fix loading custom interface frms

This commit is contained in:
Alexander Batalov 2023-01-21 15:24:18 +03:00
parent ba49abcea6
commit 3aea6a98ef
3 changed files with 46 additions and 25 deletions

View File

@ -1079,6 +1079,40 @@ static int artReadHeader(Art* art, File* stream)
return 0;
}
// NOTE: Original function was slightly different, but never used. Basically
// it's a memory allocating variant of `artRead` (which reads data into given
// buffer). This function is useful to load custom `frm` files since `Art` now
// needs more memory then it's on-disk size (due to memory padding).
//
// 0x419EC0
Art* artLoad(const char* path)
{
File* stream = fileOpen(path, "rb");
if (stream == nullptr) {
return nullptr;
}
Art header;
if (artReadHeader(&header, stream) != 0) {
fileClose(stream);
return nullptr;
}
fileClose(stream);
unsigned char* data = reinterpret_cast<unsigned char*>(internal_malloc(artGetDataSize(&header)));
if (data == nullptr) {
return nullptr;
}
if (artRead(path, data) != 0) {
internal_free(data);
return nullptr;
}
return reinterpret_cast<Art*>(data);
}
// 0x419FC0
int artRead(const char* path, unsigned char* data)
{

View File

@ -147,6 +147,7 @@ int _art_alias_num(int a1);
int artCritterFidShouldRun(int a1);
int artAliasFid(int fid);
int buildFid(int objectType, int frmId, int animType, int a4, int rotation);
Art* artLoad(const char* path);
int artRead(const char* path, unsigned char* data);
int artWrite(const char* path, unsigned char* data);

View File

@ -2483,30 +2483,26 @@ static void customInterfaceBarInit()
{
gInterfaceBarContentOffset = gInterfaceBarWidth - 640;
char path[COMPAT_MAX_PATH];
snprintf(path, sizeof(path), "art\\intrface\\HR_IFACE_%d.FRM", gInterfaceBarWidth);
if (gInterfaceBarContentOffset > 0 && screenGetWidth() > 640) {
char path[COMPAT_MAX_PATH];
snprintf(path, sizeof(path), "art\\intrface\\HR_IFACE_%d.FRM", gInterfaceBarWidth);
int size;
if (dbGetFileSize(path, &size) != 0 || gInterfaceBarContentOffset <= 0 || screenGetWidth() <= 640) {
gCustomInterfaceBarBackground = artLoad(path);
}
if (gCustomInterfaceBarBackground != nullptr) {
gInterfaceBarIsCustom = true;
} else {
gInterfaceBarContentOffset = 0;
gInterfaceBarWidth = 640;
gInterfaceBarIsCustom = false;
} else {
gInterfaceBarIsCustom = true;
gCustomInterfaceBarBackground = (Art*)(malloc(size));
if (artRead(path, (unsigned char*)gCustomInterfaceBarBackground) != 0) {
gInterfaceBarIsCustom = false;
free(gCustomInterfaceBarBackground);
gCustomInterfaceBarBackground = nullptr;
}
}
}
static void customInterfaceBarExit()
{
if (gCustomInterfaceBarBackground != nullptr) {
free(gCustomInterfaceBarBackground);
internal_free(gCustomInterfaceBarBackground);
gCustomInterfaceBarBackground = nullptr;
}
}
@ -2585,21 +2581,11 @@ static void sidePanelsShow()
static void sidePanelsDraw(const char* path, int win, bool isLeading)
{
int size;
if (dbGetFileSize(path, &size) != 0) {
return;
}
Art* image = reinterpret_cast<Art*>(internal_malloc(size));
Art* image = artLoad(path);
if (image == nullptr) {
return;
}
if (artRead(path, reinterpret_cast<unsigned char*>(image)) != 0) {
internal_free(image);
return;
}
unsigned char* imageData = artGetFrameData(image, 0, 0);
int imageWidth = artGetWidth(image, 0, 0);