Skip to content

Commit

Permalink
Merge branch 'SDL3'
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGibson committed Oct 29, 2024
2 parents 7354fff + 81ba620 commit c142dac
Show file tree
Hide file tree
Showing 25 changed files with 1,080 additions and 298 deletions.
54 changes: 45 additions & 9 deletions neo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ 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)
option(D3XP "Build the d3xp game code" ON)
if(MSVC)
option(TOOLS "Build the tools game code (Visual Studio+SDL2 only)" OFF)
option(TOOLS "Build the tools game code (Visual Studio+SDL2/SDL3 only)" OFF)
endif()
option(DEDICATED "Build the dedicated server" OFF)
option(ONATIVE "Optimize for the host CPU" OFF)
option(SDL2 "Use SDL2 instead of SDL1.2" ON)
option(IMGUI "Build with Dear ImGui integration - requires SDL2 and C++11" ON)
option(SDL3 "Use SDL3 instead of SDL2 or SDL1.2" OFF)
option(IMGUI "Build with Dear ImGui integration - requires SDL2/SDL3 and C++11" ON)
option(REPRODUCIBLE_BUILD "Replace __DATE__ and __TIME__ by hardcoded values for reproducible builds" OFF)

option(HARDLINK_GAME "Compile gamecode into executable (no game DLLs)" OFF)
Expand Down Expand Up @@ -193,7 +195,19 @@ endif()
find_package(OpenAL REQUIRED)
include_directories(${OPENAL_INCLUDE_DIR})

if (SDL2)
if (SDL3)
if(SDL2)
message(WARNING "You enabled both SDL2 and SDL3. Only one can be used at a time, disabling SDL2")
set(SDL2 OFF CACHE BOOL "Use SDL2 (make sure SDL3 is disabled!)" FORCE)
endif()
# 1. Look for a SDL3 package,
# 2. look for the SDL3-shared component, and
# 3. fail if the shared component cannot be found.
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
# TODO: include dirs?
set(SDLx_LIBRARY SDL3::SDL3)
add_definitions(-DD3_SDL3=1)
elseif (SDL2)
# skip SDL2main
if(APPLE OR WIN32)
set(SDL2_BUILDING_LIBRARY TRUE)
Expand All @@ -217,12 +231,12 @@ if(REPRODUCIBLE_BUILD)
endif()

if(IMGUI)
if(SDL2)
if(SDL2 OR SDL3)
# we need C++11 for ImGui
set (CMAKE_CXX_STANDARD 11)
message(STATUS "Dear ImGui integration enabled")
else()
message(WARNING "Disabling IMGUI because SDL1.2 is used - it needs SDL2!")
message(WARNING "Disabling IMGUI because SDL1.2 is used - it needs SDL2 or SDL3!")
set(IMGUI OFF)
add_definitions(-DIMGUI_DISABLE)
endif()
Expand Down Expand Up @@ -273,7 +287,7 @@ if(NOT WIN32)
endif()

# check if our SDL2 supports X11 in SDL_syswm so we can use it for DPI scaling ImGui
if(SDL2)
if(SDL2) # TODO: SDL3? Or just kick this feature?
set(CMAKE_REQUIRED_LIBRARIES SDL2)
check_c_source_compiles( "#include <SDL_syswm.h>
int main() { SDL_SysWMinfo wmInfo = {}; wmInfo.info.x11.display = NULL; return 0; }" HAVE_SDL_X11)
Expand All @@ -300,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)
Expand Down Expand Up @@ -799,8 +822,14 @@ set(src_idlib
add_globbed_headers(src_idlib "idlib")

if(IMGUI)
set(src_imgui
libs/imgui/backends/imgui_impl_sdl2.cpp

if(SDL3)
set(src_imgui libs/imgui/backends/imgui_impl_sdl3.cpp)
else()
set(src_imgui libs/imgui/backends/imgui_impl_sdl2.cpp)
endif()

set(src_imgui ${src_imgui}
libs/imgui/backends/imgui_impl_opengl2.cpp

libs/imgui/imgui.h
Expand Down Expand Up @@ -1152,9 +1181,13 @@ elseif(WIN32)
sys/win32/win_net.cpp
sys/win32/win_shared.cpp
sys/win32/win_syscon.cpp
sys/win32/SDL_win32_main.c
)

if(NOT SDL2 AND NOT SDL3)
# only SDL1.2 still uses SDL_win32_main.c
set(src_sys_base ${src_sys_base} sys/win32/SDL_win32_main.c)
endif()

# adding the few relevant headers in sys/ manually..
set(src_sys_base ${src_sys_base}
sys/platform.h
Expand Down Expand Up @@ -1195,6 +1228,9 @@ include_directories(${CMAKE_BINARY_DIR})
include_directories(${CMAKE_SOURCE_DIR})

add_library(idlib STATIC ${src_idlib})
if(SDL3)
target_link_libraries(idlib SDL3::Headers) # so it can use SDL_Endian.h
endif()
if (AROS)
add_library(dll STATIC ${src_arosdll})
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i386")
Expand Down
9 changes: 6 additions & 3 deletions neo/d3xp/Game_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ If you have questions concerning this license or the applicable additional terms
===========================================================================
*/

#include <SDL_endian.h>

#include "sys/platform.h"
#include "idlib/LangDict.h"
#include "idlib/Timer.h"
Expand Down Expand Up @@ -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
Expand Down
42 changes: 34 additions & 8 deletions neo/framework/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ If you have questions concerning this license or the applicable additional terms
===========================================================================
*/

#include <SDL.h>
#include "sys/sys_sdl.h"

#if SDL_VERSION_ATLEAST(3, 0, 0)
// DG: compat with SDL2
#define SDL_setenv SDL_setenv_unsafe
#endif

#include "sys/platform.h"
#include "idlib/containers/HashTable.h"
Expand Down Expand Up @@ -2799,7 +2804,12 @@ void idCommonLocal::SetMachineSpec( void ) {
}
}

static unsigned int AsyncTimer(unsigned int interval, void *) {
#if SDL_VERSION_ATLEAST(3, 0, 0)
static Uint32 AsyncTimer(void * /*userdata*/, SDL_TimerID /* timerID */, Uint32 interval)
#else // SDL2 or SDL1.2
static unsigned int AsyncTimer(unsigned int interval, void *)
#endif
{
common->Async();
Sys_TriggerEvent(TRIGGER_EVENT_ONE);

Expand Down Expand Up @@ -2939,21 +2949,29 @@ void idCommonLocal::Init( int argc, char **argv ) {
// we want to use the SDL event queue for dedicated servers. That
// requires video to be initialized, so we just use the dummy
// driver for headless boxen
#if SDL_VERSION_ATLEAST(2, 0, 0)
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "dummy");
#elif SDL_VERSION_ATLEAST(2, 0, 0)
SDL_setenv("SDL_VIDEODRIVER", "dummy", 1);
#else
char dummy[] = "SDL_VIDEODRIVER=dummy\0";
SDL_putenv(dummy);
#endif
#endif

#if SDL_VERSION_ATLEAST(2, 0, 0)
#if SDL_VERSION_ATLEAST(3, 0, 0)
if ( ! SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD) )
{
if ( SDL_Init(SDL_INIT_VIDEO) ) { // retry without joystick/gamepad if it failed
Sys_Printf( "WARNING: Couldn't get SDL gamepad support! Gamepads won't work!\n" );
} else
#elif SDL_VERSION_ATLEAST(2, 0, 0)
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) != 0)
{
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO) == 0) { // retry without joystick/gamecontroller if it failed
Sys_Printf( "WARNING: Couldn't get SDL gamecontroller support! Gamepads won't work!\n" );
} else
#else
#else // SDL1.2
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO) != 0) // no gamecontroller support in SDL1
{
#endif
Expand Down Expand Up @@ -3005,14 +3023,22 @@ void idCommonLocal::Init( int argc, char **argv ) {
idCVar::RegisterStaticVars();

// print engine version
#if SDL_VERSION_ATLEAST(2, 0, 0)
#if SDL_VERSION_ATLEAST(3, 0, 0)
int sdlv = SDL_GetVersion();
int sdlvmaj = SDL_VERSIONNUM_MAJOR(sdlv);
int sdlvmin = SDL_VERSIONNUM_MINOR(sdlv);
int sdlvmicro = SDL_VERSIONNUM_MICRO(sdlv);
Printf( "%s using SDL v%d.%d.%d\n", version.string, sdlvmaj, sdlvmin, sdlvmicro );
#else
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_version sdlv;
SDL_GetVersion(&sdlv);
#else
#else
SDL_version sdlv = *SDL_Linked_Version();
#endif
#endif
Printf( "%s using SDL v%u.%u.%u\n",
version.string, sdlv.major, sdlv.minor, sdlv.patch );
#endif

#if SDL_VERSION_ATLEAST(2, 0, 0)
Printf( "SDL video driver: %s\n", SDL_GetCurrentVideoDriver() );
Expand Down
12 changes: 9 additions & 3 deletions neo/framework/Dhewm3SettingsMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <algorithm> // std::sort - TODO: replace with something custom..

#include <SDL.h> // to show display size
#include "sys/sys_sdl.h"

#define IMGUI_DEFINE_MATH_OPERATORS

Expand Down Expand Up @@ -1903,9 +1903,15 @@ static void DrawVideoOptionsMenu()
}

// resolution info text
int sdlDisplayIdx = SDL_GetWindowDisplayIndex( SDL_GL_GetCurrentWindow() );
#if SDL_VERSION_ATLEAST(3, 0, 0)
// in SDL3 it's a Display ID, in SDL2 a Display Index, in both cases the value
// can be fed into SDL_GetDisplayBounds()
SDL_DisplayID sdlDisplayId_x = SDL_GetDisplayForWindow( SDL_GL_GetCurrentWindow() );
#else // SDL2
int sdlDisplayId_x = SDL_GetWindowDisplayIndex( SDL_GL_GetCurrentWindow() );
#endif
SDL_Rect displayRect = {};
SDL_GetDisplayBounds( sdlDisplayIdx, &displayRect );
SDL_GetDisplayBounds( sdlDisplayId_x, &displayRect );
if ( (int)glConfig.winWidth != glConfig.vidWidth ) {
ImGui::TextDisabled( "Current Resolution: %g x %g (Physical: %d x %d)",
glConfig.winWidth, glConfig.winHeight, glConfig.vidWidth, glConfig.vidHeight );
Expand Down
9 changes: 6 additions & 3 deletions neo/game/Game_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ If you have questions concerning this license or the applicable additional terms
===========================================================================
*/

#include <SDL_endian.h>

#include "sys/platform.h"
#include "idlib/LangDict.h"
#include "idlib/Timer.h"
Expand Down Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion neo/idlib/Lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,32 @@ If you have questions concerning this license or the applicable additional terms
#include <unistd.h>
#endif

#include <SDL_endian.h>
#ifdef D3_SDL3
#include <SDL3/SDL_endian.h>
// 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 <SDL_endian.h>
#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"
Expand Down
6 changes: 5 additions & 1 deletion neo/renderer/qgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ If you have questions concerning this license or the applicable additional terms
#endif
#endif

#include <SDL_opengl.h>
#ifdef D3_SDL3
#include <SDL3/SDL_opengl.h>
#else // SDL1.2 or SDL2
#include <SDL_opengl.h>
#endif

#if defined( ID_DEDICATED ) && defined( _WIN32 )
// restore WINGDIAPI
Expand Down
2 changes: 1 addition & 1 deletion neo/renderer/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ If you have questions concerning this license or the applicable additional terms

#include "renderer/tr_local.h"

static idCVar r_fillWindowAlphaChan( "r_fillWindowAlphaChan", "-1", CVAR_SYSTEM | CVAR_NOCHEAT | CVAR_ARCHIVE, "Make sure alpha channel of windows default framebuffer is completely opaque at the end of each frame. Needed at least when using Wayland.\n 1: do this, 0: don't do it, -1: let dhewm3 decide (default)" );
static idCVar r_fillWindowAlphaChan( "r_fillWindowAlphaChan", "-1", CVAR_SYSTEM | CVAR_NOCHEAT | CVAR_ARCHIVE, "Make sure alpha channel of windows default framebuffer is completely opaque at the end of each frame. Needed at least when using Wayland with older drivers.\n 1: do this, 0: don't do it, -1: let dhewm3 decide (default)" );

frameData_t *frameData;
backEndState_t backEnd;
Expand Down
3 changes: 2 additions & 1 deletion neo/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ typedef struct {
bool fullScreen;
bool fullScreenDesktop;
bool stereo;
int displayHz;
int displayHz; // TODO: SDL3 uses float
int multiSamples;
} glimpParms_t;

Expand Down Expand Up @@ -1119,6 +1119,7 @@ void GLimp_DeactivateContext( void );
const int GRAB_GRABMOUSE = (1 << 0);
const int GRAB_HIDECURSOR = (1 << 1);
const int GRAB_RELATIVEMOUSE = (1 << 2);
const int GRAB_ENABLETEXTINPUT = (1 << 3); // only used with SDL3, where textinput must be explicitly activated

void GLimp_GrabInput(int flags);

Expand Down
4 changes: 1 addition & 3 deletions neo/sound/snd_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions neo/sound/stbvorbis_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit c142dac

Please sign in to comment.