OpenTTD Source  20241121-master-g67a0fccfad
animated_tile_sl.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 
12 #include "saveload.h"
14 
15 #include "../tile_type.h"
16 
17 #include "../safeguards.h"
18 
19 extern std::vector<TileIndex> _animated_tiles;
20 
21 static const SaveLoad _animated_tile_desc[] = {
22  SLEG_VECTOR("tiles", _animated_tiles, SLE_UINT32),
23 };
24 
26  ANITChunkHandler() : ChunkHandler('ANIT', CH_TABLE) {}
27 
28  void Save() const override
29  {
30  SlTableHeader(_animated_tile_desc);
31 
32  SlSetArrayIndex(0);
33  SlGlobList(_animated_tile_desc);
34  }
35 
36  void Load() const override
37  {
38  /* Before version 80 we did NOT have a variable length animated tile table */
40  /* In pre version 6, we has 16bit per tile, now we have 32bit per tile, convert it ;) */
41  TileIndex anim_list[256];
42  SlCopy(anim_list, 256, IsSavegameVersionBefore(SLV_6) ? (SLE_FILE_U16 | SLE_VAR_U32) : SLE_UINT32);
43 
44  for (int i = 0; i < 256; i++) {
45  if (anim_list[i] == 0) break;
46  _animated_tiles.push_back(anim_list[i]);
47  }
48  return;
49  }
50 
52  size_t count = SlGetFieldLength() / sizeof(_animated_tiles.front());
53  _animated_tiles.clear();
54  _animated_tiles.resize(_animated_tiles.size() + count);
55  SlCopy(_animated_tiles.data(), count, SLE_UINT32);
56  return;
57  }
58 
59  const std::vector<SaveLoad> slt = SlCompatTableHeader(_animated_tile_desc, _animated_tile_sl_compat);
60 
61  if (SlIterateArray() == -1) return;
62  SlGlobList(slt);
63  if (SlIterateArray() != -1) SlErrorCorrupt("Too many ANIT entries");
64  }
65 };
66 
67 
68 static const ANITChunkHandler ANIT;
69 static const ChunkHandlerRef animated_tile_chunk_handlers[] = {
70  ANIT,
71 };
72 
73 extern const ChunkHandlerTable _animated_tile_chunk_handlers(animated_tile_chunk_handlers);
std::vector< TileIndex > _animated_tiles
The table/list with animated tiles.
Loading for animated_tile chunks before table headers were added.
const SaveLoadCompat _animated_tile_sl_compat[]
Original field order for _animated_tile_desc.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1029
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:781
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:351
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1893
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1755
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1947
Functions/types related to saving and loading games.
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:507
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:510
#define SLEG_VECTOR(name, variable, type)
Storage of a global vector of SL_VAR elements in every savegame version.
Definition: saveload.h:1210
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1239
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
@ SLV_80
80 11228
Definition: saveload.h:139
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:332
void Save() const override
Save the chunk.
void Load() const override
Load the chunk.
Handlers and description of chunk.
Definition: saveload.h:461
SaveLoad type struct.
Definition: saveload.h:713