spritecache.c
author tron
Thu, 10 Feb 2005 22:26:28 +0000
changeset 1352 5880ab4de192
parent 1351 3e7aa0d35f8f
child 1353 48b59d472641
permissions -rw-r--r--
(svn r1856) Make GfxInitSpriteMem() static, because it's only used withing spritecache.c
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     1
#include "stdafx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     2
#include "ttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1250
diff changeset
     3
#include "debug.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     4
#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
     5
#include "spritecache.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     6
#include "fileio.h"
463
91bcad840bcd (svn r687) Export InitNewGRFFile() and DecodeSpecialSprite() properly.
pasky
parents: 452
diff changeset
     7
#include "newgrf.h"
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
     8
#include "md5.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include <ctype.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#define SPRITECACHE_ID 0xF00F0006
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#define SPRITE_CACHE_SIZE 1024*1024
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
//#define WANT_SPRITESIZES
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#define WANT_NEW_LRU
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
//#define WANT_LOCKED
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
452
520e4ed6945d (svn r662) [newgrf] Moved grfspecial.c to newgrf.c/newgrf.h
dominik
parents: 442
diff changeset
    20
/* 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
    21
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
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
    23
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
    24
int _replace_sprites_offset[16];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
    26
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
    27
static int _loading_stage;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
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
    29
uint16 _custom_sprites_base;
1348
1d123409026e (svn r1852) Start cleaning up sprite handling:
tron
parents: 1329
diff changeset
    30
static Sprite _cur_sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
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
    32
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
static byte *_sprite_ptr[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
static uint16 _sprite_size[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
static uint32 _sprite_file_pos[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
// This one is probably not needed.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
#if defined(WANT_LOCKED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
static bool _sprite_locked[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
static int16 _sprite_lru_new[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
static uint16 _sprite_lru[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
static uint16 _sprite_lru_cur[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
#ifdef WANT_SPRITESIZES
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
static int8 _sprite_xoffs[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
static int8 _sprite_yoffs[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
static uint16 _sprite_xsize[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
static uint8 _sprite_ysize[NUM_SPRITES];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
static uint _sprite_lru_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
static byte *_spritecache_ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
static uint32 _spritecache_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
static int _compact_cache_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    61
typedef struct MD5File {
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    62
	const char * const filename;     // filename
962
c1de16d93e46 (svn r1454) -Fix: small warnings in spritecache.c
darkvater
parents: 961
diff changeset
    63
	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
    64
} MD5File;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    65
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
    66
typedef struct FileList {
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    67
	const MD5File basic[4];     // grf files that always have to be loaded
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    68
	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
    69
} FileList;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
    71
#include "table/files.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
#include "table/landscape_sprite.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
static const uint16 * const _landscape_spriteindexes[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
	_landscape_spriteindexes_1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
	_landscape_spriteindexes_2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
	_landscape_spriteindexes_3,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    80
static const uint16 * const _slopes_spriteindexes[] = {
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    81
	_slopes_spriteindexes_0,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    82
	_slopes_spriteindexes_1,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    83
	_slopes_spriteindexes_2,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    84
	_slopes_spriteindexes_3,
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    85
};
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
    86
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
    87
static void CompactSpriteCache(void);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
static void ReadSpriteHeaderSkipData(int num, int load_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
	byte type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	int8 i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
	int deaf = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
	if (_skip_sprites) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
		if (_skip_sprites > 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
			_skip_sprites--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
		deaf = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   100
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
	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
   102
	_cur_sprite.info = type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
	if (type == 0xFF) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
		/* 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
   105
		 * 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
   106
		 * because if no special sprite referencing to them is
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
		 * processed, they themselves are never referenced and loaded
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
		 * on their own. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
		if (_skip_specials || deaf) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
			FioSkipBytes(num);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
		} else {
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   112
			DecodeSpecialSprite(_cur_grffile, num, load_index, _loading_stage);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   116
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
#ifdef WANT_SPRITESIZES
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
	_cur_sprite.height = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	_cur_sprite.width = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	_cur_sprite.x_offs = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	_cur_sprite.y_offs = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
	FioSkipBytes(7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	num -= 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	if (num == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
	if (type & 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
		FioSkipBytes(num);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
	while (num) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
		i = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
		if (i>=0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
			num -= i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
			FioSkipBytes(i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
			i = -(i >> 3);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
			num -= i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
			FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
		}
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
static void ReadSprite(int num, byte *dest)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
	byte type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
	byte *rel;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
	int8 i;
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   152
	int dist;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   153
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
	type = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
	/* We've decoded special sprites when reading headers. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
	if (type != 0xFF) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
		/* read sprite hdr */
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   158
		Sprite* sprite = dest;
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   159
		sprite->info = type;
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   160
		sprite->height = FioReadByte();
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   161
		sprite->width = FioReadWord();
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   162
		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
   163
		sprite->y_offs = FioReadWord();
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   164
		dest = sprite->data;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
		num -= 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
	if (type & 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
		while (num--)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
			*dest++ = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
	while (num) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
		i = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		if (i>=0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
			num -= i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
			while (i--)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
				*dest++ = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
			dist = -(((i&7)<<8)|FioReadByte());
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
			i = -(i >> 3);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
			num -= i;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   184
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
			rel = &dest[dist];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
			while (i--)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
				*dest++ = *rel++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
static bool LoadNextSprite(int load_index, byte file_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
	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
   196
	uint32 file_pos;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
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
   198
	if ((size = FioReadWord()) == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
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
   201
	file_pos = FioGetPos() | (file_index << 24);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
	ReadSpriteHeaderSkipData(size, load_index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
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
   205
	if ((_replace_sprites_count[0] > 0) && (_cur_sprite.info != 0xFF)) {
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
   206
		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
   207
		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
   208
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
   209
		_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
   210
		_replace_sprites_count[0]--;
862
d7e52ef99f42 (svn r1343) -Fix: [Graphic] Autorail icon is now correct (Darkvater)
truelight
parents: 614
diff changeset
   211
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
   212
		if ((offset + count) <= NUM_SPRITES) {
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
   213
			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
   214
		} 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
   215
			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
   216
					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
   217
			_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
   218
			_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
   219
		}
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
   220
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
		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
   222
			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
   223
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
   224
			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
   225
				_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
   226
				_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
   227
			}
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
			_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
   229
			_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
   230
		}
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
   231
	}
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
   232
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
	_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
   234
	_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
   235
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
#ifdef WANT_SPRITESIZES
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
	_sprite_xsize[load_index] = _cur_sprite.width;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
	_sprite_ysize[load_index] = _cur_sprite.height;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
	_sprite_xoffs[load_index] = _cur_sprite.x_offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
	_sprite_yoffs[load_index] = _cur_sprite.y_offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
	_sprite_ptr[load_index] = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
#if defined(WANT_LOCKED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
	_sprite_locked[load_index] = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
	_sprite_lru_new[load_index] = 0;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   251
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
	_sprite_lru[load_index] = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
	_sprite_lru_cur[load_index] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   259
static void SkipSprites(int count)
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   260
{
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   261
	while(count>0)
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   262
	{
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   263
		uint16 size;
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   264
		if ( (size = FioReadWord()) == 0)
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   265
			return;
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   266
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   267
		ReadSpriteHeaderSkipData(size, NUM_SPRITES-1);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   268
		count--;
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   269
	}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   270
}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   271
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
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
   273
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
	int load_index_org = load_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
	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
   277
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
   278
	/* 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
   279
	 * 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
   280
	_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
   281
	_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
   282
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
   283
	DEBUG(spritecache, 2) ("Reading grf-file ``%s''", filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
	while (LoadNextSprite(load_index, file_index)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
		load_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
		if (load_index >= NUM_SPRITES) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   288
			error("Too many sprites. Recompile with higher NUM_SPRITES value or remove some custom GRF files.");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
		}
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
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
   292
	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
   293
}
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
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
   296
{
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
	int i;
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   298
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
   299
	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
   300
	_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
   301
	_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
   302
	_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
   303
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
   304
	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
   305
			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
   306
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   307
	/* 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
   308
	 * 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
   309
	 * 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
   310
	{
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
		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
   312
		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
   313
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
		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
   315
		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
   316
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
		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
   318
			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
   319
		} 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
   320
			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
   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
	}
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
   323
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   324
	for (i = 0; LoadNextSprite(load_index + i, file_index); i++) {
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   325
		if (load_index + i >= NUM_SPRITES)
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   326
			error("Too many sprites (%x). Recompile with higher NUM_SPRITES value or remove some custom GRF files.",
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   327
			      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
   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
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
   330
	/* 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
   331
	_skip_sprites = 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
   332
	memset(_replace_sprites_count, 0, 16 * sizeof(*_replace_sprites_count));
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
   333
	memset(_replace_sprites_offset, 0, 16 * sizeof(*_replace_sprites_offset));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
442
913a307af50d (svn r651) LoadNewGrfFile() now doesn't care about num_sprites and just loads whatever is
miham
parents: 425
diff changeset
   335
	return i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
static void LoadGrfIndexed(const char *filename, const uint16 *index_tbl, int file_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
{
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   340
	int start;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
	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
   343
	_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
   344
	_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
   345
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
   346
	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
   347
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
	for(;(start=*index_tbl++) != 0xffff;) {
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   349
		int end = *index_tbl++;
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   350
		if(start==0xfffe) { // skip sprites (amount in second var)
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   351
			SkipSprites(end);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   352
		} 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
   353
			do {
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   354
				bool b = LoadNextSprite(start, file_index);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   355
				assert(b);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   356
			} while (++start <= end);
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   357
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   359
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
typedef size_t CDECL fread_t(void*,size_t,size_t,FILE*);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
static bool HandleCachedSpriteHeaders(const char *filename, bool read)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
	FILE *f;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
	fread_t *proc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
	uint32 hdr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
	if (!_cache_sprites)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
	if (read) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
		f = fopen(filename, "rb");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
		proc = fread;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
		if (f == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
		proc(&hdr, sizeof(hdr), 1, f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
		if (hdr != SPRITECACHE_ID) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
			fclose(f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
			return false;
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
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
		f = fopen(filename, "wb");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
		proc = (fread_t*) fwrite;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
		if (f == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
		hdr = SPRITECACHE_ID;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
		proc(&hdr, sizeof(hdr), 1, f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
	proc(_sprite_size, 1, sizeof(_sprite_size), f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
	proc(_sprite_file_pos, 1, sizeof(_sprite_file_pos), f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
#if 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
	proc(_sprite_xsize, 1, sizeof(_sprite_xsize), f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	proc(_sprite_ysize, 1, sizeof(_sprite_ysize), f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
	proc(_sprite_xoffs, 1, sizeof(_sprite_xoffs), f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
	proc(_sprite_yoffs, 1, sizeof(_sprite_yoffs), f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
#endif
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
#if !defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
	if (read)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
		memset(_sprite_lru, 0xFF, sizeof(_sprite_lru));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
	fclose(f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
#define S_DATA(x) (*(uint32*)(x))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
#define S_FREE_MASK 1
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
#define S_HDRSIZE sizeof(uint32)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
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
   418
static uint32 GetSpriteCacheUsage(void)
0
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
	byte *s = _spritecache_ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
	size_t cur_size, tot_size = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
	for(; (cur_size=S_DATA(s)) != 0; s+=cur_size) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
		if ( cur_size & S_FREE_MASK ) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
			cur_size--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
			tot_size += cur_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
	}
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
	return tot_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
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
   434
void IncreaseSpriteLRU(void)
0
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
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
	// Increase all LRU values
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
	if (_sprite_lru_counter > 16384) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
		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
   442
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
		for(i=0; i!=NUM_SPRITES; i++)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
			if (_sprite_ptr[i] != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
				if (_sprite_lru_new[i] >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
					_sprite_lru_new[i] = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
				} else if (_sprite_lru_new[i] != -32768) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
					_sprite_lru_new[i]--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
		_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
	for(i=0; i!=NUM_SPRITES; i++)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
		if (_sprite_ptr[i] != NULL && _sprite_lru[i] != 65535)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
			_sprite_lru[i]++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
	// Reset the lru counter.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
	// Compact sprite cache every now and then.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
	if (++_compact_cache_counter >= 740) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
		CompactSpriteCache();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
		_compact_cache_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
// 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
   469
// 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
   470
static void CompactSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
	byte *s, *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
	size_t size, sizeb, cur_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
	int i;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   475
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
	DEBUG(spritecache, 2) ("compacting sprite cache, inuse=%d", GetSpriteCacheUsage());
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
	s = _spritecache_ptr;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   479
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
	while (true) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   481
		size = S_DATA(s);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   482
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
		// Only look for free blocks.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
		if (size & S_FREE_MASK) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
			size -= S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
			// Since free blocks are automatically coalesced, this should hold true.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
			assert(!(S_DATA(s+size) & S_FREE_MASK));
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   488
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
			// If the next block is the sentinel block, we can safely return
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
			if ( (sizeb=S_DATA(s + size)) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
			// Locate the sprite number belonging to the next pointer.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
			for(i=0,t=s+size+S_HDRSIZE; _sprite_ptr[i] != t; i++) {assert(i < NUM_SPRITES);}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
			// If it's locked, we must not move it.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
#if defined(WANT_LOCKED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
			if (!_sprite_locked[i]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
#endif
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
				// Offset the sprite pointer by the size of the free block
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
				_sprite_ptr[i] -= size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   504
				// Move the memory
425
a8a07a6fbe1c (svn r624) Merge r377 to trunk:
tron
parents: 410
diff changeset
   505
				memmove(s + S_HDRSIZE, s + S_HDRSIZE + size, sizeb - S_HDRSIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
				// What we just did had the effect of swapping the allocated block with the free block, so we need to update
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
				//  the block pointers. First update the allocated one. It is in use.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
				S_DATA(s) = sizeb;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   510
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
				// Then coalesce the free ones that follow.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
				s += sizeb;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
				while ((cur_size = S_DATA(s+size)) & S_FREE_MASK)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
					size += cur_size - S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
				S_DATA(s) = size + S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
				continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
#if defined(WANT_LOCKED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
		// Continue with next block until the sentinel is reached.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
		s += size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
		if (size == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
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
   528
static void DeleteEntryFromSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
	int best = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
	byte *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
	size_t cur_size, cur;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
	int cur_lru;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
	DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
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
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
	cur_lru = 0xffff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	for(i=0; i!=NUM_SPRITES; i++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
		if (_sprite_ptr[i] != 0 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
				_sprite_lru_new[i] < cur_lru
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
#if defined(WANT_LOCKED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
				 && !_sprite_locked[i]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
				) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
			cur_lru = _sprite_lru_new[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
	uint16 cur_lru = 0, cur_lru_cur = 0xffff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
	for(i=0; i!=NUM_SPRITES; i++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
		if (_sprite_ptr[i] == 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
#if defined(WANT_LOCKED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
				_sprite_locked[i] ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
				_sprite_lru[i] < cur_lru)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
					continue;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   562
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
		// 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
   564
		if (_sprite_lru[i] != cur_lru) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
			cur_lru = _sprite_lru[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
		// 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
   569
		} 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
   570
			cur_lru_cur = _sprite_lru_cur[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
			cur_lru = _sprite_lru[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
	}
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
#endif
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
	// 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
   579
	// 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
   580
	if (best == -1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
		error("Out of sprite memory");
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
	// Mark the block as free (the block must be in use)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
	s = _sprite_ptr[best];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
	assert(!(S_DATA(s - S_HDRSIZE) & S_FREE_MASK));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
	S_DATA(s - S_HDRSIZE) += S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
	_sprite_ptr[best] = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
	// And coalesce adjacent free blocks
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
	s = _spritecache_ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
	for(; (cur_size=S_DATA(s)) != 0; s+=cur_size) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
		if ( cur_size & S_FREE_MASK ) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
			while ((cur=S_DATA(s+cur_size-S_FREE_MASK)) & S_FREE_MASK) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
				cur_size += cur - S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
				S_DATA(s) = cur_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
			cur_size--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
static byte *LoadSpriteToMem(int sprite)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
	byte *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
	size_t mem_req, cur_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
	DEBUG(spritecache, 9) ("load sprite %d", sprite);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
restart:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	// Number of needed bytes
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
	mem_req = _sprite_size[sprite] + S_HDRSIZE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
	// Align this to an uint32 boundary. This also makes sure that the 2 least bit are not used,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	//  so we could use those for other things.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
	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
   616
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
	s = _spritecache_ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
	for(;;) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
		for(;;) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
			cur_size = S_DATA(s);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
			if (! (cur_size & S_FREE_MASK) ) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
			cur_size -= S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
			// Now s points at a free block.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
			// The block is exactly the size we need?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
			if (cur_size != mem_req) {
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   628
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
				// No.. is it too small?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
				if (cur_size < mem_req + S_HDRSIZE)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
				// Block was big enough, and we need to inject a free block too.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
				S_DATA(s + mem_req) = cur_size - mem_req + S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
			// Set size and in use
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
			S_DATA(s) = mem_req;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
			_sprite_ptr[sprite] = (s += S_HDRSIZE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
			FioSeekToFile(_sprite_file_pos[sprite]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
			ReadSprite(_sprite_size[sprite], s);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   644
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
			// Patch the height to compensate for a TTD bug?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
			if (sprite == 142) { s[1] = 10; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
			// Return sprite ptr
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
			return s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
		// Reached sentinel, but no block found yet. Need to delete some old entries.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
		if (cur_size == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
			DeleteEntryFromSpriteCache();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
			goto restart;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
		}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   657
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
		s += cur_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
	}
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
#if defined(NEW_ROTATION)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   663
#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
   664
#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
   665
#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
   666
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 392
diff changeset
   668
static uint RotateSprite(uint s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   669
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
	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
   671
	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
   672
	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
   673
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
	if (0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
	X19(752)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
	X15(990-1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
	X19(3924)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
	X19(3943)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
	X19(3962)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
	X19(3981)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
	X19(4000)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
	X19(4023)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
	X19(4042)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
	MAP(4061,4069,_coast_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
	X19(4126)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
	X19(4145)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
	X19(4164)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   688
	X19(4183)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
	X19(4202)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
	X19(4221)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
	X19(4240)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
	X19(4259)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
	X19(4259)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
	X19(4278)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
	MAP(4090, 4095, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
	MAP(4096, 4101, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
	MAP(4102, 4107, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
	MAP(4108, 4113, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
	MAP(4114, 4119, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
	MAP(4120, 4125, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
	return s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
1350
067b22970f19 (svn r1854) Split GetSpritePtr() into GetSprite() for regular sprites (returning a Sprite*) and GetNonSprite() for "sprites" of type 0xFF (returning byte*)
tron
parents: 1349
diff changeset
   705
Sprite *GetSprite(SpriteID sprite)
067b22970f19 (svn r1854) Split GetSpritePtr() into GetSprite() for regular sprites (returning a Sprite*) and GetNonSprite() for "sprites" of type 0xFF (returning byte*)
tron
parents: 1349
diff changeset
   706
{
067b22970f19 (svn r1854) Split GetSpritePtr() into GetSprite() for regular sprites (returning a Sprite*) and GetNonSprite() for "sprites" of type 0xFF (returning byte*)
tron
parents: 1349
diff changeset
   707
	return GetNonSprite(sprite);
067b22970f19 (svn r1854) Split GetSpritePtr() into GetSprite() for regular sprites (returning a Sprite*) and GetNonSprite() for "sprites" of type 0xFF (returning byte*)
tron
parents: 1349
diff changeset
   708
}
067b22970f19 (svn r1854) Split GetSpritePtr() into GetSprite() for regular sprites (returning a Sprite*) and GetNonSprite() for "sprites" of type 0xFF (returning byte*)
tron
parents: 1349
diff changeset
   709
067b22970f19 (svn r1854) Split GetSpritePtr() into GetSprite() for regular sprites (returning a Sprite*) and GetNonSprite() for "sprites" of type 0xFF (returning byte*)
tron
parents: 1349
diff changeset
   710
byte *GetNonSprite(SpriteID sprite)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
	byte *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
	assert(sprite < NUM_SPRITES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
#if defined(NEW_ROTATION)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
	sprite = RotateSprite(sprite);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
	// Update LRU
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   721
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
	_sprite_lru_new[sprite] = ++_sprite_lru_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
	_sprite_lru_cur[sprite]	= ++_sprite_lru_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
	_sprite_lru[sprite] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
	// Check if the sprite is loaded already?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
	p = _sprite_ptr[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
	if (p == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
		p = LoadSpriteToMem(sprite);  // No, need to load it.
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   732
	return p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
byte _sprite_page_to_load = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
static const char * const _cached_filenames[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
	"cached_sprites.xxx",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
	"cached_sprites.xx1",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
	"cached_sprites.xx2",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
	"cached_sprites.xx3",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
884
9750c41c5860 (svn r1369) -Feature(ish): Added sprites for up/down and left/right arrows. Anywhere where there are horizontal scrollers these are now left/right arrows
darkvater
parents: 862
diff changeset
   744
#define OPENTTD_SPRITES_COUNT 98
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
static const uint16 _openttd_grf_indexes[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
	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
   747
	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
   748
	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
   749
	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
   750
	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
   751
	648, 648, // nordic char: æ
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
	616, 616, // nordic char: Æ
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
	666, 666, // nordic char: Ø
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
	634, 634, // nordic char: Ø
143
31611f0dabd4 (svn r144) Whoops, wrong variable
dominik
parents: 142
diff changeset
   755
	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
   756
	0xffff,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   759
/* 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
   760
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   761
/* 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
   762
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
   763
{
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   764
	int i, matching_bytes=0;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   765
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   766
	/* Loop through each byte of the file MD5 and the stored MD5... */
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   767
	for (i = 0; i < 16; i++)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   768
	{
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   769
		if (file.hash[i] == digest[i])
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   770
			matching_bytes++;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   771
	};
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   772
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   773
		/* If all bytes of the MD5's match (i.e. the MD5's match)... */
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   774
	if (matching_bytes == 16) {
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   775
		return true;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   776
	} else {
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   777
		if (warn) printf("MD5 of %s is ****INCORRECT**** - File Corrupt.\n", file.filename);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   778
		return false;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   779
	};
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   780
}
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   781
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 966
diff changeset
   782
/* 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
   783
 * returns true if the checksum is correct */
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   784
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
   785
{
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   786
	FILE *f;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   787
	char buf[MAX_PATH];
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   788
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   789
	md5_state_t filemd5state;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   790
	int len=0;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   791
	md5_byte_t buffer[1024], digest[16];
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   792
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   793
	// open file
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   794
	sprintf(buf, "%s%s", _path.data_dir, file.filename);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   795
	f = fopen(buf, "rb");
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   796
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   797
#if !defined(WIN32)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   798
	if (f == NULL) {
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1299
diff changeset
   799
		char *s;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   800
	// make lower case and check again
966
04770174a7f8 (svn r1458) -Fix: remove byte *s again for windows.
darkvater
parents: 963
diff changeset
   801
		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
   802
			*s = tolower(*s);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   803
		f = fopen(buf, "rb");
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   804
	}
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   805
#endif
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   806
862
d7e52ef99f42 (svn r1343) -Fix: [Graphic] Autorail icon is now correct (Darkvater)
truelight
parents: 614
diff changeset
   807
	if (f != NULL) {
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   808
		md5_init(&filemd5state);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   809
		while ( (len = fread (buffer, 1, 1024, f)) )
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   810
			md5_append(&filemd5state, buffer, len);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   811
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   812
		if (ferror(f))
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   813
			if (warn) printf ("Error Reading from %s \n", buf);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   814
		fclose(f);
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 966
diff changeset
   815
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   816
		md5_finish(&filemd5state, digest);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   817
	  return CheckMD5Digest(file, digest, warn);
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   818
	} else { // file not found
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   819
		return false;
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 966
diff changeset
   820
	}
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   821
}
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   822
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   823
/* 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
   824
 * 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
   825
 * If neither are found, Windows palette is assumed.
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   826
 *
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   827
 * (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
   828
void CheckExternalFiles(void)
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   829
{
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   830
	int i;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   831
	int dos=0, win=0; // count of files from this version
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   832
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   833
	for (i=0; i<2; i++)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   834
	  if ( FileMD5(files_dos.basic[i], true) )
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   835
			dos++;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   836
	for (i=0; i<3; i++)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   837
	  if ( FileMD5(files_dos.landscape[i], true) )
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   838
			dos++;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   839
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   840
	for (i=0; i<2; i++)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   841
	  if ( FileMD5(files_win.basic[i], true) )
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   842
			win++;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   843
	for (i=0; i<3; i++)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   844
	  if ( FileMD5(files_win.landscape[i], true) )
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   845
			win++;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   846
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   847
	if ( !FileMD5(sample_cat_win, false) && !FileMD5(sample_cat_dos, false) )
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   848
		printf("Your sample.cat file is corrupted or missing!");
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   849
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   850
	if (win == 5) {       // always use the Windows palette if all Windows files are present
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   851
		_use_dos_palette = false;
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   852
	} else if (dos == 5) { // else use the DOS palette if all DOS files are present
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   853
		_use_dos_palette = true;
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   854
	} else {							// some files are missing, regardless of palette. Use Windows
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   855
		_use_dos_palette = false;
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   856
	}
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   857
}
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   858
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
   859
static void LoadSpriteTables(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
	int i,j;
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   862
	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
   863
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   864
	_loading_stage = 1;
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   865
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   866
	/*
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   867
	 * Note for developers:
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   868
	 *   Keep in mind that when you add a LoadGrfIndexed in the 'if'-section below
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   869
	 *   that you should also add the corresponding FioOpenFile to the 'else'-section
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   870
	 *   below.
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
   871
	 *
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
   872
	 * 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
   873
	 *   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
   874
	 *   invest that further. --octo
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   875
	 */
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   876
614
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   877
	files = _use_dos_palette?(&files_dos):(&files_win);
e016770cb781 (svn r1038) Feature: OpenTTD runs with the grf files of the DOS version
dominik
parents: 579
diff changeset
   878
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   879
	// Try to load the sprites from cache
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
	if (!HandleCachedSpriteHeaders(_cached_filenames[_opt.landscape], true)) {
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   881
		// We do not have the sprites in cache yet, or cache is disabled
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   882
		// So just load all files from disk..
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   883
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
		int load_index = 0;
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   885
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   886
		for(i=0; files->basic[i].filename != NULL; i++) {
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   887
			load_index += LoadGrfFile(files->basic[i].filename, load_index, (byte)i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
		LoadGrfIndexed("openttd.grf", _openttd_grf_indexes, i++);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   892
		if (_sprite_page_to_load != 0)
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   893
			LoadGrfIndexed(files->landscape[_sprite_page_to_load-1].filename, _landscape_spriteindexes[_sprite_page_to_load-1], i++);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   894
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   895
		LoadGrfIndexed("trkfoundw.grf", _slopes_spriteindexes[_opt.landscape], i++);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
1070
bef634a62323 (svn r1571) Feature: Visually enhanced autorail placing
dominik
parents: 1019
diff changeset
   897
		load_index = SPR_AUTORAIL_BASE;
bef634a62323 (svn r1571) Feature: Visually enhanced autorail placing
dominik
parents: 1019
diff changeset
   898
		load_index += LoadGrfFile("autorail.grf", load_index, i++);
bef634a62323 (svn r1571) Feature: Visually enhanced autorail placing
dominik
parents: 1019
diff changeset
   899
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
		load_index = SPR_CANALS_BASE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
		load_index += LoadGrfFile("canalsw.grf", load_index, i++);
142
0c2bd172af05 (svn r143) Fix: newgrfs are working again
dominik
parents: 74
diff changeset
   902
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   903
		load_index = SPR_OPENTTD_BASE + OPENTTD_SPRITES_COUNT + 1;
142
0c2bd172af05 (svn r143) Fix: newgrfs are working again
dominik
parents: 74
diff changeset
   904
368
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   905
32aad6ad36e1 (svn r556) -newgrf: Some seemingly proper support for loading stages of grf files (octo & pasky).
darkvater
parents: 366
diff changeset
   906
		/* Load newgrf sprites */
1198
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   907
		// in each loading stage, (try to) open each file specified in the config and load information from it.
392
3657d06c6564 (svn r584) -newgrf: Increase chance to get a TTDPatch savegame using custom GRF files loaded (pasky)
darkvater
parents: 368
diff changeset
   908
		_custom_sprites_base = load_index;
1198
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   909
		for (_loading_stage = 0; _loading_stage < 2; _loading_stage++) {
1250
1b8d4b11aab3 (svn r1754) - Fix: you can once again load newgrf files with lots of sprites. Index wasn't reset in second run, thus counting them double.
darkvater
parents: 1198
diff changeset
   910
			load_index = _custom_sprites_base;
1198
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   911
			for (j = 0; j != lengthof(_newgrf_files) && _newgrf_files[j]; j++) {
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   912
				if ( !FiosCheckFileExists(_newgrf_files[j]) )
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   913
					continue;
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   914
				if (_loading_stage == 0)
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   915
					InitNewGRFFile(_newgrf_files[j], load_index);
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   916
				load_index += LoadNewGrfFile(_newgrf_files[j], load_index, i++);
010e5986ed74 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1093
diff changeset
   917
			}
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
   918
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   920
		// If needed, save the cache to file
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   921
		HandleCachedSpriteHeaders(_cached_filenames[_opt.landscape], false);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
	} else {
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   923
		// We have sprites cached. We just loaded the cached files
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   924
		//  now we only have to open a file-pointer to all the original grf-files
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   925
		// This is very important. Not all sprites are in the cache. So sometimes
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   926
		//  the game needs to load the sprite from disk. When the file is not
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   927
		//  open it can not read. So all files that are in the 'if'-section
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   928
		//  above should also be in this 'else'-section.
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   929
		//
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   930
		// NOTE: the order of the files must be identical as in the section above!!
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   931
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   932
		for(i = 0; files->basic[i].filename != NULL; i++)
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   933
			FioOpenFile(i,files->basic[i].filename);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   934
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   935
		FioOpenFile(i++, "openttd.grf");
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   936
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
		if (_sprite_page_to_load != 0)
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   938
			FioOpenFile(i++, files->landscape[_sprite_page_to_load-1].filename);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   939
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   940
		FioOpenFile(i++, "trkfoundw.grf");
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   941
		FioOpenFile(i++, "canalsw.grf");
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   942
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   943
		// FIXME: if a user changes his newgrf's, the cached-sprites gets
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   944
		//  invalid. We should have some kind of check for this.
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   945
		// The best solution for this is to delete the cached-sprites.. but how
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   946
		//  do we detect it?
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   947
		for(j=0; j!=lengthof(_newgrf_files) && _newgrf_files[j]; j++)
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   948
			FioOpenFile(i++, _newgrf_files[j]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
	_compact_cache_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
1352
5880ab4de192 (svn r1856) Make GfxInitSpriteMem() static, because it's only used withing spritecache.c
tron
parents: 1351
diff changeset
   954
static void GfxInitSpriteMem(byte *ptr, uint32 size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
	// initialize sprite cache heap
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
	_spritecache_ptr = ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
	_spritecache_size = size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
	// Sentinel block (identified by size=0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
	S_DATA(ptr + size - S_HDRSIZE) = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
	// A big free block
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
	S_DATA(ptr) = size - S_HDRSIZE + S_FREE_MASK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
	memset(_sprite_ptr, 0, sizeof(_sprite_ptr));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
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
   969
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
   970
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
	static byte *_sprite_mem;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   972
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
	// 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
   974
	if (_sprite_page_to_load != _opt.landscape) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
		_sprite_page_to_load = _opt.landscape;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
		// Sprite cache
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
		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
   979
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
		// Reuse existing memory?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
		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
   982
		GfxInitSpriteMem(_sprite_mem, SPRITE_CACHE_SIZE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
		LoadSpriteTables();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
		GfxInitPalettes();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
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
   989
const SpriteDimension *GetSpriteDimension(SpriteID sprite)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
	static SpriteDimension sd_static;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
	SpriteDimension *sd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
#ifndef WANT_SPRITESIZES
1348
1d123409026e (svn r1852) Start cleaning up sprite handling:
tron
parents: 1329
diff changeset
   995
	const Sprite* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
	p = _sprite_ptr[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
	if (p == NULL)
1350
067b22970f19 (svn r1854) Split GetSpritePtr() into GetSprite() for regular sprites (returning a Sprite*) and GetNonSprite() for "sprites" of type 0xFF (returning byte*)
tron
parents: 1349
diff changeset
   999
		p = GetSprite(sprite);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
	/* decode sprite header */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
	sd = &sd_static;
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
  1003
	sd->xoffs = p->x_offs;
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
  1004
	sd->yoffs = p->y_offs;
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
  1005
	sd->xsize = p->width;
1348
1d123409026e (svn r1852) Start cleaning up sprite handling:
tron
parents: 1329
diff changeset
  1006
	sd->ysize = p->height;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
	sd = &sd_static;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
	sd->xoffs = _sprite_xoffs[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
	sd->yoffs = _sprite_yoffs[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
	sd->xsize = _sprite_xsize[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
	sd->ysize = _sprite_ysize[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
/*	sd->xoffs = _sprite_xoffs[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
	sd->yoffs = _sprite_yoffs[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
	sd->xsize = _sprite_xsize[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1017
	sd->ysize = _sprite_ysize[sprite];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
*/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
	return sd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
  1021