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