11#include "../gfx_func.h"
13#include "../settings_type.h"
14#include "../strings_func.h"
15#include "table/strings.h"
17#include "../core/math_func.hpp"
18#include "../core/alloc_type.hpp"
19#include "../core/bitmath_func.hpp"
20#include "../spritecache.h"
23#include "../safeguards.h"
25extern const uint8_t _palmap_w2d[];
37 static uint8_t warning_level = 0;
38 if (warning_level == 0) {
42 Debug(sprite, warning_level,
"[{}] Loading corrupted sprite from {} at position {}", line, file.
GetSimplifiedFilename(), file_pos);
69 if (num < 0 || num > 64 * 1024 * 1024)
return WarnCorruptSprite(file, file_pos, __LINE__);
71 std::unique_ptr<uint8_t[]> dest_orig = std::make_unique<uint8_t[]>(num);
72 uint8_t *dest = dest_orig.get();
73 const int64_t dest_size = num;
81 int size = (code == 0) ? 0x80 : code;
84 for (; size > 0; size--) {
90 const uint data_offset = ((code & 7) << 8) | file.
ReadByte();
91 if (dest - data_offset < dest_orig.get())
return WarnCorruptSprite(file, file_pos, __LINE__);
92 int size = -(code >> 3);
95 for (; size > 0; size--) {
96 *dest = *(dest - data_offset);
108 if (colour_fmt &
SCC_RGB) bpp += 3;
110 if (colour_fmt &
SCC_PAL) bpp++;
114 for (
int y = 0; y < sprite->
height; y++) {
115 bool last_item =
false;
118 if (container_format >= 2 && dest_size > UINT16_MAX) {
119 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
121 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
125 dest = dest_orig.get() + offset;
128 if (dest + (container_format >= 2 && sprite->
width > 256 ? 4 : 2) > dest_orig.get() + dest_size) {
135 if (container_format >= 2 && sprite->
width > 256) {
139 last_item = (dest[1] & 0x80) != 0;
140 length = ((dest[1] & 0x7F) << 8) | dest[0];
141 skip = (dest[3] << 8) | dest[2];
147 last_item = ((*dest) & 0x80) != 0;
148 length = (*dest++) & 0x7F;
152 data = &sprite->
data[y * sprite->
width + skip];
154 if (skip + length > sprite->
width || dest + length * bpp > dest_orig.get() + dest_size) {
158 for (
int x = 0; x < length; x++) {
164 data->
a = (colour_fmt &
SCC_ALPHA) ? *dest++ : 0xFF;
166 switch (sprite_type) {
169 default: data->
m = *dest;
break;
172 if (colour_fmt ==
SCC_PAL && *dest == 0) data->
a = 0x00;
177 }
while (!last_item);
180 int64_t sprite_size =
static_cast<int64_t
>(sprite->
width) * sprite->
height * bpp;
181 if (dest_size < sprite_size) {
185 if (dest_size > sprite_size) {
186 static uint8_t warning_level = 0;
187 Debug(sprite, warning_level,
"Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite_size, file.
GetSimplifiedFilename(), file_pos);
191 dest = dest_orig.get();
193 for (
int i = 0; i < sprite->
width * sprite->
height; i++) {
194 uint8_t *pixel = &dest[i * bpp];
197 sprite->
data[i].
r = *pixel++;
198 sprite->
data[i].
g = *pixel++;
199 sprite->
data[i].
b = *pixel++;
203 switch (sprite_type) {
206 default: sprite->
data[i].
m = *pixel;
break;
209 if (colour_fmt ==
SCC_PAL && *pixel == 0) sprite->
data[i].
a = 0x00;
221 if (load_32bpp)
return 0;
224 file.
SeekTo(file_pos, SEEK_SET);
231 if (type == 0xFF)
return 0;
235 sprite[zoom_lvl].height = file.
ReadByte();
236 sprite[zoom_lvl].width = file.
ReadWord();
237 sprite[zoom_lvl].x_offs = file.
ReadWord();
238 sprite[zoom_lvl].y_offs = file.
ReadWord();
239 sprite[zoom_lvl].colours =
SCC_PAL;
241 if (sprite[zoom_lvl].width > INT16_MAX) {
248 num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8;
250 if (
DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, num, type, zoom_lvl,
SCC_PAL, 1))
return 1 << zoom_lvl;
260 if (file_pos == SIZE_MAX)
return 0;
263 file.
SeekTo(file_pos, SEEK_SET);
267 uint8_t loaded_sprites = 0;
270 size_t start_pos = file.
GetPos();
274 if (type == 0xFF)
return 0;
279 bool is_wanted_colour_depth = (colour != 0 && (load_32bpp ? colour !=
SCC_PAL : colour ==
SCC_PAL));
280 bool is_wanted_zoom_lvl;
283 if (zoom <
lengthof(zoom_lvl_map)) {
284 is_wanted_zoom_lvl =
true;
288 is_wanted_zoom_lvl =
false;
292 is_wanted_zoom_lvl =
false;
295 is_wanted_zoom_lvl =
false;
298 is_wanted_zoom_lvl = (zoom == 0);
301 if (is_wanted_colour_depth && is_wanted_zoom_lvl) {
304 if (
HasBit(loaded_sprites, zoom_lvl)) {
311 sprite[zoom_lvl].height = file.
ReadWord();
312 sprite[zoom_lvl].width = file.
ReadWord();
313 sprite[zoom_lvl].x_offs = file.
ReadWord();
314 sprite[zoom_lvl].y_offs = file.
ReadWord();
316 if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) {
322 type = type & ~SCC_MASK;
326 if (colour &
SCC_RGB) bpp += 3;
334 uint decomp_size = (type & 0x08) ? file.
ReadDword() : sprite[zoom_lvl].width * sprite[zoom_lvl].height * bpp;
336 bool valid =
DecodeSingleSprite(&sprite[zoom_lvl], file, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
337 if (file.
GetPos() != start_pos + num) {
350 return loaded_sprites;
355 if (this->container_ver >= 2) {
356 return LoadSpriteV2(sprite, file, file_pos, sprite_type, load_32bpp, control_flags);
358 return LoadSpriteV1(sprite, file, file_pos, sprite_type, load_32bpp);
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
uint8_t ReadByte()
Read a byte from the file.
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
RandomAccessFile with some extra information specific for sprite files.
bool NeedsPaletteRemap() const
Whether a palette remap is needed when loading sprites from this file.
uint8_t LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
@ WL_ERROR
Errors (eg. saving/loading failed)
SpriteType
Types of sprites that might be loaded.
@ Font
A sprite used for fonts.
@ MapGen
Special sprite for the map generator.
@ Normal
The most basic (normal) sprite.
static bool WarnCorruptSprite(const SpriteFile &file, size_t file_pos, int line)
We found a corrupted sprite.
bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, int64_t num, uint8_t type, ZoomLevel zoom_lvl, uint8_t colour_fmt, uint8_t container_format)
Decode the image data of a single sprite.
Base for reading sprites from (New)GRFs.
uint8_t valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
ClientSettings _settings_client
The current settings for this game.
@ SCCF_ALLOW_ZOOM_MIN_2X_32BPP
Allow use of sprite min zoom setting at 2x in 32bpp mode.
@ SCCF_ALLOW_ZOOM_MIN_1X_32BPP
Allow use of sprite min zoom setting at 1x in 32bpp mode.
@ SCCF_ALLOW_ZOOM_MIN_1X_PAL
Allow use of sprite min zoom setting at 1x in palette mode.
@ SCCF_ALLOW_ZOOM_MIN_2X_PAL
Allow use of sprite min zoom setting at 2x in palette mode.
SpriteColourComponent
The different colour components a sprite can have.
@ SCC_MASK
Mask of valid colour bits.
@ SCC_ALPHA
Sprite has alpha.
@ SCC_PAL
Sprite has palette data.
#define lengthof(array)
Return the length of an fixed size array.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
GUISettings gui
settings related to the GUI
ZoomLevel sprite_zoom_min
maximum zoom level at which higher-resolution alternative sprites will be used (if available) instead...
Definition of a common pixel in OpenTTD's realm.
Structure for passing information from the sprite loader to the blitter.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
uint16_t width
Width of the sprite.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
ZoomLevel
All zoom levels we know.
@ ZOOM_LVL_NORMAL
The normal zoom level.
@ ZOOM_LVL_OUT_4X
Zoomed 4 times out.
@ ZOOM_LVL_OUT_2X
Zoomed 2 times out.
@ ZOOM_LVL_OUT_8X
Zoomed 8 times out.
@ ZOOM_LVL_IN_2X
Zoomed 2 times in.
@ ZOOM_LVL_MIN
Minimum zoom level.
@ ZOOM_LVL_IN_4X
Zoomed 4 times in.