10 #include "../stdafx.h" 11 #include "../gfx_func.h" 12 #include "../fileio_func.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" 22 #include "../safeguards.h" 45 static byte warning_level = 0;
46 if (warning_level == 0) {
50 DEBUG(sprite, warning_level,
"[%i] Loading corrupted sprite from %s at position %i", line,
FioGetFilename(file_slot), (
int)file_pos);
70 std::unique_ptr<byte[]> dest_orig(
new byte[num]);
71 byte *dest = dest_orig.get();
72 const int64 dest_size = num;
80 int size = (code == 0) ? 0x80 : code;
83 for (; size > 0; size--) {
89 const uint data_offset = ((code & 7) << 8) |
FioReadByte();
90 if (dest - data_offset < dest_orig.get())
return WarnCorruptSprite(file_slot, file_pos, __LINE__);
91 int size = -(code >> 3);
94 for (; size > 0; size--) {
95 *dest = *(dest - data_offset);
107 if (colour_fmt &
SCC_RGB) bpp += 3;
109 if (colour_fmt &
SCC_PAL) bpp++;
113 for (
int y = 0; y < sprite->
height; y++) {
114 bool last_item =
false;
117 if (container_format >= 2 && dest_size > UINT16_MAX) {
118 offset = (dest_orig[y * 4 + 3] << 24) | (dest_orig[y * 4 + 2] << 16) | (dest_orig[y * 4 + 1] << 8) | dest_orig[y * 4];
120 offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
124 dest = dest_orig.get() + offset;
127 if (dest + (container_format >= 2 && sprite->
width > 256 ? 4 : 2) > dest_orig.get() + dest_size) {
134 if (container_format >= 2 && sprite->
width > 256) {
138 last_item = (dest[1] & 0x80) != 0;
139 length = ((dest[1] & 0x7F) << 8) | dest[0];
140 skip = (dest[3] << 8) | dest[2];
146 last_item = ((*dest) & 0x80) != 0;
147 length = (*dest++) & 0x7F;
151 data = &sprite->
data[y * sprite->
width + skip];
153 if (skip + length > sprite->
width || dest + length * bpp > dest_orig.get() + dest_size) {
157 for (
int x = 0; x < length; x++) {
158 if (colour_fmt & SCC_RGB) {
163 data->
a = (colour_fmt &
SCC_ALPHA) ? *dest++ : 0xFF;
164 if (colour_fmt & SCC_PAL) {
165 switch (sprite_type) {
168 default: data->
m = *dest;
break;
171 if (colour_fmt == SCC_PAL && *dest == 0) data->
a = 0x00;
176 }
while (!last_item);
179 if (dest_size < sprite->width * sprite->
height * bpp) {
183 if (dest_size > sprite->
width * sprite->
height * bpp) {
184 static byte warning_level = 0;
185 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);
189 dest = dest_orig.get();
191 for (
int i = 0; i < sprite->
width * sprite->
height; i++) {
192 byte *pixel = &dest[i * bpp];
194 if (colour_fmt & SCC_RGB) {
195 sprite->
data[i].
r = *pixel++;
196 sprite->
data[i].
g = *pixel++;
197 sprite->
data[i].
b = *pixel++;
200 if (colour_fmt & SCC_PAL) {
201 switch (sprite_type) {
204 default: sprite->
data[i].
m = *pixel;
break;
207 if (colour_fmt == SCC_PAL && *pixel == 0) sprite->
data[i].
a = 0x00;
219 if (load_32bpp)
return 0;
229 if (type == 0xFF)
return 0;
238 if (sprite[zoom_lvl].width > INT16_MAX) {
245 num = (type & 0x02) ? sprite[zoom_lvl].width * sprite[zoom_lvl].height : num - 8;
247 if (
DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, num, type, zoom_lvl,
SCC_PAL, 1))
return 1 << zoom_lvl;
257 if (file_pos == SIZE_MAX)
return 0;
264 uint8 loaded_sprites = 0;
271 if (type == 0xFF)
return 0;
279 if (
HasBit(loaded_sprites, zoom_lvl)) {
281 DEBUG(sprite, 1,
"Ignoring duplicate zoom level sprite %u from %s",
id,
FioGetFilename(file_slot));
291 if (sprite[zoom_lvl].width > INT16_MAX || sprite[zoom_lvl].height > INT16_MAX) {
297 type = type & ~SCC_MASK;
301 if (colour &
SCC_RGB) bpp += 3;
309 bool valid =
DecodeSingleSprite(&sprite[zoom_lvl], file_slot, file_pos, sprite_type, decomp_size, type, zoom_lvl, colour, 2);
315 if (valid)
SetBit(loaded_sprites, zoom_lvl);
323 return loaded_sprites;
328 if (this->container_ver >= 2) {
329 return LoadSpriteV2(sprite, file_slot, file_pos, sprite_type, load_32bpp);
331 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 ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
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.
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 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).
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.
SpriteType
Types of sprites that might be loaded.
The most basic (normal) sprite.
#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.
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.
ZoomLevel
All zoom levels we know.
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.