author | KUDr |
Fri, 20 Apr 2007 19:43:06 +0000 | |
changeset 6513 | 454347ca3dfb |
parent 6481 | 85ad87daf4b0 |
child 6799 | 169d0b7fe821 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6420
diff
changeset
|
3 |
/** @file spritecache.cpp */ |
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
4 |
|
0 | 5 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1844
diff
changeset
|
6 |
#include "openttd.h" |
1299
39c06aba09aa
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1250
diff
changeset
|
7 |
#include "debug.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
8 |
#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
|
9 |
#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
|
10 |
#include "spritecache.h" |
1363
775a7ee52369
(svn r1867) Include tables/sprites.h only in files which need it
tron
parents:
1361
diff
changeset
|
11 |
#include "table/sprites.h" |
0 | 12 |
#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
|
13 |
#include "helpers.hpp" |
0 | 14 |
|
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
|
15 |
#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
|
16 |
# 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
|
17 |
#endif /* SPRITE_CACHE_SIZE */ |
0 | 18 |
|
19 |
||
6248
e4a2ed7e5613
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6247
diff
changeset
|
20 |
struct SpriteCache { |
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
|
21 |
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
|
22 |
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
|
23 |
int16 lru; |
6248
e4a2ed7e5613
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6247
diff
changeset
|
24 |
}; |
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
|
25 |
|
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 |
|
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 |
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
|
28 |
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
|
29 |
|
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 |
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
|
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 |
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
|
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 |
|
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 |
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
|
38 |
{ |
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 |
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
|
40 |
/* 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
|
41 |
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
|
42 |
|
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
|
43 |
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
|
44 |
|
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
|
45 |
_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
|
46 |
|
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 |
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
|
48 |
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
|
49 |
} |
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 |
|
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 |
/* 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
|
52 |
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
|
53 |
_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
|
54 |
} |
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 |
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
|
57 |
} |
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
|
58 |
|
0 | 59 |
|
6248
e4a2ed7e5613
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6247
diff
changeset
|
60 |
struct MemBlock { |
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
|
61 |
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
|
62 |
byte data[VARARRAY_SIZE]; |
6248
e4a2ed7e5613
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6247
diff
changeset
|
63 |
}; |
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 |
|
0 | 65 |
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
|
66 |
static MemBlock *_spritecache_ptr; |
0 | 67 |
static int _compact_cache_counter; |
68 |
||
6247 | 69 |
static void CompactSpriteCache(); |
0 | 70 |
|
6247 | 71 |
static bool ReadSpriteHeaderSkipData() |
0 | 72 |
{ |
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
|
73 |
uint16 num = FioReadWord(); |
0 | 74 |
byte type; |
75 |
||
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
|
76 |
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
|
77 |
|
0 | 78 |
type = FioReadByte(); |
79 |
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
|
80 |
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
|
81 |
/* 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
|
82 |
* 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
|
83 |
return num != 1; |
0 | 84 |
} |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
85 |
|
0 | 86 |
FioSkipBytes(7); |
87 |
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
|
88 |
if (num == 0) return true; |
0 | 89 |
|
90 |
if (type & 2) { |
|
91 |
FioSkipBytes(num); |
|
1355 | 92 |
} else { |
93 |
while (num > 0) { |
|
94 |
int8 i = FioReadByte(); |
|
95 |
if (i >= 0) { |
|
96 |
num -= i; |
|
97 |
FioSkipBytes(i); |
|
98 |
} else { |
|
99 |
i = -(i >> 3); |
|
100 |
num -= i; |
|
101 |
FioReadByte(); |
|
102 |
} |
|
0 | 103 |
} |
104 |
} |
|
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
|
105 |
|
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
|
106 |
return true; |
0 | 107 |
} |
108 |
||
3565
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
109 |
/* Check if the given Sprite ID exists */ |
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
110 |
bool SpriteExists(SpriteID id) |
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
111 |
{ |
ef0a9ef56fa0
(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... */ |
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 |
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
|
114 |
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
|
115 |
return GetSpriteCache(id)->file_pos != 0; |
3565
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
116 |
} |
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
117 |
|
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
|
118 |
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
|
119 |
|
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
|
120 |
static void* ReadSprite(SpriteCache *sc, SpriteID id) |
0 | 121 |
{ |
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
|
122 |
uint num; |
0 | 123 |
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
|
124 |
|
5380
8ea58542b6e0
(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
ef0a9ef56fa0
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
127 |
if (!SpriteExists(id)) { |
5962
c861a188a760
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5720
diff
changeset
|
128 |
DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", id); |
c861a188a760
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5720
diff
changeset
|
129 |
|
c861a188a760
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5720
diff
changeset
|
130 |
/* SPR_IMG_QUERY is a BIG FAT RED ? */ |
c861a188a760
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5720
diff
changeset
|
131 |
id = SPR_IMG_QUERY; |
c861a188a760
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5720
diff
changeset
|
132 |
sc = GetSpriteCache(SPR_IMG_QUERY); |
1378
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
133 |
} |
23d4f642c989
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
134 |
|
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
|
135 |
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
|
136 |
|
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
|
137 |
num = FioReadWord(); |
0 | 138 |
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
|
139 |
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
|
140 |
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
|
141 |
|
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
|
142 |
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
|
143 |
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
|
144 |
|
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 |
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
|
146 |
} 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
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
|
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
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
sprite->info = type; |
2015 | 156 |
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
|
157 |
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
|
158 |
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
|
159 |
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
|
160 |
|
1351
a08ab043dd08
(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 | 162 |
while (num > 0) { |
163 |
int8 i = FioReadByte(); |
|
0 | 164 |
|
1355 | 165 |
if (i >= 0) { |
166 |
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
|
167 |
for (; i > 0; --i) *dest++ = FioReadByte(); |
1355 | 168 |
} else { |
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 | 171 |
i = -(i >> 3); |
172 |
num -= i; |
|
173 |
||
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
|
174 |
for (; i > 0; --i) *dest++ = *rel++; |
1355 | 175 |
} |
0 | 176 |
} |
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
|
177 |
|
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
|
178 |
return sprite; |
0 | 179 |
} |
180 |
} |
|
181 |
||
182 |
||
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
|
183 |
bool LoadNextSprite(int load_index, byte file_index) |
0 | 184 |
{ |
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
|
185 |
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
|
186 |
uint32 file_pos = FioGetPos() | (file_index << 24); |
0 | 187 |
|
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
|
188 |
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
|
189 |
|
3591
a5febb93bc11
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
190 |
if (load_index >= MAX_SPRITES) { |
a5febb93bc11
(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); |
a5febb93bc11
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
192 |
} |
a5febb93bc11
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
193 |
|
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
sc->lru = 0; |
0 | 198 |
|
199 |
return true; |
|
200 |
} |
|
201 |
||
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 |
void DupSprite(SpriteID old_spr, SpriteID new_spr) |
2407 | 204 |
{ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
205 |
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
|
206 |
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
|
207 |
|
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
|
208 |
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
|
209 |
scnew->ptr = NULL; |
2407 | 210 |
} |
211 |
||
212 |
||
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
|
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
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
|
215 |
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
|
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 | 220 |
|
221 |
#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
|
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 |
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
|
224 |
{ |
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
|
225 |
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
|
226 |
} |
0 | 227 |
|
6247 | 228 |
static uint32 GetSpriteCacheUsage() |
0 | 229 |
{ |
4321
958a8e9c012b
(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
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 |
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
|
232 |
|
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
|
233 |
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
|
234 |
if (!(s->size & S_FREE_MASK)) tot_size += s->size; |
0 | 235 |
|
236 |
return tot_size; |
|
237 |
} |
|
238 |
||
239 |
||
6247 | 240 |
void IncreaseSpriteLRU() |
0 | 241 |
{ |
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
242 |
/* Increase all LRU values */ |
0 | 243 |
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
|
244 |
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
|
245 |
|
5380
8ea58542b6e0
(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 | 247 |
|
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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
} 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
|
254 |
sc->lru--; |
0 | 255 |
} |
256 |
} |
|
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
|
257 |
} |
0 | 258 |
_sprite_lru_counter = 0; |
259 |
} |
|
260 |
||
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
261 |
/* Compact sprite cache every now and then. */ |
0 | 262 |
if (++_compact_cache_counter >= 740) { |
263 |
CompactSpriteCache(); |
|
264 |
_compact_cache_counter = 0; |
|
265 |
} |
|
266 |
} |
|
267 |
||
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
268 |
/** Called when holes in the sprite cache should be removed. |
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
269 |
* That is accomplished by moving the cached data. */ |
6247 | 270 |
static void CompactSpriteCache() |
0 | 271 |
{ |
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
|
272 |
MemBlock *s; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
273 |
|
5380
8ea58542b6e0
(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
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 |
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
|
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
282 |
/* 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
|
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 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
285 |
/* 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
|
286 |
if (next->size == 0) |
0 | 287 |
break; |
288 |
||
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
289 |
/* 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
|
290 |
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
|
291 |
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
|
292 |
} |
0 | 293 |
|
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
|
294 |
GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry |
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
295 |
/* Swap this and the next block */ |
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
|
296 |
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
|
297 |
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
|
298 |
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
|
299 |
*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
|
300 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
301 |
/* Coalesce 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
|
302 |
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
|
303 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 304 |
} |
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
|
305 |
} 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
|
306 |
s = NextBlock(s); |
0 | 307 |
} |
308 |
} |
|
309 |
} |
|
310 |
||
6247 | 311 |
static void DeleteEntryFromSpriteCache() |
0 | 312 |
{ |
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
|
313 |
SpriteID i; |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
314 |
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
|
315 |
MemBlock* s; |
0 | 316 |
int cur_lru; |
317 |
||
5380
8ea58542b6e0
(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 | 319 |
|
320 |
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
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
cur_lru = sc->lru; |
0 | 325 |
best = i; |
326 |
} |
|
327 |
} |
|
328 |
||
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
329 |
/* Display an error message and die, in case we found no sprite at all. |
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
330 |
* 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
|
331 |
if (best == (uint)-1) |
0 | 332 |
error("Out of sprite memory"); |
333 |
||
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
334 |
/* 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
|
335 |
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
|
336 |
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
|
337 |
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
|
338 |
GetSpriteCache(best)->ptr = NULL; |
0 | 339 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
340 |
/* 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
|
341 |
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
|
342 |
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
|
343 |
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
|
344 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 345 |
} |
346 |
} |
|
347 |
} |
|
348 |
} |
|
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 |
static void* AllocSprite(size_t mem_req) |
0 | 351 |
{ |
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
|
352 |
mem_req += sizeof(MemBlock); |
0 | 353 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
354 |
/* 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
|
355 |
* 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
|
356 |
mem_req = ALIGN(mem_req, sizeof(uint32)); |
0 | 357 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
358 |
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
|
359 |
MemBlock* s; |
0 | 360 |
|
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
|
361 |
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
|
362 |
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
|
363 |
size_t cur_size = s->size & ~S_FREE_MASK; |
0 | 364 |
|
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
365 |
/* 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
|
366 |
* 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
|
367 |
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
|
368 |
cur_size >= mem_req + sizeof(MemBlock)) { |
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
369 |
/* Set size and in use */ |
1353
c5892a0dadad
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
370 |
s->size = mem_req; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
371 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
372 |
/* Do we need to inject a free block too? */ |
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
|
373 |
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
|
374 |
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
|
375 |
} |
0 | 376 |
|
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
|
377 |
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
|
378 |
} |
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
|
379 |
} |
0 | 380 |
} |
381 |
||
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
382 |
/* Reached sentinel, but no block found yet. Delete some old 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
|
383 |
DeleteEntryFromSpriteCache(); |
0 | 384 |
} |
385 |
} |
|
386 |
||
387 |
||
1361 | 388 |
const void *GetRawSprite(SpriteID sprite) |
0 | 389 |
{ |
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
|
390 |
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
|
391 |
void* p; |
0 | 392 |
|
5702
f98beec96c66
(svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents:
5609
diff
changeset
|
393 |
assert(sprite < _spritecache_items); |
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 |
sc = GetSpriteCache(sprite); |
0 | 396 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
397 |
/* Update LRU */ |
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
|
398 |
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
|
399 |
|
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
|
400 |
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
|
401 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
402 |
/* 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
|
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 | 405 |
} |
406 |
||
961 | 407 |
|
6247 | 408 |
void GfxInitSpriteMem() |
0 | 409 |
{ |
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
410 |
/* 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
|
411 |
if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(SPRITE_CACHE_SIZE); |
0 | 412 |
|
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
413 |
/* A big free block */ |
2339
b03b1fb8ccfe
(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; |
6420
456c275f3313
(svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents:
6248
diff
changeset
|
415 |
/* Sentinel block (identified by 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
|
416 |
NextBlock(_spritecache_ptr)->size = 0; |
0 | 417 |
|
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
|
418 |
/* 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
|
419 |
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
|
420 |
_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
|
421 |
_spritecache = NULL; |
0 | 422 |
|
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
|
423 |
_compact_cache_counter = 0; |
0 | 424 |
} |