OpenTTD Source: src/spriteloader/sprite_file.cpp Source File

OpenTTD Source 14.0-RC3

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 byte _grf_cont_v2_sig[8] = {'G', 'R', 'F', 0x82, 0x0D, 0x0A, 0x1A, 0x0A};
15
20 static byte 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 (uint i = 0; i < lengthof(_grf_cont_v2_sig); i++) {
28 if (file.ReadByte() != _grf_cont_v2_sig[i]) 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 {
48 this->container_version = GetGRFContainerVersion(*this);
49 this->content_begin = this->GetPos();
50 }
SpriteFile::container_version
byte container_version
Container format of the sprite file.
Definition: sprite_file_type.hpp:21
GetGRFContainerVersion
static byte GetGRFContainerVersion(SpriteFile &file)
Get the container version of the currently opened GRF file.
Definition: sprite_file.cpp:20
RandomAccessFile::ReadByte
byte ReadByte()
Read a byte from the file.
Definition: random_access_file.cpp:101
SpriteFile::content_begin
size_t content_begin
The begin of the content of the sprite file, i.e. after the container metadata.
Definition: sprite_file_type.hpp:22
sprite_file_type.hpp
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition: random_access_file.cpp:74
SpriteFile
RandomAccessFile with some extra information specific for sprite files.
Definition: sprite_file_type.hpp:19
RandomAccessFile::ReadWord
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: random_access_file.cpp:118
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:84
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
_grf_cont_v2_sig
const byte _grf_cont_v2_sig[8]
Signature of a container version 2 GRF.
SpriteFile::SpriteFile
SpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
Create the SpriteFile.
Definition: sprite_file.cpp:45
RandomAccessFile
A file from which bytes, words and double words are read in (potentially) a random order.
Definition: random_access_file_type.h:22