src/spritecache.cpp
author KUDr
Sat, 21 Apr 2007 08:23:57 +0000
branchcpp_gui
changeset 6308 646711c5feaa
parent 6307 f40e88cff863
child 9912 1ac8aac92385
permissions -rw-r--r--
(svn r9708) [cpp_gui] -Sync with trunk (r9633:9707)
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
6308
646711c5feaa (svn r9708) [cpp_gui] -Sync with trunk (r9633:9707)
KUDr
parents: 6307
diff changeset
     3
/** @file spritecache.cpp */
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1844
diff changeset
     6
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1250
diff changeset
     7
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#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
     9
#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
    10
#include "spritecache.h"
1363
01d3de5d8039 (svn r1867) Include tables/sprites.h only in files which need it
tron
parents: 1361
diff changeset
    11
#include "table/sprites.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#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
    13
#include "helpers.hpp"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
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
    15
#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
    16
# 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
    17
#endif /* SPRITE_CACHE_SIZE */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
    20
struct SpriteCache {
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
    21
	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
    22
	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
    23
	int16 lru;
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
    24
};
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
    25
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
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
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
    28
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
    29
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
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
    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
	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
    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
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
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
    38
{
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
	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
    40
		/* 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
    41
		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
    42
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
    43
		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
    44
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
    45
		_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
    46
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
		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
    48
			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
    49
		}
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
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
		/* 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
    52
		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
    53
		_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
    54
	}
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
	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
    57
}
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
    58
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
    60
struct MemBlock {
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
    61
	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
    62
	byte data[VARARRAY_SIZE];
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
    63
};
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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
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
    66
static MemBlock *_spritecache_ptr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
static int _compact_cache_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
    69
static void CompactSpriteCache();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
    71
static bool ReadSpriteHeaderSkipData()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
{
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
    73
	uint16 num = FioReadWord();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
	byte type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
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
    76
	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
    77
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
	type = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	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
    80
		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
    81
		/* 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
    82
		 * 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
    83
		return num != 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
    85
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
	FioSkipBytes(7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
	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
    88
	if (num == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
	if (type & 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
		FioSkipBytes(num);
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    92
	} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    93
		while (num > 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    94
			int8 i = FioReadByte();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    95
			if (i >= 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    96
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    97
				FioSkipBytes(i);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    98
			} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
    99
				i = -(i >> 3);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   100
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   101
				FioReadByte();
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   102
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
	}
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
   105
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
   106
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
3565
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   109
/* Check if the given Sprite ID exists */
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   110
bool SpriteExists(SpriteID id)
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   111
{
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   112
	/* 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
   113
	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
   114
	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
   115
	return GetSpriteCache(id)->file_pos != 0;
3565
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   116
}
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   117
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
   118
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
   119
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
   120
static void* ReadSprite(SpriteCache *sc, SpriteID id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
{
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
   122
	uint num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
	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
   124
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   125
	DEBUG(sprite, 9, "Load sprite %d", id);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   126
3565
03d870cc3dcd (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   127
	if (!SpriteExists(id)) {
6213
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   128
		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
   129
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   130
		/* 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
   131
		id = SPR_IMG_QUERY;
164d84e1c5b4 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5971
diff changeset
   132
		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
   133
	}
ebb8d52f0352 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   134
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
   135
	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
   136
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
   137
	num  = FioReadWord();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
	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
   139
	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
   140
		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
   141
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
   142
		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
   143
		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
   144
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
		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
   146
	} 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
   147
		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
   148
		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
   149
		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
   150
		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
   151
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
   152
		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
   153
		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
   154
		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
   155
		sprite->info   = type;
2015
3eec709611f8 (svn r2523) Readd a comment which got lost in r2522
tron
parents: 2014
diff changeset
   156
		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
   157
		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
   158
		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
   159
		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
   160
1351
3e7aa0d35f8f (svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents: 1350
diff changeset
   161
		dest = sprite->data;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   162
		while (num > 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   163
			int8 i = FioReadByte();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   165
			if (i >= 0) {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   166
				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
   167
				for (; i > 0; --i) *dest++ = FioReadByte();
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   168
			} else {
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   169
				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
   170
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   171
				i = -(i >> 3);
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   172
				num -= i;
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   173
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
   174
				for (; i > 0; --i) *dest++ = *rel++;
1355
aa6c2b776727 (svn r1859) Miscellaneous style changes
tron
parents: 1354
diff changeset
   175
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		}
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
   177
0230ed9186bc (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   178
		return sprite;
0
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
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
   183
bool LoadNextSprite(int load_index, byte file_index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
{
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
   185
	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
   186
	uint32 file_pos = FioGetPos() | (file_index << 24);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
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
   188
	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
   189
3591
05a4b88d875f (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   190
	if (load_index >= MAX_SPRITES) {
05a4b88d875f (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   191
		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
   192
	}
05a4b88d875f (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   193
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
   194
	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
   195
	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
   196
	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
   197
	sc->lru = 0;
0
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
	return true;
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
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
void DupSprite(SpriteID old_spr, SpriteID new_spr)
2407
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   204
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   205
	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
   206
	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
   207
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
   208
	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
   209
	scnew->ptr = NULL;
2407
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   210
}
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   211
983f1d8a0614 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   212
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
   213
void SkipSprites(uint count)
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   214
{
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
   215
	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
   216
		if (!ReadSpriteHeaderSkipData()) return;
37
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   217
	}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   218
}
61bf1df68d82 (svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents: 0
diff changeset
   219
0
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
#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
   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
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
   224
{
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
   225
	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
   226
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
   228
static uint32 GetSpriteCacheUsage()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
{
4321
b763b7007162 (svn r5974) -Codechange: added casts all around the place to make Windows 64bit happy (michi_cc)
truelight
parents: 3591
diff changeset
   230
	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
   231
	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
   232
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
   233
	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
   234
		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
   235
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
	return tot_size;
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
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
   240
void IncreaseSpriteLRU()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
{
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   242
	/* Increase all LRU values */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
	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
   244
		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
   245
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   246
		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
   247
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
   248
		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
   249
			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
   250
			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
   251
				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
   252
					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
   253
				} 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
   254
					sc->lru--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
			}
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
   257
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
		_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   261
	/* Compact sprite cache every now and then. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
	if (++_compact_cache_counter >= 740) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
		CompactSpriteCache();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
		_compact_cache_counter = 0;
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   268
/** Called when holes in the sprite cache should be removed.
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   269
 * That is accomplished by moving the cached data. */
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
   270
static void CompactSpriteCache()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
{
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
   272
	MemBlock *s;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   273
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   274
	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
   275
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
   276
	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
   277
		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
   278
			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
   279
			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
   280
			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
   281
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   282
			/* 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
   283
			assert(!(next->size & S_FREE_MASK));
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   284
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   285
			/* 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
   286
			if (next->size == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   288
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   289
			/* 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
   290
			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
   291
				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
   292
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
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
   294
			GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   295
			/* Swap this and the next block */
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   296
			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
   297
			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
   298
			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
   299
			*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
   300
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   301
			/* Coalesce 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
   302
			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
   303
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
			}
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
   305
		} 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
   306
			s = NextBlock(s);
0
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
   311
static void DeleteEntryFromSpriteCache()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
{
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
   313
	SpriteID i;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   314
	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
   315
	MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
	int cur_lru;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   318
	DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
0
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
	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
   321
	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
   322
		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
   323
		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
   324
			cur_lru = sc->lru;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
			best = i;
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   329
	/* Display an error message and die, in case we found no sprite at all.
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   330
	 * 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
   331
	if (best == (uint)-1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
		error("Out of sprite memory");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   334
	/* 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
   335
	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
   336
	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
   337
	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
   338
	GetSpriteCache(best)->ptr = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   340
	/* 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
   341
	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
   342
		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
   343
			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
   344
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
}
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
static void* AllocSprite(size_t mem_req)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
{
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
   352
	mem_req += sizeof(MemBlock);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
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
   354
	/* 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
   355
	 * 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
   356
	mem_req = ALIGN(mem_req, sizeof(uint32));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
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
   358
	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
   359
		MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
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
   361
		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
   362
			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
   363
				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
   364
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
   365
				/* 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
   366
				 * 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
   367
				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
   368
						cur_size >= mem_req + sizeof(MemBlock)) {
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   369
					/* Set size and in use */
1353
48b59d472641 (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   370
					s->size = mem_req;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   371
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   372
					/* Do we need to inject a free block too? */
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
   373
					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
   374
						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
   375
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
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
   377
					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
   378
				}
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
   379
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   382
		/* Reached sentinel, but no block found yet. Delete some old 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
   383
		DeleteEntryFromSpriteCache();
0
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
1361
10d9b95e7a81 (svn r1865) Fix some warnings
tron
parents: 1357
diff changeset
   388
const void *GetRawSprite(SpriteID sprite)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
{
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
   390
	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
   391
	void* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
5953
7000c5bdd70b (svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents: 5860
diff changeset
   393
	assert(sprite < _spritecache_items);
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
	sc = GetSpriteCache(sprite);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   397
	/* Update LRU */
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
   398
	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
   399
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
   400
	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
   401
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   402
	/* 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
   403
	if (p == NULL) p = ReadSprite(sc, sprite);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   404
	return p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
961
26fdd7e62075 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   407
6298
c30fe89622df (svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents: 6213
diff changeset
   408
void GfxInitSpriteMem()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
{
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   410
	/* 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
   411
	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
   412
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   413
	/* A big free block */
2339
1c64119d5a3b (svn r2865) Push the responsibility for allocating the sprite heap into GfxInitSpriteMem()
tron
parents: 2329
diff changeset
   414
	_spritecache_ptr->size = (SPRITE_CACHE_SIZE - sizeof(MemBlock)) | S_FREE_MASK;
6307
f40e88cff863 (svn r9639) [cpp_gui] -Sync with trunk (r9476:9633)
KUDr
parents: 6298
diff changeset
   415
	/* Sentinel block (identified by 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
   416
	NextBlock(_spritecache_ptr)->size = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
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
   418
	/* 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
   419
	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
   420
	_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
   421
	_spritecache = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
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
   423
	_compact_cache_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
}