src/spriteloader/grf.cpp
author rubidium
Tue, 16 Dec 2008 17:58:27 +0000
changeset 10426 4a77f7049b5e
parent 10359 64e5bdedbbfa
permissions -rw-r--r--
(svn r14679) -Fix [FS#2431]: opening the OSK on the chatbox did disable map scrolling (with keyboard) until another window with editbox was opened and closed. Just "refcount" the open edit boxes instead of setting/clearing a bit when opening/closing a window.
6872
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6852
diff changeset
     1
/* $Id$ */
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6852
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8374
diff changeset
     3
/** @file grf.cpp Reading graphics data from (New)GRF files. */
6872
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6852
diff changeset
     4
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
     5
#include "../stdafx.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 7570
diff changeset
     6
#include "../gfx_func.h"
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 10031
diff changeset
     7
#include "../fileio_func.h"
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
     8
#include "../debug.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
     9
#include "../core/alloc_func.hpp"
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    10
#include "../strings_func.h"
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    11
#include "table/strings.h"
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    12
#include "../gui.h"
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    13
#include "grf.hpp"
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    14
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    15
/**
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    16
 * We found a corrupted sprite. This means that the sprite itself
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    17
 * contains invalid data or is too small for the given dimensions.
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    18
 * @param file_slot the file the errored sprite is in
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    19
 * @param file_pos the location in the file of the errored sprite
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    20
 * @param line the line where the error occurs.
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    21
 * @return always false (to tell loading the sprite failed)
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    22
 */
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    23
static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line)
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    24
{
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    25
	static byte warning_level = 0;
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    26
	if (warning_level == 0) {
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    27
		SetDParamStr(0, FioGetFilename(file_slot));
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    28
		ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_ERROR_CORRUPT_SPRITE, 0, 0);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    29
	}
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    30
	DEBUG(sprite, warning_level, "[%i] Loading corrupted sprite from %s at position %i", line, FioGetFilename(file_slot), (int)file_pos);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    31
	warning_level = 6;
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    32
	return false;
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    33
}
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    34
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10039
diff changeset
    35
bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    36
{
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    37
	/* Open the right file and go to the correct position */
7570
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6896
diff changeset
    38
	FioSeekToFile(file_slot, file_pos);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    39
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    40
	/* Read the size and type */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    41
	int num = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    42
	byte type = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    43
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    44
	/* Type 0xFF indicates either a colormap or some other non-sprite info; we do not handle them here */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    45
	if (type == 0xFF) return false;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    46
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    47
	sprite->height = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    48
	sprite->width  = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    49
	sprite->x_offs = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    50
	sprite->y_offs = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    51
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    52
	/* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    53
	 *  In case it is uncompressed, the size is 'num' - 8 (header-size). */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    54
	num = (type & 0x02) ? sprite->width * sprite->height : num - 8;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    55
10031
a787bda1be6f (svn r14190) -Codechange: use alloc instead of malloc+free when the allocated memory shouldn't be used after the function ended.
rubidium
parents: 9390
diff changeset
    56
	byte *dest_orig = AllocaM(byte, num);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    57
	byte *dest = dest_orig;
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    58
	const int dest_size = num;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    59
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    60
	/* Read the file, which has some kind of compression */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    61
	while (num > 0) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    62
		int8 code = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    63
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    64
		if (code >= 0) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    65
			/* Plain bytes to read */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    66
			int size = (code == 0) ? 0x80 : code;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    67
			num -= size;
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    68
			if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    69
			for (; size > 0; size--) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    70
				*dest = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    71
				dest++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    72
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    73
		} else {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    74
			/* Copy bytes from earlier in the sprite */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    75
			const uint data_offset = ((code & 7) << 8) | FioReadByte();
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    76
			if (dest - data_offset < dest_orig) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    77
			int size = -(code >> 3);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    78
			num -= size;
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    79
			if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    80
			for (; size > 0; size--) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    81
				*dest = *(dest - data_offset);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    82
				dest++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    83
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    84
		}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    85
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    86
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    87
	if (num != 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    88
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    89
	sprite->data = CallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    90
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    91
	/* When there are transparency pixels, this format has an other trick.. decode it */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    92
	if (type & 0x08) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    93
		for (int y = 0; y < sprite->height; y++) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    94
			bool last_item = false;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    95
			/* Look up in the header-table where the real data is stored for this row */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    96
			int offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    97
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    98
			/* Go to that row */
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
    99
			dest = dest_orig + offset;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   100
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   101
			do {
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   102
				if (dest + 2 > dest_orig + dest_size) {
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   103
					free(sprite->data);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   104
					return WarnCorruptSprite(file_slot, file_pos, __LINE__);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   105
				}
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   106
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   107
				SpriteLoader::CommonPixel *data;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   108
				/* Read the header:
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   109
				 *  0 .. 14  - length
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   110
				 *  15       - last_item
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   111
				 *  16 .. 31 - transparency bytes */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   112
				last_item  = ((*dest) & 0x80) != 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   113
				int length =  (*dest++) & 0x7F;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   114
				int skip   =   *dest++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   115
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   116
				data = &sprite->data[y * sprite->width + skip];
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   117
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   118
				if (skip + length > sprite->width || dest + length > dest_orig + dest_size) {
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   119
					free(sprite->data);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   120
					return WarnCorruptSprite(file_slot, file_pos, __LINE__);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   121
				}
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   122
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   123
				for (int x = 0; x < length; x++) {
10062
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   124
					data->m = ((sprite_type == ST_NORMAL && _palette_remap_grf[file_slot]) ? _palette_remap[*dest] : *dest);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   125
					dest++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   126
					data++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   127
				}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   128
			} while (!last_item);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   129
		}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   130
	} else {
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   131
		if (dest_size < sprite->width * sprite->height) {
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   132
			free(sprite->data);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   133
			return WarnCorruptSprite(file_slot, file_pos, __LINE__);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   134
		}
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   135
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   136
		if (dest_size > sprite->width * sprite->height) {
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   137
			static byte warning_level = 0;
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   138
			DEBUG(sprite, warning_level, "Ignoring %i unused extra bytes from the sprite from %s at position %i", dest_size - sprite->width * sprite->height, FioGetFilename(file_slot), (int)file_pos);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   139
			warning_level = 6;
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   140
		}
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10062
diff changeset
   141
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   142
		dest = dest_orig;
10062
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   143
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   144
		for (int i = 0; i < sprite->width * sprite->height; i++) {
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   145
			sprite->data[i].m = ((sprite_type == ST_NORMAL && _palette_remap_grf[file_slot]) ? _palette_remap[dest[i]] : dest[i]);
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   146
		}
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   147
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   148
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
   149
	/* Make sure to mark all transparent pixels transparent on the alpha channel too */
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
   150
	for (int i = 0; i < sprite->width * sprite->height; i++)
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
   151
		if (sprite->data[i].m != 0) sprite->data[i].a = 0xFF;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
   152
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   153
	return true;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   154
}