author | truelight |
Fri, 13 Jul 2007 09:18:50 +0000 | |
branch | noai |
changeset 9641 | 855e32c08c9b |
parent 9631 | 8a2d1c2ceb88 |
child 9703 | d2a6acdbd665 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
9601
b499fdd106d5
(svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents:
9574
diff
changeset
|
3 |
/** @file spritecache.cpp */ |
9574 | 4 |
|
0 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1844
diff
changeset
|
6 |
#include "openttd.h" |
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
7 |
#include "variables.h" |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
8 |
#include "string.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1250
diff
changeset
|
9 |
#include "debug.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
10 |
#include "functions.h" |
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2407
diff
changeset
|
11 |
#include "macros.h" |
1349
07514c2cc6d1
(svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents:
1348
diff
changeset
|
12 |
#include "spritecache.h" |
1363
01d3de5d8039
(svn r1867) Include tables/sprites.h only in files which need it
tron
parents:
1361
diff
changeset
|
13 |
#include "table/sprites.h" |
0 | 14 |
#include "fileio.h" |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
15 |
#include "helpers.hpp" |
9626 | 16 |
#include "spriteloader/grf.hpp" |
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
17 |
#ifdef WITH_PNG |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
18 |
#include "spriteloader/png.hpp" |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
19 |
#endif /* WITH_PNG */ |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
20 |
#include "blitter/factory.hpp" |
0 | 21 |
|
9626 | 22 |
/* Default of 4MB spritecache */ |
23 |
uint _sprite_cache_size = 4; |
|
0 | 24 |
|
25 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
26 |
struct SpriteCache { |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
27 |
void *ptr; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
28 |
uint32 file_pos; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
29 |
int16 lru; |
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
30 |
uint32 id; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
31 |
const char *grf_name; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
32 |
}; |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
33 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
34 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
35 |
static uint _spritecache_items = 0; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
36 |
static SpriteCache *_spritecache = NULL; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
37 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
38 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
39 |
static inline SpriteCache *GetSpriteCache(uint index) |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
40 |
{ |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
41 |
return &_spritecache[index]; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
42 |
} |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
43 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
44 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
45 |
static SpriteCache *AllocateSpriteCache(uint index) |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
46 |
{ |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
47 |
if (index >= _spritecache_items) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
48 |
/* Add another 1024 items to the 'pool' */ |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
49 |
uint items = ALIGN(index + 1, 1024); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
50 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
51 |
DEBUG(sprite, 4, "Increasing sprite cache to %d items (%d bytes)", items, items * sizeof(*_spritecache)); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
52 |
|
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
53 |
_spritecache = ReallocT(_spritecache, items); |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
54 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
55 |
if (_spritecache == NULL) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
56 |
error("Unable to allocate sprite cache of %d items (%d bytes)", items, items * sizeof(*_spritecache)); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
57 |
} |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
58 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
59 |
/* Reset the new items and update the count */ |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
60 |
memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache)); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
61 |
_spritecache_items = items; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
62 |
} |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
63 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
64 |
return GetSpriteCache(index); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
65 |
} |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
66 |
|
0 | 67 |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
68 |
struct MemBlock { |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
69 |
uint32 size; |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
70 |
byte data[VARARRAY_SIZE]; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
71 |
}; |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
72 |
|
0 | 73 |
static uint _sprite_lru_counter; |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
74 |
static MemBlock *_spritecache_ptr; |
0 | 75 |
static int _compact_cache_counter; |
76 |
||
6573 | 77 |
static void CompactSpriteCache(); |
0 | 78 |
|
6573 | 79 |
static bool ReadSpriteHeaderSkipData() |
0 | 80 |
{ |
2329
f68428464540
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
81 |
uint16 num = FioReadWord(); |
0 | 82 |
byte type; |
83 |
||
2329
f68428464540
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
84 |
if (num == 0) return false; |
f68428464540
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
85 |
|
0 | 86 |
type = FioReadByte(); |
87 |
if (type == 0xFF) { |
|
2342
c19fb4f2df30
(svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents:
2340
diff
changeset
|
88 |
FioSkipBytes(num); |
5150
247343201280
(svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents:
4522
diff
changeset
|
89 |
/* Some NewGRF files have "empty" pseudo-sprites which are 1 |
247343201280
(svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents:
4522
diff
changeset
|
90 |
* byte long. Catch these so the sprites won't be displayed. */ |
247343201280
(svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents:
4522
diff
changeset
|
91 |
return num != 1; |
0 | 92 |
} |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
93 |
|
0 | 94 |
FioSkipBytes(7); |
95 |
num -= 8; |
|
2329
f68428464540
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
96 |
if (num == 0) return true; |
0 | 97 |
|
98 |
if (type & 2) { |
|
99 |
FioSkipBytes(num); |
|
1355 | 100 |
} else { |
101 |
while (num > 0) { |
|
102 |
int8 i = FioReadByte(); |
|
103 |
if (i >= 0) { |
|
104 |
num -= i; |
|
105 |
FioSkipBytes(i); |
|
106 |
} else { |
|
107 |
i = -(i >> 3); |
|
108 |
num -= i; |
|
109 |
FioReadByte(); |
|
110 |
} |
|
0 | 111 |
} |
112 |
} |
|
2329
f68428464540
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
113 |
|
f68428464540
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
114 |
return true; |
0 | 115 |
} |
116 |
||
3565
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
117 |
/* Check if the given Sprite ID exists */ |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
118 |
bool SpriteExists(SpriteID id) |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
119 |
{ |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
120 |
/* Special case for Sprite ID zero -- its position is also 0... */ |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
121 |
if (id == 0) return true; |
5953
7000c5bdd70b
(svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents:
5860
diff
changeset
|
122 |
if (id >= _spritecache_items) return false; |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
123 |
return GetSpriteCache(id)->file_pos != 0; |
3565
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
124 |
} |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
125 |
|
9626 | 126 |
void* AllocSprite(size_t); |
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
127 |
|
9627 | 128 |
static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite) |
0 | 129 |
{ |
9626 | 130 |
uint32 file_pos = sc->file_pos; |
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
131 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5150
diff
changeset
|
132 |
DEBUG(sprite, 9, "Load sprite %d", id); |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
133 |
|
3565
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
134 |
if (!SpriteExists(id)) { |
6213
164d84e1c5b4
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5971
diff
changeset
|
135 |
DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", id); |
164d84e1c5b4
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5971
diff
changeset
|
136 |
|
164d84e1c5b4
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5971
diff
changeset
|
137 |
/* SPR_IMG_QUERY is a BIG FAT RED ? */ |
164d84e1c5b4
(svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents:
5971
diff
changeset
|
138 |
id = SPR_IMG_QUERY; |
9626 | 139 |
file_pos = GetSpriteCache(SPR_IMG_QUERY)->file_pos; |
1378
ebb8d52f0352
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
140 |
} |
ebb8d52f0352
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
141 |
|
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
142 |
if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) { |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
143 |
#ifdef WITH_PNG |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
144 |
/* Try loading 32bpp graphics in case we are 32bpp output */ |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
145 |
SpriteLoaderPNG sprite_loader; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
146 |
SpriteLoader::Sprite sprite; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
147 |
|
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
148 |
if (sprite_loader.LoadSprite(&sprite, sc->grf_name, sc->id)) { |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
149 |
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
150 |
free(sprite.data); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
151 |
|
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
152 |
return sc->ptr; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
153 |
} |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
154 |
/* If the PNG couldn't be loaded, fall back to 8bpp grfs */ |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
155 |
#else |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
156 |
static bool show_once = true; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
157 |
if (show_once) { |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
158 |
DEBUG(misc, 0, "You are running a 32bpp blitter, but this build is without libpng support; falling back to 8bpp graphics"); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
159 |
show_once = false; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
160 |
} |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
161 |
#endif /* WITH_PNG */ |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
162 |
} |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
163 |
|
9626 | 164 |
FioSeekToFile(file_pos); |
1354
5e5c89b9b169
(svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents:
1353
diff
changeset
|
165 |
|
9626 | 166 |
/* Read the size and type */ |
167 |
int num = FioReadWord(); |
|
168 |
byte type = FioReadByte(); |
|
169 |
/* Type 0xFF indicates either a colormap or some other non-sprite info */ |
|
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
170 |
if (type == 0xFF) { |
9627 | 171 |
if (real_sprite) { |
172 |
static byte warning_level = 0; |
|
173 |
DEBUG(sprite, warning_level, "Tried to load non sprite #%d as a real sprite. Probable cause: NewGRF interference", id); |
|
174 |
warning_level = 6; |
|
175 |
if (id == SPR_IMG_QUERY) error("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non- sprite?"); |
|
176 |
return (void*)GetSprite(SPR_IMG_QUERY); |
|
177 |
} |
|
178 |
||
9626 | 179 |
byte *dest = (byte *)AllocSprite(num); |
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
180 |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
181 |
sc->ptr = dest; |
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
182 |
FioReadBlock(dest, num); |
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
183 |
|
9627 | 184 |
return sc->ptr; |
185 |
} |
|
186 |
/* Ugly hack to work around the problem that the old landscape |
|
187 |
* generator assumes that those sprites are stored uncompressed in |
|
188 |
* the memory, and they are only read directly by the code, never |
|
189 |
* send to the blitter. So do not send it to the blitter (which will |
|
190 |
* result in a data array in the format the blitter likes most), but |
|
191 |
* read the data directly from disk and store that as sprite. |
|
192 |
* Ugly: yes. Other solution: no. Blame the original author or |
|
193 |
* something ;) The image should really have been a data-stream |
|
194 |
* (so type = 0xFF basicly). */ |
|
195 |
if (id >= 4845 && id <= 4881) { |
|
196 |
uint height = FioReadByte(); |
|
197 |
uint width = FioReadWord(); |
|
198 |
Sprite *sprite; |
|
199 |
byte *dest; |
|
200 |
||
201 |
num = width * height; |
|
202 |
sprite = (Sprite *)AllocSprite(sizeof(*sprite) + num); |
|
203 |
sc->ptr = sprite; |
|
204 |
sprite->height = height; |
|
205 |
sprite->width = width; |
|
206 |
sprite->x_offs = FioReadWord(); |
|
207 |
sprite->y_offs = FioReadWord(); |
|
208 |
||
209 |
dest = sprite->data; |
|
210 |
while (num > 0) { |
|
211 |
int8 i = FioReadByte(); |
|
212 |
if (i >= 0) { |
|
213 |
num -= i; |
|
214 |
for (; i > 0; --i) *dest++ = FioReadByte(); |
|
215 |
} else { |
|
216 |
const byte* rel = dest - (((i & 7) << 8) | FioReadByte()); |
|
217 |
i = -(i >> 3); |
|
218 |
num -= i; |
|
219 |
for (; i > 0; --i) *dest++ = *rel++; |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
return sc->ptr; |
|
224 |
} |
|
225 |
||
226 |
if (!real_sprite) { |
|
227 |
static byte warning_level = 0; |
|
228 |
DEBUG(sprite, warning_level, "Tried to load real sprite #%d as a non sprite. Probable cause: NewGRF interference", id); |
|
229 |
warning_level = 6; |
|
9626 | 230 |
} |
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
231 |
|
9626 | 232 |
SpriteLoaderGrf sprite_loader; |
233 |
SpriteLoader::Sprite sprite; |
|
0 | 234 |
|
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
235 |
if (!sprite_loader.LoadSprite(&sprite, sc->grf_name, file_pos)) return NULL; |
9626 | 236 |
if (id == 142) sprite.height = 10; // Compensate for a TTD bug |
237 |
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite); |
|
238 |
free(sprite.data); |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
239 |
|
9626 | 240 |
return sc->ptr; |
0 | 241 |
} |
242 |
||
243 |
||
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
244 |
bool LoadNextSprite(int load_index, byte file_index, uint file_sprite_id) |
0 | 245 |
{ |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
246 |
SpriteCache *sc; |
2321
455694cdbada
(svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents:
2319
diff
changeset
|
247 |
uint32 file_pos = FioGetPos() | (file_index << 24); |
0 | 248 |
|
2342
c19fb4f2df30
(svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents:
2340
diff
changeset
|
249 |
if (!ReadSpriteHeaderSkipData()) return false; |
361
ad7a042ee0eb
(svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents:
184
diff
changeset
|
250 |
|
3591
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
251 |
if (load_index >= MAX_SPRITES) { |
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
252 |
error("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES); |
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
253 |
} |
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
254 |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
255 |
sc = AllocateSpriteCache(load_index); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
256 |
sc->file_pos = file_pos; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
257 |
sc->ptr = NULL; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
258 |
sc->lru = 0; |
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
259 |
sc->id = file_sprite_id; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
260 |
|
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
261 |
const char *fio_grf_name = FioGetFilename(); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
262 |
const char *t = strrchr(fio_grf_name, PATHSEPCHAR); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
263 |
char *grf_name; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
264 |
if (t == NULL) grf_name = strdup(fio_grf_name); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
265 |
else grf_name = strdup(t); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
266 |
/* Make the string lowercase and strip extension */ |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
267 |
char *t2 = strrchr(grf_name, '.'); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
268 |
if (t2 != NULL) *t2 = '\0'; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
269 |
strtolower(grf_name); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
270 |
|
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
271 |
free((char *)sc->grf_name); |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
272 |
sc->grf_name = grf_name; |
0 | 273 |
|
274 |
return true; |
|
275 |
} |
|
276 |
||
2407 | 277 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
278 |
void DupSprite(SpriteID old_spr, SpriteID new_spr) |
2407 | 279 |
{ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
280 |
SpriteCache *scold = GetSpriteCache(old_spr); |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
281 |
SpriteCache *scnew = AllocateSpriteCache(new_spr); |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
282 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
283 |
scnew->file_pos = scold->file_pos; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
284 |
scnew->ptr = NULL; |
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
285 |
scnew->id = scold->id; |
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9628
diff
changeset
|
286 |
free((char *)scnew->grf_name); |
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
287 |
scnew->grf_name = strdup(scold->grf_name); |
2407 | 288 |
} |
289 |
||
290 |
||
2340
0a9f3eeccb96
(svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents:
2339
diff
changeset
|
291 |
void SkipSprites(uint count) |
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
292 |
{ |
2329
f68428464540
(svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents:
2321
diff
changeset
|
293 |
for (; count > 0; --count) { |
2342
c19fb4f2df30
(svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents:
2340
diff
changeset
|
294 |
if (!ReadSpriteHeaderSkipData()) return; |
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
295 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
296 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
297 |
|
0 | 298 |
|
299 |
#define S_FREE_MASK 1 |
|
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
300 |
|
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
301 |
static inline MemBlock* NextBlock(MemBlock* block) |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
302 |
{ |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
303 |
return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK)); |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
304 |
} |
0 | 305 |
|
6573 | 306 |
static uint32 GetSpriteCacheUsage() |
0 | 307 |
{ |
4321
b763b7007162
(svn r5974) -Codechange: added casts all around the place to make Windows 64bit happy (michi_cc)
truelight
parents:
3591
diff
changeset
|
308 |
uint32 tot_size = 0; |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
309 |
MemBlock* s; |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
310 |
|
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
311 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
312 |
if (!(s->size & S_FREE_MASK)) tot_size += s->size; |
0 | 313 |
|
314 |
return tot_size; |
|
315 |
} |
|
316 |
||
317 |
||
6573 | 318 |
void IncreaseSpriteLRU() |
0 | 319 |
{ |
9574 | 320 |
/* Increase all LRU values */ |
0 | 321 |
if (_sprite_lru_counter > 16384) { |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
322 |
SpriteID i; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
323 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5150
diff
changeset
|
324 |
DEBUG(sprite, 3, "Fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage()); |
0 | 325 |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
326 |
for (i = 0; i != _spritecache_items; i++) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
327 |
SpriteCache *sc = GetSpriteCache(i); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
328 |
if (sc->ptr != NULL) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
329 |
if (sc->lru >= 0) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
330 |
sc->lru = -1; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
331 |
} else if (sc->lru != -32768) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
332 |
sc->lru--; |
0 | 333 |
} |
334 |
} |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
335 |
} |
0 | 336 |
_sprite_lru_counter = 0; |
337 |
} |
|
338 |
||
9574 | 339 |
/* Compact sprite cache every now and then. */ |
0 | 340 |
if (++_compact_cache_counter >= 740) { |
341 |
CompactSpriteCache(); |
|
342 |
_compact_cache_counter = 0; |
|
343 |
} |
|
344 |
} |
|
345 |
||
9574 | 346 |
/** Called when holes in the sprite cache should be removed. |
347 |
* That is accomplished by moving the cached data. */ |
|
6573 | 348 |
static void CompactSpriteCache() |
0 | 349 |
{ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
350 |
MemBlock *s; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
351 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5150
diff
changeset
|
352 |
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
|
353 |
|
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
354 |
for (s = _spritecache_ptr; s->size != 0;) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
355 |
if (s->size & S_FREE_MASK) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
356 |
MemBlock* next = NextBlock(s); |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
357 |
MemBlock temp; |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
358 |
SpriteID i; |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
359 |
|
9574 | 360 |
/* Since free blocks are automatically coalesced, this should hold true. */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
361 |
assert(!(next->size & S_FREE_MASK)); |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
362 |
|
9574 | 363 |
/* If the next block is the sentinel block, we can safely return */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
364 |
if (next->size == 0) |
0 | 365 |
break; |
366 |
||
9574 | 367 |
/* Locate the sprite belonging to the next pointer. */ |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
368 |
for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
369 |
assert(i != _spritecache_items); |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
370 |
} |
0 | 371 |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
372 |
GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry |
9574 | 373 |
/* Swap this and the next block */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
374 |
temp = *s; |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
375 |
memmove(s, next, next->size); |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
376 |
s = NextBlock(s); |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
377 |
*s = temp; |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
378 |
|
9574 | 379 |
/* Coalesce free blocks */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
380 |
while (NextBlock(s)->size & S_FREE_MASK) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
381 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 382 |
} |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
383 |
} else { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
384 |
s = NextBlock(s); |
0 | 385 |
} |
386 |
} |
|
387 |
} |
|
388 |
||
6573 | 389 |
static void DeleteEntryFromSpriteCache() |
0 | 390 |
{ |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
391 |
SpriteID i; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
392 |
uint best = UINT_MAX; |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
393 |
MemBlock* s; |
0 | 394 |
int cur_lru; |
395 |
||
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5150
diff
changeset
|
396 |
DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage()); |
0 | 397 |
|
398 |
cur_lru = 0xffff; |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
399 |
for (i = 0; i != _spritecache_items; i++) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
400 |
SpriteCache *sc = GetSpriteCache(i); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
401 |
if (sc->ptr != NULL && sc->lru < cur_lru) { |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
402 |
cur_lru = sc->lru; |
0 | 403 |
best = i; |
404 |
} |
|
405 |
} |
|
406 |
||
9574 | 407 |
/* Display an error message and die, in case we found no sprite at all. |
408 |
* This shouldn't really happen, unless all sprites are locked. */ |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
409 |
if (best == (uint)-1) |
0 | 410 |
error("Out of sprite memory"); |
411 |
||
9574 | 412 |
/* Mark the block as free (the block must be in use) */ |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
413 |
s = (MemBlock*)GetSpriteCache(best)->ptr - 1; |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
414 |
assert(!(s->size & S_FREE_MASK)); |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
415 |
s->size |= S_FREE_MASK; |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
416 |
GetSpriteCache(best)->ptr = NULL; |
0 | 417 |
|
9574 | 418 |
/* And coalesce adjacent free blocks */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
419 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
420 |
if (s->size & S_FREE_MASK) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
421 |
while (NextBlock(s)->size & S_FREE_MASK) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
422 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 423 |
} |
424 |
} |
|
425 |
} |
|
426 |
} |
|
427 |
||
9626 | 428 |
void* AllocSprite(size_t mem_req) |
0 | 429 |
{ |
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
430 |
mem_req += sizeof(MemBlock); |
0 | 431 |
|
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
432 |
/* Align this to an uint32 boundary. This also makes sure that the 2 least |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
433 |
* bits are not used, so we could use those for other things. */ |
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2407
diff
changeset
|
434 |
mem_req = ALIGN(mem_req, sizeof(uint32)); |
0 | 435 |
|
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
436 |
for (;;) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
437 |
MemBlock* s; |
0 | 438 |
|
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
439 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
440 |
if (s->size & S_FREE_MASK) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
441 |
size_t cur_size = s->size & ~S_FREE_MASK; |
0 | 442 |
|
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
443 |
/* Is the block exactly the size we need or |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
444 |
* big enough for an additional free block? */ |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
445 |
if (cur_size == mem_req || |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
446 |
cur_size >= mem_req + sizeof(MemBlock)) { |
9574 | 447 |
/* Set size and in use */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
448 |
s->size = mem_req; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
449 |
|
9574 | 450 |
/* Do we need to inject a free block too? */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
451 |
if (cur_size != mem_req) { |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
452 |
NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK; |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
453 |
} |
0 | 454 |
|
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
455 |
return s->data; |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
456 |
} |
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
457 |
} |
0 | 458 |
} |
459 |
||
9574 | 460 |
/* Reached sentinel, but no block found yet. Delete some old entry. */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
461 |
DeleteEntryFromSpriteCache(); |
0 | 462 |
} |
463 |
} |
|
464 |
||
465 |
||
9627 | 466 |
const void *GetRawSprite(SpriteID sprite, bool real_sprite) |
0 | 467 |
{ |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
468 |
SpriteCache *sc; |
2014
0230ed9186bc
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents:
1891
diff
changeset
|
469 |
void* p; |
0 | 470 |
|
5953
7000c5bdd70b
(svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents:
5860
diff
changeset
|
471 |
assert(sprite < _spritecache_items); |
0 | 472 |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
473 |
sc = GetSpriteCache(sprite); |
0 | 474 |
|
9574 | 475 |
/* Update LRU */ |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
476 |
sc->lru = ++_sprite_lru_counter; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
477 |
|
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
478 |
p = sc->ptr; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
479 |
|
9574 | 480 |
/* Load the sprite, if it is not loaded, yet */ |
9627 | 481 |
if (p == NULL) p = ReadSprite(sc, sprite, real_sprite); |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
482 |
return p; |
0 | 483 |
} |
484 |
||
961 | 485 |
|
6573 | 486 |
void GfxInitSpriteMem() |
0 | 487 |
{ |
9574 | 488 |
/* initialize sprite cache heap */ |
9626 | 489 |
if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(_sprite_cache_size * 1024 * 1024); |
0 | 490 |
|
9574 | 491 |
/* A big free block */ |
9626 | 492 |
_spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK; |
9574 | 493 |
/* Sentinel block (identified by size == 0) */ |
1353
48b59d472641
(svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents:
1352
diff
changeset
|
494 |
NextBlock(_spritecache_ptr)->size = 0; |
0 | 495 |
|
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
496 |
/* Reset the spritecache 'pool' */ |
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9628
diff
changeset
|
497 |
for (uint i = 0; i < _spritecache_items; i++) free((char *)_spritecache[i].grf_name); |
5755
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
498 |
free(_spritecache); |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
499 |
_spritecache_items = 0; |
5db1cb757991
(svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents:
5752
diff
changeset
|
500 |
_spritecache = NULL; |
0 | 501 |
|
2340
0a9f3eeccb96
(svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents:
2339
diff
changeset
|
502 |
_compact_cache_counter = 0; |
0 | 503 |
} |