author | rubidium |
Thu, 01 Feb 2007 15:49:12 +0000 | |
changeset 5893 | 7e431a4abebb |
parent 5720 | 33faabcca1dd |
child 5962 | c861a188a760 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1844
diff
changeset
|
4 |
#include "openttd.h" |
1299
39c06aba09aa
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1250
diff
changeset
|
5 |
#include "debug.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
6 |
#include "functions.h" |
2548
49c8a096033f
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2407
diff
changeset
|
7 |
#include "macros.h" |
1349
15979a2e9001
(svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents:
1348
diff
changeset
|
8 |
#include "spritecache.h" |
1363
775a7ee52369
(svn r1867) Include tables/sprites.h only in files which need it
tron
parents:
1361
diff
changeset
|
9 |
#include "table/sprites.h" |
0 | 10 |
#include "fileio.h" |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
11 |
#include "helpers.hpp" |
0 | 12 |
|
5720
33faabcca1dd
(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:
5702
diff
changeset
|
13 |
#ifndef SPRITE_CACHE_SIZE |
33faabcca1dd
(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:
5702
diff
changeset
|
14 |
# define SPRITE_CACHE_SIZE 2*1024*1024 |
33faabcca1dd
(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:
5702
diff
changeset
|
15 |
#endif /* SPRITE_CACHE_SIZE */ |
0 | 16 |
|
17 |
||
5504
a6ef917aa095
(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:
5501
diff
changeset
|
18 |
typedef struct SpriteCache { |
a6ef917aa095
(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:
5501
diff
changeset
|
19 |
void *ptr; |
a6ef917aa095
(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:
5501
diff
changeset
|
20 |
uint32 file_pos; |
a6ef917aa095
(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:
5501
diff
changeset
|
21 |
int16 lru; |
a6ef917aa095
(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:
5501
diff
changeset
|
22 |
} SpriteCache; |
a6ef917aa095
(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:
5501
diff
changeset
|
23 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
24 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
25 |
static uint _spritecache_items = 0; |
a6ef917aa095
(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:
5501
diff
changeset
|
26 |
static SpriteCache *_spritecache = NULL; |
a6ef917aa095
(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:
5501
diff
changeset
|
27 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
28 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
29 |
static inline SpriteCache *GetSpriteCache(uint index) |
a6ef917aa095
(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:
5501
diff
changeset
|
30 |
{ |
a6ef917aa095
(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:
5501
diff
changeset
|
31 |
return &_spritecache[index]; |
a6ef917aa095
(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:
5501
diff
changeset
|
32 |
} |
a6ef917aa095
(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:
5501
diff
changeset
|
33 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
34 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
35 |
static SpriteCache *AllocateSpriteCache(uint index) |
a6ef917aa095
(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:
5501
diff
changeset
|
36 |
{ |
a6ef917aa095
(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:
5501
diff
changeset
|
37 |
if (index >= _spritecache_items) { |
a6ef917aa095
(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:
5501
diff
changeset
|
38 |
/* Add another 1024 items to the 'pool' */ |
a6ef917aa095
(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:
5501
diff
changeset
|
39 |
uint items = ALIGN(index + 1, 1024); |
a6ef917aa095
(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:
5501
diff
changeset
|
40 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
41 |
DEBUG(sprite, 4, "Increasing sprite cache to %d items (%d bytes)", items, items * sizeof(*_spritecache)); |
a6ef917aa095
(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:
5501
diff
changeset
|
42 |
|
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
43 |
_spritecache = ReallocT(_spritecache, items); |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
44 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
45 |
if (_spritecache == NULL) { |
a6ef917aa095
(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:
5501
diff
changeset
|
46 |
error("Unable to allocate sprite cache of %d items (%d bytes)", items, items * sizeof(*_spritecache)); |
a6ef917aa095
(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:
5501
diff
changeset
|
47 |
} |
a6ef917aa095
(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:
5501
diff
changeset
|
48 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
49 |
/* Reset the new items and update the count */ |
a6ef917aa095
(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:
5501
diff
changeset
|
50 |
memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache)); |
a6ef917aa095
(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:
5501
diff
changeset
|
51 |
_spritecache_items = items; |
a6ef917aa095
(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:
5501
diff
changeset
|
52 |
} |
a6ef917aa095
(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:
5501
diff
changeset
|
53 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
54 |
return GetSpriteCache(index); |
a6ef917aa095
(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:
5501
diff
changeset
|
55 |
} |
a6ef917aa095
(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:
5501
diff
changeset
|
56 |
|
0 | 57 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
58 |
typedef struct MemBlock { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
59 |
uint32 size; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
60 |
byte data[VARARRAY_SIZE]; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
61 |
} MemBlock; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
62 |
|
0 | 63 |
static uint _sprite_lru_counter; |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
64 |
static MemBlock *_spritecache_ptr; |
0 | 65 |
static int _compact_cache_counter; |
66 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
67 |
static void CompactSpriteCache(void); |
0 | 68 |
|
2342
aae24c9661ba
(svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents:
2340
diff
changeset
|
69 |
static bool ReadSpriteHeaderSkipData(void) |
0 | 70 |
{ |
2329
c4d9fc4006b1
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
71 |
uint16 num = FioReadWord(); |
0 | 72 |
byte type; |
73 |
||
2329
c4d9fc4006b1
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
74 |
if (num == 0) return false; |
c4d9fc4006b1
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
75 |
|
0 | 76 |
type = FioReadByte(); |
77 |
if (type == 0xFF) { |
|
2342
aae24c9661ba
(svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents:
2340
diff
changeset
|
78 |
FioSkipBytes(num); |
5150
fe410b84b302
(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 |
fe410b84b302
(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. */ |
fe410b84b302
(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 | 82 |
} |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
83 |
|
0 | 84 |
FioSkipBytes(7); |
85 |
num -= 8; |
|
2329
c4d9fc4006b1
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
86 |
if (num == 0) return true; |
0 | 87 |
|
88 |
if (type & 2) { |
|
89 |
FioSkipBytes(num); |
|
1355 | 90 |
} else { |
91 |
while (num > 0) { |
|
92 |
int8 i = FioReadByte(); |
|
93 |
if (i >= 0) { |
|
94 |
num -= i; |
|
95 |
FioSkipBytes(i); |
|
96 |
} else { |
|
97 |
i = -(i >> 3); |
|
98 |
num -= i; |
|
99 |
FioReadByte(); |
|
100 |
} |
|
0 | 101 |
} |
102 |
} |
|
2329
c4d9fc4006b1
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
103 |
|
c4d9fc4006b1
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
104 |
return true; |
0 | 105 |
} |
106 |
||
3565
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
107 |
/* Check if the given Sprite ID exists */ |
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
108 |
bool SpriteExists(SpriteID id) |
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
109 |
{ |
ef0a9ef56fa0
(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... */ |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
111 |
if (id == 0) return true; |
5702
f98beec96c66
(svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents:
5609
diff
changeset
|
112 |
if (id >= _spritecache_items) return false; |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
113 |
return GetSpriteCache(id)->file_pos != 0; |
3565
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
114 |
} |
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
115 |
|
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
116 |
static void* AllocSprite(size_t); |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
117 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
118 |
static void* ReadSprite(SpriteCache *sc, SpriteID id) |
0 | 119 |
{ |
2321
75be68dfaaf2
(svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents:
2319
diff
changeset
|
120 |
uint num; |
0 | 121 |
byte type; |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
122 |
|
5380
8ea58542b6e0
(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
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
125 |
if (!SpriteExists(id)) { |
1378
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
126 |
error( |
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
127 |
"Tried to load non-existing sprite #%d.\n" |
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
128 |
"Probable cause: Wrong/missing NewGRFs", |
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
129 |
id |
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
130 |
); |
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
131 |
} |
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
132 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
133 |
FioSeekToFile(sc->file_pos); |
1354
4f9416233d06
(svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents:
1353
diff
changeset
|
134 |
|
2321
75be68dfaaf2
(svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents:
2319
diff
changeset
|
135 |
num = FioReadWord(); |
0 | 136 |
type = FioReadByte(); |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
137 |
if (type == 0xFF) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
138 |
byte* dest = (byte*)AllocSprite(num); |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
139 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
140 |
sc->ptr = dest; |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
141 |
FioReadBlock(dest, num); |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
142 |
|
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
143 |
return dest; |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
144 |
} else { |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
145 |
uint height = FioReadByte(); |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
146 |
uint width = FioReadWord(); |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
147 |
Sprite* sprite; |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
148 |
byte* dest; |
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
149 |
|
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
150 |
num = (type & 0x02) ? width * height : num - 8; |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
151 |
sprite = (Sprite*)AllocSprite(sizeof(*sprite) + num); |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
152 |
sc->ptr = sprite; |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
153 |
sprite->info = type; |
2015 | 154 |
sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
155 |
sprite->width = width; |
1351
a08ab043dd08
(svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents:
1350
diff
changeset
|
156 |
sprite->x_offs = FioReadWord(); |
a08ab043dd08
(svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents:
1350
diff
changeset
|
157 |
sprite->y_offs = FioReadWord(); |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
158 |
|
1351
a08ab043dd08
(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 | 160 |
while (num > 0) { |
161 |
int8 i = FioReadByte(); |
|
0 | 162 |
|
1355 | 163 |
if (i >= 0) { |
164 |
num -= i; |
|
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
165 |
for (; i > 0; --i) *dest++ = FioReadByte(); |
1355 | 166 |
} else { |
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 | 169 |
i = -(i >> 3); |
170 |
num -= i; |
|
171 |
||
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
172 |
for (; i > 0; --i) *dest++ = *rel++; |
1355 | 173 |
} |
0 | 174 |
} |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
175 |
|
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
176 |
return sprite; |
0 | 177 |
} |
178 |
} |
|
179 |
||
180 |
||
2340
e18ef06bc59a
(svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents:
2339
diff
changeset
|
181 |
bool LoadNextSprite(int load_index, byte file_index) |
0 | 182 |
{ |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
183 |
SpriteCache *sc; |
2321
75be68dfaaf2
(svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents:
2319
diff
changeset
|
184 |
uint32 file_pos = FioGetPos() | (file_index << 24); |
0 | 185 |
|
2342
aae24c9661ba
(svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents:
2340
diff
changeset
|
186 |
if (!ReadSpriteHeaderSkipData()) return false; |
361
95307fb1c3f8
(svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents:
184
diff
changeset
|
187 |
|
3591
a5febb93bc11
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
188 |
if (load_index >= MAX_SPRITES) { |
a5febb93bc11
(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); |
a5febb93bc11
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
190 |
} |
a5febb93bc11
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
191 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
192 |
sc = AllocateSpriteCache(load_index); |
a6ef917aa095
(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:
5501
diff
changeset
|
193 |
sc->file_pos = file_pos; |
a6ef917aa095
(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:
5501
diff
changeset
|
194 |
sc->ptr = NULL; |
a6ef917aa095
(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:
5501
diff
changeset
|
195 |
sc->lru = 0; |
0 | 196 |
|
197 |
return true; |
|
198 |
} |
|
199 |
||
2407 | 200 |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
201 |
void DupSprite(SpriteID old_spr, SpriteID new_spr) |
2407 | 202 |
{ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
203 |
SpriteCache *scold = GetSpriteCache(old_spr); |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
204 |
SpriteCache *scnew = AllocateSpriteCache(new_spr); |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
205 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
206 |
scnew->file_pos = scold->file_pos; |
a6ef917aa095
(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:
5501
diff
changeset
|
207 |
scnew->ptr = NULL; |
2407 | 208 |
} |
209 |
||
210 |
||
2340
e18ef06bc59a
(svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents:
2339
diff
changeset
|
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
c4d9fc4006b1
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
213 |
for (; count > 0; --count) { |
2342
aae24c9661ba
(svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents:
2340
diff
changeset
|
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 | 218 |
|
219 |
#define S_FREE_MASK 1 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
220 |
|
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
221 |
static inline MemBlock* NextBlock(MemBlock* block) |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
222 |
{ |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
223 |
return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK)); |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
224 |
} |
0 | 225 |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
226 |
static uint32 GetSpriteCacheUsage(void) |
0 | 227 |
{ |
4321
958a8e9c012b
(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
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
229 |
MemBlock* s; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
230 |
|
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
231 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
232 |
if (!(s->size & S_FREE_MASK)) tot_size += s->size; |
0 | 233 |
|
234 |
return tot_size; |
|
235 |
} |
|
236 |
||
237 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
238 |
void IncreaseSpriteLRU(void) |
0 | 239 |
{ |
240 |
// Increase all LRU values |
|
241 |
if (_sprite_lru_counter > 16384) { |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
242 |
SpriteID i; |
a6ef917aa095
(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:
5501
diff
changeset
|
243 |
|
5380
8ea58542b6e0
(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 | 245 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
246 |
for (i = 0; i != _spritecache_items; i++) { |
a6ef917aa095
(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:
5501
diff
changeset
|
247 |
SpriteCache *sc = GetSpriteCache(i); |
a6ef917aa095
(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:
5501
diff
changeset
|
248 |
if (sc->ptr != NULL) { |
a6ef917aa095
(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:
5501
diff
changeset
|
249 |
if (sc->lru >= 0) { |
a6ef917aa095
(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:
5501
diff
changeset
|
250 |
sc->lru = -1; |
a6ef917aa095
(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:
5501
diff
changeset
|
251 |
} else if (sc->lru != -32768) { |
a6ef917aa095
(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:
5501
diff
changeset
|
252 |
sc->lru--; |
0 | 253 |
} |
254 |
} |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
255 |
} |
0 | 256 |
_sprite_lru_counter = 0; |
257 |
} |
|
258 |
||
259 |
// Compact sprite cache every now and then. |
|
260 |
if (++_compact_cache_counter >= 740) { |
|
261 |
CompactSpriteCache(); |
|
262 |
_compact_cache_counter = 0; |
|
263 |
} |
|
264 |
} |
|
265 |
||
266 |
// Called when holes in the sprite cache should be removed. |
|
267 |
// That is accomplished by moving the cached data. |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
268 |
static void CompactSpriteCache(void) |
0 | 269 |
{ |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
270 |
MemBlock *s; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
271 |
|
5380
8ea58542b6e0
(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
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
274 |
for (s = _spritecache_ptr; s->size != 0;) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
275 |
if (s->size & S_FREE_MASK) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
276 |
MemBlock* next = NextBlock(s); |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
277 |
MemBlock temp; |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
278 |
SpriteID i; |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
279 |
|
0 | 280 |
// Since free blocks are automatically coalesced, this should hold true. |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
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 | 283 |
// If the next block is the sentinel block, we can safely return |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
284 |
if (next->size == 0) |
0 | 285 |
break; |
286 |
||
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
287 |
// Locate the sprite belonging to the next pointer. |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
288 |
for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) { |
a6ef917aa095
(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:
5501
diff
changeset
|
289 |
assert(i != _spritecache_items); |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
290 |
} |
0 | 291 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
292 |
GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
293 |
// Swap this and the next block |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
294 |
temp = *s; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
295 |
memmove(s, next, next->size); |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
296 |
s = NextBlock(s); |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
297 |
*s = temp; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
298 |
|
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
299 |
// Coalesce free blocks |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
300 |
while (NextBlock(s)->size & S_FREE_MASK) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
301 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 302 |
} |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
303 |
} else { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
304 |
s = NextBlock(s); |
0 | 305 |
} |
306 |
} |
|
307 |
} |
|
308 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
309 |
static void DeleteEntryFromSpriteCache(void) |
0 | 310 |
{ |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
311 |
SpriteID i; |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
312 |
uint best = UINT_MAX; |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
313 |
MemBlock* s; |
0 | 314 |
int cur_lru; |
315 |
||
5380
8ea58542b6e0
(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 | 317 |
|
318 |
cur_lru = 0xffff; |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
319 |
for (i = 0; i != _spritecache_items; i++) { |
a6ef917aa095
(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:
5501
diff
changeset
|
320 |
SpriteCache *sc = GetSpriteCache(i); |
a6ef917aa095
(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:
5501
diff
changeset
|
321 |
if (sc->ptr != NULL && sc->lru < cur_lru) { |
a6ef917aa095
(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:
5501
diff
changeset
|
322 |
cur_lru = sc->lru; |
0 | 323 |
best = i; |
324 |
} |
|
325 |
} |
|
326 |
||
327 |
// Display an error message and die, in case we found no sprite at all. |
|
328 |
// This shouldn't really happen, unless all sprites are locked. |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
329 |
if (best == (uint)-1) |
0 | 330 |
error("Out of sprite memory"); |
331 |
||
332 |
// Mark the block as free (the block must be in use) |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
333 |
s = (MemBlock*)GetSpriteCache(best)->ptr - 1; |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
334 |
assert(!(s->size & S_FREE_MASK)); |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
335 |
s->size |= S_FREE_MASK; |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
336 |
GetSpriteCache(best)->ptr = NULL; |
0 | 337 |
|
338 |
// And coalesce adjacent free blocks |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
339 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
340 |
if (s->size & S_FREE_MASK) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
341 |
while (NextBlock(s)->size & S_FREE_MASK) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
342 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 343 |
} |
344 |
} |
|
345 |
} |
|
346 |
} |
|
347 |
||
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
348 |
static void* AllocSprite(size_t mem_req) |
0 | 349 |
{ |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
350 |
mem_req += sizeof(MemBlock); |
0 | 351 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
352 |
/* Align this to an uint32 boundary. This also makes sure that the 2 least |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
353 |
* bits are not used, so we could use those for other things. */ |
2548
49c8a096033f
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2407
diff
changeset
|
354 |
mem_req = ALIGN(mem_req, sizeof(uint32)); |
0 | 355 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
356 |
for (;;) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
357 |
MemBlock* s; |
0 | 358 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
359 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
360 |
if (s->size & S_FREE_MASK) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
361 |
size_t cur_size = s->size & ~S_FREE_MASK; |
0 | 362 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
363 |
/* Is the block exactly the size we need or |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
364 |
* big enough for an additional free block? */ |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
365 |
if (cur_size == mem_req || |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
366 |
cur_size >= mem_req + sizeof(MemBlock)) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
367 |
// Set size and in use |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
368 |
s->size = mem_req; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
369 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
370 |
// Do we need to inject a free block too? |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
371 |
if (cur_size != mem_req) { |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
372 |
NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
373 |
} |
0 | 374 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
375 |
return s->data; |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
376 |
} |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
377 |
} |
0 | 378 |
} |
379 |
||
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
380 |
// Reached sentinel, but no block found yet. Delete some old entry. |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
381 |
DeleteEntryFromSpriteCache(); |
0 | 382 |
} |
383 |
} |
|
384 |
||
385 |
||
1361 | 386 |
const void *GetRawSprite(SpriteID sprite) |
0 | 387 |
{ |
5504
a6ef917aa095
(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:
5501
diff
changeset
|
388 |
SpriteCache *sc; |
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
389 |
void* p; |
0 | 390 |
|
5702
f98beec96c66
(svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents:
5609
diff
changeset
|
391 |
assert(sprite < _spritecache_items); |
0 | 392 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
393 |
sc = GetSpriteCache(sprite); |
0 | 394 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
395 |
// Update LRU |
a6ef917aa095
(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:
5501
diff
changeset
|
396 |
sc->lru = ++_sprite_lru_counter; |
a6ef917aa095
(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:
5501
diff
changeset
|
397 |
|
a6ef917aa095
(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:
5501
diff
changeset
|
398 |
p = sc->ptr; |
a6ef917aa095
(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:
5501
diff
changeset
|
399 |
|
2014
ccfe4fa81a14
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
400 |
// Load the sprite, if it is not loaded, yet |
5504
a6ef917aa095
(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:
5501
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 | 403 |
} |
404 |
||
961 | 405 |
|
2340
e18ef06bc59a
(svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents:
2339
diff
changeset
|
406 |
void GfxInitSpriteMem(void) |
0 | 407 |
{ |
408 |
// initialize sprite cache heap |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
409 |
if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(SPRITE_CACHE_SIZE); |
0 | 410 |
|
411 |
// A big free block |
|
2339
b03b1fb8ccfe
(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
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
413 |
// Sentinel block (identified by size == 0) |
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
414 |
NextBlock(_spritecache_ptr)->size = 0; |
0 | 415 |
|
5504
a6ef917aa095
(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:
5501
diff
changeset
|
416 |
/* Reset the spritecache 'pool' */ |
a6ef917aa095
(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:
5501
diff
changeset
|
417 |
free(_spritecache); |
a6ef917aa095
(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:
5501
diff
changeset
|
418 |
_spritecache_items = 0; |
a6ef917aa095
(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:
5501
diff
changeset
|
419 |
_spritecache = NULL; |
0 | 420 |
|
2340
e18ef06bc59a
(svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents:
2339
diff
changeset
|
421 |
_compact_cache_counter = 0; |
0 | 422 |
} |