spritecache.c
author peter1138
Thu, 20 Apr 2006 05:57:47 +0000
changeset 3591 a5febb93bc11
parent 3565 ef0a9ef56fa0
child 4321 958a8e9c012b
permissions -rw-r--r--
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1844
diff changeset
     4
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1250
diff changeset
     5
#include "debug.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     6
#include "functions.h"
2548
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2407
diff changeset
     7
#include "macros.h"
1349
15979a2e9001 (svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents: 1348
diff changeset
     8
#include "spritecache.h"
1363
775a7ee52369 (svn r1867) Include tables/sprites.h only in files which need it
tron
parents: 1361
diff changeset
     9
#include "table/sprites.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "fileio.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
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
#define WANT_NEW_LRU
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    17
static void* _sprite_ptr[MAX_SPRITES];
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    18
static uint32 _sprite_file_pos[MAX_SPRITES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#if defined(WANT_NEW_LRU)
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    21
static int16 _sprite_lru_new[MAX_SPRITES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#else
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    23
static uint16 _sprite_lru[MAX_SPRITES];
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    24
static uint16 _sprite_lru_cur[MAX_SPRITES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    27
typedef struct MemBlock {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    28
	uint32 size;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    29
	byte data[VARARRAY_SIZE];
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    30
} MemBlock;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    31
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
static uint _sprite_lru_counter;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    33
static MemBlock *_spritecache_ptr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
static int _compact_cache_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
    36
static void CompactSpriteCache(void);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
2342
aae24c9661ba (svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents: 2340
diff changeset
    38
static bool ReadSpriteHeaderSkipData(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
{
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
    40
	uint16 num = FioReadWord();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
	byte type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
    43
	if (num == 0) return false;
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
    44
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
	type = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
	if (type == 0xFF) {
2342
aae24c9661ba (svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents: 2340
diff changeset
    47
		FioSkipBytes(num);
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
    48
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
    50
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	FioSkipBytes(7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	num -= 8;
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
    53
	if (num == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	if (type & 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
		FioSkipBytes(num);
1355
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    57
	} else {
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    58
		while (num > 0) {
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    59
			int8 i = FioReadByte();
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    60
			if (i >= 0) {
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    61
				num -= i;
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    62
				FioSkipBytes(i);
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    63
			} else {
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    64
				i = -(i >> 3);
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    65
				num -= i;
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    66
				FioReadByte();
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    67
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
	}
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
    70
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
    71
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
3565
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    74
/* Check if the given Sprite ID exists */
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    75
bool SpriteExists(SpriteID id)
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    76
{
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    77
	/* Special case for Sprite ID zero -- its position is also 0... */
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    78
	return _sprite_file_pos[id] != 0 || id == 0;
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    79
}
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    80
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
    81
static void* AllocSprite(size_t);
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
    82
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
    83
static void* ReadSprite(SpriteID id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
{
2321
75be68dfaaf2 (svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents: 2319
diff changeset
    85
	uint num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
	byte type;
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
    87
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
    88
	DEBUG(spritecache, 9) ("load sprite %d", id);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
    89
3565
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
    90
	if (!SpriteExists(id)) {
1378
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
    91
		error(
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
    92
			"Tried to load non-existing sprite #%d.\n"
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
    93
			"Probable cause: Wrong/missing NewGRFs",
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
    94
			id
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
    95
		);
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
    96
	}
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
    97
1354
4f9416233d06 (svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents: 1353
diff changeset
    98
	FioSeekToFile(_sprite_file_pos[id]);
4f9416233d06 (svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents: 1353
diff changeset
    99
2321
75be68dfaaf2 (svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents: 2319
diff changeset
   100
	num  = FioReadWord();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
	type = FioReadByte();
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   102
	if (type == 0xFF) {
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   103
		byte* dest = AllocSprite(num);
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   104
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   105
		_sprite_ptr[id] = dest;
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   106
		FioReadBlock(dest, num);
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   107
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   108
		return dest;
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   109
	} else {
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   110
		uint height = FioReadByte();
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   111
		uint width  = FioReadWord();
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   112
		Sprite* sprite;
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   113
		byte* dest;
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   114
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   115
		num = (type & 0x02) ? width * height : num - 8;
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   116
		sprite = AllocSprite(sizeof(*sprite) + num);
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   117
		_sprite_ptr[id] = sprite;
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   118
		sprite->info   = type;
2015
9b872f3a0c2b (svn r2523) Readd a comment which got lost in r2522
tron
parents: 2014
diff changeset
   119
		sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   120
		sprite->width  = width;
1351
a08ab043dd08 (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   121
		sprite->x_offs = FioReadWord();
a08ab043dd08 (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   122
		sprite->y_offs = FioReadWord();
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   123
1351
a08ab043dd08 (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   124
		dest = sprite->data;
1355
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   125
		while (num > 0) {
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   126
			int8 i = FioReadByte();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
1355
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   128
			if (i >= 0) {
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   129
				num -= i;
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   130
				for (; i > 0; --i) *dest++ = FioReadByte();
1355
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   131
			} else {
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   132
				const byte* rel = dest - (((i & 7) << 8) | FioReadByte());
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   133
1355
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   134
				i = -(i >> 3);
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   135
				num -= i;
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   136
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   137
				for (; i > 0; --i) *dest++ = *rel++;
1355
fbb8ffafb25a (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   138
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
		}
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   140
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   141
		return sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
	}
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
2340
e18ef06bc59a (svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents: 2339
diff changeset
   146
bool LoadNextSprite(int load_index, byte file_index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
{
2321
75be68dfaaf2 (svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents: 2319
diff changeset
   148
	uint32 file_pos = FioGetPos() | (file_index << 24);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
2342
aae24c9661ba (svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents: 2340
diff changeset
   150
	if (!ReadSpriteHeaderSkipData()) return false;
361
95307fb1c3f8 (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
   151
3591
a5febb93bc11 (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   152
	if (load_index >= MAX_SPRITES) {
a5febb93bc11 (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   153
		error("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
a5febb93bc11 (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   154
	}
a5febb93bc11 (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   155
361
95307fb1c3f8 (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
   156
	_sprite_file_pos[load_index] = file_pos;
95307fb1c3f8 (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
   157
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
	_sprite_ptr[load_index] = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
	_sprite_lru_new[load_index] = 0;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   162
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
	_sprite_lru[load_index] = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
	_sprite_lru_cur[load_index] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
#endif
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
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
2407
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   170
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   171
void DupSprite(SpriteID old, SpriteID new)
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   172
{
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   173
	_sprite_file_pos[new] = _sprite_file_pos[old];
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   174
	_sprite_ptr[new] = NULL;
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   175
}
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   176
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   177
2340
e18ef06bc59a (svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents: 2339
diff changeset
   178
void SkipSprites(uint count)
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   179
{
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
   180
	for (; count > 0; --count) {
2342
aae24c9661ba (svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents: 2340
diff changeset
   181
		if (!ReadSpriteHeaderSkipData()) return;
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   182
	}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   183
}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   184
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
#define S_FREE_MASK 1
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   187
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   188
static inline MemBlock* NextBlock(MemBlock* block)
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   189
{
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   190
	return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   191
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   193
static uint32 GetSpriteCacheUsage(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
{
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   195
	size_t tot_size = 0;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   196
	MemBlock* s;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   197
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   198
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s))
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   199
		if (!(s->size & S_FREE_MASK)) tot_size += s->size;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
	return tot_size;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   205
void IncreaseSpriteLRU(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
	// Increase all LRU values
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
	if (_sprite_lru_counter > 16384) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
		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
   213
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   214
		for (i = 0; i != MAX_SPRITES; i++)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
			if (_sprite_ptr[i] != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
				if (_sprite_lru_new[i] >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
					_sprite_lru_new[i] = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
				} else if (_sprite_lru_new[i] != -32768) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
					_sprite_lru_new[i]--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
		_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
#else
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   225
	for (i = 0; i != MAX_SPRITES; i++)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
		if (_sprite_ptr[i] != NULL && _sprite_lru[i] != 65535)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
			_sprite_lru[i]++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
	// Reset the lru counter.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
	_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
	// Compact sprite cache every now and then.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
	if (++_compact_cache_counter >= 740) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
		CompactSpriteCache();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
		_compact_cache_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
// 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
   240
// That is accomplished by moving the cached data.
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   241
static void CompactSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
{
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   243
	MemBlock *s;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   244
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   245
	DEBUG(spritecache, 2) (
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   246
		"compacting sprite cache, inuse=%d", GetSpriteCacheUsage()
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   247
	);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   248
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   249
	for (s = _spritecache_ptr; s->size != 0;) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   250
		if (s->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   251
			MemBlock* next = NextBlock(s);
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   252
			MemBlock temp;
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   253
			void** i;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   254
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
			// Since free blocks are automatically coalesced, this should hold true.
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   256
			assert(!(next->size & S_FREE_MASK));
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   257
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
			// If the next block is the sentinel block, we can safely return
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   259
			if (next->size == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   262
			// Locate the sprite belonging to the next pointer.
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   263
			for (i = _sprite_ptr; *i != next->data; ++i) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   264
				assert(i != endof(_sprite_ptr));
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   265
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   267
			*i = s->data; // Adjust sprite array entry
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   268
			// Swap this and the next block
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   269
			temp = *s;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   270
			memmove(s, next, next->size);
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   271
			s = NextBlock(s);
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   272
			*s = temp;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   273
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   274
			// Coalesce free blocks
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   275
			while (NextBlock(s)->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   276
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
			}
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   278
		} else {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   279
			s = NextBlock(s);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1070
diff changeset
   284
static void DeleteEntryFromSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
	int best = -1;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   288
	MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
	int cur_lru;
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
	DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	cur_lru = 0xffff;
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   295
	for (i = 0; i != MAX_SPRITES; i++) {
2305
c0b38090ec7a (svn r2829) Remove sprite locking, it was never used anyway
tron
parents: 2244
diff changeset
   296
		if (_sprite_ptr[i] != NULL && _sprite_lru_new[i] < cur_lru) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
			cur_lru = _sprite_lru_new[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
	uint16 cur_lru = 0, cur_lru_cur = 0xffff;
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   304
	for (i = 0; i != MAX_SPRITES; i++) {
2305
c0b38090ec7a (svn r2829) Remove sprite locking, it was never used anyway
tron
parents: 2244
diff changeset
   305
		if (_sprite_ptr[i] == NULL || _sprite_lru[i] < cur_lru) continue;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   306
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
		// 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
   308
		if (_sprite_lru[i] != cur_lru) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
			cur_lru = _sprite_lru[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
		// 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
   313
		} 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
   314
			cur_lru_cur = _sprite_lru_cur[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
			cur_lru = _sprite_lru[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
	// 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
   323
	// 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
   324
	if (best == -1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
		error("Out of sprite memory");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
	// Mark the block as free (the block must be in use)
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   328
	s = (MemBlock*)_sprite_ptr[best] - 1;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   329
	assert(!(s->size & S_FREE_MASK));
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   330
	s->size |= S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
	_sprite_ptr[best] = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
	// And coalesce adjacent free blocks
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   334
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   335
		if (s->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   336
			while (NextBlock(s)->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   337
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
	}
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
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   343
static void* AllocSprite(size_t mem_req)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
{
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   345
	mem_req += sizeof(MemBlock);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   347
	/* Align this to an uint32 boundary. This also makes sure that the 2 least
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   348
	 * bits are not used, so we could use those for other things. */
2548
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2407
diff changeset
   349
	mem_req = ALIGN(mem_req, sizeof(uint32));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   351
	for (;;) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   352
		MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   354
		for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   355
			if (s->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   356
				size_t cur_size = s->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   358
				/* Is the block exactly the size we need or
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   359
				 * big enough for an additional free block? */
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   360
				if (cur_size == mem_req ||
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   361
						cur_size >= mem_req + sizeof(MemBlock)) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   362
					// Set size and in use
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   363
					s->size = mem_req;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   364
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   365
					// Do we need to inject a free block too?
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   366
					if (cur_size != mem_req) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   367
						NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   368
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   370
					return s->data;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   371
				}
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   372
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   375
		// Reached sentinel, but no block found yet. Delete some old entry.
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   376
		DeleteEntryFromSpriteCache();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
	}
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
#if defined(NEW_ROTATION)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
#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
   382
#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
   383
#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
   384
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
410
0efd84450b01 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 392
diff changeset
   386
static uint RotateSprite(uint s)
0
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
	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
   389
	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
   390
	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
   391
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
	if (0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
	X19(752)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
	X15(990-1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
	X19(3924)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
	X19(3943)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
	X19(3962)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
	X19(3981)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
	X19(4000)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	X19(4023)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
	X19(4042)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
	MAP(4061,4069,_coast_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
	X19(4126)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
	X19(4145)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
	X19(4164)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
	X19(4183)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
	X19(4202)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
	X19(4221)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
	X19(4240)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
	X19(4259)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
	X19(4259)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
	X19(4278)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
	MAP(4090, 4095, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
	MAP(4096, 4101, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
	MAP(4102, 4107, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
	MAP(4108, 4113, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
	MAP(4114, 4119, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
	MAP(4120, 4125, _fence_map)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
	return s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
1361
5833194df433 (svn r1865) Fix some warnings
tron
parents: 1357
diff changeset
   423
const void *GetRawSprite(SpriteID sprite)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
{
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   425
	void* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   427
	assert(sprite < MAX_SPRITES);
0
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
#if defined(NEW_ROTATION)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
	sprite = RotateSprite(sprite);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
#endif
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
	// Update LRU
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
#if defined(WANT_NEW_LRU)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
	_sprite_lru_new[sprite] = ++_sprite_lru_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
#else
2026
567e3bc9af72 (svn r2535) Tabs
tron
parents: 2015
diff changeset
   437
	_sprite_lru_cur[sprite] = ++_sprite_lru_counter;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
	_sprite_lru[sprite] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
	p = _sprite_ptr[sprite];
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   442
	// Load the sprite, if it is not loaded, yet
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   443
	if (p == NULL) p = ReadSprite(sprite);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   444
	return p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
961
e9abf6f087f8 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   447
2340
e18ef06bc59a (svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents: 2339
diff changeset
   448
void GfxInitSpriteMem(void)
0
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
	// initialize sprite cache heap
2339
b03b1fb8ccfe (svn r2865) Push the responsibility for allocating the sprite heap into GfxInitSpriteMem()
tron
parents: 2329
diff changeset
   451
	if (_spritecache_ptr == NULL) _spritecache_ptr = malloc(SPRITE_CACHE_SIZE);
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
	// A big free block
2339
b03b1fb8ccfe (svn r2865) Push the responsibility for allocating the sprite heap into GfxInitSpriteMem()
tron
parents: 2329
diff changeset
   454
	_spritecache_ptr->size = (SPRITE_CACHE_SIZE - sizeof(MemBlock)) | S_FREE_MASK;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   455
	// Sentinel block (identified by size == 0)
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   456
	NextBlock(_spritecache_ptr)->size = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	memset(_sprite_ptr, 0, sizeof(_sprite_ptr));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
2340
e18ef06bc59a (svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents: 2339
diff changeset
   460
	_compact_cache_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
}