11#include "../gfx_func.h"
13#include "../settings_type.h"
14#include "../strings_func.h"
16#include "../spritecache.h"
19#include "table/strings.h"
21#include "../safeguards.h"
23extern const uint8_t _palmap_w2d[];
35 static uint8_t warning_level = 0;
36 if (warning_level == 0) {
39 Debug(sprite, warning_level,
"[{}] Loading corrupted sprite from {} at position {}", line, file.
GetSimplifiedFilename(), file_pos);
66 if (num < 0 || num > 64 * 1024 * 1024)
return WarnCorruptSprite(file, file_pos, __LINE__);
68 std::unique_ptr<uint8_t[]> dest_orig = std::make_unique<uint8_t[]>(num);
69 uint8_t *dest = dest_orig.get();
70 const int64_t dest_size = num;
78 int size = (code == 0) ? 0x80 : code;
81 for (; size > 0; size--) {
87 const uint data_offset = ((code & 7) << 8) | file.
ReadByte();
88 if (dest - data_offset < dest_orig.get())
return WarnCorruptSprite(file, file_pos, __LINE__);
89 int size = -(code >> 3);
92 for (; size > 0; size--) {
93 *dest = *(dest - data_offset);
111 for (
int y = 0; y < sprite->
height; y++) {
112 bool last_item =
false;
115 if (container_format >= 2 && dest_size > UINT16_MAX) {
116 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
118 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
122 dest = dest_orig.get() + offset;
125 if (dest + (container_format >= 2 && sprite->
width > 256 ? 4 : 2) > dest_orig.get() + dest_size) {
132 if (container_format >= 2 && sprite->
width > 256) {
136 last_item = (dest[1] & 0x80) != 0;
137 length = ((dest[1] & 0x7F) << 8) | dest[0];
138 skip = (dest[3] << 8) | dest[2];
144 last_item = ((*dest) & 0x80) != 0;
145 length = (*dest++) & 0x7F;
149 data = &sprite->
data[y * sprite->
width + skip];
151 if (skip + length > sprite->
width || dest + length * bpp > dest_orig.get() + dest_size) {
155 for (
int x = 0; x < length; x++) {
163 switch (sprite_type) {
166 default: data->
m = *dest;
break;
174 }
while (!last_item);
177 int64_t sprite_size =
static_cast<int64_t
>(sprite->
width) * sprite->
height * bpp;
178 if (dest_size < sprite_size) {
182 if (dest_size > sprite_size) {
183 static uint8_t warning_level = 0;
184 Debug(sprite, warning_level,
"Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite_size, file.
GetSimplifiedFilename(), file_pos);
188 dest = dest_orig.get();
190 for (
int i = 0; i < sprite->
width * sprite->
height; i++) {
191 uint8_t *pixel = &dest[i * bpp];
194 sprite->
data[i].
r = *pixel++;
195 sprite->
data[i].
g = *pixel++;
196 sprite->
data[i].
b = *pixel++;
200 switch (sprite_type) {
203 default: sprite->
data[i].
m = *pixel;
break;
218 if (load_32bpp)
return {};
221 file.
SeekTo(file_pos, SEEK_SET);
228 if (type == 0xFF)
return {};
231 auto &dest_sprite = sprite[zoom_lvl];
233 dest_sprite.height = file.
ReadByte();
234 dest_sprite.width = file.
ReadWord();
235 dest_sprite.x_offs = file.
ReadWord();
236 dest_sprite.y_offs = file.
ReadWord();
239 if (dest_sprite.width > INT16_MAX) {
246 num = (type & 0x02) ? dest_sprite.width * dest_sprite.height : num - 8;
249 avail_8bpp.
Set(zoom_lvl);
261 if (file_pos == SIZE_MAX)
return {};
264 file.
SeekTo(file_pos, SEEK_SET);
271 size_t start_pos = file.
GetPos();
275 if (type == 0xFF)
return {};
284 bool is_wanted_zoom_lvl;
287 if (zoom <
lengthof(zoom_lvl_map)) {
292 is_wanted_zoom_lvl =
true;
296 is_wanted_zoom_lvl =
false;
300 is_wanted_zoom_lvl =
false;
303 is_wanted_zoom_lvl =
false;
306 is_wanted_zoom_lvl = (zoom == 0);
309 if (is_wanted_colour_depth && is_wanted_zoom_lvl) {
312 if (loaded_sprites.
Test(zoom_lvl)) {
319 auto &dest_sprite = sprite[zoom_lvl];
320 dest_sprite.height = file.
ReadWord();
321 dest_sprite.width = file.
ReadWord();
322 dest_sprite.x_offs = file.
ReadWord();
323 dest_sprite.y_offs = file.
ReadWord();
325 if (dest_sprite.width > INT16_MAX || dest_sprite.height > INT16_MAX) {
336 dest_sprite.colours = colour;
340 uint decomp_size = (type & 0x08) ? file.
ReadDword() : dest_sprite.width * dest_sprite.height * bpp;
342 bool valid =
DecodeSingleSprite(&dest_sprite, file, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
343 if (file.
GetPos() != start_pos + num) {
348 if (valid) loaded_sprites.
Set(zoom_lvl);
356 return loaded_sprites;
361 if (this->container_ver >= 2) {
362 return LoadSpriteV2(sprite, file, file_pos, sprite_type, load_32bpp, control_flags, avail_8bpp, avail_32bpp);
364 return LoadSpriteV1(sprite, file, file_pos, sprite_type, load_32bpp, avail_8bpp);
@ Normal
Perform the simple blitting.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
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.
ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
@ WL_ERROR
Errors (eg. saving/loading failed)
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
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, SpriteComponents colour_fmt, uint8_t container_format)
Decode the image data of a single sprite.
Base for reading sprites from (New)GRFs.
ClientSettings _settings_client
The current settings for this game.
@ AllowZoomMin1x32bpp
Allow use of sprite min zoom setting at 1x in 32bpp mode.
@ AllowZoomMin2x32bpp
Allow use of sprite min zoom setting at 2x in 32bpp mode.
@ AllowZoomMin1xPal
Allow use of sprite min zoom setting at 1x in palette mode.
@ AllowZoomMin2xPal
Allow use of sprite min zoom setting at 2x in palette mode.
@ Palette
Sprite has palette data.
#define lengthof(array)
Return the length of an fixed size array.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
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.
@ Out2x
Zoomed 2 times out.
@ Normal
The normal zoom level.
@ Out8x
Zoomed 8 times out.
@ Out4x
Zoomed 4 times out.