OpenTTD Source: src/saveload/animated_tile_sl.cpp Source File

OpenTTD Source 15.0-beta1

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"
13#include "compat/animated_tile_sl_compat.h"
14
15#include "../tile_type.h"
16
17#include "../safeguards.h"
18
19extern std::vector<TileIndex> _animated_tiles;
20
21static const SaveLoad _animated_tile_desc[] = {
22 SLEG_VECTOR("tiles", _animated_tiles, SLE_UINT32),
23};
24
25 struct ANITChunkHandler : ChunkHandler {
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 */
39 if (IsSavegameVersionBefore(SLV_80)) {
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
51 if (IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY)) {
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
68static const ANITChunkHandler ANIT;
69static const ChunkHandlerRef animated_tile_chunk_handlers[] = {
70 ANIT,
71};
72
73extern const ChunkHandlerTable _animated_tile_chunk_handlers(animated_tile_chunk_handlers);
_animated_tiles
std::vector< TileIndex > _animated_tiles
The table/list with animated tiles.
Definition animated_tile.cpp:20
animated_tile_sl_compat.h
Loading for animated_tile chunks before table headers were added.
_animated_tile_sl_compat
const SaveLoadCompat _animated_tile_sl_compat[]
Original field order for _animated_tile_desc.
Definition animated_tile_sl_compat.h:16
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:1924
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:659
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition saveload.cpp:1030
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:782
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:351
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1786
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition saveload.cpp:1978
saveload.h
Functions/types related to saving and loading games.
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:509
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:512
SLEG_VECTOR
#define SLEG_VECTOR(name, variable, type)
Storage of a global vector of SL_VAR elements in every savegame version.
Definition saveload.h:1234
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1263
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition saveload.h:46
SLV_80
@ SLV_80
80 11228
Definition saveload.h:139
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition saveload.h:332
ANITChunkHandler
Definition animated_tile_sl.cpp:25
ANITChunkHandler::Save
void Save() const override
Save the chunk.
Definition animated_tile_sl.cpp:28
ANITChunkHandler::Load
void Load() const override
Load the chunk.
Definition animated_tile_sl.cpp:36
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:463
SaveLoad
SaveLoad type struct.
Definition saveload.h:717
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >