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