OpenTTD Source  12.0-beta2
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 #include "../core/alloc_func.hpp"
17 #include "../core/smallvec_type.hpp"
18 
19 #include "../safeguards.h"
20 
21 extern std::vector<TileIndex> _animated_tiles;
22 
23 static const SaveLoad _animated_tile_desc[] = {
24  SLEG_VECTOR("tiles", _animated_tiles, SLE_UINT32),
25 };
26 
28  ANITChunkHandler() : ChunkHandler('ANIT', CH_TABLE) {}
29 
30  void Save() const override
31  {
32  SlTableHeader(_animated_tile_desc);
33 
34  SlSetArrayIndex(0);
35  SlGlobList(_animated_tile_desc);
36  }
37 
38  void Load() const override
39  {
40  /* Before version 80 we did NOT have a variable length animated tile table */
42  /* In pre version 6, we has 16bit per tile, now we have 32bit per tile, convert it ;) */
43  TileIndex anim_list[256];
44  SlCopy(anim_list, 256, IsSavegameVersionBefore(SLV_6) ? (SLE_FILE_U16 | SLE_VAR_U32) : SLE_UINT32);
45 
46  for (int i = 0; i < 256; i++) {
47  if (anim_list[i] == 0) break;
48  _animated_tiles.push_back(anim_list[i]);
49  }
50  return;
51  }
52 
54  size_t count = SlGetFieldLength() / sizeof(_animated_tiles.front());
55  _animated_tiles.clear();
56  _animated_tiles.resize(_animated_tiles.size() + count);
57  SlCopy(_animated_tiles.data(), count, SLE_UINT32);
58  return;
59  }
60 
61  const std::vector<SaveLoad> slt = SlCompatTableHeader(_animated_tile_desc, _animated_tile_sl_compat);
62 
63  if (SlIterateArray() == -1) return;
64  SlGlobList(slt);
65  if (SlIterateArray() != -1) SlErrorCorrupt("Too many ANIT entries");
66  }
67 };
68 
69 
70 static const ANITChunkHandler ANIT;
71 static const ChunkHandlerRef animated_tile_chunk_handlers[] = {
72  ANIT,
73 };
74 
75 extern const ChunkHandlerTable _animated_tile_chunk_handlers(animated_tile_chunk_handlers);
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
animated_tile_sl_compat.h
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:336
SLEG_VECTOR
#define SLEG_VECTOR(name, variable, type)
Storage of a global vector of SL_VAR elements in every savegame version.
Definition: saveload.h:992
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:443
ANITChunkHandler::Load
void Load() const override
Load the chunk.
Definition: animated_tile_sl.cpp:38
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1151
saveload.h
_animated_tiles
std::vector< TileIndex > _animated_tiles
The table/list with animated tiles.
Definition: animated_tile.cpp:20
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
ANITChunkHandler::Save
void Save() const override
Save the chunk.
Definition: animated_tile_sl.cpp:30
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:2083
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
ANITChunkHandler
Definition: animated_tile_sl.cpp:27
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:792
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:50
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
_animated_tile_sl_compat
const SaveLoadCompat _animated_tile_sl_compat[]
Original field order for _animated_tile_desc.
Definition: animated_tile_sl_compat.h:16
SLV_80
@ SLV_80
80 11228
Definition: saveload.h:143
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670