author | darkvater |
Fri, 12 Nov 2004 21:51:34 +0000 | |
changeset 365 | 5752af05ed75 |
parent 364 | 9e93d102fd4a |
child 366 | 9d2630b8b4de |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "gfx.h" |
|
4 |
#include "fileio.h" |
|
5 |
#include <ctype.h> |
|
6 |
||
7 |
#define SPRITECACHE_ID 0xF00F0006 |
|
8 |
#define SPRITE_CACHE_SIZE 1024*1024 |
|
9 |
||
10 |
||
11 |
//#define WANT_SPRITESIZES |
|
12 |
#define WANT_NEW_LRU |
|
13 |
//#define WANT_LOCKED |
|
14 |
||
15 |
||
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
|
16 |
/* These are used in grfspecial.c: */ |
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
|
17 |
|
0 | 18 |
int _skip_sprites = 0; |
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
|
19 |
int _replace_sprites_count[16]; |
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
|
20 |
int _replace_sprites_offset[16]; |
0 | 21 |
|
22 |
static const char *_cur_grffile; |
|
23 |
static int _skip_specials; |
|
24 |
static SpriteHdr _cur_sprite; |
|
25 |
||
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
|
26 |
|
0 | 27 |
static byte *_sprite_ptr[NUM_SPRITES]; |
28 |
static uint16 _sprite_size[NUM_SPRITES]; |
|
29 |
static uint32 _sprite_file_pos[NUM_SPRITES]; |
|
30 |
||
31 |
// This one is probably not needed. |
|
32 |
#if defined(WANT_LOCKED) |
|
33 |
static bool _sprite_locked[NUM_SPRITES]; |
|
34 |
#endif |
|
35 |
||
36 |
#if defined(WANT_NEW_LRU) |
|
37 |
static int16 _sprite_lru_new[NUM_SPRITES]; |
|
38 |
#else |
|
39 |
static uint16 _sprite_lru[NUM_SPRITES]; |
|
40 |
static uint16 _sprite_lru_cur[NUM_SPRITES]; |
|
41 |
#endif |
|
42 |
||
43 |
#ifdef WANT_SPRITESIZES |
|
44 |
static int8 _sprite_xoffs[NUM_SPRITES]; |
|
45 |
static int8 _sprite_yoffs[NUM_SPRITES]; |
|
46 |
static uint16 _sprite_xsize[NUM_SPRITES]; |
|
47 |
static uint8 _sprite_ysize[NUM_SPRITES]; |
|
48 |
#endif |
|
49 |
||
50 |
static uint _sprite_lru_counter; |
|
51 |
static byte *_spritecache_ptr; |
|
52 |
static uint32 _spritecache_size; |
|
53 |
static int _compact_cache_counter; |
|
54 |
||
55 |
||
56 |
static const char * const _filename_list[] = { |
|
57 |
"TRG1R.GRF", |
|
58 |
"TRGIR.GRF", |
|
59 |
"signalsw.grf", //0x1320 - 0x1405 inclusive |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
60 |
// "openttd.grf", //0x1406 - |
0 | 61 |
NULL |
62 |
}; |
|
63 |
||
64 |
static const char * const _landscape_filenames[] = { |
|
65 |
"TRGCR.GRF", |
|
66 |
"TRGHR.GRF", |
|
67 |
"TRGTR.GRF" |
|
68 |
}; |
|
69 |
||
70 |
||
71 |
#include "table/landscape_sprite.h" |
|
72 |
||
73 |
static const uint16 * const _landscape_spriteindexes[] = { |
|
74 |
_landscape_spriteindexes_1, |
|
75 |
_landscape_spriteindexes_2, |
|
76 |
_landscape_spriteindexes_3, |
|
77 |
}; |
|
78 |
||
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
79 |
static const uint16 * const _slopes_spriteindexes[] = { |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
80 |
_slopes_spriteindexes_0, |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
81 |
_slopes_spriteindexes_1, |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
82 |
_slopes_spriteindexes_2, |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
83 |
_slopes_spriteindexes_3, |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
84 |
}; |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
85 |
|
0 | 86 |
static void CompactSpriteCache(); |
87 |
||
88 |
void DecodeSpecialSprite(const char *filename, int num, int load_index); |
|
89 |
||
90 |
static void ReadSpriteHeaderSkipData(int num, int load_index) |
|
91 |
{ |
|
92 |
byte type; |
|
93 |
int8 i; |
|
94 |
int deaf = 0; |
|
95 |
||
96 |
if (_skip_sprites) { |
|
97 |
if (_skip_sprites > 0) |
|
98 |
_skip_sprites--; |
|
99 |
deaf = 1; |
|
100 |
} |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
101 |
|
0 | 102 |
type = FioReadByte(); |
364
9e93d102fd4a
(svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents:
361
diff
changeset
|
103 |
_cur_sprite.info = type; |
0 | 104 |
if (type == 0xFF) { |
105 |
/* We need to really skip only special sprites in the deaf |
|
106 |
* mode. It won't hurt to proceed regular sprites as usual |
|
107 |
* because if no special sprite referencing to them is |
|
108 |
* processed, they themselves are never referenced and loaded |
|
109 |
* on their own. */ |
|
110 |
if (_skip_specials || deaf) { |
|
111 |
FioSkipBytes(num); |
|
112 |
} else { |
|
113 |
DecodeSpecialSprite(_cur_grffile, num, load_index); |
|
114 |
} |
|
115 |
return; |
|
116 |
} |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
117 |
|
0 | 118 |
#ifdef WANT_SPRITESIZES |
119 |
_cur_sprite.height = FioReadByte(); |
|
120 |
_cur_sprite.width = FioReadWord(); |
|
121 |
_cur_sprite.x_offs = FioReadWord(); |
|
122 |
_cur_sprite.y_offs = FioReadWord(); |
|
123 |
#else |
|
124 |
FioSkipBytes(7); |
|
125 |
#endif |
|
126 |
num -= 8; |
|
127 |
if (num == 0) |
|
128 |
return; |
|
129 |
||
130 |
if (type & 2) { |
|
131 |
FioSkipBytes(num); |
|
132 |
return; |
|
133 |
} |
|
134 |
||
135 |
while (num) { |
|
136 |
i = FioReadByte(); |
|
137 |
if (i>=0) { |
|
138 |
num -= i; |
|
139 |
FioSkipBytes(i); |
|
140 |
} else { |
|
141 |
i = -(i >> 3); |
|
142 |
num -= i; |
|
143 |
FioReadByte(); |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
static void ReadSprite(int num, byte *dest) |
|
149 |
{ |
|
150 |
byte type; |
|
151 |
byte *rel; |
|
152 |
int8 i; |
|
153 |
int j, dist; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
154 |
|
0 | 155 |
type = FioReadByte(); |
156 |
/* We've decoded special sprites when reading headers. */ |
|
157 |
if (type != 0xFF) { |
|
158 |
/* read sprite hdr */ |
|
159 |
*dest++ = type; |
|
160 |
for(j=0; j!=7; j++) |
|
161 |
*dest++ = FioReadByte(); |
|
162 |
num -= 8; |
|
163 |
} |
|
164 |
||
165 |
if (type & 2) { |
|
166 |
while (num--) |
|
167 |
*dest++ = FioReadByte(); |
|
168 |
return; |
|
169 |
} |
|
170 |
||
171 |
while (num) { |
|
172 |
i = FioReadByte(); |
|
173 |
if (i>=0) { |
|
174 |
num -= i; |
|
175 |
while (i--) |
|
176 |
*dest++ = FioReadByte(); |
|
177 |
} else { |
|
178 |
dist = -(((i&7)<<8)|FioReadByte()); |
|
179 |
i = -(i >> 3); |
|
180 |
num -= i; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
181 |
|
0 | 182 |
rel = &dest[dist]; |
183 |
while (i--) |
|
184 |
*dest++ = *rel++; |
|
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
||
190 |
static bool LoadNextSprite(int load_index, byte file_index) |
|
191 |
{ |
|
192 |
uint16 size; |
|
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
|
193 |
uint32 file_pos; |
0 | 194 |
|
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
|
195 |
if ((size = FioReadWord()) == 0) |
0 | 196 |
return false; |
197 |
||
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
|
198 |
file_pos = FioGetPos() | (file_index << 24); |
0 | 199 |
|
200 |
ReadSpriteHeaderSkipData(size, load_index); |
|
201 |
||
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
|
202 |
if ((_replace_sprites_count[0] > 0) && (_cur_sprite.info != 0xFF)) { |
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
|
203 |
int count = _replace_sprites_count[0]; |
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
|
204 |
int offset = _replace_sprites_offset[0]; |
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
|
205 |
|
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
|
206 |
_replace_sprites_offset[0]++; |
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
|
207 |
_replace_sprites_count[0]--; |
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
|
208 |
|
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
|
209 |
if ((offset + count) <= NUM_SPRITES) { |
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
|
210 |
load_index = offset; |
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
|
211 |
} else { |
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
|
212 |
DEBUG(spritecache, 1) ("Sprites to be replaced are out of range: %x+%x", |
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
|
213 |
count, offset); |
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
|
214 |
_replace_sprites_offset[0] = 0; |
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
|
215 |
_replace_sprites_count[0] = 0; |
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
|
216 |
} |
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
|
217 |
|
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
|
218 |
if (_replace_sprites_count[0] == 0) { |
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
|
219 |
int i; |
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
|
220 |
|
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
|
221 |
for (i = 0; i < 15; i++) { |
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
|
222 |
_replace_sprites_count[i] = _replace_sprites_count[i + 1]; |
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
|
223 |
_replace_sprites_offset[i] = _replace_sprites_offset[i + 1]; |
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
|
224 |
} |
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
|
225 |
_replace_sprites_count[i] = 0; |
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
|
226 |
_replace_sprites_offset[i] = 0; |
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
|
227 |
} |
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
|
228 |
} |
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
|
229 |
|
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
|
230 |
_sprite_size[load_index] = size; |
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
|
231 |
_sprite_file_pos[load_index] = file_pos; |
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
|
232 |
|
0 | 233 |
#ifdef WANT_SPRITESIZES |
234 |
_sprite_xsize[load_index] = _cur_sprite.width; |
|
235 |
_sprite_ysize[load_index] = _cur_sprite.height; |
|
236 |
||
237 |
_sprite_xoffs[load_index] = _cur_sprite.x_offs; |
|
238 |
_sprite_yoffs[load_index] = _cur_sprite.y_offs; |
|
239 |
#endif |
|
240 |
||
241 |
_sprite_ptr[load_index] = NULL; |
|
242 |
#if defined(WANT_LOCKED) |
|
243 |
_sprite_locked[load_index] = false; |
|
244 |
#endif |
|
245 |
||
246 |
#if defined(WANT_NEW_LRU) |
|
247 |
_sprite_lru_new[load_index] = 0; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
248 |
#else |
0 | 249 |
_sprite_lru[load_index] = 0xFFFF; |
250 |
_sprite_lru_cur[load_index] = 0; |
|
251 |
#endif |
|
252 |
||
253 |
return true; |
|
254 |
} |
|
255 |
||
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
256 |
static void SkipSprites(int count) |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
257 |
{ |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
258 |
while(count>0) |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
259 |
{ |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
260 |
uint16 size; |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
261 |
if ( (size = FioReadWord()) == 0) |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
262 |
return; |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
263 |
|
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
264 |
ReadSpriteHeaderSkipData(size, NUM_SPRITES-1); |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
265 |
count--; |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
266 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
267 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
268 |
|
0 | 269 |
// Checks, if trg1r.grf is the Windows version |
270 |
static bool CheckGrfFile() |
|
271 |
{ |
|
272 |
byte check; |
|
273 |
FioSeekToFile(38); // Byte 38 has the value 0x21 in Windows version, 0x07 in DOS |
|
274 |
check = FioReadWord(); |
|
275 |
FioSeekToFile(0); |
|
276 |
return (check==0x21); |
|
277 |
} |
|
278 |
||
279 |
static int LoadGrfFile(const char *filename, int load_index, int file_index) |
|
280 |
{ |
|
281 |
int load_index_org = load_index; |
|
282 |
||
283 |
FioOpenFile(file_index, filename); |
|
284 |
_cur_grffile = filename; |
|
365
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
285 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
286 |
/* Thou shalt use LoadNewGrfFile() if thou loadeth a GRF file that |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
287 |
* might contain some special sprites. */ |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
288 |
_skip_specials = 1; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
289 |
_skip_sprites = 0; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
290 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
291 |
DEBUG(spritecache, 2) ("Reading grf-file ``%s''", _cur_grffile); |
0 | 292 |
|
293 |
if(file_index==0 && !_ignore_wrong_grf) |
|
294 |
if(!CheckGrfFile()) |
|
295 |
error("Wrong version of grf files!\nThe Windows 95 edition of Transport Tycoon Deluxe is required to play OTTD!\n(you can disable this message by starting with the \"-i\" switch."); |
|
296 |
||
297 |
while (LoadNextSprite(load_index, file_index)) { |
|
298 |
load_index++; |
|
299 |
if (load_index >= NUM_SPRITES) { |
|
300 |
error("Too many sprites. Recompile with higher NUM_SPRITES value or remove some custom GRF files."); |
|
301 |
} |
|
302 |
} |
|
303 |
||
365
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
304 |
return load_index - load_index_org; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
305 |
} |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
306 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
307 |
static int LoadNewGrfFile(const char *filename, int load_index, int file_index) |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
308 |
{ |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
309 |
/* XXX: Is it better to fervently follow the num_sprites information or |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
310 |
* be tolerant and comply with more/less sprites too? --pasky */ |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
311 |
int num_sprites = 0; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
312 |
int i; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
313 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
314 |
FioOpenFile(file_index, filename); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
315 |
_cur_grffile = filename; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
316 |
_skip_specials = 0; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
317 |
_skip_sprites = 0; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
318 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
319 |
DEBUG(spritecache, 2) ("Reading newgrf-file ``%s'' [offset: %u]", |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
320 |
_cur_grffile, load_index); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
321 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
322 |
{ |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
323 |
int length; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
324 |
byte type; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
325 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
326 |
length = FioReadWord(); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
327 |
type = FioReadByte(); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
328 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
329 |
if ((length == 4) && (type == 0xFF)) { |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
330 |
num_sprites = FioReadDword(); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
331 |
} else { |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
332 |
error("Custom .grf has invalid format."); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
333 |
} |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
334 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
335 |
/* Ignore last sprite, it's only used to mark end-of-file */ |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
336 |
num_sprites--; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
337 |
load_index++; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
338 |
} |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
339 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
340 |
if ((load_index + num_sprites) > NUM_SPRITES) |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
341 |
error("Too many sprites (%x). Recompile with higher NUM_SPRITES value or remove some custom GRF files.", |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
342 |
load_index + num_sprites); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
343 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
344 |
for (i = 0; i < num_sprites; i++) { |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
345 |
if (!LoadNextSprite(load_index + i, file_index)) |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
346 |
error("NEWGRF: Header was talking abount %d sprites, but only %d found..", |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
347 |
num_sprites, i); |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
348 |
} |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
349 |
|
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
|
350 |
/* Clean up. */ |
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
|
351 |
_skip_sprites = 0; |
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
|
352 |
memset(_replace_sprites_count, 0, 16 * sizeof(*_replace_sprites_count)); |
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
|
353 |
memset(_replace_sprites_offset, 0, 16 * sizeof(*_replace_sprites_offset)); |
0 | 354 |
|
365
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
355 |
return num_sprites; |
0 | 356 |
} |
357 |
||
358 |
static void LoadGrfIndexed(const char *filename, const uint16 *index_tbl, int file_index) |
|
359 |
{ |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
360 |
int start; |
0 | 361 |
|
362 |
FioOpenFile(file_index, filename); |
|
74
d23a80ef6361
(svn r75) -Add proper crediting to graphics artists to about box.
darkvater
parents:
37
diff
changeset
|
363 |
_cur_grffile = filename; |
365
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
364 |
_skip_specials = 1; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
365 |
_skip_sprites = 0; |
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
366 |
|
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
367 |
DEBUG(spritecache, 2) ("Reading indexed grf-file ``%s''", _cur_grffile); |
0 | 368 |
|
369 |
for(;(start=*index_tbl++) != 0xffff;) { |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
370 |
int end = *index_tbl++; |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
371 |
if(start==0xfffe) { // skip sprites (amount in second var) |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
372 |
SkipSprites(end); |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
373 |
} else { // load sprites and use indexes from start to end |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
374 |
do { |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
375 |
bool b = LoadNextSprite(start, file_index); |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
376 |
assert(b); |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
377 |
} while (++start <= end); |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
378 |
} |
0 | 379 |
} |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
380 |
} |
0 | 381 |
|
382 |
typedef size_t CDECL fread_t(void*,size_t,size_t,FILE*); |
|
383 |
||
384 |
static bool HandleCachedSpriteHeaders(const char *filename, bool read) |
|
385 |
{ |
|
386 |
FILE *f; |
|
387 |
fread_t *proc; |
|
388 |
uint32 hdr; |
|
389 |
||
390 |
if (!_cache_sprites) |
|
391 |
return false; |
|
392 |
||
393 |
if (read) { |
|
394 |
f = fopen(filename, "rb"); |
|
395 |
proc = fread; |
|
396 |
||
397 |
if (f == NULL) |
|
398 |
return false; |
|
399 |
||
400 |
proc(&hdr, sizeof(hdr), 1, f); |
|
401 |
if (hdr != SPRITECACHE_ID) { |
|
402 |
fclose(f); |
|
403 |
return false; |
|
404 |
} |
|
405 |
} else { |
|
406 |
f = fopen(filename, "wb"); |
|
407 |
proc = (fread_t*) fwrite; |
|
408 |
||
409 |
if (f == NULL) |
|
410 |
return false; |
|
411 |
||
412 |
hdr = SPRITECACHE_ID; |
|
413 |
proc(&hdr, sizeof(hdr), 1, f); |
|
414 |
} |
|
415 |
||
416 |
proc(_sprite_size, 1, sizeof(_sprite_size), f); |
|
417 |
proc(_sprite_file_pos, 1, sizeof(_sprite_file_pos), f); |
|
418 |
||
419 |
#if 0 |
|
420 |
proc(_sprite_xsize, 1, sizeof(_sprite_xsize), f); |
|
421 |
proc(_sprite_ysize, 1, sizeof(_sprite_ysize), f); |
|
422 |
proc(_sprite_xoffs, 1, sizeof(_sprite_xoffs), f); |
|
423 |
proc(_sprite_yoffs, 1, sizeof(_sprite_yoffs), f); |
|
424 |
#endif |
|
425 |
||
426 |
#if !defined(WANT_NEW_LRU) |
|
427 |
if (read) |
|
428 |
memset(_sprite_lru, 0xFF, sizeof(_sprite_lru)); |
|
429 |
#endif |
|
430 |
||
431 |
fclose(f); |
|
432 |
return true; |
|
433 |
} |
|
434 |
||
435 |
#define S_DATA(x) (*(uint32*)(x)) |
|
436 |
#define S_FREE_MASK 1 |
|
437 |
#define S_HDRSIZE sizeof(uint32) |
|
438 |
||
439 |
static uint32 GetSpriteCacheUsage() |
|
440 |
{ |
|
441 |
byte *s = _spritecache_ptr; |
|
442 |
size_t cur_size, tot_size = 0; |
|
443 |
for(; (cur_size=S_DATA(s)) != 0; s+=cur_size) { |
|
444 |
if ( cur_size & S_FREE_MASK ) { |
|
445 |
cur_size--; |
|
446 |
} else { |
|
447 |
tot_size += cur_size; |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
return tot_size; |
|
452 |
} |
|
453 |
||
454 |
||
455 |
void IncreaseSpriteLRU() |
|
456 |
{ |
|
457 |
int i; |
|
458 |
||
459 |
// Increase all LRU values |
|
460 |
#if defined(WANT_NEW_LRU) |
|
461 |
if (_sprite_lru_counter > 16384) { |
|
462 |
DEBUG(spritecache, 2) ("fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage()); |
|
463 |
||
464 |
for(i=0; i!=NUM_SPRITES; i++) |
|
465 |
if (_sprite_ptr[i] != NULL) { |
|
466 |
if (_sprite_lru_new[i] >= 0) { |
|
467 |
_sprite_lru_new[i] = -1; |
|
468 |
} else if (_sprite_lru_new[i] != -32768) { |
|
469 |
_sprite_lru_new[i]--; |
|
470 |
} |
|
471 |
} |
|
472 |
_sprite_lru_counter = 0; |
|
473 |
} |
|
474 |
#else |
|
475 |
for(i=0; i!=NUM_SPRITES; i++) |
|
476 |
if (_sprite_ptr[i] != NULL && _sprite_lru[i] != 65535) |
|
477 |
_sprite_lru[i]++; |
|
478 |
// Reset the lru counter. |
|
479 |
_sprite_lru_counter = 0; |
|
480 |
#endif |
|
481 |
||
482 |
// Compact sprite cache every now and then. |
|
483 |
if (++_compact_cache_counter >= 740) { |
|
484 |
CompactSpriteCache(); |
|
485 |
_compact_cache_counter = 0; |
|
486 |
} |
|
487 |
} |
|
488 |
||
489 |
// Called when holes in the sprite cache should be removed. |
|
490 |
// That is accomplished by moving the cached data. |
|
491 |
static void CompactSpriteCache() |
|
492 |
{ |
|
493 |
byte *s, *t; |
|
494 |
size_t size, sizeb, cur_size; |
|
495 |
int i; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
496 |
|
0 | 497 |
DEBUG(spritecache, 2) ("compacting sprite cache, inuse=%d", GetSpriteCacheUsage()); |
498 |
||
499 |
s = _spritecache_ptr; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
500 |
|
0 | 501 |
while (true) { |
502 |
size = S_DATA(s); |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
503 |
|
0 | 504 |
// Only look for free blocks. |
505 |
if (size & S_FREE_MASK) { |
|
506 |
size -= S_FREE_MASK; |
|
507 |
// Since free blocks are automatically coalesced, this should hold true. |
|
508 |
assert(!(S_DATA(s+size) & S_FREE_MASK)); |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
509 |
|
0 | 510 |
// If the next block is the sentinel block, we can safely return |
511 |
if ( (sizeb=S_DATA(s + size)) == 0) |
|
512 |
break; |
|
513 |
||
514 |
// Locate the sprite number belonging to the next pointer. |
|
515 |
for(i=0,t=s+size+S_HDRSIZE; _sprite_ptr[i] != t; i++) {assert(i < NUM_SPRITES);} |
|
516 |
||
517 |
// If it's locked, we must not move it. |
|
518 |
#if defined(WANT_LOCKED) |
|
519 |
if (!_sprite_locked[i]) { |
|
520 |
#endif |
|
521 |
||
522 |
// Offset the sprite pointer by the size of the free block |
|
523 |
_sprite_ptr[i] -= size; |
|
524 |
||
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
525 |
// Move the memory |
0 | 526 |
memcpy_overlapping(s+S_HDRSIZE, s+S_HDRSIZE+size, sizeb - S_HDRSIZE ); |
527 |
||
528 |
// What we just did had the effect of swapping the allocated block with the free block, so we need to update |
|
529 |
// the block pointers. First update the allocated one. It is in use. |
|
530 |
S_DATA(s) = sizeb; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
531 |
|
0 | 532 |
// Then coalesce the free ones that follow. |
533 |
s += sizeb; |
|
534 |
while ((cur_size = S_DATA(s+size)) & S_FREE_MASK) |
|
535 |
size += cur_size - S_FREE_MASK; |
|
536 |
S_DATA(s) = size + S_FREE_MASK; |
|
537 |
continue; |
|
538 |
#if defined(WANT_LOCKED) |
|
539 |
} |
|
540 |
#endif |
|
541 |
} |
|
542 |
// Continue with next block until the sentinel is reached. |
|
543 |
s += size; |
|
544 |
if (size == 0) |
|
545 |
break; |
|
546 |
} |
|
547 |
} |
|
548 |
||
549 |
static void DeleteEntryFromSpriteCache() |
|
550 |
{ |
|
551 |
int i; |
|
552 |
int best = -1; |
|
553 |
byte *s; |
|
554 |
size_t cur_size, cur; |
|
555 |
int cur_lru; |
|
556 |
||
557 |
DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage()); |
|
558 |
||
559 |
#if defined(WANT_NEW_LRU) |
|
560 |
cur_lru = 0xffff; |
|
561 |
for(i=0; i!=NUM_SPRITES; i++) { |
|
562 |
if (_sprite_ptr[i] != 0 && |
|
563 |
_sprite_lru_new[i] < cur_lru |
|
564 |
#if defined(WANT_LOCKED) |
|
565 |
&& !_sprite_locked[i]) { |
|
566 |
#else |
|
567 |
) { |
|
568 |
#endif |
|
569 |
cur_lru = _sprite_lru_new[i]; |
|
570 |
best = i; |
|
571 |
} |
|
572 |
} |
|
573 |
#else |
|
574 |
{ |
|
575 |
uint16 cur_lru = 0, cur_lru_cur = 0xffff; |
|
576 |
for(i=0; i!=NUM_SPRITES; i++) { |
|
577 |
if (_sprite_ptr[i] == 0 || |
|
578 |
#if defined(WANT_LOCKED) |
|
579 |
_sprite_locked[i] || |
|
580 |
#endif |
|
581 |
_sprite_lru[i] < cur_lru) |
|
582 |
continue; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
583 |
|
0 | 584 |
// Found a sprite with a higher LRU value, then remember it. |
585 |
if (_sprite_lru[i] != cur_lru) { |
|
586 |
cur_lru = _sprite_lru[i]; |
|
587 |
best = i; |
|
588 |
||
589 |
// Else if both sprites were very recently referenced, compare by the cur value instead. |
|
590 |
} else if (cur_lru == 0 && _sprite_lru_cur[i] <= cur_lru_cur) { |
|
591 |
cur_lru_cur = _sprite_lru_cur[i]; |
|
592 |
cur_lru = _sprite_lru[i]; |
|
593 |
best = i; |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
#endif |
|
598 |
||
599 |
// Display an error message and die, in case we found no sprite at all. |
|
600 |
// This shouldn't really happen, unless all sprites are locked. |
|
601 |
if (best == -1) |
|
602 |
error("Out of sprite memory"); |
|
603 |
||
604 |
// Mark the block as free (the block must be in use) |
|
605 |
s = _sprite_ptr[best]; |
|
606 |
assert(!(S_DATA(s - S_HDRSIZE) & S_FREE_MASK)); |
|
607 |
S_DATA(s - S_HDRSIZE) += S_FREE_MASK; |
|
608 |
_sprite_ptr[best] = NULL; |
|
609 |
||
610 |
// And coalesce adjacent free blocks |
|
611 |
s = _spritecache_ptr; |
|
612 |
for(; (cur_size=S_DATA(s)) != 0; s+=cur_size) { |
|
613 |
if ( cur_size & S_FREE_MASK ) { |
|
614 |
while ((cur=S_DATA(s+cur_size-S_FREE_MASK)) & S_FREE_MASK) { |
|
615 |
cur_size += cur - S_FREE_MASK; |
|
616 |
S_DATA(s) = cur_size; |
|
617 |
} |
|
618 |
cur_size--; |
|
619 |
} |
|
620 |
} |
|
621 |
} |
|
622 |
||
623 |
static byte *LoadSpriteToMem(int sprite) |
|
624 |
{ |
|
625 |
byte *s; |
|
626 |
size_t mem_req, cur_size; |
|
627 |
||
628 |
DEBUG(spritecache, 9) ("load sprite %d", sprite); |
|
629 |
||
630 |
restart: |
|
631 |
// Number of needed bytes |
|
632 |
mem_req = _sprite_size[sprite] + S_HDRSIZE; |
|
633 |
||
634 |
// Align this to an uint32 boundary. This also makes sure that the 2 least bit are not used, |
|
635 |
// so we could use those for other things. |
|
636 |
mem_req = (mem_req + sizeof(uint32) - 1) & ~(sizeof(uint32) - 1); |
|
637 |
||
638 |
s = _spritecache_ptr; |
|
639 |
for(;;) { |
|
640 |
for(;;) { |
|
641 |
cur_size = S_DATA(s); |
|
642 |
if (! (cur_size & S_FREE_MASK) ) break; |
|
643 |
||
644 |
cur_size -= S_FREE_MASK; |
|
645 |
||
646 |
// Now s points at a free block. |
|
647 |
// The block is exactly the size we need? |
|
648 |
if (cur_size != mem_req) { |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
649 |
|
0 | 650 |
// No.. is it too small? |
651 |
if (cur_size < mem_req + S_HDRSIZE) |
|
652 |
break; |
|
653 |
||
654 |
// Block was big enough, and we need to inject a free block too. |
|
655 |
S_DATA(s + mem_req) = cur_size - mem_req + S_FREE_MASK; |
|
656 |
||
657 |
} |
|
658 |
// Set size and in use |
|
659 |
S_DATA(s) = mem_req; |
|
660 |
||
661 |
_sprite_ptr[sprite] = (s += S_HDRSIZE); |
|
662 |
||
663 |
FioSeekToFile(_sprite_file_pos[sprite]); |
|
664 |
ReadSprite(_sprite_size[sprite], s); |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
665 |
|
0 | 666 |
// Patch the height to compensate for a TTD bug? |
667 |
if (sprite == 142) { s[1] = 10; } |
|
668 |
||
669 |
// Return sprite ptr |
|
670 |
return s; |
|
671 |
} |
|
672 |
||
673 |
// Reached sentinel, but no block found yet. Need to delete some old entries. |
|
674 |
if (cur_size == 0) { |
|
675 |
DeleteEntryFromSpriteCache(); |
|
676 |
goto restart; |
|
677 |
} |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
678 |
|
0 | 679 |
s += cur_size; |
680 |
} |
|
681 |
} |
|
682 |
||
683 |
#if defined(NEW_ROTATION) |
|
684 |
#define X15(x) else if (s >= x && s < (x+15)) { s = _rotate_tile_sprite[s - x] + x; } |
|
685 |
#define X19(x) else if (s >= x && s < (x+19)) { s = _rotate_tile_sprite[s - x] + x; } |
|
686 |
#define MAP(from,to,map) else if (s >= from && s <= to) { s = map[s - from] + from; } |
|
687 |
||
688 |
||
689 |
uint RotateSprite(uint s) |
|
690 |
{ |
|
691 |
static const byte _rotate_tile_sprite[19] = { 0,2,4,6,8,10,12,14,1,3,5,7,9,11,13,17,18,16,15 }; |
|
692 |
static const byte _coast_map[9] = {0, 4, 3, 1, 2, 6, 8, 5, 7}; |
|
693 |
static const byte _fence_map[6] = {1, 0, 5, 4, 3, 2}; |
|
694 |
||
695 |
if (0); |
|
696 |
X19(752) |
|
697 |
X15(990-1) |
|
698 |
X19(3924) |
|
699 |
X19(3943) |
|
700 |
X19(3962) |
|
701 |
X19(3981) |
|
702 |
X19(4000) |
|
703 |
X19(4023) |
|
704 |
X19(4042) |
|
705 |
MAP(4061,4069,_coast_map) |
|
706 |
X19(4126) |
|
707 |
X19(4145) |
|
708 |
X19(4164) |
|
709 |
X19(4183) |
|
710 |
X19(4202) |
|
711 |
X19(4221) |
|
712 |
X19(4240) |
|
713 |
X19(4259) |
|
714 |
X19(4259) |
|
715 |
X19(4278) |
|
716 |
MAP(4090, 4095, _fence_map) |
|
717 |
MAP(4096, 4101, _fence_map) |
|
718 |
MAP(4102, 4107, _fence_map) |
|
719 |
MAP(4108, 4113, _fence_map) |
|
720 |
MAP(4114, 4119, _fence_map) |
|
721 |
MAP(4120, 4125, _fence_map) |
|
722 |
return s; |
|
723 |
} |
|
724 |
#endif |
|
725 |
||
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
726 |
byte *GetSpritePtr(uint sprite) |
0 | 727 |
{ |
728 |
byte *p; |
|
729 |
||
730 |
assert(sprite < NUM_SPRITES); |
|
731 |
||
732 |
#if defined(NEW_ROTATION) |
|
733 |
sprite = RotateSprite(sprite); |
|
734 |
#endif |
|
735 |
||
736 |
// Update LRU |
|
737 |
#if defined(WANT_NEW_LRU) |
|
738 |
_sprite_lru_new[sprite] = ++_sprite_lru_counter; |
|
739 |
#else |
|
740 |
_sprite_lru_cur[sprite] = ++_sprite_lru_counter; |
|
741 |
_sprite_lru[sprite] = 0; |
|
742 |
#endif |
|
743 |
||
744 |
// Check if the sprite is loaded already? |
|
745 |
p = _sprite_ptr[sprite]; |
|
746 |
if (p == NULL) |
|
747 |
p = LoadSpriteToMem(sprite); // No, need to load it. |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
748 |
return p; |
0 | 749 |
} |
750 |
||
751 |
byte _sprite_page_to_load = 0xFF; |
|
752 |
||
753 |
static const char * const _cached_filenames[4] = { |
|
754 |
"cached_sprites.xxx", |
|
755 |
"cached_sprites.xx1", |
|
756 |
"cached_sprites.xx2", |
|
757 |
"cached_sprites.xx3", |
|
758 |
}; |
|
759 |
||
143 | 760 |
#define OPENTTD_SPRITES_COUNT 70 |
0 | 761 |
static const uint16 _openttd_grf_indexes[] = { |
762 |
SPR_OPENTTD_BASE+0, SPR_OPENTTD_BASE+7, // icons etc |
|
763 |
98,98, // euro symbol medium size |
|
764 |
546,546, // euro symbol large size |
|
765 |
SPR_OPENTTD_BASE+10, SPR_OPENTTD_BASE+57, // more icons |
|
766 |
648, 648, // nordic char: æ |
|
767 |
616, 616, // nordic char: Æ |
|
768 |
666, 666, // nordic char: Ø |
|
769 |
634, 634, // nordic char: Ø |
|
143 | 770 |
SPR_OPENTTD_BASE+62, SPR_OPENTTD_BASE + OPENTTD_SPRITES_COUNT, // more icons |
0 | 771 |
0xffff, |
772 |
}; |
|
773 |
||
774 |
static void LoadSpriteTables() |
|
775 |
{ |
|
776 |
int i,j; |
|
777 |
||
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
778 |
/* |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
779 |
* Note for developers: |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
780 |
* Keep in mind that when you add a LoadGrfIndexed in the 'if'-section below |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
781 |
* that you should also add the corresponding FioOpenFile to the 'else'-section |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
782 |
* below. |
364
9e93d102fd4a
(svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents:
361
diff
changeset
|
783 |
* |
9e93d102fd4a
(svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents:
361
diff
changeset
|
784 |
* TODO: |
9e93d102fd4a
(svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents:
361
diff
changeset
|
785 |
* I think we can live entirely without Indexed GRFs, but I have to |
9e93d102fd4a
(svn r552) -newgrf: Include bits forgotten when merging octo's ReplaceSprites support - it would replace even special sprites in the way now. (pasky)
darkvater
parents:
361
diff
changeset
|
786 |
* invest that further. --octo |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
787 |
*/ |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
788 |
|
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
789 |
// Try to load the sprites from cache |
0 | 790 |
if (!HandleCachedSpriteHeaders(_cached_filenames[_opt.landscape], true)) { |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
791 |
// We do not have the sprites in cache yet, or cache is disabled |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
792 |
// So just load all files from disk.. |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
793 |
|
0 | 794 |
int load_index = 0; |
795 |
for(i=0; _filename_list[i] != NULL; i++) { |
|
796 |
load_index += LoadGrfFile(_filename_list[i], load_index, (byte)i); |
|
797 |
} |
|
798 |
||
799 |
LoadGrfIndexed("openttd.grf", _openttd_grf_indexes, i++); |
|
800 |
||
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
801 |
if (_sprite_page_to_load != 0) |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
802 |
LoadGrfIndexed(_landscape_filenames[_sprite_page_to_load-1], _landscape_spriteindexes[_sprite_page_to_load-1], i++); |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
803 |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
804 |
LoadGrfIndexed("trkfoundw.grf", _slopes_spriteindexes[_opt.landscape], i++); |
0 | 805 |
|
806 |
load_index = SPR_CANALS_BASE; |
|
807 |
load_index += LoadGrfFile("canalsw.grf", load_index, i++); |
|
142 | 808 |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
809 |
load_index = SPR_OPENTTD_BASE + OPENTTD_SPRITES_COUNT + 1; |
142 | 810 |
|
0 | 811 |
for(j=0; j!=lengthof(_newgrf_files) && _newgrf_files[j]; j++) |
365
5752af05ed75
(svn r553) -newgrf: Special routine for loading newgrf files. LoadNewGrfFile() introduced; will get more handy when loading stages will be introduced (octo and pasky).
darkvater
parents:
364
diff
changeset
|
812 |
load_index += LoadNewGrfFile(_newgrf_files[j], load_index, i++); |
0 | 813 |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
814 |
// If needed, save the cache to file |
0 | 815 |
HandleCachedSpriteHeaders(_cached_filenames[_opt.landscape], false); |
816 |
} else { |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
817 |
// We have sprites cached. We just loaded the cached files |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
818 |
// now we only have to open a file-pointer to all the original grf-files |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
819 |
// This is very important. Not all sprites are in the cache. So sometimes |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
820 |
// the game needs to load the sprite from disk. When the file is not |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
821 |
// open it can not read. So all files that are in the 'if'-section |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
822 |
// above should also be in this 'else'-section. |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
823 |
// |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
824 |
// NOTE: the order of the files must be identical as in the section above!! |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
825 |
|
0 | 826 |
for(i=0; _filename_list[i] != NULL; i++) |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
827 |
FioOpenFile(i,_filename_list[i]); |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
828 |
|
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
829 |
FioOpenFile(i++, "openttd.grf"); |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
830 |
|
0 | 831 |
if (_sprite_page_to_load != 0) |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
832 |
FioOpenFile(i++, _landscape_filenames[_sprite_page_to_load-1]); |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
833 |
|
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
834 |
FioOpenFile(i++, "trkfoundw.grf"); |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
835 |
FioOpenFile(i++, "canalsw.grf"); |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
836 |
|
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
837 |
// FIXME: if a user changes his newgrf's, the cached-sprites gets |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
838 |
// invalid. We should have some kind of check for this. |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
839 |
// The best solution for this is to delete the cached-sprites.. but how |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
840 |
// do we detect it? |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
841 |
for(j=0; j!=lengthof(_newgrf_files) && _newgrf_files[j]; j++) |
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
842 |
FioOpenFile(i++, _newgrf_files[j]); |
0 | 843 |
} |
844 |
||
845 |
_compact_cache_counter = 0; |
|
846 |
} |
|
847 |
||
848 |
void GfxInitSpriteMem(byte *ptr, uint32 size) |
|
849 |
{ |
|
850 |
// initialize sprite cache heap |
|
851 |
_spritecache_ptr = ptr; |
|
852 |
_spritecache_size = size; |
|
853 |
||
854 |
// Sentinel block (identified by size=0) |
|
855 |
S_DATA(ptr + size - S_HDRSIZE) = 0; |
|
856 |
// A big free block |
|
857 |
S_DATA(ptr) = size - S_HDRSIZE + S_FREE_MASK; |
|
858 |
||
859 |
memset(_sprite_ptr, 0, sizeof(_sprite_ptr)); |
|
860 |
} |
|
861 |
||
862 |
||
863 |
void GfxLoadSprites() { |
|
864 |
static byte *_sprite_mem; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
865 |
|
0 | 866 |
// Need to reload the sprites only if the landscape changed |
867 |
if (_sprite_page_to_load != _opt.landscape) { |
|
868 |
_sprite_page_to_load = _opt.landscape; |
|
869 |
||
870 |
// Sprite cache |
|
871 |
DEBUG(spritecache, 1) ("Loading sprite set %d.", _sprite_page_to_load); |
|
872 |
||
873 |
// Reuse existing memory? |
|
874 |
if (_sprite_mem == NULL) _sprite_mem = malloc(SPRITE_CACHE_SIZE); |
|
875 |
GfxInitSpriteMem(_sprite_mem, SPRITE_CACHE_SIZE); |
|
876 |
LoadSpriteTables(); |
|
877 |
GfxInitPalettes(); |
|
878 |
} |
|
879 |
} |
|
880 |
||
881 |
||
882 |
const SpriteDimension *GetSpriteDimension(uint sprite) |
|
883 |
{ |
|
884 |
static SpriteDimension sd_static; |
|
885 |
SpriteDimension *sd; |
|
886 |
||
887 |
#ifndef WANT_SPRITESIZES |
|
888 |
byte *p; |
|
889 |
||
890 |
p = _sprite_ptr[sprite]; |
|
891 |
if (p == NULL) |
|
892 |
p = GetSpritePtr(sprite); |
|
893 |
||
894 |
/* decode sprite header */ |
|
895 |
sd = &sd_static; |
|
896 |
sd->xoffs = (int16)READ_LE_UINT16(&((SpriteHdr*)p)->x_offs); |
|
897 |
sd->yoffs = (int16)READ_LE_UINT16(&((SpriteHdr*)p)->y_offs); |
|
898 |
sd->xsize = READ_LE_UINT16(&((SpriteHdr*)p)->width); |
|
899 |
sd->ysize = ((SpriteHdr*)p)->height; |
|
900 |
#else |
|
901 |
sd = &sd_static; |
|
902 |
sd->xoffs = _sprite_xoffs[sprite]; |
|
903 |
sd->yoffs = _sprite_yoffs[sprite]; |
|
904 |
sd->xsize = _sprite_xsize[sprite]; |
|
905 |
sd->ysize = _sprite_ysize[sprite]; |
|
906 |
#endif |
|
907 |
/* sd->xoffs = _sprite_xoffs[sprite]; |
|
908 |
sd->yoffs = _sprite_yoffs[sprite]; |
|
909 |
sd->xsize = _sprite_xsize[sprite]; |
|
910 |
sd->ysize = _sprite_ysize[sprite]; |
|
911 |
*/ |
|
912 |
return sd; |
|
913 |
} |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
914 |