library: Don't expect palette objects when loading GRF files

Currently, if the user wants to load a GRF file with a palette, the
function NE_MaterialTexLoadGRF() will fail unless it is provided a palette
object.

With this change, if a palette object isn't provided, it will be created
and marked for automatic deletion when the material is deleted.
This commit is contained in:
Antonio Niño Díaz 2024-03-06 22:17:49 +00:00
parent edf4457d49
commit 30ac9ab8e9

View File

@ -252,22 +252,42 @@ int NE_MaterialTexLoadGRF(NE_Material *tex, NE_Palette *pal,
goto cleanup;
}
// Okay, there is a palette to load. Make sure that the user has provided a
// palette object.
// There is a palette to load.
// If the user has provided a palette object, use that one to store the
// palette. If not, create a palette object and mark it to be autodeleted
// when the material is deleted.
bool create_palette = false;
if (pal == NULL)
{
NE_DebugPrint("GRF with a palette, but no palette object provided");
goto cleanup;
create_palette = true;
}
if (create_palette)
{
pal = NE_PaletteCreate();
if (pal == NULL)
{
NE_DebugPrint("Not enough memory for palette object");
goto cleanup;
}
}
if (NE_PaletteLoadSize(pal, palDst, header.palAttr * 2, fmt) == 0)
{
NE_DebugPrint("Failed to load GRF palette");
if (create_palette)
NE_PaletteDelete(pal);
goto cleanup;
}
NE_MaterialSetPalette(tex, pal);
if (create_palette)
NE_MaterialAutodeletePalette(tex);
ret = 1; // Success
cleanup: