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