OpenTTD Source: src/newgrf/newgrf_act1.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 GrfSpecFeature 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_gps.skip_sprites = num_sets * num_ents;
49 GrfMsg(1, "NewSpriteSet: Unsupported feature 0x{:02X}, skipping {} sprites", feature, _cur_gps.skip_sprites);
50 return;
51 }
52
53 _cur_gps.AddSpriteSets(feature, _cur_gps.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_gps.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_gps.nfo_line++;
61 LoadNextSprite(_cur_gps.spriteid++, *_cur_gps.file, _cur_gps.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_gps.skip_sprites = num_sets * num_ents;
80
81 GrfMsg(3, "SkipAct1: Skipping {} sprites", _cur_gps.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); }
ByteReader
Class to read from a NewGRF file.
Definition newgrf_bytereader.h:18
ByteReader::ReadExtendedByte
uint16_t ReadExtendedByte()
Read a single Extended Byte (8 or 16 bits).
Definition newgrf_bytereader.h:56
ByteReader::ReadByte
uint8_t ReadByte()
Read a single byte (8 bits).
Definition newgrf_bytereader.h:34
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:68
newgrf_bytereader.h
NewGRF buffer reader definition.
newgrf_internal.h
NewGRF internal processing state.
LoadNextSprite
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
Definition spritecache.cpp:614
GrfActionHandler
GRF action handler.
Definition newgrf_internal.h:37
GrfProcessingState::file
SpriteFile * file
File of currently processed GRF file.
Definition newgrf_internal.h:66
GrfProcessingState::nfo_line
uint32_t nfo_line
Currently processed pseudo sprite number in the GRF.
Definition newgrf_internal.h:69
GrfProcessingState::spriteid
SpriteID spriteid
First available SpriteID for loading realsprites.
Definition newgrf_internal.h:63
GrfProcessingState::AddSpriteSets
void AddSpriteSets(GrfSpecFeature feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents)
Records new spritesets.
Definition newgrf_internal.h:98
GrfProcessingState::skip_sprites
int skip_sprites
Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file)
Definition newgrf_internal.h:72