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