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