src/spritecache.cpp
author rubidium
Thu, 08 Feb 2007 14:02:12 +0000
changeset 6213 164d84e1c5b4
parent 5971 55554e8989ad
child 6298 c30fe89622df
child 6573 7624f942237f
permissions -rw-r--r--
(svn r8634) -Codechange: replace missing sprites with a red question mark.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1844
diff changeset
     4
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1250
diff changeset
     5
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     6
#include "functions.h"
2548
97ada3bd2702 (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
07514c2cc6d1 (svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents: 1348
diff changeset
     8
#include "spritecache.h"
1363
01d3de5d8039 (svn r1867) Include tables/sprites.h only in files which need it
tron
parents: 1361
diff changeset
     9
#include "table/sprites.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "fileio.h"
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    11
#include "helpers.hpp"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
5971
55554e8989ad (svn r8218) -Codechange: Increase spritecache size to 2MB. This will vastly improve performance when using (lots of) grf files. You can change it yourself easily with the SPRITE_CACHE_SIZE compile flag. (peter1138).
Darkvater
parents: 5953
diff changeset
    13
#ifndef SPRITE_CACHE_SIZE
55554e8989ad (svn r8218) -Codechange: Increase spritecache size to 2MB. This will vastly improve performance when using (lots of) grf files. You can change it yourself easily with the SPRITE_CACHE_SIZE compile flag. (peter1138).
Darkvater
parents: 5953
diff changeset
    14
# define SPRITE_CACHE_SIZE 2*1024*1024
55554e8989ad (svn r8218) -Codechange: Increase spritecache size to 2MB. This will vastly improve performance when using (lots of) grf files. You can change it yourself easily with the SPRITE_CACHE_SIZE compile flag. (peter1138).
Darkvater
parents: 5953
diff changeset
    15
#endif /* SPRITE_CACHE_SIZE */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    18
typedef struct SpriteCache {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    19
	void *ptr;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    20
	uint32 file_pos;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    21
	int16 lru;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    22
} SpriteCache;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    23
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    24
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    25
static uint _spritecache_items = 0;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    26
static SpriteCache *_spritecache = NULL;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    27
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    28
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    29
static inline SpriteCache *GetSpriteCache(uint index)
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    30
{
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    31
	return &_spritecache[index];
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    32
}
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    33
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    34
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    35
static SpriteCache *AllocateSpriteCache(uint index)
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    36
{
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    37
	if (index >= _spritecache_items) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    38
		/* Add another 1024 items to the 'pool' */
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    39
		uint items = ALIGN(index + 1, 1024);
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    40
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    41
		DEBUG(sprite, 4, "Increasing sprite cache to %d items (%d bytes)", items, items * sizeof(*_spritecache));
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    42
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
    43
		_spritecache = ReallocT(_spritecache, items);
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    44
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    45
		if (_spritecache == NULL) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    46
			error("Unable to allocate sprite cache of %d items (%d bytes)", items, items * sizeof(*_spritecache));
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    47
		}
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    48
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    49
		/* Reset the new items and update the count */
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    50
		memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    51
		_spritecache_items = items;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    52
	}
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    53
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    54
	return GetSpriteCache(index);
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    55
}
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
    56
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    58
typedef struct MemBlock {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    59
	uint32 size;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    60
	byte data[VARARRAY_SIZE];
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    61
} MemBlock;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    62
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
static uint _sprite_lru_counter;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    64
static MemBlock *_spritecache_ptr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
static int _compact_cache_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
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
    67
static void CompactSpriteCache(void);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
2342
c19fb4f2df30 (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
    69
static bool ReadSpriteHeaderSkipData(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
{
2329
f68428464540 (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
	uint16 num = FioReadWord();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
	byte type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
2329
f68428464540 (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
    74
	if (num == 0) return false;
f68428464540 (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
    75
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
	type = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
	if (type == 0xFF) {
2342
c19fb4f2df30 (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
    78
		FioSkipBytes(num);
5150
247343201280 (svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents: 4522
diff changeset
    79
		/* Some NewGRF files have "empty" pseudo-sprites which are 1
247343201280 (svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents: 4522
diff changeset
    80
		 * byte long. Catch these so the sprites won't be displayed. */
247343201280 (svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents: 4522
diff changeset
    81
		return num != 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
    83
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
	FioSkipBytes(7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
	num -= 8;
2329
f68428464540 (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
    86
	if (num == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
	if (type & 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
		FioSkipBytes(num);
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    90
	} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    91
		while (num > 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    92
			int8 i = FioReadByte();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    93
			if (i >= 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    94
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    95
				FioSkipBytes(i);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    96
			} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    97
				i = -(i >> 3);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    98
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    99
				FioReadByte();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   100
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
	}
2329
f68428464540 (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
   103
f68428464540 (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
   104
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
3565
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   107
/* Check if the given Sprite ID exists */
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   108
bool SpriteExists(SpriteID id)
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   109
{
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   110
	/* Special case for Sprite ID zero -- its position is also 0... */
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   111
	if (id == 0) return true;
5953
7000c5bdd70b (svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents: 5860
diff changeset
   112
	if (id >= _spritecache_items) return false;
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   113
	return GetSpriteCache(id)->file_pos != 0;
3565
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   114
}
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   115
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   116
static void* AllocSprite(size_t);
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   117
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   118
static void* ReadSprite(SpriteCache *sc, SpriteID id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
{
2321
455694cdbada (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
   120
	uint num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	byte type;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   122
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   123
	DEBUG(sprite, 9, "Load sprite %d", id);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   124
3565
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   125
	if (!SpriteExists(id)) {
6213
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   126
		DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", id);
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   127
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   128
		/* SPR_IMG_QUERY is a BIG FAT RED ? */
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   129
		id = SPR_IMG_QUERY;
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   130
		sc = GetSpriteCache(SPR_IMG_QUERY);
1378
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   131
	}
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   132
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   133
	FioSeekToFile(sc->file_pos);
1354
5e5c89b9b169 (svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents: 1353
diff changeset
   134
2321
455694cdbada (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
   135
	num  = FioReadWord();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
	type = FioReadByte();
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   137
	if (type == 0xFF) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   138
		byte* dest = (byte*)AllocSprite(num);
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   139
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   140
		sc->ptr = dest;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   141
		FioReadBlock(dest, num);
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   142
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   143
		return dest;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   144
	} else {
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   145
		uint height = FioReadByte();
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   146
		uint width  = FioReadWord();
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   147
		Sprite* sprite;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   148
		byte* dest;
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   149
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   150
		num = (type & 0x02) ? width * height : num - 8;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   151
		sprite = (Sprite*)AllocSprite(sizeof(*sprite) + num);
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   152
		sc->ptr = sprite;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   153
		sprite->info   = type;
2015
3eec709611f8 (svn r2523) Readd a comment which got lost in r2522
tron
parents: 2014
diff changeset
   154
		sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   155
		sprite->width  = width;
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   156
		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
   157
		sprite->y_offs = FioReadWord();
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   158
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   159
		dest = sprite->data;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   160
		while (num > 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   161
			int8 i = FioReadByte();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   163
			if (i >= 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   164
				num -= i;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   165
				for (; i > 0; --i) *dest++ = FioReadByte();
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   166
			} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   167
				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
   168
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   169
				i = -(i >> 3);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   170
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   171
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   172
				for (; i > 0; --i) *dest++ = *rel++;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   173
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
		}
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   175
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   176
		return sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
2340
0a9f3eeccb96 (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
   181
bool LoadNextSprite(int load_index, byte file_index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
{
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   183
	SpriteCache *sc;
2321
455694cdbada (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
   184
	uint32 file_pos = FioGetPos() | (file_index << 24);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
2342
c19fb4f2df30 (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
   186
	if (!ReadSpriteHeaderSkipData()) return false;
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
   187
3591
05a4b88d875f (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   188
	if (load_index >= MAX_SPRITES) {
05a4b88d875f (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   189
		error("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
05a4b88d875f (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   190
	}
05a4b88d875f (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   191
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   192
	sc = AllocateSpriteCache(load_index);
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   193
	sc->file_pos = file_pos;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   194
	sc->ptr = NULL;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   195
	sc->lru = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
2407
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   200
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   201
void DupSprite(SpriteID old_spr, SpriteID new_spr)
2407
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   202
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   203
	SpriteCache *scold = GetSpriteCache(old_spr);
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   204
	SpriteCache *scnew = AllocateSpriteCache(new_spr);
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   205
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   206
	scnew->file_pos = scold->file_pos;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   207
	scnew->ptr = NULL;
2407
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   208
}
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   209
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   210
2340
0a9f3eeccb96 (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
   211
void SkipSprites(uint count)
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   212
{
2329
f68428464540 (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
   213
	for (; count > 0; --count) {
2342
c19fb4f2df30 (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
   214
		if (!ReadSpriteHeaderSkipData()) return;
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   215
	}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   216
}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   217
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
#define S_FREE_MASK 1
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   220
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   221
static inline MemBlock* NextBlock(MemBlock* block)
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   222
{
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   223
	return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   224
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
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
   226
static uint32 GetSpriteCacheUsage(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
{
4321
b763b7007162 (svn r5974) -Codechange: added casts all around the place to make Windows 64bit happy (michi_cc)
truelight
parents: 3591
diff changeset
   228
	uint32 tot_size = 0;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   229
	MemBlock* s;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   230
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   231
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s))
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   232
		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
   233
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
	return tot_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
}
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
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
   238
void IncreaseSpriteLRU(void)
0
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
	// Increase all LRU values
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
	if (_sprite_lru_counter > 16384) {
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   242
		SpriteID i;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   243
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   244
		DEBUG(sprite, 3, "Fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   246
		for (i = 0; i != _spritecache_items; i++) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   247
			SpriteCache *sc = GetSpriteCache(i);
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   248
			if (sc->ptr != NULL) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   249
				if (sc->lru >= 0) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   250
					sc->lru = -1;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   251
				} else if (sc->lru != -32768) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   252
					sc->lru--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
			}
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   255
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
		_sprite_lru_counter = 0;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
	// Compact sprite cache every now and then.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
	if (++_compact_cache_counter >= 740) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
		CompactSpriteCache();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
		_compact_cache_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
// 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
   267
// 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
   268
static void CompactSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
{
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   270
	MemBlock *s;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   271
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   272
	DEBUG(sprite, 3, "Compacting sprite cache, inuse=%d", GetSpriteCacheUsage());
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   273
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   274
	for (s = _spritecache_ptr; s->size != 0;) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   275
		if (s->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   276
			MemBlock* next = NextBlock(s);
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   277
			MemBlock temp;
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   278
			SpriteID i;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   279
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
			// Since free blocks are automatically coalesced, this should hold true.
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   281
			assert(!(next->size & S_FREE_MASK));
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   282
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
			// If the next block is the sentinel block, we can safely return
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   284
			if (next->size == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   287
			// Locate the sprite belonging to the next pointer.
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   288
			for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   289
				assert(i != _spritecache_items);
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   290
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   292
			GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   293
			// Swap this and the next block
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   294
			temp = *s;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   295
			memmove(s, next, next->size);
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   296
			s = NextBlock(s);
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   297
			*s = temp;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   298
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   299
			// Coalesce free blocks
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   300
			while (NextBlock(s)->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   301
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
			}
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   303
		} else {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   304
			s = NextBlock(s);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
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
   309
static void DeleteEntryFromSpriteCache(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
{
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   311
	SpriteID i;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   312
	uint best = UINT_MAX;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   313
	MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
	int cur_lru;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   316
	DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
0
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
	cur_lru = 0xffff;
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   319
	for (i = 0; i != _spritecache_items; i++) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   320
		SpriteCache *sc = GetSpriteCache(i);
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   321
		if (sc->ptr != NULL && sc->lru < cur_lru) {
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   322
			cur_lru = sc->lru;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
	}
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
	// 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
   328
	// This shouldn't really happen, unless all sprites are locked.
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   329
	if (best == (uint)-1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
		error("Out of sprite memory");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
	// Mark the block as free (the block must be in use)
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   333
	s = (MemBlock*)GetSpriteCache(best)->ptr - 1;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   334
	assert(!(s->size & S_FREE_MASK));
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   335
	s->size |= S_FREE_MASK;
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   336
	GetSpriteCache(best)->ptr = NULL;
0
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
	// And coalesce adjacent free blocks
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   339
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   340
		if (s->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   341
			while (NextBlock(s)->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   342
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   348
static void* AllocSprite(size_t mem_req)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
{
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   350
	mem_req += sizeof(MemBlock);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   352
	/* Align this to an uint32 boundary. This also makes sure that the 2 least
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   353
	 * bits are not used, so we could use those for other things. */
2548
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2407
diff changeset
   354
	mem_req = ALIGN(mem_req, sizeof(uint32));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   356
	for (;;) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   357
		MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   359
		for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   360
			if (s->size & S_FREE_MASK) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   361
				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
   362
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   363
				/* Is the block exactly the size we need or
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   364
				 * big enough for an additional free block? */
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   365
				if (cur_size == mem_req ||
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   366
						cur_size >= mem_req + sizeof(MemBlock)) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   367
					// Set size and in use
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   368
					s->size = mem_req;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   369
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   370
					// Do we need to inject a free block too?
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   371
					if (cur_size != mem_req) {
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   372
						NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   373
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   375
					return s->data;
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   376
				}
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   377
			}
0
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
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   380
		// Reached sentinel, but no block found yet. Delete some old entry.
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   381
		DeleteEntryFromSpriteCache();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
1361
10d9b95e7a81 (svn r1865) Fix some warnings
tron
parents: 1357
diff changeset
   386
const void *GetRawSprite(SpriteID sprite)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
{
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   388
	SpriteCache *sc;
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   389
	void* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
5953
7000c5bdd70b (svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents: 5860
diff changeset
   391
	assert(sprite < _spritecache_items);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   393
	sc = GetSpriteCache(sprite);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   395
	// Update LRU
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   396
	sc->lru = ++_sprite_lru_counter;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   397
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   398
	p = sc->ptr;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   399
2014
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   400
	// Load the sprite, if it is not loaded, yet
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   401
	if (p == NULL) p = ReadSprite(sc, sprite);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   402
	return p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   405
2340
0a9f3eeccb96 (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
   406
void GfxInitSpriteMem(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
	// initialize sprite cache heap
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   409
	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(SPRITE_CACHE_SIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
	// A big free block
2339
1c64119d5a3b (svn r2865) Push the responsibility for allocating the sprite heap into GfxInitSpriteMem()
tron
parents: 2329
diff changeset
   412
	_spritecache_ptr->size = (SPRITE_CACHE_SIZE - sizeof(MemBlock)) | S_FREE_MASK;
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   413
	// Sentinel block (identified by size == 0)
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   414
	NextBlock(_spritecache_ptr)->size = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
5755
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   416
	/* Reset the spritecache 'pool' */
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   417
	free(_spritecache);
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   418
	_spritecache_items = 0;
5db1cb757991 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5752
diff changeset
   419
	_spritecache = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
2340
0a9f3eeccb96 (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
   421
	_compact_cache_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
}