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