OpenTTD Source 20250331-master-g3c15e0c889
newgrf_act1.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../spritecache.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
15
16#include "../safeguards.h"
17
18/* Action 0x01 */
19static void NewSpriteSet(ByteReader &buf)
20{
21 /* Basic format: <01> <feature> <num-sets> <num-ent>
22 * Extended format: <01> <feature> 00 <first-set> <num-sets> <num-ent>
23 *
24 * B feature feature to define sprites for
25 * 0, 1, 2, 3: veh-type, 4: train stations
26 * E first-set first sprite set to define
27 * B num-sets number of sprite sets (extended byte in extended format)
28 * E num-ent how many entries per sprite set
29 * For vehicles, this is the number of different
30 * vehicle directions in each sprite set
31 * Set num-dirs=8, unless your sprites are symmetric.
32 * In that case, use num-dirs=4.
33 */
34
35 uint8_t feature = buf.ReadByte();
36 uint16_t num_sets = buf.ReadByte();
37 uint16_t first_set = 0;
38
39 if (num_sets == 0 && buf.HasData(3)) {
40 /* Extended Action1 format.
41 * Some GRFs define zero sets of zero sprites, though there is actually no use in that. Ignore them. */
42 first_set = buf.ReadExtendedByte();
43 num_sets = buf.ReadExtendedByte();
44 }
45 uint16_t num_ents = buf.ReadExtendedByte();
46
47 if (feature >= GSF_END) {
48 _cur.skip_sprites = num_sets * num_ents;
49 GrfMsg(1, "NewSpriteSet: Unsupported feature 0x{:02X}, skipping {} sprites", feature, _cur.skip_sprites);
50 return;
51 }
52
53 _cur.AddSpriteSets(feature, _cur.spriteid, first_set, num_sets, num_ents);
54
55 GrfMsg(7, "New sprite set at {} of feature 0x{:02X}, consisting of {} sets with {} views each (total {})",
56 _cur.spriteid, feature, num_sets, num_ents, num_sets * num_ents
57 );
58
59 for (int i = 0; i < num_sets * num_ents; i++) {
60 _cur.nfo_line++;
61 LoadNextSprite(_cur.spriteid++, *_cur.file, _cur.nfo_line);
62 }
63}
64
65/* Action 0x01 (SKIP) */
66static void SkipAct1(ByteReader &buf)
67{
68 buf.ReadByte();
69 uint16_t num_sets = buf.ReadByte();
70
71 if (num_sets == 0 && buf.HasData(3)) {
72 /* Extended Action1 format.
73 * Some GRFs define zero sets of zero sprites, though there is actually no use in that. Ignore them. */
74 buf.ReadExtendedByte(); // first_set
75 num_sets = buf.ReadExtendedByte();
76 }
77 uint16_t num_ents = buf.ReadExtendedByte();
78
79 _cur.skip_sprites = num_sets * num_ents;
80
81 GrfMsg(3, "SkipAct1: Skipping {} sprites", _cur.skip_sprites);
82}
83
84template <> void GrfActionHandler<0x01>::FileScan(ByteReader &buf) { SkipAct1(buf); }
85template <> void GrfActionHandler<0x01>::SafetyScan(ByteReader &buf) { SkipAct1(buf); }
86template <> void GrfActionHandler<0x01>::LabelScan(ByteReader &buf) { SkipAct1(buf); }
87template <> void GrfActionHandler<0x01>::Init(ByteReader &buf) { SkipAct1(buf); }
88template <> void GrfActionHandler<0x01>::Reserve(ByteReader &buf) { SkipAct1(buf); }
89template <> void GrfActionHandler<0x01>::Activation(ByteReader &buf) { NewSpriteSet(buf); }
Class to read from a NewGRF file.
uint16_t ReadExtendedByte()
Read a single Extended Byte (8 or 16 bits).
uint8_t ReadByte()
Read a single byte (8 bits).
NewGRF buffer reader definition.
NewGRF internal processing state.
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
GRF action handler.
SpriteFile * file
File of currently processed GRF file.
void AddSpriteSets(uint8_t feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents)
Records new spritesets.
uint32_t nfo_line
Currently processed pseudo sprite number in the GRF.
SpriteID spriteid
First available SpriteID for loading realsprites.
int skip_sprites
Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file)