Codechange: introduce SpriteFile to be used by the sprite loader instead of the global FIO slot functionality

(cherry picked from commit fdc11a9f94)
This commit is contained in:
Rubidium
2021-04-14 17:20:39 +02:00
committed by Jonathan G Rennison
parent 04b38dbfca
commit 6bd12e24d7
16 changed files with 335 additions and 237 deletions

View File

@@ -33,9 +33,6 @@
#include "safeguards.h"
/** Whether the given NewGRFs must get a palette remap from windows to DOS or not. */
bool _palette_remap_grf[MAX_FILE_SLOTS];
#include "table/landscape_sprite.h"
/** Offsets for loading the different "replacement" sprites in the files. */
@@ -55,27 +52,28 @@ int _progsig_grf_file_index;
* @param filename The name of the file to open.
* @param load_index The offset of the first sprite.
* @param file_index The Fio offset to load the file in.
* @param needs_palette_remap Whether the colours in the GRF file need a palette remap.
* @return The number of loaded sprites.
*/
static uint LoadGrfFile(const char *filename, uint load_index, int file_index)
static uint LoadGrfFile(const char *filename, uint load_index, int file_index, bool needs_palette_remap)
{
uint load_index_org = load_index;
uint sprite_id = 0;
FioOpenFile(file_index, filename, BASESET_DIR);
SpriteFile &file = FioOpenFile(file_index, filename, BASESET_DIR, needs_palette_remap);
DEBUG(sprite, 2, "Reading grf-file '%s'", filename);
byte container_ver = GetGRFContainerVersion();
byte container_ver = file.GetContainerVersion();
if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename);
ReadGRFSpriteOffsets(container_ver);
ReadGRFSpriteOffsets(file);
if (container_ver >= 2) {
/* Read compression. */
byte compression = FioReadByte();
byte compression = file.ReadByte();
if (compression != 0) usererror("Unsupported compression format");
}
while (LoadNextSprite(load_index, file_index, sprite_id, container_ver)) {
while (LoadNextSprite(load_index, file, sprite_id)) {
load_index++;
sprite_id++;
if (load_index >= MAX_SPRITES) {
@@ -92,23 +90,24 @@ static uint LoadGrfFile(const char *filename, uint load_index, int file_index)
* @param filename The name of the file to open.
* @param index_tbl The offsets of each of the sprites.
* @param file_index The Fio offset to load the file in.
* @param needs_palette_remap Whether the colours in the GRF file need a palette remap.
* @return The number of loaded sprites.
*/
static void LoadGrfFileIndexed(const char *filename, const SpriteID *index_tbl, int file_index)
static void LoadGrfFileIndexed(const char *filename, const SpriteID *index_tbl, int file_index, bool needs_palette_remap)
{
uint start;
uint sprite_id = 0;
FioOpenFile(file_index, filename, BASESET_DIR);
SpriteFile &file = FioOpenFile(file_index, filename, BASESET_DIR, needs_palette_remap);
DEBUG(sprite, 2, "Reading indexed grf-file '%s'", filename);
byte container_ver = GetGRFContainerVersion();
byte container_ver = file.GetContainerVersion();
if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename);
ReadGRFSpriteOffsets(container_ver);
ReadGRFSpriteOffsets(file);
if (container_ver >= 2) {
/* Read compression. */
byte compression = FioReadByte();
byte compression = file.ReadByte();
if (compression != 0) usererror("Unsupported compression format");
}
@@ -116,7 +115,7 @@ static void LoadGrfFileIndexed(const char *filename, const SpriteID *index_tbl,
uint end = *index_tbl++;
do {
bool b = LoadNextSprite(start, file_index, sprite_id, container_ver);
bool b = LoadNextSprite(start, file, sprite_id);
(void)b; // Unused without asserts
assert(b);
sprite_id++;
@@ -174,16 +173,14 @@ void CheckExternalFiles()
/** Actually load the sprite tables. */
static void LoadSpriteTables()
{
memset(_palette_remap_grf, 0, sizeof(_palette_remap_grf));
uint i = FIRST_GRF_SLOT;
const GraphicsSet *used_set = BaseGraphics::GetUsedSet();
_palette_remap_grf[i] = (PAL_DOS != used_set->palette);
LoadGrfFile(used_set->files[GFT_BASE].filename, 0, i++);
LoadGrfFile(used_set->files[GFT_BASE].filename, 0, i++, (PAL_DOS != used_set->palette));
/* Progsignal sprites. */
_progsig_grf_file_index = i;
LoadGrfFile("progsignals.grf", SPR_PROGSIGNAL_BASE, i++);
LoadGrfFile("progsignals.grf", SPR_PROGSIGNAL_BASE, i++, false);
/* Fill duplicate programmable pre-signal graphics sprite block */
for (uint i = 0; i < PROGSIGNAL_SPRITE_COUNT; i++) {
@@ -191,7 +188,7 @@ static void LoadSpriteTables()
}
/* Tracerestrict sprites. */
LoadGrfFile("tracerestrict.grf", SPR_TRACERESTRICT_BASE, i++);
LoadGrfFile("tracerestrict.grf", SPR_TRACERESTRICT_BASE, i++, false);
/* Fill duplicate original signal graphics sprite block */
for (uint i = 0; i < DUP_ORIGINAL_SIGNALS_SPRITE_COUNT; i++) {
@@ -204,8 +201,7 @@ static void LoadSpriteTables()
* has a few sprites less. However, we do not care about those missing
* sprites as they are not shown anyway (logos in intro game).
*/
_palette_remap_grf[i] = (PAL_DOS != used_set->palette);
LoadGrfFile(used_set->files[GFT_LOGOS].filename, 4793, i++);
LoadGrfFile(used_set->files[GFT_LOGOS].filename, 4793, i++, (PAL_DOS != used_set->palette));
/*
* Load additional sprites for climates other than temperate.
@@ -213,18 +209,18 @@ static void LoadSpriteTables()
* and the ground sprites.
*/
if (_settings_game.game_creation.landscape != LT_TEMPERATE) {
_palette_remap_grf[i] = (PAL_DOS != used_set->palette);
LoadGrfFileIndexed(
used_set->files[GFT_ARCTIC + _settings_game.game_creation.landscape - 1].filename,
_landscape_spriteindexes[_settings_game.game_creation.landscape - 1],
i++
i++,
(PAL_DOS != used_set->palette)
);
}
LoadGrfFile("innerhighlight.grf", SPR_ZONING_INNER_HIGHLIGHT_BASE, i++);
LoadGrfFile("innerhighlight.grf", SPR_ZONING_INNER_HIGHLIGHT_BASE, i++, false);
/* Load route step graphics */
LoadGrfFile("route_step.grf", SPR_ROUTE_STEP_BASE, i++);
LoadGrfFile("route_step.grf", SPR_ROUTE_STEP_BASE, i++, false);
/* Initialize the unicode to sprite mapping table */
InitializeUnicodeGlyphMap();
@@ -264,7 +260,7 @@ static void LoadSpriteTables()
LoadNewGRF(SPR_NEWGRFS_BASE, i, 2);
uint total_extra_graphics = SPR_NEWGRFS_BASE - SPR_OPENTTD_BASE;
_missing_extra_graphics = GetSpriteCountForSlot(i, SPR_OPENTTD_BASE, SPR_NEWGRFS_BASE);
_missing_extra_graphics = GetSpriteCountForFile(used_set->files[GFT_EXTRA].filename, SPR_OPENTTD_BASE, SPR_NEWGRFS_BASE);
DEBUG(sprite, 1, "%u extra sprites, %u from baseset, %u from fallback", total_extra_graphics, total_extra_graphics - _missing_extra_graphics, _missing_extra_graphics);
/* The original baseset extra graphics intentionally make use of the fallback graphics.