diff options
author | Rubidium <rubidium@openttd.org> | 2021-04-14 17:20:39 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-05-08 12:39:34 +0200 |
commit | fdc11a9f943bcbd674b8b69a2bb0895414928028 (patch) | |
tree | ccd04bb045ed1aeebbff2b88224f9c52988ddc92 /src/spriteloader | |
parent | 0dd339ecd8fab0ae3a4901e0ad185798e04cccf3 (diff) | |
download | openttd-fdc11a9f943bcbd674b8b69a2bb0895414928028.tar.xz |
Codechange: introduce SpriteFile to be used by the sprite loader instead of the global FIO slot functionality
Diffstat (limited to 'src/spriteloader')
-rw-r--r-- | src/spriteloader/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/spriteloader/grf.cpp | 103 | ||||
-rw-r--r-- | src/spriteloader/grf.hpp | 2 | ||||
-rw-r--r-- | src/spriteloader/sprite_file.cpp | 50 | ||||
-rw-r--r-- | src/spriteloader/sprite_file_type.hpp | 46 | ||||
-rw-r--r-- | src/spriteloader/spriteloader.hpp | 3 |
6 files changed, 152 insertions, 54 deletions
diff --git a/src/spriteloader/CMakeLists.txt b/src/spriteloader/CMakeLists.txt index 5d6a2f865..804bb1a2e 100644 --- a/src/spriteloader/CMakeLists.txt +++ b/src/spriteloader/CMakeLists.txt @@ -1,5 +1,7 @@ add_files( grf.cpp grf.hpp + sprite_file.cpp + sprite_file_type.hpp spriteloader.hpp ) diff --git a/src/spriteloader/grf.cpp b/src/spriteloader/grf.cpp index 30ea09444..fae87b87f 100644 --- a/src/spriteloader/grf.cpp +++ b/src/spriteloader/grf.cpp @@ -9,7 +9,6 @@ #include "../stdafx.h" #include "../gfx_func.h" -#include "../fileio_func.h" #include "../debug.h" #include "../settings_type.h" #include "../strings_func.h" @@ -32,14 +31,14 @@ extern const byte _palmap_w2d[]; * @param line the line where the error occurs. * @return always false (to tell loading the sprite failed) */ -static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line) +static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line) { static byte warning_level = 0; if (warning_level == 0) { - SetDParamStr(0, FioGetFilename(file_slot)); + SetDParamStr(0, file.GetSimplifiedFilename().c_str()); ShowErrorMessage(STR_NEWGRF_ERROR_CORRUPT_SPRITE, INVALID_STRING_ID, WL_ERROR); } - DEBUG(sprite, warning_level, "[%i] Loading corrupted sprite from %s at position %i", line, FioGetFilename(file_slot), (int)file_pos); + DEBUG(sprite, warning_level, "[%i] Loading corrupted sprite from %s at position %i", line, file.GetSimplifiedFilename().c_str(), (int)file_pos); warning_level = 6; return false; } @@ -47,7 +46,7 @@ static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line) /** * Decode the image data of a single sprite. * @param[in,out] sprite Filled with the sprite image data. - * @param file_slot File slot. + * @param file The file with the sprite data. * @param file_pos File position. * @param sprite_type Type of the sprite we're decoding. * @param num Size of the decompressed sprite. @@ -57,7 +56,7 @@ static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line) * @param container_format Container format of the GRF this sprite is in. * @return True if the sprite was successfully loaded. */ -bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, int64 num, byte type, ZoomLevel zoom_lvl, byte colour_fmt, byte container_format) +bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, int64 num, byte type, ZoomLevel zoom_lvl, byte colour_fmt, byte container_format) { std::unique_ptr<byte[]> dest_orig(new byte[num]); byte *dest = dest_orig.get(); @@ -65,24 +64,24 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi /* Read the file, which has some kind of compression */ while (num > 0) { - int8 code = FioReadByte(); + int8 code = file.ReadByte(); if (code >= 0) { /* Plain bytes to read */ int size = (code == 0) ? 0x80 : code; num -= size; - if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__); + if (num < 0) return WarnCorruptSprite(file, file_pos, __LINE__); for (; size > 0; size--) { - *dest = FioReadByte(); + *dest = file.ReadByte(); dest++; } } else { /* Copy bytes from earlier in the sprite */ - const uint data_offset = ((code & 7) << 8) | FioReadByte(); - if (dest - data_offset < dest_orig.get()) return WarnCorruptSprite(file_slot, file_pos, __LINE__); + const uint data_offset = ((code & 7) << 8) | file.ReadByte(); + if (dest - data_offset < dest_orig.get()) return WarnCorruptSprite(file, file_pos, __LINE__); int size = -(code >> 3); num -= size; - if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__); + if (num < 0) return WarnCorruptSprite(file, file_pos, __LINE__); for (; size > 0; size--) { *dest = *(dest - data_offset); dest++; @@ -90,7 +89,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi } } - if (num != 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__); + if (num != 0) return WarnCorruptSprite(file, file_pos, __LINE__); sprite->AllocateData(zoom_lvl, sprite->width * sprite->height); @@ -117,7 +116,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi do { if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig.get() + dest_size) { - return WarnCorruptSprite(file_slot, file_pos, __LINE__); + return WarnCorruptSprite(file, file_pos, __LINE__); } SpriteLoader::CommonPixel *data; @@ -143,7 +142,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi data = &sprite->data[y * sprite->width + skip]; if (skip + length > sprite->width || dest + length * bpp > dest_orig.get() + dest_size) { - return WarnCorruptSprite(file_slot, file_pos, __LINE__); + return WarnCorruptSprite(file, file_pos, __LINE__); } for (int x = 0; x < length; x++) { @@ -155,7 +154,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi data->a = (colour_fmt & SCC_ALPHA) ? *dest++ : 0xFF; if (colour_fmt & SCC_PAL) { switch (sprite_type) { - case ST_NORMAL: data->m = _palette_remap_grf[file_slot] ? _palmap_w2d[*dest] : *dest; break; + case ST_NORMAL: data->m = file.NeedsPaletteRemap() ? _palmap_w2d[*dest] : *dest; break; case ST_FONT: data->m = std::min<uint>(*dest, 2u); break; default: data->m = *dest; break; } @@ -169,12 +168,12 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi } } else { if (dest_size < sprite->width * sprite->height * bpp) { - return WarnCorruptSprite(file_slot, file_pos, __LINE__); + return WarnCorruptSprite(file, file_pos, __LINE__); } if (dest_size > sprite->width * sprite->height * bpp) { static byte warning_level = 0; - DEBUG(sprite, warning_level, "Ignoring " OTTD_PRINTF64 " unused extra bytes from the sprite from %s at position %i", dest_size - sprite->width * sprite->height * bpp, FioGetFilename(file_slot), (int)file_pos); + DEBUG(sprite, warning_level, "Ignoring " OTTD_PRINTF64 " unused extra bytes from the sprite from %s at position %i", dest_size - sprite->width * sprite->height * bpp, file.GetSimplifiedFilename().c_str(), (int)file_pos); warning_level = 6; } @@ -191,7 +190,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi sprite->data[i].a = (colour_fmt & SCC_ALPHA) ? *pixel++ : 0xFF; if (colour_fmt & SCC_PAL) { switch (sprite_type) { - case ST_NORMAL: sprite->data[i].m = _palette_remap_grf[file_slot] ? _palmap_w2d[*pixel] : *pixel; break; + case ST_NORMAL: sprite->data[i].m = file.NeedsPaletteRemap() ? _palmap_w2d[*pixel] : *pixel; break; case ST_FONT: sprite->data[i].m = std::min<uint>(*pixel, 2u); break; default: sprite->data[i].m = *pixel; break; } @@ -205,31 +204,31 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi return true; } -uint8 LoadSpriteV1(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp) +uint8 LoadSpriteV1(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp) { /* Check the requested colour depth. */ if (load_32bpp) return 0; /* Open the right file and go to the correct position */ - FioSeekToFile(file_slot, file_pos); + file.SeekTo(file_pos, SEEK_SET); /* Read the size and type */ - int num = FioReadWord(); - byte type = FioReadByte(); + int num = file.ReadWord(); + byte type = file.ReadByte(); /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */ if (type == 0xFF) return 0; ZoomLevel zoom_lvl = (sprite_type != ST_MAPGEN) ? ZOOM_LVL_OUT_4X : ZOOM_LVL_NORMAL; - sprite[zoom_lvl].height = FioReadByte(); - sprite[zoom_lvl].width = FioReadWord(); - sprite[zoom_lvl].x_offs = FioReadWord(); - sprite[zoom_lvl].y_offs = FioReadWord(); + sprite[zoom_lvl].height = file.ReadByte(); + sprite[zoom_lvl].width = file.ReadWord(); + sprite[zoom_lvl].x_offs = file.ReadWord(); + sprite[zoom_lvl].y_offs = file.ReadWord(); sprite[zoom_lvl].colours = SCC_PAL; if (sprite[zoom_lvl].width > INT16_MAX) { - WarnCorruptSprite(file_slot, file_pos, __LINE__); + WarnCorruptSprite(file, file_pos, __LINE__); return 0; } @@ -237,12 +236,12 @@ uint8 LoadSpriteV1(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_po * In case it is uncompressed, the size is 'num' - 8 (header-size). */ num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8; - if (DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, num, type, zoom_lvl, SCC_PAL, 1)) return 1 << zoom_lvl; + if (DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, num, type, zoom_lvl, SCC_PAL, 1)) return 1 << zoom_lvl; return 0; } -uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp) +uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp) { static const ZoomLevel zoom_lvl_map[6] = {ZOOM_LVL_OUT_4X, ZOOM_LVL_NORMAL, ZOOM_LVL_OUT_2X, ZOOM_LVL_OUT_8X, ZOOM_LVL_OUT_16X, ZOOM_LVL_OUT_32X}; @@ -250,21 +249,21 @@ uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_po if (file_pos == SIZE_MAX) return 0; /* Open the right file and go to the correct position */ - FioSeekToFile(file_slot, file_pos); + file.SeekTo(file_pos, SEEK_SET); - uint32 id = FioReadDword(); + uint32 id = file.ReadDword(); uint8 loaded_sprites = 0; do { - int64 num = FioReadDword(); - size_t start_pos = FioGetPos(); - byte type = FioReadByte(); + int64 num = file.ReadDword(); + size_t start_pos = file.GetPos(); + byte type = file.ReadByte(); /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here. */ if (type == 0xFF) return 0; byte colour = type & SCC_MASK; - byte zoom = FioReadByte(); + byte zoom = file.ReadByte(); bool is_wanted_colour_depth = (colour != 0 && (load_32bpp ? colour != SCC_PAL : colour == SCC_PAL)); bool is_wanted_zoom_lvl; @@ -280,18 +279,18 @@ uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_po if (HasBit(loaded_sprites, zoom_lvl)) { /* We already have this zoom level, skip sprite. */ - DEBUG(sprite, 1, "Ignoring duplicate zoom level sprite %u from %s", id, FioGetFilename(file_slot)); - FioSkipBytes(num - 2); + DEBUG(sprite, 1, "Ignoring duplicate zoom level sprite %u from %s", id, file.GetSimplifiedFilename().c_str()); + file.SkipBytes(num - 2); continue; } - sprite[zoom_lvl].height = FioReadWord(); - sprite[zoom_lvl].width = FioReadWord(); - sprite[zoom_lvl].x_offs = FioReadWord(); - sprite[zoom_lvl].y_offs = FioReadWord(); + sprite[zoom_lvl].height = file.ReadWord(); + sprite[zoom_lvl].width = file.ReadWord(); + sprite[zoom_lvl].x_offs = file.ReadWord(); + sprite[zoom_lvl].y_offs = file.ReadWord(); if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) { - WarnCorruptSprite(file_slot, file_pos, __LINE__); + WarnCorruptSprite(file, file_pos, __LINE__); return 0; } @@ -308,30 +307,30 @@ uint8 LoadSpriteV2(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_po /* For chunked encoding we store the decompressed size in the file, * otherwise we can calculate it from the image dimensions. */ - uint decomp_size = (type & 0x08) ? FioReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp; + uint decomp_size = (type & 0x08) ? file.ReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp; - bool valid = DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2); - if (FioGetPos() != start_pos + num) { - WarnCorruptSprite(file_slot, file_pos, __LINE__); + bool valid = DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2); + if (file.GetPos() != start_pos + num) { + WarnCorruptSprite(file, file_pos, __LINE__); return 0; } if (valid) SetBit(loaded_sprites, zoom_lvl); } else { /* Not the wanted zoom level or colour depth, continue searching. */ - FioSkipBytes(num - 2); + file.SkipBytes(num - 2); } - } while (FioReadDword() == id); + } while (file.ReadDword() == id); return loaded_sprites; } -uint8 SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp) +uint8 SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp) { if (this->container_ver >= 2) { - return LoadSpriteV2(sprite, file_slot, file_pos, sprite_type, load_32bpp); + return LoadSpriteV2(sprite, file, file_pos, sprite_type, load_32bpp); } else { - return LoadSpriteV1(sprite, file_slot, file_pos, sprite_type, load_32bpp); + return LoadSpriteV1(sprite, file, file_pos, sprite_type, load_32bpp); } } diff --git a/src/spriteloader/grf.hpp b/src/spriteloader/grf.hpp index 20d60edf5..c100590af 100644 --- a/src/spriteloader/grf.hpp +++ b/src/spriteloader/grf.hpp @@ -17,7 +17,7 @@ class SpriteLoaderGrf : public SpriteLoader { byte container_ver; public: SpriteLoaderGrf(byte container_ver) : container_ver(container_ver) {} - uint8 LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp); + uint8 LoadSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp); }; #endif /* SPRITELOADER_GRF_HPP */ diff --git a/src/spriteloader/sprite_file.cpp b/src/spriteloader/sprite_file.cpp new file mode 100644 index 000000000..be7160628 --- /dev/null +++ b/src/spriteloader/sprite_file.cpp @@ -0,0 +1,50 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. + */ + +/** @file sprite_file.cpp Implementation of logic specific to the SpriteFile class. */ + +#include "../stdafx.h" +#include "sprite_file_type.hpp" + +/** Signature of a container version 2 GRF. */ +extern const byte _grf_cont_v2_sig[8] = {'G', 'R', 'F', 0x82, 0x0D, 0x0A, 0x1A, 0x0A}; + +/** + * Get the container version of the currently opened GRF file. + * @return Container version of the GRF file or 0 if the file is corrupt/no GRF file. + */ +static byte GetGRFContainerVersion(SpriteFile &file) +{ + size_t pos = file.GetPos(); + + if (file.ReadWord() == 0) { + /* Check for GRF container version 2, which is identified by the bytes + * '47 52 46 82 0D 0A 1A 0A' at the start of the file. */ + for (uint i = 0; i < lengthof(_grf_cont_v2_sig); i++) { + if (file.ReadByte() != _grf_cont_v2_sig[i]) return 0; // Invalid format + } + + return 2; + } + + /* Container version 1 has no header, rewind to start. */ + file.SeekTo(pos, SEEK_SET); + return 1; +} + +/** + * Create the SpriteFile. + * @param filename Name of the file at the disk. + * @param subdir The sub directory to search this file in. + * @param palette_remap Whether a palette remap needs to be performed for this file. + */ +SpriteFile::SpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap) + : RandomAccessFile(filename, subdir), palette_remap(palette_remap) +{ + this->container_version = GetGRFContainerVersion(*this); + this->content_begin = this->GetPos(); +} diff --git a/src/spriteloader/sprite_file_type.hpp b/src/spriteloader/sprite_file_type.hpp new file mode 100644 index 000000000..b7492afad --- /dev/null +++ b/src/spriteloader/sprite_file_type.hpp @@ -0,0 +1,46 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. + */ + +/** @file sprite_file_type.hpp Random Access File specialised for accessing sprites. */ + +#ifndef SPRITE_FILE_TYPE_HPP +#define SPRITE_FILE_TYPE_HPP + +#include "../random_access_file_type.h" + +/** + * RandomAccessFile with some extra information specific for sprite files. + * It automatically detects and stores the container version upload opening the file. + */ +class SpriteFile : public RandomAccessFile { + bool palette_remap; ///< Whether or not a remap of the palette is required for this file. + byte container_version; ///< Container format of the sprite file. + size_t content_begin; ///< The begin of the content of the sprite file, i.e. after the container metadata. +public: + SpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap); + SpriteFile(const SpriteFile&) = delete; + void operator=(const SpriteFile&) = delete; + + /** + * Whether a palette remap is needed when loading sprites from this file. + * @return True when needed, otherwise false. + */ + bool NeedsPaletteRemap() const { return this->palette_remap; } + + /** + * Get the version number of container type used by the file. + * @return The version. + */ + byte GetContainerVersion() const { return this->container_version; } + + /** + * Seek to the begin of the content, i.e. the position just after the container version has been determined. + */ + void SeekToBegin() { this->SeekTo(this->content_begin, SEEK_SET); } +}; + +#endif /* SPRITE_FILE_TYPE_HPP */ diff --git a/src/spriteloader/spriteloader.hpp b/src/spriteloader/spriteloader.hpp index 7b2474633..beadbb659 100644 --- a/src/spriteloader/spriteloader.hpp +++ b/src/spriteloader/spriteloader.hpp @@ -13,6 +13,7 @@ #include "../core/alloc_type.hpp" #include "../core/enum_type.hpp" #include "../gfx_type.h" +#include "sprite_file_type.hpp" struct Sprite; typedef void *AllocatorProc(size_t size); @@ -73,7 +74,7 @@ public: * @param load_32bpp True if 32bpp sprites should be loaded, false for a 8bpp sprite. * @return Bit mask of the zoom levels successfully loaded or 0 if no sprite could be loaded. */ - virtual uint8 LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp) = 0; + virtual uint8 LoadSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp) = 0; virtual ~SpriteLoader() { } }; |