1
0
Fork 0

Codechange: Use std::endian instead of TTD_ENDIAN defines.

pull/13383/head
Peter Nelson 2024-06-11 18:47:16 +01:00 committed by Peter Nelson
parent b70438b76a
commit fb6781015a
6 changed files with 52 additions and 55 deletions

View File

@ -5,7 +5,6 @@ add_files(
backup_type.hpp
bitmath_func.hpp
endian_func.hpp
endian_type.hpp
enum_type.hpp
format.hpp
geometry_func.cpp

View File

@ -10,32 +10,54 @@
#ifndef ENDIAN_FUNC_HPP
#define ENDIAN_FUNC_HPP
#include "endian_type.hpp"
#include "bitmath_func.hpp"
/* Setup alignment and conversion macros */
#if TTD_ENDIAN == TTD_BIG_ENDIAN
# define FROM_BE16(x) (x)
# define FROM_BE32(x) (x)
# define TO_BE16(x) (x)
# define TO_BE32(x) (x)
# define TO_BE32X(x) (x)
# define FROM_LE16(x) std::byteswap(x)
# define FROM_LE32(x) std::byteswap(x)
# define TO_LE16(x) std::byteswap(x)
# define TO_LE32(x) std::byteswap(x)
# define TO_LE32X(x) std::byteswap(x)
#else
# define FROM_BE16(x) std::byteswap(x)
# define FROM_BE32(x) std::byteswap(x)
# define TO_BE16(x) std::byteswap(x)
# define TO_BE32(x) std::byteswap(x)
# define TO_BE32X(x) std::byteswap(x)
# define FROM_LE16(x) (x)
# define FROM_LE32(x) (x)
# define TO_LE16(x) (x)
# define TO_LE32(x) (x)
# define TO_LE32X(x) (x)
#endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
static constexpr uint16_t FROM_BE16(uint16_t x)
{
if constexpr (std::endian::native == std::endian::big) return x;
return std::byteswap(x);
}
static constexpr uint32_t FROM_BE32(uint32_t x)
{
if constexpr (std::endian::native == std::endian::big) return x;
return std::byteswap(x);
}
static constexpr uint16_t TO_BE16(uint16_t x)
{
if constexpr (std::endian::native == std::endian::big) return x;
return std::byteswap(x);
}
static constexpr uint32_t TO_BE32(uint32_t x)
{
if constexpr (std::endian::native == std::endian::big) return x;
return std::byteswap(x);
}
static constexpr uint16_t FROM_LE16(uint16_t x)
{
if constexpr (std::endian::native == std::endian::little) return x;
return std::byteswap(x);
}
static constexpr uint32_t FROM_LE32(uint32_t x)
{
if constexpr (std::endian::native == std::endian::little) return x;
return std::byteswap(x);
}
static constexpr uint16_t TO_LE16(uint16_t x)
{
if constexpr (std::endian::native == std::endian::little) return x;
return std::byteswap(x);
}
static constexpr uint32_t TO_LE32(uint32_t x)
{
if constexpr (std::endian::native == std::endian::little) return x;
return std::byteswap(x);
}
#endif /* ENDIAN_FUNC_HPP */

View File

@ -1,22 +0,0 @@
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file endian_type.hpp Definition of various endian-dependant macros. */
#ifndef ENDIAN_TYPE_HPP
#define ENDIAN_TYPE_HPP
/** Little endian builds use this for TTD_ENDIAN. */
#define TTD_LITTLE_ENDIAN 0
/** Big endian builds use this for TTD_ENDIAN. */
#define TTD_BIG_ENDIAN 1
#if !defined(TTD_ENDIAN)
# error "TTD_ENDIAN is not defined; please set it to either TTD_LITTLE_ENDIAN or TTD_BIG_ENDIAN"
#endif /* !TTD_ENDIAN */
#endif /* ENDIAN_TYPE_HPP */

View File

@ -2697,10 +2697,10 @@ struct SaveLoadFormat {
uint8_t max_compression; ///< the maximum compression level of this format
};
static const uint32_t SAVEGAME_TAG_LZO = TO_BE32X('OTTD');
static const uint32_t SAVEGAME_TAG_NONE = TO_BE32X('OTTN');
static const uint32_t SAVEGAME_TAG_ZLIB = TO_BE32X('OTTZ');
static const uint32_t SAVEGAME_TAG_LZMA = TO_BE32X('OTTX');
static const uint32_t SAVEGAME_TAG_LZO = TO_BE32('OTTD');
static const uint32_t SAVEGAME_TAG_NONE = TO_BE32('OTTN');
static const uint32_t SAVEGAME_TAG_ZLIB = TO_BE32('OTTZ');
static const uint32_t SAVEGAME_TAG_LZMA = TO_BE32('OTTX');
/** The different saveload formats known/understood by OpenTTD. */
static const SaveLoadFormat _saveload_formats[] = {

View File

@ -256,7 +256,7 @@ static const LegendAndColour * const _legend_table[] = {
_legend_land_owners,
};
#define MKCOLOUR(x) TO_LE32X(x)
#define MKCOLOUR(x) TO_LE32(x)
#define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x))
#define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x))

View File

@ -7,8 +7,6 @@
/** @file palettes.h The colour translation of the GRF palettes. */
#include "../core/endian_type.hpp"
#define M(r, g, b) Colour(r, g, b)
/** Colour palette (DOS) */