From 5d3f1432202643e490a1fcb4005ec34b1d828345 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Mon, 7 Oct 2024 03:15:50 +0200 Subject: [PATCH] Detect endianess in CMakeLists.txt, get rid of most SDL_endian.h uses .. except where SDL_Swap* is actually used (idlib/Lib.cpp). Otherwise #if D3_IS_BIG_ENDIAN suffices, (NOT #ifdef, it's always set, but to either 0 or 1!) --- neo/CMakeLists.txt | 10 ++++++++++ neo/d3xp/Game_local.cpp | 9 ++++++--- neo/game/Game_local.cpp | 9 ++++++--- neo/idlib/Lib.cpp | 27 ++++++++++++++++++++++++++- neo/sound/snd_decoder.cpp | 4 +--- neo/sound/stbvorbis_impl.c | 3 +-- 6 files changed, 50 insertions(+), 12 deletions(-) diff --git a/neo/CMakeLists.txt b/neo/CMakeLists.txt index 593c6d30..5d31f19a 100644 --- a/neo/CMakeLists.txt +++ b/neo/CMakeLists.txt @@ -46,6 +46,7 @@ set(DHEWM3BINARY "dhewm3") include(CheckCXXCompilerFlag) include(GNUInstallDirs OPTIONAL RESULT_VARIABLE GNUINSTALLDIRS) +include(TestBigEndian) option(CORE "Build the core" ON) option(BASE "Build the base game code" ON) @@ -313,6 +314,15 @@ unset(compiler_id_lower) endif() # not MSVC +TEST_BIG_ENDIAN(is_big_endian) +if(is_big_endian) + message(STATUS "Detected Big Endian architecture, setting -DD3_IS_BIG_ENDIAN=1") + add_definitions(-DD3_IS_BIG_ENDIAN=1) +else() + message(STATUS "Detected Little Endian architecture, setting -DD3_IS_BIG_ENDIAN=0") + add_definitions(-DD3_IS_BIG_ENDIAN=0) +endif() + # compiler specific flags if(D3_COMPILER_IS_GCC_OR_CLANG) add_compile_options(-pipe) diff --git a/neo/d3xp/Game_local.cpp b/neo/d3xp/Game_local.cpp index 51c113ba..64688712 100644 --- a/neo/d3xp/Game_local.cpp +++ b/neo/d3xp/Game_local.cpp @@ -26,8 +26,6 @@ If you have questions concerning this license or the applicable additional terms =========================================================================== */ -#include - #include "sys/platform.h" #include "idlib/LangDict.h" #include "idlib/Timer.h" @@ -527,7 +525,12 @@ void idGameLocal::SaveGame( idFile *f ) { savegame.WriteString( D3_ARCH ); // CPU architecture (e.g. "x86" or "x86_64") - from CMake savegame.WriteString( ENGINE_VERSION ); savegame.WriteShort( (short)sizeof(void*) ); // tells us if it's from a 32bit (4) or 64bit system (8) - savegame.WriteShort( SDL_BYTEORDER ) ; // SDL_LIL_ENDIAN or SDL_BIG_ENDIAN +#if D3_IS_BIG_ENDIAN + const short byteOrder = 4321; // SDL_BIG_ENDIAN +#else + const short byteOrder = 1234; // SDL_LIL_ENDIAN +#endif + savegame.WriteShort( byteOrder ) ; // DG end // go through all entities and threads and add them to the object list diff --git a/neo/game/Game_local.cpp b/neo/game/Game_local.cpp index 4ca98d6f..047ac283 100644 --- a/neo/game/Game_local.cpp +++ b/neo/game/Game_local.cpp @@ -26,8 +26,6 @@ If you have questions concerning this license or the applicable additional terms =========================================================================== */ -#include - #include "sys/platform.h" #include "idlib/LangDict.h" #include "idlib/Timer.h" @@ -469,7 +467,12 @@ void idGameLocal::SaveGame( idFile *f ) { savegame.WriteString( D3_ARCH ); // CPU architecture (e.g. "x86" or "x86_64") - from CMake savegame.WriteString( ENGINE_VERSION ); savegame.WriteShort( (short)sizeof(void*) ); // tells us if it's from a 32bit (4) or 64bit system (8) - savegame.WriteShort( SDL_BYTEORDER ) ; // SDL_LIL_ENDIAN or SDL_BIG_ENDIAN +#if D3_IS_BIG_ENDIAN + const short byteOrder = 4321; // SDL_BIG_ENDIAN +#else + const short byteOrder = 1234; // SDL_LIL_ENDIAN +#endif + savegame.WriteShort( byteOrder ) ; // DG end // go through all entities and threads and add them to the object list diff --git a/neo/idlib/Lib.cpp b/neo/idlib/Lib.cpp index a9f0b649..515e819f 100644 --- a/neo/idlib/Lib.cpp +++ b/neo/idlib/Lib.cpp @@ -35,7 +35,32 @@ If you have questions concerning this license or the applicable additional terms #include #endif -#include +#ifdef D3_SDL3 + #include + // some defines for backwards-compat with SDL2 + #define SDL_SwapBE16(X) SDL_Swap16BE(X) + #define SDL_SwapLE16(X) SDL_Swap16LE(X) + #define SDL_SwapBE32(X) SDL_Swap32BE(X) + #define SDL_SwapLE32(X) SDL_Swap32LE(X) +#else // SDL1.2 or SDL2 + #include +#endif + +#ifndef D3_IS_BIG_ENDIAN + #error "D3_IS_BIG_ENDIAN should be defined by the build system (CMake)!" +#endif + +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + #if D3_IS_BIG_ENDIAN != 1 + #error "CMake (which sets D3_IS_BIG_ENDIAN) and SDL disagree about the endianess! CMake says little, SDL says big" + #endif +#elif SDL_BYTEORDER == SDL_LIL_ENDIAN + #if D3_IS_BIG_ENDIAN != 0 + #error "CMake (which sets D3_IS_BIG_ENDIAN) and SDL disagree about the endianess! CMake says big, SDL says little" + #endif +#else + #error "According to SDL, endianess is neither Big nor Little - dhewm3 doesn't support other byteorders!" +#endif #include "sys/platform.h" #include "idlib/math/Vector.h" diff --git a/neo/sound/snd_decoder.cpp b/neo/sound/snd_decoder.cpp index 1ec1eac5..79343f40 100644 --- a/neo/sound/snd_decoder.cpp +++ b/neo/sound/snd_decoder.cpp @@ -26,9 +26,7 @@ If you have questions concerning this license or the applicable additional terms =========================================================================== */ - -#include "SDL_endian.h" -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#if D3_IS_BIG_ENDIAN #define STB_VORBIS_BIG_ENDIAN #endif #define STB_VORBIS_NO_STDIO diff --git a/neo/sound/stbvorbis_impl.c b/neo/sound/stbvorbis_impl.c index 806f61ea..a98127f0 100644 --- a/neo/sound/stbvorbis_impl.c +++ b/neo/sound/stbvorbis_impl.c @@ -5,8 +5,7 @@ // (I'm doing this instead of renaming stb_vorbis.h to stb_vorbis.c so the configuration // like STB_VORBIS_BIG_ENDIAN etc can be done here in code) -#include "SDL_endian.h" -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#if D3_IS_BIG_ENDIAN #define STB_VORBIS_BIG_ENDIAN #endif #define STB_VORBIS_NO_STDIO