Files @ r28520:f9aebe299cae
Branch filter:

Location: cpp/openttd-patchpack/source/src/gfxinit.cpp - annotation

Patric Stout
Codechange: MacOS already has MIN/MAX macros defined

This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6179:c0508e7aefec
r5584:545d748cc681
r7805:7b5af263f275
r5584:545d748cc681
r12767:3d23682b3fda
r5584:545d748cc681
r8123:dde0a9a84019
r21132:989d17eeea3b
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r21774:fed2f59787b8
r28328:6b936d36ce25
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r5584:545d748cc681
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r5584:545d748cc681
r5584:545d748cc681
r18009:875021ab0452
r5584:545d748cc681
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r5584:545d748cc681
r5584:545d748cc681
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r25413:71c42f3ee7f2
r18009:875021ab0452
r18009:875021ab0452
r27159:693b9615eaea
r5584:545d748cc681
r5584:545d748cc681
r6908:7e03190d606c
r5584:545d748cc681
r25414:d3e1262b4805
r5584:545d748cc681
r25655:1030dcb7eb52
r5584:545d748cc681
r25413:71c42f3ee7f2
r27160:192bc991c2e6
r25413:71c42f3ee7f2
r19020:24d71054fe13
r19020:24d71054fe13
r25413:71c42f3ee7f2
r27160:192bc991c2e6
r19020:24d71054fe13
r19020:24d71054fe13
r25413:71c42f3ee7f2
r5584:545d748cc681
r6908:7e03190d606c
r5584:545d748cc681
r27160:192bc991c2e6
r5584:545d748cc681
r5584:545d748cc681
r25655:1030dcb7eb52
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r23023:7b8669afd1db
r25413:71c42f3ee7f2
r18009:875021ab0452
r18009:875021ab0452
r27159:693b9615eaea
r7772:260dc421b905
r7772:260dc421b905
r18009:875021ab0452
r18009:875021ab0452
r25414:d3e1262b4805
r18009:875021ab0452
r25655:1030dcb7eb52
r18009:875021ab0452
r25413:71c42f3ee7f2
r27160:192bc991c2e6
r25413:71c42f3ee7f2
r19020:24d71054fe13
r19020:24d71054fe13
r25413:71c42f3ee7f2
r27160:192bc991c2e6
r19020:24d71054fe13
r19020:24d71054fe13
r7772:260dc421b905
r7772:260dc421b905
r7772:260dc421b905
r8432:f08b2af9e176
r25590:6544087fd869
r8432:f08b2af9e176
r18009:875021ab0452
r8432:f08b2af9e176
r7772:260dc421b905
r7772:260dc421b905
r7772:260dc421b905
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r23607:36c15679007d
r12674:8254ab83ecad
r12668:95f5d46a2c14
r7841:0e193e9f1f5b
r25655:1030dcb7eb52
r10062:9a719b26b56e
r27434:07a7eae56362
r27434:07a7eae56362
r12761:a366bd42c075
r15594:ba7e42fbae24
r27434:07a7eae56362
r12759:edb51729a58e
r19019:2eb1d8782aac
r27434:07a7eae56362
r7841:0e193e9f1f5b
r27434:07a7eae56362
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r12674:8254ab83ecad
r12761:a366bd42c075
r27434:07a7eae56362
r12759:edb51729a58e
r24521:57ec498b9221
r12759:edb51729a58e
r12759:edb51729a58e
r27434:07a7eae56362
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r27434:07a7eae56362
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r17630:7d818445376d
r6247:96e840dbefcc
r5584:545d748cc681
r12668:95f5d46a2c14
r5584:545d748cc681
r25414:d3e1262b4805
r5584:545d748cc681
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r25414:d3e1262b4805
r5584:545d748cc681
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r9413:fcf267325763
r18009:875021ab0452
r12668:95f5d46a2c14
r9413:fcf267325763
r25414:d3e1262b4805
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7882:cdf03a4ac84a
r22523:2816509b7317
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r22523:2816509b7317
r22523:2816509b7317
r25416:06155ad35def
r25416:06155ad35def
r22523:2816509b7317
r22523:2816509b7317
r22523:2816509b7317
r22523:2816509b7317
r22523:2816509b7317
r28058:09cc94fbd2eb
r28059:72edd14954dc
r22523:2816509b7317
r15630:3f138941b535
r22523:2816509b7317
r22523:2816509b7317
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r25414:d3e1262b4805
r7882:cdf03a4ac84a
r22525:8b64738b95fa
r27659:efb2f4024b6e
r25416:06155ad35def
r25655:1030dcb7eb52
r22525:8b64738b95fa
r22525:8b64738b95fa
r22525:8b64738b95fa
r22525:8b64738b95fa
r22525:8b64738b95fa
r7882:cdf03a4ac84a
r22523:2816509b7317
r14667:a7ee51a0cfe3
r7882:cdf03a4ac84a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25655:1030dcb7eb52
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25655:1030dcb7eb52
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r27160:192bc991c2e6
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r25363:aeaaaf1c6dce
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r21132:989d17eeea3b
r18161:7dcc16c70ffe
r21132:989d17eeea3b
r18161:7dcc16c70ffe
r21131:60551c858781
r21132:989d17eeea3b
r18161:7dcc16c70ffe
r21131:60551c858781
r21132:989d17eeea3b
r21131:60551c858781
r21421:7897be36f196
r21421:7897be36f196
r21421:7897be36f196
r21421:7897be36f196
r21421:7897be36f196
r21421:7897be36f196
r21131:60551c858781
r24948:1a610af95c16
r23607:36c15679007d
r18162:11349b218a54
r21131:60551c858781
r18162:11349b218a54
r24931:be251d09ce0e
r24931:be251d09ce0e
r18162:11349b218a54
r21131:60551c858781
r21421:7897be36f196
r21131:60551c858781
r21132:989d17eeea3b
r21131:60551c858781
r21131:60551c858781
r24910:137e83ad9c3d
r24910:137e83ad9c3d
r21131:60551c858781
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21131:60551c858781
r21132:989d17eeea3b
r22870:f3bd5fe2429d
r22870:f3bd5fe2429d
r22870:f3bd5fe2429d
r21132:989d17eeea3b
r21131:60551c858781
r21131:60551c858781
r21132:989d17eeea3b
r21131:60551c858781
r21131:60551c858781
r21131:60551c858781
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21131:60551c858781
r21131:60551c858781
r21131:60551c858781
r21131:60551c858781
r22568:df7ef8916103
r22568:df7ef8916103
r22568:df7ef8916103
r23607:36c15679007d
r21131:60551c858781
r25015:8deed239577a
r25363:aeaaaf1c6dce
r21131:60551c858781
r21131:60551c858781
r21131:60551c858781
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r21132:989d17eeea3b
r25324:71e56110ba9f
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r17630:7d818445376d
r6247:96e840dbefcc
r5584:545d748cc681
r25655:1030dcb7eb52
r5584:545d748cc681
r18161:7dcc16c70ffe
r24942:614da862fdaf
r19116:609e37882a23
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15707:d8c7fbf1db5d
r15707:d8c7fbf1db5d
r5584:545d748cc681
r9994:9032cf79748f
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28017:b8af78becb18
r10037:46fc9ca9c918
r18368:c399e53439ff
r12668:95f5d46a2c14
r28017:b8af78becb18
r28012:8dd5df36d9d9
r28017:b8af78becb18
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r24433:05e89d8e369b
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r27526:da755c10127c
r24433:05e89d8e369b
r10037:46fc9ca9c918
r15044:862b76e20a87
r10037:46fc9ca9c918
r10037:46fc9ca9c918
r19020:24d71054fe13
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28058:09cc94fbd2eb
r28060:53690f1ef210
r28060:53690f1ef210
r28060:53690f1ef210
r28060:53690f1ef210
r28060:53690f1ef210
r28060:53690f1ef210
r28060:53690f1ef210
r28060:53690f1ef210
r28059:72edd14954dc
r28059:72edd14954dc
r28059:72edd14954dc
r28059:72edd14954dc
r28059:72edd14954dc
r28059:72edd14954dc
r28059:72edd14954dc
r28059:72edd14954dc
r28059:72edd14954dc
r28058:09cc94fbd2eb
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r23607:36c15679007d
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r10037:46fc9ca9c918
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r14055:e4168a875df8
r19019:2eb1d8782aac
r12761:a366bd42c075
r17674:8ba1e586d6d1
r17674:8ba1e586d6d1
r17674:8ba1e586d6d1
r10037:46fc9ca9c918
r19019:2eb1d8782aac
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r14055:e4168a875df8
r10037:46fc9ca9c918
r23607:36c15679007d
r12761:a366bd42c075
r24597:afde5721a3b6
r19019:2eb1d8782aac
r12761:a366bd42c075
r27737:728d55b97775
r27391:048886674223
r12761:a366bd42c075
r10037:46fc9ca9c918
r12761:a366bd42c075
r12761:a366bd42c075
r12761:a366bd42c075
r12761:a366bd42c075
r12088:b6616548f47d
r12761:a366bd42c075
r12088:b6616548f47d
r12761:a366bd42c075
r27391:048886674223
r10037:46fc9ca9c918
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r13086:71e929261037
r12676:3124748a27b7
r12676:3124748a27b7
r18368:c399e53439ff
r18368:c399e53439ff
r9994:9032cf79748f
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r23607:36c15679007d
r11049:6eb2c87ed64a
r23607:36c15679007d
r23607:36c15679007d
r19944:25a78576fb5e
r12761:a366bd42c075
r12761:a366bd42c075
r23607:36c15679007d
r14619:fff04644c054
r12761:a366bd42c075
r12761:a366bd42c075
r12668:95f5d46a2c14
r17637:0d7e49507a12
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r11049:6eb2c87ed64a
r11049:6eb2c87ed64a
r12668:95f5d46a2c14
r23607:36c15679007d
r11049:6eb2c87ed64a
r11049:6eb2c87ed64a
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r11934:17c3a38acbd0
r12668:95f5d46a2c14
r11934:17c3a38acbd0
r11934:17c3a38acbd0
r12668:95f5d46a2c14
/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file gfxinit.cpp Initializing of the (GRF) graphics. */

#include "stdafx.h"
#include "fios.h"
#include "newgrf.h"
#include "3rdparty/md5/md5.h"
#include "fontcache.h"
#include "gfx_func.h"
#include "transparency.h"
#include "blitter/factory.hpp"
#include "video/video_driver.hpp"
#include "window_func.h"
#include "palette_func.h"

/* The type of set we're replacing */
#define SET_TYPE "graphics"
#include "base_media_func.h"

#include "table/sprites.h"

#include "safeguards.h"

#include "table/landscape_sprite.h"

/** Offsets for loading the different "replacement" sprites in the files. */
static const SpriteID * const _landscape_spriteindexes[] = {
	_landscape_spriteindexes_arctic,
	_landscape_spriteindexes_tropic,
	_landscape_spriteindexes_toyland,
};

/**
 * Load an old fashioned GRF file.
 * @param filename   The name of the file to open.
 * @param load_index The offset of the first sprite.
 * @param needs_palette_remap Whether the colours in the GRF file need a palette remap.
 * @return The number of loaded sprites.
 */
static uint LoadGrfFile(const std::string &filename, uint load_index, bool needs_palette_remap)
{
	uint load_index_org = load_index;
	uint sprite_id = 0;

	SpriteFile &file = OpenCachedSpriteFile(filename, BASESET_DIR, needs_palette_remap);

	Debug(sprite, 2, "Reading grf-file '{}'", filename);

	byte container_ver = file.GetContainerVersion();
	if (container_ver == 0) UserError("Base grf '{}' is corrupt", filename);
	ReadGRFSpriteOffsets(file);
	if (container_ver >= 2) {
		/* Read compression. */
		byte compression = file.ReadByte();
		if (compression != 0) UserError("Unsupported compression format");
	}

	while (LoadNextSprite(load_index, file, sprite_id)) {
		load_index++;
		sprite_id++;
		if (load_index >= MAX_SPRITES) {
			UserError("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
		}
	}
	Debug(sprite, 2, "Currently {} sprites are loaded", load_index);

	return load_index - load_index_org;
}

/**
 * Load an old fashioned GRF file to replace already loaded sprites.
 * @param filename   The name of the file to open.
 * @param index_tbl  The offsets of each of the sprites.
 * @param needs_palette_remap Whether the colours in the GRF file need a palette remap.
 * @return The number of loaded sprites.
 */
static void LoadGrfFileIndexed(const std::string &filename, const SpriteID *index_tbl, bool needs_palette_remap)
{
	uint start;
	uint sprite_id = 0;

	SpriteFile &file = OpenCachedSpriteFile(filename, BASESET_DIR, needs_palette_remap);

	Debug(sprite, 2, "Reading indexed grf-file '{}'", filename);

	byte container_ver = file.GetContainerVersion();
	if (container_ver == 0) UserError("Base grf '{}' is corrupt", filename);
	ReadGRFSpriteOffsets(file);
	if (container_ver >= 2) {
		/* Read compression. */
		byte compression = file.ReadByte();
		if (compression != 0) UserError("Unsupported compression format");
	}

	while ((start = *index_tbl++) != END) {
		uint end = *index_tbl++;

		do {
			[[maybe_unused]] bool b = LoadNextSprite(start, file, sprite_id);
			assert(b);
			sprite_id++;
		} while (++start <= end);
	}
}

/**
 * Checks whether the MD5 checksums of the files are correct.
 *
 * @note Also checks sample.cat and other required non-NewGRF GRFs for corruption.
 */
void CheckExternalFiles()
{
	if (BaseGraphics::GetUsedSet() == nullptr || BaseSounds::GetUsedSet() == nullptr) return;

	const GraphicsSet *used_set = BaseGraphics::GetUsedSet();

	Debug(grf, 1, "Using the {} base graphics set", used_set->name);

	std::string error_msg;
	auto output_iterator = std::back_inserter(error_msg);
	if (used_set->GetNumInvalid() != 0) {
		/* Not all files were loaded successfully, see which ones */
		fmt::format_to(output_iterator, "Trying to load graphics set '{}', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of README.md.\n\nThe following files are corrupted or missing:\n", used_set->name);
		for (uint i = 0; i < GraphicsSet::NUM_FILES; i++) {
			MD5File::ChecksumResult res = GraphicsSet::CheckMD5(&used_set->files[i], BASESET_DIR);
			if (res != MD5File::CR_MATCH) fmt::format_to(output_iterator, "\t{} is {} ({})\n", used_set->files[i].filename, res == MD5File::CR_MISMATCH ? "corrupt" : "missing", used_set->files[i].missing_warning);
		}
		fmt::format_to(output_iterator, "\n");
	}

	const SoundsSet *sounds_set = BaseSounds::GetUsedSet();
	if (sounds_set->GetNumInvalid() != 0) {
		fmt::format_to(output_iterator, "Trying to load sound set '{}', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of README.md.\n\nThe following files are corrupted or missing:\n", sounds_set->name);

		static_assert(SoundsSet::NUM_FILES == 1);
		/* No need to loop each file, as long as there is only a single
		 * sound file. */
		fmt::format_to(output_iterator, "\t{} is {} ({})\n", sounds_set->files->filename, SoundsSet::CheckMD5(sounds_set->files, BASESET_DIR) == MD5File::CR_MISMATCH ? "corrupt" : "missing", sounds_set->files->missing_warning);
	}

	if (!error_msg.empty()) ShowInfoI(error_msg);
}

/** Actually load the sprite tables. */
static void LoadSpriteTables()
{
	const GraphicsSet *used_set = BaseGraphics::GetUsedSet();

	LoadGrfFile(used_set->files[GFT_BASE].filename, 0, PAL_DOS != used_set->palette);

	/*
	 * The second basic file always starts at the given location and does
	 * contain a different amount of sprites depending on the "type"; DOS
	 * has a few sprites less. However, we do not care about those missing
	 * sprites as they are not shown anyway (logos in intro game).
	 */
	LoadGrfFile(used_set->files[GFT_LOGOS].filename, 4793, PAL_DOS != used_set->palette);

	/*
	 * Load additional sprites for climates other than temperate.
	 * This overwrites some of the temperate sprites, such as foundations
	 * and the ground sprites.
	 */
	if (_settings_game.game_creation.landscape != LT_TEMPERATE) {
		LoadGrfFileIndexed(
			used_set->files[GFT_ARCTIC + _settings_game.game_creation.landscape - 1].filename,
			_landscape_spriteindexes[_settings_game.game_creation.landscape - 1],
			PAL_DOS != used_set->palette
		);
	}

	/* Initialize the unicode to sprite mapping table */
	InitializeUnicodeGlyphMap();

	/*
	 * Load the base and extra NewGRF with OTTD required graphics as first NewGRF.
	 * However, we do not want it to show up in the list of used NewGRFs,
	 * so we have to manually add it, and then remove it later.
	 */
	GRFConfig *top = _grfconfig;

	/* Default extra graphics */
	static const char *master_filename = "OPENTTD.GRF";
	GRFConfig *master = new GRFConfig(master_filename);
	master->palette |= GRFP_GRF_DOS;
	FillGRFDetails(master, false, BASESET_DIR);
	ClrBit(master->flags, GCF_INIT_ONLY);

	/* Baseset extra graphics */
	GRFConfig *extra = new GRFConfig(used_set->GetOrCreateExtraConfig());
	if (extra->num_params == 0) extra->SetParameterDefaults();
	ClrBit(extra->flags, GCF_INIT_ONLY);

	extra->next = top;
	master->next = extra;
	_grfconfig = master;

	LoadNewGRF(SPR_NEWGRFS_BASE, 2);

	uint total_extra_graphics = SPR_NEWGRFS_BASE - SPR_OPENTTD_BASE;
	Debug(sprite, 4, "Checking sprites from fallback grf");
	_missing_extra_graphics = GetSpriteCountForFile(master_filename, SPR_OPENTTD_BASE, SPR_NEWGRFS_BASE);
	Debug(sprite, 1, "{} extra sprites, {} from baseset, {} from fallback", total_extra_graphics, total_extra_graphics - _missing_extra_graphics, _missing_extra_graphics);

	/* The original baseset extra graphics intentionally make use of the fallback graphics.
	 * Let's say everything which provides less than 500 sprites misses the rest intentionally. */
	if (500 + _missing_extra_graphics > total_extra_graphics) _missing_extra_graphics = 0;

	/* Free and remove the top element. */
	delete extra;
	delete master;
	_grfconfig = top;
}


static void RealChangeBlitter(const char *repl_blitter)
{
	const char *cur_blitter = BlitterFactory::GetCurrentBlitter()->GetName();
	if (strcmp(cur_blitter, repl_blitter) == 0) return;

	Debug(driver, 1, "Switching blitter from '{}' to '{}'... ", cur_blitter, repl_blitter);
	Blitter *new_blitter = BlitterFactory::SelectBlitter(repl_blitter);
	if (new_blitter == nullptr) NOT_REACHED();
	Debug(driver, 1, "Successfully switched to {}.", repl_blitter);

	if (!VideoDriver::GetInstance()->AfterBlitterChange()) {
		/* Failed to switch blitter, let's hope we can return to the old one. */
		if (BlitterFactory::SelectBlitter(cur_blitter) == nullptr || !VideoDriver::GetInstance()->AfterBlitterChange()) UserError("Failed to reinitialize video driver. Specify a fixed blitter in the config");
	}

	/* Clear caches that might have sprites for another blitter. */
	VideoDriver::GetInstance()->ClearSystemSprites();
	ClearFontCache();
	GfxClearSpriteCache();
	ReInitAllWindows(false);
}

/**
 * Check blitter needed by NewGRF config and switch if needed.
 * @return False when nothing changed, true otherwise.
 */
static bool SwitchNewGRFBlitter()
{
	/* Never switch if the blitter was specified by the user. */
	if (!_blitter_autodetected) return false;

	/* Null driver => dedicated server => do nothing. */
	if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return false;

	/* Get preferred depth.
	 *  - depth_wanted_by_base: Depth required by the baseset, i.e. the majority of the sprites.
	 *  - depth_wanted_by_grf:  Depth required by some NewGRF.
	 * Both can force using a 32bpp blitter. depth_wanted_by_base is used to select
	 * between multiple 32bpp blitters, which perform differently with 8bpp sprites.
	 */
	uint depth_wanted_by_base = BaseGraphics::GetUsedSet()->blitter == BLT_32BPP ? 32 : 8;
	uint depth_wanted_by_grf = _support8bpp != S8BPP_NONE ? 8 : 32;
	for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
		if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND || HasBit(c->flags, GCF_INIT_ONLY)) continue;
		if (c->palette & GRFP_BLT_32BPP) depth_wanted_by_grf = 32;
	}
	/* We need a 32bpp blitter for font anti-alias. */
	if (HasAntialiasedFonts()) depth_wanted_by_grf = 32;

	/* Search the best blitter. */
	static const struct {
		const char *name;
		uint animation; ///< 0: no support, 1: do support, 2: both
		uint min_base_depth, max_base_depth, min_grf_depth, max_grf_depth;
	} replacement_blitters[] = {
		{ "8bpp-optimized",  2,  8,  8,  8,  8 },
		{ "40bpp-anim",      2,  8, 32,  8, 32 },
#ifdef WITH_SSE
		{ "32bpp-sse4",      0, 32, 32,  8, 32 },
		{ "32bpp-ssse3",     0, 32, 32,  8, 32 },
		{ "32bpp-sse2",      0, 32, 32,  8, 32 },
		{ "32bpp-sse4-anim", 1, 32, 32,  8, 32 },
#endif
		{ "32bpp-optimized", 0,  8, 32,  8, 32 },
#ifdef WITH_SSE
		{ "32bpp-sse2-anim", 1,  8, 32,  8, 32 },
#endif
		{ "32bpp-anim",      1,  8, 32,  8, 32 },
	};

	const bool animation_wanted = HasBit(_display_opt, DO_FULL_ANIMATION);
	const char *cur_blitter = BlitterFactory::GetCurrentBlitter()->GetName();

	for (uint i = 0; i < lengthof(replacement_blitters); i++) {
		if (animation_wanted && (replacement_blitters[i].animation == 0)) continue;
		if (!animation_wanted && (replacement_blitters[i].animation == 1)) continue;

		if (!IsInsideMM(depth_wanted_by_base, replacement_blitters[i].min_base_depth, replacement_blitters[i].max_base_depth + 1)) continue;
		if (!IsInsideMM(depth_wanted_by_grf, replacement_blitters[i].min_grf_depth, replacement_blitters[i].max_grf_depth + 1)) continue;
		const char *repl_blitter = replacement_blitters[i].name;

		if (strcmp(repl_blitter, cur_blitter) == 0) {
			return false;
		}
		if (BlitterFactory::GetBlitterFactory(repl_blitter) == nullptr) continue;

		/* Inform the video driver we want to switch blitter as soon as possible. */
		VideoDriver::GetInstance()->QueueOnMainThread(std::bind(&RealChangeBlitter, repl_blitter));
		break;
	}

	return true;
}

/** Check whether we still use the right blitter, or use another (better) one. */
void CheckBlitter()
{
	if (!SwitchNewGRFBlitter()) return;

	ClearFontCache();
	GfxClearSpriteCache();
	ReInitAllWindows(false);
}

/** Initialise and load all the sprites. */
void GfxLoadSprites()
{
	Debug(sprite, 2, "Loading sprite set {}", _settings_game.game_creation.landscape);

	SwitchNewGRFBlitter();
	VideoDriver::GetInstance()->ClearSystemSprites();
	ClearFontCache();
	GfxInitSpriteMem();
	LoadSpriteTables();
	GfxInitPalettes();

	UpdateCursorSize();
}

GraphicsSet::GraphicsSet()
	: BaseSet<GraphicsSet, MAX_GFT, true>{}, palette{}, blitter{}
{
	// instantiate here, because unique_ptr needs a complete type
}

GraphicsSet::~GraphicsSet()
{
	// instantiate here, because unique_ptr needs a complete type
}

bool GraphicsSet::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename)
{
	bool ret = this->BaseSet<GraphicsSet, MAX_GFT, true>::FillSetDetails(ini, path, full_filename, false);
	if (ret) {
		const IniGroup *metadata = ini.GetGroup("metadata");
		assert(metadata != nullptr); /* ret can't be true if metadata isn't present. */
		const IniItem *item;

		fetch_metadata("palette");
		this->palette = ((*item->value)[0] == 'D' || (*item->value)[0] == 'd') ? PAL_DOS : PAL_WINDOWS;

		/* Get optional blitter information. */
		item = metadata->GetItem("blitter");
		this->blitter = (item != nullptr && (*item->value)[0] == '3') ? BLT_32BPP : BLT_8BPP;
	}
	return ret;
}

/**
 * Return configuration for the extra GRF, or lazily create it.
 * @return NewGRF configuration
 */
GRFConfig &GraphicsSet::GetOrCreateExtraConfig() const
{
	if (!this->extra_cfg) {
		this->extra_cfg.reset(new GRFConfig(this->files[GFT_EXTRA].filename));

		/* We know the palette of the base set, so if the base NewGRF is not
		 * setting one, use the palette of the base set and not the global
		 * one which might be the wrong palette for this base NewGRF.
		 * The value set here might be overridden via action14 later. */
		switch (this->palette) {
			case PAL_DOS:     this->extra_cfg->palette |= GRFP_GRF_DOS;     break;
			case PAL_WINDOWS: this->extra_cfg->palette |= GRFP_GRF_WINDOWS; break;
			default: break;
		}
		FillGRFDetails(this->extra_cfg.get(), false, BASESET_DIR);
	}
	return *this->extra_cfg;
}

bool GraphicsSet::IsConfigurable() const
{
	const GRFConfig &cfg = this->GetOrCreateExtraConfig();
	/* This check is more strict than the one for NewGRF Settings.
	 * There are no legacy basesets with parameters, but without Action14 */
	return !cfg.param_info.empty();
}

void GraphicsSet::CopyCompatibleConfig(const GraphicsSet &src)
{
	const GRFConfig *src_cfg = src.GetExtraConfig();
	if (src_cfg == nullptr || src_cfg->num_params == 0) return;
	GRFConfig &dest_cfg = this->GetOrCreateExtraConfig();
	if (dest_cfg.IsCompatible(src_cfg->version)) return;
	dest_cfg.CopyParams(*src_cfg);
}

/**
 * Calculate and check the MD5 hash of the supplied GRF.
 * @param file The file get the hash of.
 * @param subdir The sub directory to get the files from.
 * @return
 * - #CR_MATCH if the MD5 hash matches
 * - #CR_MISMATCH if the MD5 does not match
 * - #CR_NO_FILE if the file misses
 */
/* static */ MD5File::ChecksumResult GraphicsSet::CheckMD5(const MD5File *file, Subdirectory subdir)
{
	size_t size = 0;
	FILE *f = FioFOpenFile(file->filename, "rb", subdir, &size);
	if (f == nullptr) return MD5File::CR_NO_FILE;

	size_t max = GRFGetSizeOfDataSection(f);

	FioFCloseFile(f);

	return file->CheckMD5(subdir, max);
}


/**
 * Calculate and check the MD5 hash of the supplied filename.
 * @param subdir The sub directory to get the files from
 * @param max_size Only calculate the hash for this many bytes from the file start.
 * @return
 * - #CR_MATCH if the MD5 hash matches
 * - #CR_MISMATCH if the MD5 does not match
 * - #CR_NO_FILE if the file misses
 */
MD5File::ChecksumResult MD5File::CheckMD5(Subdirectory subdir, size_t max_size) const
{
	size_t size;
	FILE *f = FioFOpenFile(this->filename, "rb", subdir, &size);

	if (f == nullptr) return CR_NO_FILE;

	size = std::min(size, max_size);

	Md5 checksum;
	uint8_t buffer[1024];
	MD5Hash digest;
	size_t len;

	while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
		size -= len;
		checksum.Append(buffer, len);
	}

	FioFCloseFile(f);

	checksum.Finish(digest);
	return this->hash == digest ? CR_MATCH : CR_MISMATCH;
}

/** Names corresponding to the GraphicsFileType */
static const char * const _graphics_file_names[] = { "base", "logos", "arctic", "tropical", "toyland", "extra" };

/** Implementation */
template <class T, size_t Tnum_files, bool Tsearch_in_tars>
/* static */ const char * const *BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names = _graphics_file_names;

template <class Tbase_set>
/* static */ bool BaseMedia<Tbase_set>::DetermineBestSet()
{
	if (BaseMedia<Tbase_set>::used_set != nullptr) return true;

	const Tbase_set *best = nullptr;
	for (const Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != nullptr; c = c->next) {
		/* Skip unusable sets */
		if (c->GetNumMissing() != 0) continue;

		if (best == nullptr ||
				(best->fallback && !c->fallback) ||
				best->valid_files < c->valid_files ||
				(best->valid_files == c->valid_files && (
					(best->shortname == c->shortname && best->version < c->version) ||
					(best->palette != PAL_DOS && c->palette == PAL_DOS)))) {
			best = c;
		}
	}

	BaseMedia<Tbase_set>::used_set = best;
	return BaseMedia<Tbase_set>::used_set != nullptr;
}

template <class Tbase_set>
/* static */ const char *BaseMedia<Tbase_set>::GetExtension()
{
	return ".obg"; // OpenTTD Base Graphics
}

INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<GraphicsSet>, GraphicsSet)