Skip to content

Commit

Permalink
Codechange: introduce SpriteFile to be used by the sprite loader inst…
Browse files Browse the repository at this point in the history
…ead of the global FIO slot functionality
  • Loading branch information
rubidium42 committed May 8, 2021
1 parent 0dd339e commit fdc11a9
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 220 deletions.
26 changes: 14 additions & 12 deletions src/fileio.cpp
Expand Up @@ -9,7 +9,7 @@

#include "stdafx.h"
#include "fileio_func.h"
#include "random_access_file_type.h"
#include "spriteloader/spriteloader.hpp"
#include "debug.h"
#include "fios.h"
#include "string_func.h"
Expand All @@ -30,8 +30,8 @@

#include "safeguards.h"

static RandomAccessFile *_fio_current_file; ///< current file handle for the Fio functions
static std::array<RandomAccessFile *, MAX_FILE_SLOTS> _fio_files; ///< array of random access files we can have open
static SpriteFile *_fio_current_file; ///< current file handle for the Fio functions
static std::array<SpriteFile *, MAX_FILE_SLOTS> _fio_files; ///< array of random access files we can have open

/** Whether the working directory should be scanned. */
static bool _do_scan_working_directory = true;
Expand All @@ -40,9 +40,9 @@ extern std::string _config_file;
extern std::string _highscore_file;

/**
* Transition helper to get the RandomAccessFile associated with a given slot.
* Transition helper to get the SpriteFile associated with a given slot.
*/
RandomAccessFile *FioGetRandomAccessFile(int slot)
SpriteFile *FioGetSpriteFile(int slot)
{
return _fio_files[slot];
}
Expand Down Expand Up @@ -83,9 +83,9 @@ void FioSeekTo(size_t pos, int mode)
*/
void FioSeekToFile(uint8 slot, size_t pos)
{
RandomAccessFile *raf = _fio_files[slot];
assert(raf != nullptr);
_fio_current_file = raf;
SpriteFile *file = _fio_files[slot];
assert(file != nullptr);
_fio_current_file = file;
_fio_current_file->SeekTo(pos, SEEK_SET);
}

Expand Down Expand Up @@ -149,13 +149,15 @@ void FioCloseAll()
* @param slot Index to assign.
* @param filename Name of the file at the disk.
* @param subdir The sub directory to search this file in.
* @param palette_remap Whether palette remapping needs to take place.
*/
void FioOpenFile(int slot, const std::string &filename, Subdirectory subdir)
SpriteFile &FioOpenFile(int slot, const std::string &filename, Subdirectory subdir, bool palette_remap)
{
RandomAccessFile *raf = new RandomAccessFile(filename, subdir);
SpriteFile *file = new SpriteFile(filename, subdir, palette_remap);
delete _fio_files[slot];
_fio_files[slot] = raf;
_fio_current_file = raf;
_fio_files[slot] = file;
_fio_current_file = file;
return *file;
}

static const char * const _subdirs[] = {
Expand Down
4 changes: 2 additions & 2 deletions src/fileio_func.h
Expand Up @@ -23,10 +23,10 @@ byte FioReadByte();
uint16 FioReadWord();
uint32 FioReadDword();
void FioCloseAll();
void FioOpenFile(int slot, const std::string &filename, Subdirectory subdir);
class SpriteFile &FioOpenFile(int slot, const std::string &filename, Subdirectory subdir, bool palette_remap = false);
void FioReadBlock(void *ptr, size_t size);
void FioSkipBytes(int n);
class RandomAccessFile *FioGetRandomAccessFile(int slot);
class SpriteFile *FioGetSpriteFile(int slot);

void FioFCloseFile(FILE *f);
FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
Expand Down
2 changes: 0 additions & 2 deletions src/gfx_func.h
Expand Up @@ -178,8 +178,6 @@ TextColour GetContrastColour(uint8 background, uint8 threshold = 128);
*/
extern byte _colour_gradient[COLOUR_END][8];

extern bool _palette_remap_grf[];

/**
* Return the colour for a particular greyscale level.
* @param level Intensity, 0 = black, 15 = white
Expand Down
42 changes: 19 additions & 23 deletions src/gfxinit.cpp
Expand Up @@ -26,9 +26,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. */
Expand All @@ -43,27 +40,28 @@ static const SpriteID * const _landscape_spriteindexes[] = {
* @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) {
Expand All @@ -80,31 +78,32 @@ 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");
}

while ((start = *index_tbl++) != END) {
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++;
Expand Down Expand Up @@ -162,33 +161,30 @@ 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));

/*
* The second basic file always starts at the given location and does
* contain a different amount of sprites depending on the "type"; DOS
* 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.
* This overwrites some of the temperate sprites, such as foundations
* 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)
);
}

Expand Down Expand Up @@ -230,7 +226,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.
Expand Down

0 comments on commit fdc11a9

Please sign in to comment.