From 6f1aebbb0234bed35377a19b4a58989df05cdf78 Mon Sep 17 00:00:00 2001 From: Christian Semmler Date: Thu, 30 May 2024 14:41:20 -0400 Subject: [PATCH] Add SDL3 and replace Windows semaphores (#4) * Add SDL3 and replace Windows semaphores * Try this * Change for now --- .github/workflows/build.yml | 8 +++++++ CMakeLists.txt | 23 ++++++++++++++++++- LEGO1/omni/include/mxsemaphore.h | 10 ++++---- .../omni/src/stream/mxdiskstreamprovider.cpp | 8 +++---- LEGO1/omni/src/system/mxsemaphore.cpp | 15 +++++++----- LEGO1/omni/src/system/mxthread.cpp | 5 ++-- 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 160497d9..2d332ff7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,6 +19,13 @@ jobs: # - { name: 'msys2 clang32', shell: 'msys2 {0}', msystem: clang32, msys-env: mingw-w64-clang-i686, clang-tidy: true, werror: true, no-dx5-libs: true } steps: + # Figure out how to build for 32-bit arch + # - name: Set up SDL + # id: sdl + # uses: libsdl-org/setup-sdl@main + # with: + # version: sdl3-head + - name: Set up MSYS2 if: ${{ !!matrix.toolchain.msystem }} uses: msys2/setup-msys2@v2 @@ -49,6 +56,7 @@ jobs: submodules: true - name: Build + # Add -DDOWNLOAD_DEPENDENCIES=OFF once setup-sdl works run: | cmake -S . -B build -GNinja \ -DCMAKE_BUILD_TYPE=Debug \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 48b6dbc2..dbec119c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,27 @@ cmake_policy(SET CMP0091 NEW) project(isle CXX C) +# By configuring CMake with -DDOWNLOAD_DEPENDENCIES=ON/OFF, +# users can choose between downloading dependencies or using system libraries +option(DOWNLOAD_DEPENDENCIES "Download dependencies" TRUE) + +if(DOWNLOAD_DEPENDENCIES) + # FetchContent downloads and configures dependencies + include(FetchContent) + FetchContent_Declare( + SDL3 + GIT_REPOSITORY "https://github.com/libsdl-org/SDL.git" + GIT_TAG "main" + EXCLUDE_FROM_ALL + ) + FetchContent_MakeAvailable(SDL3) +else() + # find_package looks for already-installed system packages. + # Configure with `-DCMAKE_PREFIX_PATH="/path/to/package1;/path/to/package2"` + # to add search paths. + find_package(SDL3 CONFIG REQUIRED) +endif() + include(CheckCXXSourceCompiles) include(CMakeDependentOption) include(CMakePushCheckState) @@ -277,7 +298,7 @@ add_library(omni STATIC register_lego1_target(omni) set_property(TARGET omni PROPERTY ARCHIVE_OUTPUT_NAME "omni$<$:d>") target_include_directories(omni PRIVATE "${CMAKE_SOURCE_DIR}/LEGO1/omni/include" "${CMAKE_SOURCE_DIR}/LEGO1" "${CMAKE_SOURCE_DIR}/util") -target_link_libraries(omni PRIVATE dsound winmm libsmacker) +target_link_libraries(omni PRIVATE dsound winmm libsmacker SDL3::SDL3) add_library(lego1 SHARED LEGO1/define.cpp diff --git a/LEGO1/omni/include/mxsemaphore.h b/LEGO1/omni/include/mxsemaphore.h index 4e692395..7fc1b42d 100644 --- a/LEGO1/omni/include/mxsemaphore.h +++ b/LEGO1/omni/include/mxsemaphore.h @@ -3,7 +3,7 @@ #include "mxtypes.h" -#include +#include // VTABLE: LEGO1 0x100dccf0 // SIZE 0x08 @@ -12,15 +12,15 @@ public: MxSemaphore(); // FUNCTION: LEGO1 0x100c87e0 - ~MxSemaphore() { CloseHandle(m_hSemaphore); } + ~MxSemaphore() { SDL_DestroySemaphore(m_semaphore); } virtual MxResult Init(MxU32 p_initialCount, MxU32 p_maxCount); - void Wait(MxU32 p_timeoutMS); - void Release(MxU32 p_releaseCount); + void Wait(); + void Release(); private: - HANDLE m_hSemaphore; // 0x04 + SDL_Semaphore* m_semaphore; // 0x04 }; #endif // MX_SEMAPHORE_H diff --git a/LEGO1/omni/src/stream/mxdiskstreamprovider.cpp b/LEGO1/omni/src/stream/mxdiskstreamprovider.cpp index d6b73a9b..2fb53905 100644 --- a/LEGO1/omni/src/stream/mxdiskstreamprovider.cpp +++ b/LEGO1/omni/src/stream/mxdiskstreamprovider.cpp @@ -70,7 +70,7 @@ MxDiskStreamProvider::~MxDiskStreamProvider() if (m_remainingWork) { m_remainingWork = FALSE; - m_busySemaphore.Release(1); + m_busySemaphore.Release(); m_thread.Terminate(); } @@ -164,7 +164,7 @@ void MxDiskStreamProvider::VTable0x20(MxDSAction* p_action) MxResult MxDiskStreamProvider::WaitForWorkToComplete() { while (m_remainingWork) { - m_busySemaphore.Wait(INFINITE); + m_busySemaphore.Wait(); if (m_unk0x35) { PerformWork(); } @@ -205,7 +205,7 @@ MxResult MxDiskStreamProvider::FUN_100d1780(MxDSStreamingAction* p_action) } m_unk0x35 = TRUE; - m_busySemaphore.Release(1); + m_busySemaphore.Release(); return SUCCESS; } @@ -222,7 +222,7 @@ void MxDiskStreamProvider::PerformWork() if (streamingAction && !FUN_100d1af0(streamingAction)) { m_thread.Sleep(500); - m_busySemaphore.Release(1); + m_busySemaphore.Release(); return; } } diff --git a/LEGO1/omni/src/system/mxsemaphore.cpp b/LEGO1/omni/src/system/mxsemaphore.cpp index daba60f4..96398150 100644 --- a/LEGO1/omni/src/system/mxsemaphore.cpp +++ b/LEGO1/omni/src/system/mxsemaphore.cpp @@ -8,27 +8,30 @@ DECOMP_SIZE_ASSERT(MxSemaphore, 0x08) // FUNCTION: LEGO1 0x100c87d0 MxSemaphore::MxSemaphore() { - m_hSemaphore = NULL; + m_semaphore = NULL; } // FUNCTION: LEGO1 0x100c8800 MxResult MxSemaphore::Init(MxU32 p_initialCount, MxU32 p_maxCount) { + // [library:synchronization] No support for max count, but shouldn't be necessary MxResult result = FAILURE; - if ((m_hSemaphore = CreateSemaphoreA(NULL, p_initialCount, p_maxCount, NULL))) { + if ((m_semaphore = SDL_CreateSemaphore(p_initialCount))) { result = SUCCESS; } return result; } // FUNCTION: LEGO1 0x100c8830 -void MxSemaphore::Wait(MxU32 p_timeoutMS) +void MxSemaphore::Wait() { - WaitForSingleObject(m_hSemaphore, p_timeoutMS); + // [library:synchronization] Removed timeout since only INFINITE is ever requested + SDL_WaitSemaphore(m_semaphore); } // FUNCTION: LEGO1 0x100c8850 -void MxSemaphore::Release(MxU32 p_releaseCount) +void MxSemaphore::Release() { - ReleaseSemaphore(m_hSemaphore, p_releaseCount, NULL); + // [library:synchronization] Removed release count since only 1 is ever requested + SDL_PostSemaphore(m_semaphore); } diff --git a/LEGO1/omni/src/system/mxthread.cpp b/LEGO1/omni/src/system/mxthread.cpp index fe327f2d..4652d114 100644 --- a/LEGO1/omni/src/system/mxthread.cpp +++ b/LEGO1/omni/src/system/mxthread.cpp @@ -5,6 +5,7 @@ #include "mxtimer.h" #include +#include DECOMP_SIZE_ASSERT(MxThread, 0x1c) DECOMP_SIZE_ASSERT(MxTickleThread, 0x20) @@ -81,7 +82,7 @@ void MxThread::Sleep(MxS32 p_milliseconds) void MxThread::Terminate() { m_running = FALSE; - m_semaphore.Wait(INFINITE); + m_semaphore.Wait(); } // FUNCTION: LEGO1 0x100bf680 @@ -93,6 +94,6 @@ unsigned MxThread::ThreadProc(void* p_thread) // FUNCTION: LEGO1 0x100bf690 MxResult MxThread::Run() { - m_semaphore.Release(1); + m_semaphore.Release(); return SUCCESS; }