spritecache.c
author tron
Mon, 08 Aug 2005 09:49:47 +0000
changeset 2313 6ebd32b859af
parent 2309 34824a8b336f
child 2319 9902d3ffa309
permissions -rw-r--r--
(svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1844
diff changeset
     4
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1250
diff changeset
     5
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     6
#include "functions.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     7
#include "gfx.h"
1349
07514c2cc6d1 (svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents: 1348
diff changeset
     8
#include "spritecache.h"
1363
01d3de5d8039 (svn r1867) Include tables/sprites.h only in files which need it
tron
parents: 1361
diff changeset
     9
#include "table/sprites.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "fileio.h"
463
91bcad840bcd (svn r687) Export InitNewGRFFile() and DecodeSpecialSprite() properly.
pasky
parents: 452
diff changeset
    11
#include "newgrf.h"
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    12
#include "md5.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2142
diff changeset
    13
#include "variables.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include <ctype.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#define SPRITE_CACHE_SIZE 1024*1024
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
//#define WANT_SPRITESIZES
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#define WANT_NEW_LRU
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
452
520e4ed6945d (svn r662) [newgrf] Moved grfspecial.c to newgrf.c/newgrf.h
dominik
parents: 442
diff changeset
    23
/* These are used in newgrf.c: */
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
    24
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
int _skip_sprites = 0;
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
    26
int _replace_sprites_count[16];
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
    27
int _replace_sprites_offset[16];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
    29
static const char *_cur_grffile;
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
    30
static int _loading_stage;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
static int _skip_specials;
392
3657d06c6564 (svn r584) -newgrf: Increase chance to get a TTDPatch savegame using custom GRF files loaded (pasky)
darkvater
parents: 368
diff changeset
    32
uint16 _custom_sprites_base;
1348
1d123409026e (svn r1852) Start cleaning up sprite handling:
tron
parents: 1329
diff changeset
    33
static Sprite _cur_sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
    35
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    36
static void* _sprite_ptr[MAX_SPRITES];
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    37
static uint16 _sprite_size[MAX_SPRITES];
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    38
static uint32 _sprite_file_pos[MAX_SPRITES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
#if defined(WANT_NEW_LRU)
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    41
static int16 _sprite_lru_new[MAX_SPRITES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
#else
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    43
static uint16 _sprite_lru[MAX_SPRITES];
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    44
static uint16 _sprite_lru_cur[MAX_SPRITES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
#ifdef WANT_SPRITESIZES
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    48
static int8 _sprite_xoffs[MAX_SPRITES];
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    49
static int8 _sprite_yoffs[MAX_SPRITES];
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    50
static uint16 _sprite_xsize[MAX_SPRITES];
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    51
static uint8 _sprite_ysize[MAX_SPRITES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    54
typedef struct MemBlock {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    55
	uint32 size;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    56
	byte data[VARARRAY_SIZE];
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    57
} MemBlock;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    58
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
static uint _sprite_lru_counter;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    60
static MemBlock *_spritecache_ptr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
static uint32 _spritecache_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
static int _compact_cache_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    64
typedef struct MD5File {
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    65
	const char * const filename;     // filename
962
c1de16d93e46 (svn r1454) -Fix: small warnings in spritecache.c
darkvater
parents: 961
diff changeset
    66
	const md5_byte_t hash[16]; // md5 sum of the file
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    67
} MD5File;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    68
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
    69
typedef struct FileList {
1725
97841c222b55 (svn r2229) - Fix: [ 1188777 ] Non-existing sprite #5125 (presignal). The DOS grf file trgi.grf has 6 less sprites than the windows one. This results in some segfaults for certain sprites. Fixed it by added a dummy grf file consisting ofr 6 sprites and loaded when using DOS gfx at the appropiate place.
Darkvater
parents: 1437
diff changeset
    70
	const MD5File basic[5];     // grf files that always have to be loaded
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    71
	const MD5File landscape[3]; // landscape specific grf files
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
    72
} FileList;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    74
#include "table/files.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
#include "table/landscape_sprite.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    77
static const SpriteID * const _landscape_spriteindexes[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
	_landscape_spriteindexes_1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	_landscape_spriteindexes_2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
	_landscape_spriteindexes_3,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    83
static const SpriteID * const _slopes_spriteindexes[] = {
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    84
	_slopes_spriteindexes_0,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    85
	_slopes_spriteindexes_1,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    86
	_slopes_spriteindexes_2,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    87
	_slopes_spriteindexes_3,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    88
};
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    89
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
    90
static void CompactSpriteCache(void);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
static void ReadSpriteHeaderSkipData(int num, int load_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
	byte type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
	int deaf = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
	if (_skip_sprites) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
		if (_skip_sprites > 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
			_skip_sprites--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
		deaf = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   102
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
	type = FioReadByte();
364
9e93d102fd4a (svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents: 361
diff changeset
   104
	_cur_sprite.info = type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
	if (type == 0xFF) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
		/* We need to really skip only special sprites in the deaf
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
		 * mode.  It won't hurt to proceed regular sprites as usual
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
		 * because if no special sprite referencing to them is
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
		 * processed, they themselves are never referenced and loaded
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
		 * on their own. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
		if (_skip_specials || deaf) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
			FioSkipBytes(num);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
		} else {
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   114
			DecodeSpecialSprite(_cur_grffile, num, load_index, _loading_stage);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   118
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
#ifdef WANT_SPRITESIZES
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	_cur_sprite.height = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	_cur_sprite.width = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
	_cur_sprite.x_offs = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
	_cur_sprite.y_offs = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	FioSkipBytes(7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	num -= 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
	if (num == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	if (type & 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
		FioSkipBytes(num);
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   133
	} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   134
		while (num > 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   135
			int8 i = FioReadByte();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   136
			if (i >= 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   137
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   138
				FioSkipBytes(i);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   139
			} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   140
				i = -(i >> 3);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   141
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   142
				FioReadByte();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   143
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   148
static void* AllocSprite(size_t);
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   149
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   150
static void* ReadSprite(SpriteID id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
{
1354
5e5c89b9b169 (svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents: 1353
diff changeset
   152
	uint num = _sprite_size[id];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
	byte type;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   154
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   155
	DEBUG(spritecache, 9) ("load sprite %d", id);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   156
1378
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   157
	if (_sprite_file_pos[id] == 0) {
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   158
		error(
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   159
			"Tried to load non-existing sprite #%d.\n"
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   160
			"Probable cause: Wrong/missing NewGRFs",
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   161
			id
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   162
		);
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   163
	}
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   164
1354
5e5c89b9b169 (svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents: 1353
diff changeset
   165
	FioSeekToFile(_sprite_file_pos[id]);
5e5c89b9b169 (svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents: 1353
diff changeset
   166
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
	type = FioReadByte();
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   168
	if (type == 0xFF) {
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   169
		byte* dest = AllocSprite(num);
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   170
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   171
		_sprite_ptr[id] = dest;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   172
		FioReadBlock(dest, num);
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   173
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   174
		return dest;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   175
	} else {
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   176
		uint height = FioReadByte();
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   177
		uint width  = FioReadWord();
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   178
		Sprite* sprite;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   179
		byte* dest;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   180
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   181
		num = (type & 0x02) ? width * height : num - 8;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   182
		sprite = AllocSprite(sizeof(*sprite) + num);
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   183
		_sprite_ptr[id] = sprite;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   184
		sprite->info   = type;
2015
3eec709611f8 (svn r2523) Readd a comment which got lost in r2522
tron
parents: 2014
diff changeset
   185
		sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   186
		sprite->width  = width;
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   187
		sprite->x_offs = FioReadWord();
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   188
		sprite->y_offs = FioReadWord();
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   189
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   190
		dest = sprite->data;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   191
		while (num > 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   192
			int8 i = FioReadByte();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   194
			if (i >= 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   195
				num -= i;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   196
				for (; i > 0; --i) *dest++ = FioReadByte();
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   197
			} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   198
				const byte* rel = dest - (((i & 7) << 8) | FioReadByte());
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   199
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   200
				i = -(i >> 3);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   201
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   202
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   203
				for (; i > 0; --i) *dest++ = *rel++;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   204
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
		}
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   206
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   207
		return sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
static bool LoadNextSprite(int load_index, byte file_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
	uint16 size;
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   215
	uint32 file_pos;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   217
	size = FioReadWord();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   218
	if (size == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   221
	file_pos = FioGetPos() | (file_index << 24);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
	ReadSpriteHeaderSkipData(size, load_index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   225
	if (_replace_sprites_count[0] > 0 && _cur_sprite.info != 0xFF) {
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   226
		int count = _replace_sprites_count[0];
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   227
		int offset = _replace_sprites_offset[0];
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   228
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   229
		_replace_sprites_offset[0]++;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   230
		_replace_sprites_count[0]--;
862
d7e52ef99f42 (svn r1343) -Fix: [Graphic] Autorail icon is now correct (Darkvater)
truelight
parents: 614
diff changeset
   231
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   232
		if ((offset + count) <= MAX_SPRITES) {
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   233
			load_index = offset;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   234
		} else {
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   235
			DEBUG(spritecache, 1) ("Sprites to be replaced are out of range: %x+%x",
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   236
					count, offset);
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   237
			_replace_sprites_offset[0] = 0;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   238
			_replace_sprites_count[0] = 0;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   239
		}
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   240
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   241
		if (_replace_sprites_count[0] == 0) {
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   242
			int i;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   243
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   244
			for (i = 0; i < 15; i++) {
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   245
				_replace_sprites_count[i] = _replace_sprites_count[i + 1];
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   246
				_replace_sprites_offset[i] = _replace_sprites_offset[i + 1];
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   247
			}
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   248
			_replace_sprites_count[i] = 0;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   249
			_replace_sprites_offset[i] = 0;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   250
		}
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   251
	}
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   252
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   253
	_sprite_size[load_index] = size;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   254
	_sprite_file_pos[load_index] = file_pos;
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   255
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
#ifdef WANT_SPRITESIZES
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
	_sprite_xsize[load_index] = _cur_sprite.width;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
	_sprite_ysize[load_index] = _cur_sprite.height;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
	_sprite_xoffs[load_index] = _cur_sprite.x_offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
	_sprite_yoffs[load_index] = _cur_sprite.y_offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
	_sprite_ptr[load_index] = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
	_sprite_lru_new[load_index] = 0;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   268
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
	_sprite_lru[load_index] = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
	_sprite_lru_cur[load_index] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   276
static void SkipSprites(uint count)
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   277
{
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   278
	for (; count > 0; --count)
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   279
	{
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   280
		uint16 size = FioReadWord();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   281
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   282
		if (size == 0)
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   283
			return;
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   284
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   285
		ReadSpriteHeaderSkipData(size, MAX_SPRITES - 1);
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   286
	}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   287
}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   288
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
static int LoadGrfFile(const char *filename, int load_index, int file_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
	int load_index_org = load_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
	FioOpenFile(file_index, filename);
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   294
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   295
	/* Thou shalt use LoadNewGrfFile() if thou loadeth a GRF file that
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   296
	 * might contain some special sprites. */
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   297
	_skip_specials = 1;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   298
	_skip_sprites = 0;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   299
366
9d2630b8b4de (svn r554) -newgrf: Keep track of GRF files. Remember them all in a linked list, this already enables tests for an already loaded GRF file in SkipIf(). Patch by octo, heavily re-hammered by pasky
darkvater
parents: 365
diff changeset
   300
	DEBUG(spritecache, 2) ("Reading grf-file ``%s''", filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
	while (LoadNextSprite(load_index, file_index)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
		load_index++;
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   304
		if (load_index >= MAX_SPRITES) {
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   305
			error("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
	}
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   308
	DEBUG(spritecache, 2) ("Currently %i sprites are loaded", load_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   310
	return load_index - load_index_org;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   311
}
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   312
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   313
static int LoadNewGrfFile(const char *filename, int load_index, int file_index)
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   314
{
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   315
	int i;
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   316
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   317
	FioOpenFile(file_index, filename);
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   318
	_cur_grffile = filename;
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   319
	_skip_specials = 0;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   320
	_skip_sprites = 0;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   321
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   322
	DEBUG(spritecache, 2) ("Reading newgrf-file ``%s'' [offset: %u]",
366
9d2630b8b4de (svn r554) -newgrf: Keep track of GRF files. Remember them all in a linked list, this already enables tests for an already loaded GRF file in SkipIf(). Patch by octo, heavily re-hammered by pasky
darkvater
parents: 365
diff changeset
   323
			filename, load_index);
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   324
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   325
	/* Skip the first sprite; we don't care about how many sprites this
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   326
	 * does contain; newest TTDPatches and George's longvehicles don't
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   327
	 * neither, apparently. */
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   328
	{
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   329
		int length;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   330
		byte type;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   331
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   332
		length = FioReadWord();
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   333
		type = FioReadByte();
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   334
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   335
		if (length == 4 && type == 0xFF) {
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   336
			FioReadDword();
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   337
		} else {
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   338
			error("Custom .grf has invalid format.");
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   339
		}
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   340
	}
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   341
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   342
	for (i = 0; LoadNextSprite(load_index + i, file_index); i++) {
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   343
		if (load_index + i >= MAX_SPRITES)
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   344
			error("Too many sprites (0x%X). Recompile with higher MAX_SPRITES value or remove some custom GRF files.",
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   345
			      load_index + i);
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   346
	}
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   347
361
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   348
	/* Clean up. */
ad7a042ee0eb (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   349
	_skip_sprites = 0;
2313
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   350
	memset(_replace_sprites_count, 0, sizeof(_replace_sprites_count));
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   351
	memset(_replace_sprites_offset, 0, sizeof(_replace_sprites_offset));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   353
	return i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   356
static void LoadGrfIndexed(const char *filename, const SpriteID *index_tbl, int file_index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
{
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   358
	int start;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
	FioOpenFile(file_index, filename);
365
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   361
	_skip_specials = 1;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   362
	_skip_sprites = 0;
5752af05ed75 (svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents: 364
diff changeset
   363
366
9d2630b8b4de (svn r554) -newgrf: Keep track of GRF files. Remember them all in a linked list, this already enables tests for an already loaded GRF file in SkipIf(). Patch by octo, heavily re-hammered by pasky
darkvater
parents: 365
diff changeset
   364
	DEBUG(spritecache, 2) ("Reading indexed grf-file ``%s''", filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   366
	for (; (start = *index_tbl++) != 0xffff;) {
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   367
		int end = *index_tbl++;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   368
		if(start == 0xfffe) { // skip sprites (amount in second var)
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   369
			SkipSprites(end);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   370
		} else { // load sprites and use indexes from start to end
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   371
			do {
1844
372dae999453 (svn r2349) - Fix: remove warning from release build when assertions are no longer active
Darkvater
parents: 1725
diff changeset
   372
			#ifdef NDEBUG
372dae999453 (svn r2349) - Fix: remove warning from release build when assertions are no longer active
Darkvater
parents: 1725
diff changeset
   373
				LoadNextSprite(start, file_index);
372dae999453 (svn r2349) - Fix: remove warning from release build when assertions are no longer active
Darkvater
parents: 1725
diff changeset
   374
			#else
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   375
				bool b = LoadNextSprite(start, file_index);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   376
				assert(b);
1844
372dae999453 (svn r2349) - Fix: remove warning from release build when assertions are no longer active
Darkvater
parents: 1725
diff changeset
   377
			#endif
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   378
			} while (++start <= end);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   379
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   381
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
#define S_FREE_MASK 1
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   385
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   386
static inline MemBlock* NextBlock(MemBlock* block)
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   387
{
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   388
	return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   389
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   391
static uint32 GetSpriteCacheUsage(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
{
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   393
	size_t tot_size = 0;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   394
	MemBlock* s;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   395
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   396
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s))
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   397
		if (!(s->size & S_FREE_MASK)) tot_size += s->size;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
	return tot_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   403
void IncreaseSpriteLRU(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
	// Increase all LRU values
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
	if (_sprite_lru_counter > 16384) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
		DEBUG(spritecache, 2) ("fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage());
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   412
		for (i = 0; i != MAX_SPRITES; i++)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
			if (_sprite_ptr[i] != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
				if (_sprite_lru_new[i] >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
					_sprite_lru_new[i] = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
				} else if (_sprite_lru_new[i] != -32768) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
					_sprite_lru_new[i]--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
		_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
#else
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   423
	for (i = 0; i != MAX_SPRITES; i++)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
		if (_sprite_ptr[i] != NULL && _sprite_lru[i] != 65535)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
			_sprite_lru[i]++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
	// Reset the lru counter.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
	_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
	// Compact sprite cache every now and then.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
	if (++_compact_cache_counter >= 740) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
		CompactSpriteCache();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
		_compact_cache_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
// Called when holes in the sprite cache should be removed.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
// That is accomplished by moving the cached data.
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   439
static void CompactSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
{
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   441
	MemBlock *s;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   442
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   443
	DEBUG(spritecache, 2) (
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   444
		"compacting sprite cache, inuse=%d", GetSpriteCacheUsage()
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   445
	);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   446
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   447
	for (s = _spritecache_ptr; s->size != 0;) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   448
		if (s->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   449
			MemBlock* next = NextBlock(s);
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   450
			MemBlock temp;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   451
			void** i;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   452
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
			// Since free blocks are automatically coalesced, this should hold true.
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   454
			assert(!(next->size & S_FREE_MASK));
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   455
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
			// If the next block is the sentinel block, we can safely return
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   457
			if (next->size == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   460
			// Locate the sprite belonging to the next pointer.
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   461
			for (i = _sprite_ptr; *i != next->data; ++i) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   462
				assert(i != endof(_sprite_ptr));
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   463
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   465
			*i = s->data; // Adjust sprite array entry
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   466
			// Swap this and the next block
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   467
			temp = *s;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   468
			memmove(s, next, next->size);
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   469
			s = NextBlock(s);
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   470
			*s = temp;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   471
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   472
			// Coalesce free blocks
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   473
			while (NextBlock(s)->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   474
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
			}
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   476
		} else {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   477
			s = NextBlock(s);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   481
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   482
static void DeleteEntryFromSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
	int best = -1;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   486
	MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
	int cur_lru;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
	DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	cur_lru = 0xffff;
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   493
	for (i = 0; i != MAX_SPRITES; i++) {
2305
f0856e98f7a5 (svn r2829) Remove sprite locking, it was never used anyway
tron
parents: 2244
diff changeset
   494
		if (_sprite_ptr[i] != NULL && _sprite_lru_new[i] < cur_lru) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
			cur_lru = _sprite_lru_new[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
	uint16 cur_lru = 0, cur_lru_cur = 0xffff;
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   502
	for (i = 0; i != MAX_SPRITES; i++) {
2305
f0856e98f7a5 (svn r2829) Remove sprite locking, it was never used anyway
tron
parents: 2244
diff changeset
   503
		if (_sprite_ptr[i] == NULL || _sprite_lru[i] < cur_lru) continue;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   504
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
		// Found a sprite with a higher LRU value, then remember it.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
		if (_sprite_lru[i] != cur_lru) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
			cur_lru = _sprite_lru[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
		// Else if both sprites were very recently referenced, compare by the cur value instead.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
		} else if (cur_lru == 0 && _sprite_lru_cur[i] <= cur_lru_cur) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
			cur_lru_cur = _sprite_lru_cur[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
			cur_lru = _sprite_lru[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
	// Display an error message and die, in case we found no sprite at all.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
	// This shouldn't really happen, unless all sprites are locked.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
	if (best == -1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
		error("Out of sprite memory");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
	// Mark the block as free (the block must be in use)
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   526
	s = (MemBlock*)_sprite_ptr[best] - 1;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   527
	assert(!(s->size & S_FREE_MASK));
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   528
	s->size |= S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
	_sprite_ptr[best] = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
	// And coalesce adjacent free blocks
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   532
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   533
		if (s->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   534
			while (NextBlock(s)->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   535
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   541
static void* AllocSprite(size_t mem_req)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
{
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   543
	mem_req += sizeof(MemBlock);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   545
	/* Align this to an uint32 boundary. This also makes sure that the 2 least
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   546
	 * bits are not used, so we could use those for other things. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
	mem_req = (mem_req + sizeof(uint32) - 1) & ~(sizeof(uint32) - 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   549
	for (;;) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   550
		MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   552
		for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   553
			if (s->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   554
				size_t cur_size = s->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   556
				/* Is the block exactly the size we need or
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   557
				 * big enough for an additional free block? */
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   558
				if (cur_size == mem_req ||
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   559
						cur_size >= mem_req + sizeof(MemBlock)) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   560
					// Set size and in use
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   561
					s->size = mem_req;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   562
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   563
					// Do we need to inject a free block too?
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   564
					if (cur_size != mem_req) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   565
						NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   566
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   568
					return s->data;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   569
				}
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   570
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   573
		// Reached sentinel, but no block found yet. Delete some old entry.
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   574
		DeleteEntryFromSpriteCache();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
#if defined(NEW_ROTATION)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
#define X15(x) else if (s >= x && s < (x+15)) { s = _rotate_tile_sprite[s - x] + x; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
#define X19(x) else if (s >= x && s < (x+19)) { s = _rotate_tile_sprite[s - x] + x; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
#define MAP(from,to,map) else if (s >= from && s <= to) { s = map[s - from] + from; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 392
diff changeset
   584
static uint RotateSprite(uint s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
	static const byte _rotate_tile_sprite[19] = { 0,2,4,6,8,10,12,14,1,3,5,7,9,11,13,17,18,16,15 };
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
	static const byte _coast_map[9] = {0, 4, 3, 1, 2, 6, 8, 5, 7};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
	static const byte _fence_map[6] = {1, 0, 5, 4, 3, 2};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
	if (0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
	X19(752)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
	X15(990-1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
	X19(3924)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
	X19(3943)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
	X19(3962)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
	X19(3981)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
	X19(4000)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
	X19(4023)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
	X19(4042)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
	MAP(4061,4069,_coast_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
	X19(4126)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
	X19(4145)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
	X19(4164)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
	X19(4183)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
	X19(4202)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
	X19(4221)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
	X19(4240)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
	X19(4259)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
	X19(4259)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	X19(4278)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
	MAP(4090, 4095, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
	MAP(4096, 4101, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
	MAP(4102, 4107, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	MAP(4108, 4113, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
	MAP(4114, 4119, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	MAP(4120, 4125, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
	return s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
1361
10d9b95e7a81 (svn r1865) Fix some warnings
tron
parents: 1357
diff changeset
   621
const void *GetRawSprite(SpriteID sprite)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
{
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   623
	void* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   625
	assert(sprite < MAX_SPRITES);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
#if defined(NEW_ROTATION)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	sprite = RotateSprite(sprite);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
	// Update LRU
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	_sprite_lru_new[sprite] = ++_sprite_lru_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
#else
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 2015
diff changeset
   635
	_sprite_lru_cur[sprite] = ++_sprite_lru_counter;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
	_sprite_lru[sprite] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
	p = _sprite_ptr[sprite];
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   640
	// Load the sprite, if it is not loaded, yet
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   641
	if (p == NULL) p = ReadSprite(sprite);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   642
	return p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
byte _sprite_page_to_load = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2187
diff changeset
   647
#define OPENTTD_SPRITES_COUNT 100
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   648
static const SpriteID _openttd_grf_indexes[] = {
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 2015
diff changeset
   649
	SPR_OPENTTD_BASE + 0, SPR_OPENTTD_BASE + 7, // icons etc
579
08ce4c50bd32 (svn r999) New icons for the network interface, newgrf gui and the tiny euro
dominik
parents: 504
diff changeset
   650
	134, 134,  // euro symbol medium size
08ce4c50bd32 (svn r999) New icons for the network interface, newgrf gui and the tiny euro
dominik
parents: 504
diff changeset
   651
	582, 582,  // euro symbol large size
08ce4c50bd32 (svn r999) New icons for the network interface, newgrf gui and the tiny euro
dominik
parents: 504
diff changeset
   652
	358, 358,  // euro symbol tiny
08ce4c50bd32 (svn r999) New icons for the network interface, newgrf gui and the tiny euro
dominik
parents: 504
diff changeset
   653
	SPR_OPENTTD_BASE+11, SPR_OPENTTD_BASE+57, // more icons
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
	648, 648, // nordic char: æ
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
	616, 616, // nordic char: Æ
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
	666, 666, // nordic char: Ø
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
	634, 634, // nordic char: Ø
143
31611f0dabd4 (svn r144) Whoops, wrong variable
dominik
parents: 142
diff changeset
   658
	SPR_OPENTTD_BASE+62, SPR_OPENTTD_BASE + OPENTTD_SPRITES_COUNT, // more icons
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
	0xffff,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   662
/* FUNCTIONS FOR CHECKING MD5 SUMS OF GRF FILES */
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   663
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   664
/* Check that the supplied MD5 hash matches that stored for the supplied filename */
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   665
static bool CheckMD5Digest(const MD5File file, md5_byte_t *digest, bool warn)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   666
{
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   667
	uint i;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   668
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   669
	/* Loop through each byte of the file MD5 and the stored MD5... */
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   670
	for (i = 0; i < 16; i++) if (file.hash[i] != digest[i]) break;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   671
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   672
		/* If all bytes of the MD5's match (i.e. the MD5's match)... */
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   673
	if (i == 16) {
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   674
		return true;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   675
	} else {
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   676
		if (warn) fprintf(stderr, "MD5 of %s is ****INCORRECT**** - File Corrupt.\n", file.filename);
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   677
		return false;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   678
	};
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   679
}
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   680
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 966
diff changeset
   681
/* Calculate and check the MD5 hash of the supplied filename.
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   682
 * returns true if the checksum is correct */
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   683
static bool FileMD5(const MD5File file, bool warn)
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   684
{
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   685
	FILE *f;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   686
	char buf[MAX_PATH];
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   687
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   688
	// open file
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   689
	sprintf(buf, "%s%s", _path.data_dir, file.filename);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   690
	f = fopen(buf, "rb");
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   691
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   692
#if !defined(WIN32)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   693
	if (f == NULL) {
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1299
diff changeset
   694
		char *s;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   695
	// make lower case and check again
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   696
		for (s = buf + strlen(_path.data_dir) - 1; *s != '\0'; s++)
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   697
			*s = tolower(*s);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   698
		f = fopen(buf, "rb");
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   699
	}
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   700
#endif
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   701
862
d7e52ef99f42 (svn r1343) -Fix: [Graphic] Autorail icon is now correct (Darkvater)
truelight
parents: 614
diff changeset
   702
	if (f != NULL) {
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   703
		md5_state_t filemd5state;
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   704
		md5_byte_t buffer[1024];
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   705
		md5_byte_t digest[16];
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   706
		size_t len;
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   707
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   708
		md5_init(&filemd5state);
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   709
		while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0)
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   710
			md5_append(&filemd5state, buffer, len);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   711
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   712
		if (ferror(f) && warn) fprintf(stderr, "Error Reading from %s \n", buf);
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   713
		fclose(f);
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 966
diff changeset
   714
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   715
		md5_finish(&filemd5state, digest);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   716
	  return CheckMD5Digest(file, digest, warn);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   717
	} else { // file not found
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   718
		return false;
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 966
diff changeset
   719
	}
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   720
}
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   721
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   722
/* Checks, if either the Windows files exist (TRG1R.GRF) or the DOS files (TRG1.GRF)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   723
 * by comparing the MD5 checksums of the files. _use_dos_palette is set accordingly.
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 966
diff changeset
   724
 * If neither are found, Windows palette is assumed.
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   725
 *
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   726
 * (Note: Also checks sample.cat for corruption) */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   727
void CheckExternalFiles(void)
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   728
{
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   729
	uint i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   730
	// count of files from this version
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   731
	uint dos = 0;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   732
	uint win = 0;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   733
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   734
	for (i = 0; i < 2; i++) if (FileMD5(files_dos.basic[i], true)) dos++;
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   735
	for (i = 0; i < 3; i++) if (FileMD5(files_dos.landscape[i], true)) dos++;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   736
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   737
	for (i = 0; i < 2; i++) if (FileMD5(files_win.basic[i], true)) win++;
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   738
	for (i = 0; i < 3; i++) if (FileMD5(files_win.landscape[i], true)) win++;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   739
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   740
	if (!FileMD5(sample_cat_win, false) && !FileMD5(sample_cat_dos, false))
2028
d7686f53adf2 (svn r2537) Small changes, especially use fprintf to stderr for warnings and errors instead of plain printf
tron
parents: 2027
diff changeset
   741
		fprintf(stderr, "Your sample.cat file is corrupted or missing!\n");
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   742
1380
7faf03f67dc3 (svn r1884) Change palette detection algorithm: Use the DOS palette if there are no Windows .grfs but at least one DOS .grf
tron
parents: 1378
diff changeset
   743
	/*
1389
238570e5c3d3 (svn r1893) If -i was specified respect it
tron
parents: 1381
diff changeset
   744
	 * forced DOS palette via command line -> leave it that way
1380
7faf03f67dc3 (svn r1884) Change palette detection algorithm: Use the DOS palette if there are no Windows .grfs but at least one DOS .grf
tron
parents: 1378
diff changeset
   745
	 * all Windows files present -> Windows palette
7faf03f67dc3 (svn r1884) Change palette detection algorithm: Use the DOS palette if there are no Windows .grfs but at least one DOS .grf
tron
parents: 1378
diff changeset
   746
	 * all DOS files present -> DOS palette
7faf03f67dc3 (svn r1884) Change palette detection algorithm: Use the DOS palette if there are no Windows .grfs but at least one DOS .grf
tron
parents: 1378
diff changeset
   747
	 * no Windows files present and any DOS file present -> DOS palette
7faf03f67dc3 (svn r1884) Change palette detection algorithm: Use the DOS palette if there are no Windows .grfs but at least one DOS .grf
tron
parents: 1378
diff changeset
   748
	 * otherwise -> Windows palette
7faf03f67dc3 (svn r1884) Change palette detection algorithm: Use the DOS palette if there are no Windows .grfs but at least one DOS .grf
tron
parents: 1378
diff changeset
   749
	 */
1389
238570e5c3d3 (svn r1893) If -i was specified respect it
tron
parents: 1381
diff changeset
   750
	if (_use_dos_palette) {
238570e5c3d3 (svn r1893) If -i was specified respect it
tron
parents: 1381
diff changeset
   751
		return;
238570e5c3d3 (svn r1893) If -i was specified respect it
tron
parents: 1381
diff changeset
   752
	} else if (win == 5) {
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   753
		_use_dos_palette = false;
1381
3837b6c64508 (svn r1885) Fix typo in r1884
tron
parents: 1380
diff changeset
   754
	} else if (dos == 5 || (win == 0 && dos > 0)) {
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   755
		_use_dos_palette = true;
1380
7faf03f67dc3 (svn r1884) Change palette detection algorithm: Use the DOS palette if there are no Windows .grfs but at least one DOS .grf
tron
parents: 1378
diff changeset
   756
	} else {
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   757
		_use_dos_palette = false;
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   758
	}
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   759
}
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   760
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   761
static void LoadSpriteTables(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
{
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   763
	int load_index = 0;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   764
	uint i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   765
	uint j;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   766
	const FileList *files; // list of grf files to be loaded. Either Windows files or DOS files
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   768
	_loading_stage = 1;
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   769
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   770
	/*
364
9e93d102fd4a (svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents: 361
diff changeset
   771
	 * TODO:
9e93d102fd4a (svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents: 361
diff changeset
   772
	 *   I think we can live entirely without Indexed GRFs, but I have to
9e93d102fd4a (svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents: 361
diff changeset
   773
	 *   invest that further. --octo
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   774
	 */
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   775
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   776
	files = _use_dos_palette? &files_dos : &files_win;
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   777
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   778
	for (i = 0; files->basic[i].filename != NULL; i++) {
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   779
		load_index += LoadGrfFile(files->basic[i].filename, load_index, i);
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   780
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   782
	LoadGrfIndexed("openttd.grf", _openttd_grf_indexes, i++);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   783
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   784
	if (_sprite_page_to_load != 0) {
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   785
		LoadGrfIndexed(
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   786
			files->landscape[_sprite_page_to_load - 1].filename,
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   787
			_landscape_spriteindexes[_sprite_page_to_load - 1],
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   788
			i++
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   789
		);
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   790
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   792
	LoadGrfIndexed("trkfoundw.grf", _slopes_spriteindexes[_opt.landscape], i++);
1070
bef634a62323 (svn r1571) Feature: Visually enhanced autorail placing
dominik
parents: 1019
diff changeset
   793
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   794
	load_index = SPR_AUTORAIL_BASE;
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   795
	load_index += LoadGrfFile("autorail.grf", load_index, i++);
142
0c2bd172af05 (svn r143) Fix: newgrfs are working again
dominik
parents: 74
diff changeset
   796
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   797
	load_index = SPR_CANALS_BASE;
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   798
	load_index += LoadGrfFile("canalsw.grf", load_index, i++);
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   799
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   800
	load_index = SPR_OPENTTD_BASE + OPENTTD_SPRITES_COUNT + 1;
142
0c2bd172af05 (svn r143) Fix: newgrfs are working again
dominik
parents: 74
diff changeset
   801
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   802
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   803
	/* Load newgrf sprites
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   804
	 * in each loading stage, (try to) open each file specified in the config
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   805
	 * and load information from it. */
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   806
	_custom_sprites_base = load_index;
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   807
	for (_loading_stage = 0; _loading_stage < 2; _loading_stage++) {
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   808
		load_index = _custom_sprites_base;
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   809
		for (j = 0; j != lengthof(_newgrf_files) && _newgrf_files[j]; j++) {
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   810
			if (!FiosCheckFileExists(_newgrf_files[j])) {
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   811
				// TODO: usrerror()
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   812
				error("NewGRF file missing: %s", _newgrf_files[j]);
1198
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   813
			}
2309
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   814
			if (_loading_stage == 0) InitNewGRFFile(_newgrf_files[j], load_index);
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   815
			load_index += LoadNewGrfFile(_newgrf_files[j], load_index, i++);
34824a8b336f (svn r2833) Remove saving of sprite cache data:
tron
parents: 2305
diff changeset
   816
			DEBUG(spritecache, 2) ("Currently %i sprites are loaded", load_index);
366
9d2630b8b4de (svn r554) -newgrf: Keep track of GRF files. Remember them all in a linked list, this already enables tests for an already loaded GRF file in SkipIf(). Patch by octo, heavily re-hammered by pasky
darkvater
parents: 365
diff changeset
   817
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
	_compact_cache_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   823
static void GfxInitSpriteMem(void *ptr, uint32 size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
	// initialize sprite cache heap
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
	_spritecache_ptr = ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
	_spritecache_size = size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
	// A big free block
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   830
	_spritecache_ptr->size = (size - sizeof(MemBlock)) | S_FREE_MASK;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   831
	// Sentinel block (identified by size == 0)
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   832
	NextBlock(_spritecache_ptr)->size = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
	memset(_sprite_ptr, 0, sizeof(_sprite_ptr));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   838
void GfxLoadSprites(void)
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   839
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
	static byte *_sprite_mem;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   841
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
	// Need to reload the sprites only if the landscape changed
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
	if (_sprite_page_to_load != _opt.landscape) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
		_sprite_page_to_load = _opt.landscape;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   846
		// Sprite cache
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
		DEBUG(spritecache, 1) ("Loading sprite set %d.", _sprite_page_to_load);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
		// Reuse existing memory?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
		if (_sprite_mem == NULL) _sprite_mem = malloc(SPRITE_CACHE_SIZE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
		GfxInitSpriteMem(_sprite_mem, SPRITE_CACHE_SIZE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
		LoadSpriteTables();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
		GfxInitPalettes();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
1349
07514c2cc6d1 (svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents: 1348
diff changeset
   858
const SpriteDimension *GetSpriteDimension(SpriteID sprite)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
{
2313
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   860
	static SpriteDimension sd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   862
#ifdef WANT_SPRITESIZES
2313
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   863
	sd.xoffs = _sprite_xoffs[sprite];
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   864
	sd.yoffs = _sprite_yoffs[sprite];
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   865
	sd.xsize = _sprite_xsize[sprite];
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   866
	sd.ysize = _sprite_ysize[sprite];
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   867
#else
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   868
	const Sprite* p = GetSprite(sprite);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
	/* decode sprite header */
2313
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   871
	sd.xoffs = p->x_offs;
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   872
	sd.yoffs = p->y_offs;
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   873
	sd.xsize = p->width;
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   874
	sd.ysize = p->height;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
#endif
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   876
2313
6ebd32b859af (svn r2839) Remove stuff missed in r2833, properly calculate the size of two arrays and simplify GetSpriteDimension()?a bit
tron
parents: 2309
diff changeset
   877
	return &sd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   878
}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   879