OpenTTD Source 20250521-master-g82876c25e0
sprite_file.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 "sprite_file_type.hpp"
12#include "../safeguards.h"
13
15extern const std::array<uint8_t, 8> _grf_cont_v2_sig = {'G', 'R', 'F', 0x82, 0x0D, 0x0A, 0x1A, 0x0A};
16
22{
23 size_t pos = file.GetPos();
24
25 if (file.ReadWord() == 0) {
26 /* Check for GRF container version 2, which is identified by the bytes
27 * '47 52 46 82 0D 0A 1A 0A' at the start of the file. */
28 for (const auto &expected_sig_byte : _grf_cont_v2_sig) {
29 if (file.ReadByte() != expected_sig_byte) return 0; // Invalid format
30 }
31
32 return 2;
33 }
34
35 /* Container version 1 has no header, rewind to start. */
36 file.SeekTo(pos, SEEK_SET);
37 return 1;
38}
39
46SpriteFile::SpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
47 : RandomAccessFile(filename, subdir), palette_remap(palette_remap)
48{
50 this->content_begin = this->GetPos();
51}
A file from which bytes, words and double words are read in (potentially) a random order.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
uint8_t ReadByte()
Read a byte from the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
RandomAccessFile with some extra information specific for sprite files.
SpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
Create the SpriteFile.
uint8_t container_version
Container format of the sprite file.
size_t content_begin
The begin of the content of the sprite file, i.e. after the container metadata.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
static uint8_t GetGRFContainerVersion(SpriteFile &file)
Get the container version of the currently opened GRF file.
const std::array< uint8_t, 8 > _grf_cont_v2_sig
Signature of a container version 2 GRF.
Random Access File specialised for accessing sprites.