Skip to content

Commit

Permalink
Add SDL3 and replace Windows semaphores (#4)
Browse files Browse the repository at this point in the history
* Add SDL3 and replace Windows semaphores

* Try this

* Change for now
  • Loading branch information
foxtacles authored May 30, 2024
1 parent 6ddd86d commit 47997da
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
23 changes: 22 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -277,7 +298,7 @@ add_library(omni STATIC
register_lego1_target(omni)
set_property(TARGET omni PROPERTY ARCHIVE_OUTPUT_NAME "omni$<$<CONFIG:Debug>: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
Expand Down
10 changes: 5 additions & 5 deletions LEGO1/omni/include/mxsemaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "mxtypes.h"

#include <windows.h>
#include <SDL3/SDL_mutex.h>

// VTABLE: LEGO1 0x100dccf0
// SIZE 0x08
Expand All @@ -12,15 +12,15 @@ class MxSemaphore {
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
8 changes: 4 additions & 4 deletions LEGO1/omni/src/stream/mxdiskstreamprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ MxDiskStreamProvider::~MxDiskStreamProvider()

if (m_remainingWork) {
m_remainingWork = FALSE;
m_busySemaphore.Release(1);
m_busySemaphore.Release();
m_thread.Terminate();
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -205,7 +205,7 @@ MxResult MxDiskStreamProvider::FUN_100d1780(MxDSStreamingAction* p_action)
}

m_unk0x35 = TRUE;
m_busySemaphore.Release(1);
m_busySemaphore.Release();
return SUCCESS;
}

Expand All @@ -222,7 +222,7 @@ void MxDiskStreamProvider::PerformWork()

if (streamingAction && !FUN_100d1af0(streamingAction)) {
m_thread.Sleep(500);
m_busySemaphore.Release(1);
m_busySemaphore.Release();
return;
}
}
Expand Down
15 changes: 9 additions & 6 deletions LEGO1/omni/src/system/mxsemaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 3 additions & 2 deletions LEGO1/omni/src/system/mxthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "mxtimer.h"

#include <process.h>
#include <windows.h>

DECOMP_SIZE_ASSERT(MxThread, 0x1c)
DECOMP_SIZE_ASSERT(MxTickleThread, 0x20)
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

0 comments on commit 47997da

Please sign in to comment.