OpenTTD Source  20241108-master-g80f628063a
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 
14 extern const uint8_t _grf_cont_v2_sig[8] = {'G', 'R', 'F', 0x82, 0x0D, 0x0A, 0x1A, 0x0A};
15 
20 static uint8_t GetGRFContainerVersion(SpriteFile &file)
21 {
22  size_t pos = file.GetPos();
23 
24  if (file.ReadWord() == 0) {
25  /* Check for GRF container version 2, which is identified by the bytes
26  * '47 52 46 82 0D 0A 1A 0A' at the start of the file. */
27  for (const auto &expected_sig_byte : _grf_cont_v2_sig) {
28  if (file.ReadByte() != expected_sig_byte) return 0; // Invalid format
29  }
30 
31  return 2;
32  }
33 
34  /* Container version 1 has no header, rewind to start. */
35  file.SeekTo(pos, SEEK_SET);
36  return 1;
37 }
38 
45 SpriteFile::SpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
46  : RandomAccessFile(filename, subdir), palette_remap(palette_remap)
47 {
49  this->content_begin = this->GetPos();
50 }
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.
Definition: sprite_file.cpp:45
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:115
static uint8_t GetGRFContainerVersion(SpriteFile &file)
Get the container version of the currently opened GRF file.
Definition: sprite_file.cpp:20
const uint8_t _grf_cont_v2_sig[8]
Signature of a container version 2 GRF.
Random Access File specialised for accessing sprites.