12 #include "../stdafx.h" 13 #include "../gfx_func.h" 14 #include "../fileio_func.h" 16 #include "../strings_func.h" 17 #include "table/strings.h" 19 #include "../core/math_func.hpp" 20 #include "../core/alloc_type.hpp" 21 #include "../core/bitmath_func.hpp" 24 #include "../safeguards.h" 47 static byte warning_level = 0;
48 if (warning_level == 0) {
52 DEBUG(sprite, warning_level,
"[%i] Loading corrupted sprite from %s at position %i", line,
FioGetFilename(file_slot), (
int)file_pos);
73 byte *dest = dest_orig;
74 const int64 dest_size = num;
82 int size = (code == 0) ? 0x80 : code;
85 for (; size > 0; size--) {
91 const uint data_offset = ((code & 7) << 8) |
FioReadByte();
92 if (dest - data_offset < dest_orig)
return WarnCorruptSprite(file_slot, file_pos, __LINE__);
93 int size = -(code >> 3);
96 for (; size > 0; size--) {
97 *dest = *(dest - data_offset);
109 if (colour_fmt &
SCC_RGB) bpp += 3;
111 if (colour_fmt &
SCC_PAL) bpp++;
115 for (
int y = 0; y < sprite->
height; y++) {
116 bool last_item =
false;
119 if (container_format >= 2 && dest_size > UINT16_MAX) {
120 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
122 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
126 dest = dest_orig + offset;
129 if (dest + (container_format >= 2 && sprite->
width > 256 ? 4 : 2) > dest_orig + dest_size) {
136 if (container_format >= 2 && sprite->
width > 256) {
140 last_item = (dest[1] & 0x80) != 0;
141 length = ((dest[1] & 0x7F) << 8) | dest[0];
142 skip = (dest[3] << 8) | dest[2];
148 last_item = ((*dest) & 0x80) != 0;
149 length = (*dest++) & 0x7F;
153 data = &sprite->
data[y * sprite->
width + skip];
155 if (skip + length > sprite->
width || dest + length * bpp > dest_orig + dest_size) {
159 for (
int x = 0; x < length; x++) {
160 if (colour_fmt & SCC_RGB) {
165 data->
a = (colour_fmt &
SCC_ALPHA) ? *dest++ : 0xFF;
166 if (colour_fmt & SCC_PAL) {
167 switch (sprite_type) {
170 default: data->
m = *dest;
break;
173 if (colour_fmt == SCC_PAL && *dest == 0) data->
a = 0x00;
178 }
while (!last_item);
181 if (dest_size < sprite->width * sprite->
height * bpp) {
185 if (dest_size > sprite->
width * sprite->
height * bpp) {
186 static byte warning_level = 0;
187 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);
193 for (
int i = 0; i < sprite->
width * sprite->
height; i++) {
194 byte *pixel = &dest[i * bpp];
196 if (colour_fmt & SCC_RGB) {
197 sprite->
data[i].
r = *pixel++;
198 sprite->
data[i].
g = *pixel++;
199 sprite->
data[i].
b = *pixel++;
202 if (colour_fmt & SCC_PAL) {
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;
231 if (type == 0xFF)
return 0;
240 if (sprite[zoom_lvl].width > INT16_MAX) {
247 num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8;
249 if (
DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, num, type, zoom_lvl,
SCC_PAL, 1))
return 1 << zoom_lvl;
259 if (file_pos == SIZE_MAX)
return 0;
266 uint8 loaded_sprites = 0;
273 if (type == 0xFF)
return 0;
281 if (
HasBit(loaded_sprites, zoom_lvl)) {
283 DEBUG(sprite, 1,
"Ignoring duplicate zoom level sprite %u from %s",
id,
FioGetFilename(file_slot));
293 if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) {
299 type = type & ~SCC_MASK;
303 if (colour &
SCC_RGB) bpp += 3;
311 bool valid =
DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
317 if (valid)
SetBit(loaded_sprites, zoom_lvl);
325 return loaded_sprites;
330 if (this->container_ver >= 2) {
331 return LoadSpriteV2(sprite, file_slot, file_pos, sprite_type, load_32bpp);
333 return LoadSpriteV1(sprite, file_slot, file_pos, sprite_type, load_32bpp);
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
uint8 LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, bool load_32bpp)
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
const char * FioGetFilename(uint8 slot)
Get the filename associated with a slot.
const byte _palmap_w2d[]
Converting from the Windows palette to the DOS palette.
The most basic (normal) sprite.
Mask of valid colour bits.
byte FioReadByte()
Read a byte from the file.
Definition of a common pixel in OpenTTD's realm.
Base for reading sprites from (New)GRFs.
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=NULL, uint textref_stack_size=0, const uint32 *textref_stack=NULL)
Display an error message in a window.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
uint8 valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
SpriteType
Types of sprites that might be loaded.
Special sprite for the map generator.
int16 x_offs
The x-offset of where the sprite will be drawn.
void FioSeekToFile(uint8 slot, size_t pos)
Switch to a different file and seek to a position.
SpriteLoader::CommonPixel * data
The sprite itself.
Structure for passing information from the sprite loader to the blitter.
bool _palette_remap_grf[]
Whether the given NewGRFs must get a palette remap from windows to DOS or not.
#define lengthof(x)
Return the length of an fixed size array.
static T min(const T a, const T b)
Returns the minimum of two values.
A smart pointer class that free()'s the pointer on destruction.
#define DEBUG(name, level,...)
Output a line of debugging information.
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
uint16 width
Width of the sprite.
ZoomLevel
All zoom levels we know.
void FioSkipBytes(int n)
Skip n bytes ahead in the file.
static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line)
We found a corrupted sprite.
SpriteColourComponent
The different colour components a sprite can have.
uint16 height
Height of the sprite.
int16 y_offs
The y-offset of where the sprite will be drawn.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
size_t FioGetPos()
Get position in the current file.
Errors (eg. saving/loading failed)
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)
Decode the image data of a single sprite.