From 7ef3fff869ed3a6080f58da3ae0cd89ec8ba0668 Mon Sep 17 00:00:00 2001 From: th-2021 <90853655+th-2021@users.noreply.github.com> Date: Sat, 6 Aug 2022 22:19:06 +0200 Subject: [PATCH 1/9] add soh icon for Windows (#1060) --- soh/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soh/CMakeLists.txt b/soh/CMakeLists.txt index faf28ed5c..54700d65d 100644 --- a/soh/CMakeLists.txt +++ b/soh/CMakeLists.txt @@ -1559,7 +1559,7 @@ set(ALL_FILES ################################################################################ # Target ################################################################################ -add_executable(${PROJECT_NAME} ${ALL_FILES}) +add_executable(${PROJECT_NAME} ${ALL_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/Resource.rc) if (CMAKE_SYSTEM_NAME STREQUAL "Windows") use_props(${PROJECT_NAME} "${CMAKE_CONFIGURATION_TYPES}" "${DEFAULT_CXX_PROPS}") From 9a126dec4e517ea12cb06a5452ec683137f6be6f Mon Sep 17 00:00:00 2001 From: briaguya Date: Sat, 6 Aug 2022 05:15:46 -0400 Subject: [PATCH 2/9] add version requirements in building.md --- BUILDING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILDING.md b/BUILDING.md index 0ab0d0d52..610ca0a57 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -53,7 +53,7 @@ cd "build/x64" ``` ## Linux -1. Requires `gcc, x11, curl, python3, sdl2, libpng, glew, ninja, cmake` +1. Requires `gcc >= 10`, `x11`, `curl`, `python3`, `sdl2 >= 2.0.22`, `libpng`, `glew >= 2.2`, `ninja`, `cmake`, `lld` **Important: For maximum performance make sure you have ninja build tools installed!** From 8efeceb2360f700a233e374d0d68958072c5b5c0 Mon Sep 17 00:00:00 2001 From: briaguya Date: Sat, 6 Aug 2022 05:18:00 -0400 Subject: [PATCH 3/9] formatting --- BUILDING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILDING.md b/BUILDING.md index 610ca0a57..685df1473 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -53,7 +53,7 @@ cd "build/x64" ``` ## Linux -1. Requires `gcc >= 10`, `x11`, `curl`, `python3`, `sdl2 >= 2.0.22`, `libpng`, `glew >= 2.2`, `ninja`, `cmake`, `lld` +1. Requires `gcc >= 10, x11, curl, python3, sdl2 >= 2.0.22, libpng, glew >= 2.2, ninja, cmake, lld` **Important: For maximum performance make sure you have ninja build tools installed!** From 7f58ef14ee0e99ee774116427d4c63ea2477ee25 Mon Sep 17 00:00:00 2001 From: briaguya Date: Sat, 6 Aug 2022 05:37:37 -0400 Subject: [PATCH 4/9] add a command to copy the otr to the build dir --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 753c34523..fb31fc818 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,7 @@ add_custom_target( COMMAND ${CMAKE_COMMAND} -E rm -f oot.otr COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/OTRExporter/extract_assets_cmake2.py COMMAND ${CMAKE_COMMAND} -E copy oot.otr ${CMAKE_SOURCE_DIR} + COMMAND ${CMAKE_COMMAND} -E copy oot.otr ${CMAKE_BINARY_DIR}/soh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/OTRExporter COMMENT "Running asset extraction..." DEPENDS ZAPD From aadabe1ecd5d75192d9e281832c0da29af324ed6 Mon Sep 17 00:00:00 2001 From: David Chavez Date: Fri, 5 Aug 2022 17:49:38 +0200 Subject: [PATCH 5/9] Better instructions for VS solutions --- BUILDING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BUILDING.md b/BUILDING.md index 685df1473..7606032ba 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -30,8 +30,8 @@ With the cmake build system you have two options for working on the project: #### Visual Studio To develop using Visual Studio you only need to use cmake to generate the solution file: ```powershell -# Generates Ship.sln at the root directory -& 'C:\Program Files\CMake\bin\cmake' -S . -G "Visual Studio 17 2022" -T v142 -A x64 +# Generates Ship.sln at `build/x64` +& 'C:\Program Files\CMake\bin\cmake' -S . -B "build/x64" -G "Visual Studio 17 2022" -T v142 -A x64 ``` #### Visual Studio Code or another editor From c70e3f38b1a85be5f30a308886c229cbd60c227e Mon Sep 17 00:00:00 2001 From: David Chavez Date: Sat, 6 Aug 2022 04:50:47 +0200 Subject: [PATCH 6/9] Enable SPD logging in libultra --- libultraship/libultraship/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/libultraship/libultraship/CMakeLists.txt b/libultraship/libultraship/CMakeLists.txt index 8070d5f93..78331cbaf 100644 --- a/libultraship/libultraship/CMakeLists.txt +++ b/libultraship/libultraship/CMakeLists.txt @@ -456,6 +456,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|AppleClang") "NDEBUG" ">" "ENABLE_OPENGL;" + "SPDLOG_ACTIVE_LEVEL=0;" ) endif() ################################################################################ From 1794683d3a723d528c3b62545daf60b98c4456af Mon Sep 17 00:00:00 2001 From: David Chavez Date: Sat, 6 Aug 2022 22:16:39 +0200 Subject: [PATCH 7/9] Ignore build.c --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 442be304f..1df76208d 100644 --- a/.gitignore +++ b/.gitignore @@ -441,4 +441,5 @@ compile_commands.json CTestTestfile.cmake _deps */extract_assets_cmake* -/build* \ No newline at end of file +/build* +build.c From b2ac01bc98f3e52f21e02ffe3e97132897178d6a Mon Sep 17 00:00:00 2001 From: qurious-pixel <62252937+qurious-pixel@users.noreply.github.com> Date: Sat, 6 Aug 2022 18:52:22 -0700 Subject: [PATCH 8/9] [CI] Link Updated SDL2 version (#1078) --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e44a08286..1c23dafa4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,7 +46,8 @@ RUN curl -sLO https://libsdl.org/release/SDL2-${SDL2VER}.tar.gz && \ cd SDL2-${SDL2VER} && \ ./configure --build=x86_64-linux-gnu && \ make -j$(nproc) && make install && \ - rm ../SDL2-${SDL2VER}.tar.gz + rm ../SDL2-${SDL2VER}.tar.gz && \ + cp -av /usr/local/lib/libSDL* /lib/x86_64-linux-gnu/ RUN \ ln -sf /proc/self/mounts /etc/mtab && \ From cb2410d9c5920a882f01b6ea4a4736e37438d2ad Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Wed, 3 Aug 2022 02:30:39 +0200 Subject: [PATCH 9/9] Handle audio thread properly --- .../libultraship/Lib/Fast3D/gfx_dxgi.cpp | 6 +- .../libultraship/Lib/Fast3D/gfx_sdl2.cpp | 7 +- soh/soh/OTRAudio.h | 3 +- soh/soh/OTRGlobals.cpp | 114 ++++++++++-------- soh/src/code/graph.c | 16 --- soh/src/code/main.c | 4 +- 6 files changed, 79 insertions(+), 71 deletions(-) diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp index c80dc4be5..3e81f56fa 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp @@ -239,8 +239,8 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par dxgi.current_height = (uint32_t)(l_param >> 16); break; case WM_DESTROY: - Ship::ExecuteHooks(); - exit(0); + PostQuitMessage(0); + break; case WM_PAINT: if (dxgi.in_paint) { dxgi.recursive_paint_detected = true; @@ -378,6 +378,8 @@ static void gfx_dxgi_main_loop(void (*run_one_game_iter)(void)) { TranslateMessage(&msg); DispatchMessage(&msg); } + + Ship::ExecuteHooks(); } static void gfx_dxgi_get_dimensions(uint32_t *width, uint32_t *height) { diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp index 8600aae1f..23cff4698 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp @@ -238,6 +238,8 @@ static void gfx_sdl_main_loop(void (*run_one_game_iter)(void)) { Ship::Switch::Exit(); #endif Ship::ExecuteHooks(); + + SDL_Quit(); } static void gfx_sdl_get_dimensions(uint32_t *width, uint32_t *height) { @@ -307,9 +309,8 @@ static void gfx_sdl_handle_events(void) { CVar_Save(); break; case SDL_QUIT: - Ship::ExecuteHooks(); - SDL_Quit(); // bandaid fix for linux window closing issue - exit(0); + is_running = false; + break; } } } diff --git a/soh/soh/OTRAudio.h b/soh/soh/OTRAudio.h index ee0ec46a2..a452fdb29 100644 --- a/soh/soh/OTRAudio.h +++ b/soh/soh/OTRAudio.h @@ -1,8 +1,9 @@ #pragma once static struct { + std::thread thread; std::condition_variable cv_to_thread, cv_from_thread; std::mutex mutex; - bool initialized; + bool running; bool processing; } audio; diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 17843ac45..48ecceb07 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -83,11 +83,73 @@ extern "C" void ResourceMgr_CacheDirectory(const char* resName); extern "C" SequenceData ResourceMgr_LoadSeqByName(const char* path); std::unordered_map ExtensionCache; +void OTRAudio_Thread() { + while (audio.running) { + { + std::unique_lock Lock(audio.mutex); + while (!audio.processing && audio.running) { + audio.cv_to_thread.wait(Lock); + } + + if (!audio.running) { + break; + } + } + std::unique_lock Lock(audio.mutex); + //AudioMgr_ThreadEntry(&gAudioMgr); + // 528 and 544 relate to 60 fps at 32 kHz 32000/60 = 533.333.. + // in an ideal world, one third of the calls should use num_samples=544 and two thirds num_samples=528 + //#define SAMPLES_HIGH 560 + //#define SAMPLES_LOW 528 + // PAL values + //#define SAMPLES_HIGH 656 + //#define SAMPLES_LOW 624 + + // 44KHZ values + #define SAMPLES_HIGH 752 + #define SAMPLES_LOW 720 + + #define AUDIO_FRAMES_PER_UPDATE (R_UPDATE_RATE > 0 ? R_UPDATE_RATE : 1 ) + #define NUM_AUDIO_CHANNELS 2 + + int samples_left = AudioPlayer_Buffered(); + u32 num_audio_samples = samples_left < AudioPlayer_GetDesiredBuffered() ? SAMPLES_HIGH : SAMPLES_LOW; + + // 3 is the maximum authentic frame divisor. + s16 audio_buffer[SAMPLES_HIGH * NUM_AUDIO_CHANNELS * 3]; + for (int i = 0; i < AUDIO_FRAMES_PER_UPDATE; i++) { + AudioMgr_CreateNextAudioBuffer(audio_buffer + i * (num_audio_samples * NUM_AUDIO_CHANNELS), num_audio_samples); + } + + AudioPlayer_Play((u8*)audio_buffer, num_audio_samples * (sizeof(int16_t) * NUM_AUDIO_CHANNELS * AUDIO_FRAMES_PER_UPDATE)); + + audio.processing = false; + audio.cv_from_thread.notify_one(); + } +} + // C->C++ Bridge extern "C" void OTRAudio_Init() { // Precache all our samples, sequences, etc... ResourceMgr_CacheDirectory("audio"); + + if (!audio.running) { + audio.running = true; + audio.thread = std::thread(OTRAudio_Thread); + } +} + +extern "C" void OTRAudio_Exit() { + // Tell the audio thread to stop + { + std::unique_lock Lock(audio.mutex); + audio.running = false; + } + audio.cv_to_thread.notify_all(); + + // Wait until the audio thread quit + audio.thread.join(); } extern "C" void OTRExtScanner() { @@ -128,6 +190,10 @@ extern "C" void InitOTR() { OTRExtScanner(); } +extern "C" void DeinitOTR() { + OTRAudio_Exit(); +} + #ifdef _WIN32 extern "C" uint64_t GetFrequency() { LARGE_INTEGER nFreq; @@ -227,56 +293,10 @@ extern "C" void Graph_StartFrame() { // C->C++ Bridge extern "C" void Graph_ProcessGfxCommands(Gfx* commands) { -#ifndef __SWITCH__ - if (!audio.initialized) { - audio.initialized = true; - std::thread([]() { - for (;;) { - { - std::unique_lock Lock(audio.mutex); - while (!audio.processing) { - audio.cv_to_thread.wait(Lock); - } - } - std::unique_lock Lock(audio.mutex); - //AudioMgr_ThreadEntry(&gAudioMgr); - // 528 and 544 relate to 60 fps at 32 kHz 32000/60 = 533.333.. - // in an ideal world, one third of the calls should use num_samples=544 and two thirds num_samples=528 - //#define SAMPLES_HIGH 560 - //#define SAMPLES_LOW 528 - // PAL values - //#define SAMPLES_HIGH 656 - //#define SAMPLES_LOW 624 - - // 44KHZ values - #define SAMPLES_HIGH 752 - #define SAMPLES_LOW 720 - - #define AUDIO_FRAMES_PER_UPDATE (R_UPDATE_RATE > 0 ? R_UPDATE_RATE : 1 ) - #define NUM_AUDIO_CHANNELS 2 - - int samples_left = AudioPlayer_Buffered(); - u32 num_audio_samples = samples_left < AudioPlayer_GetDesiredBuffered() ? SAMPLES_HIGH : SAMPLES_LOW; - - // 3 is the maximum authentic frame divisor. - s16 audio_buffer[SAMPLES_HIGH * NUM_AUDIO_CHANNELS * 3]; - for (int i = 0; i < AUDIO_FRAMES_PER_UPDATE; i++) { - AudioMgr_CreateNextAudioBuffer(audio_buffer + i * (num_audio_samples * NUM_AUDIO_CHANNELS), num_audio_samples); - } - - AudioPlayer_Play((u8*)audio_buffer, num_audio_samples * (sizeof(int16_t) * NUM_AUDIO_CHANNELS * AUDIO_FRAMES_PER_UPDATE)); - - audio.processing = false; - audio.cv_from_thread.notify_one(); - } - }).detach(); - } - { std::unique_lock Lock(audio.mutex); audio.processing = true; } -#endif audio.cv_to_thread.notify_one(); std::vector> mtx_replacements; @@ -319,14 +339,12 @@ extern "C" void Graph_ProcessGfxCommands(Gfx* commands) { last_fps = fps; last_update_rate = R_UPDATE_RATE; -#ifndef __SWITCH__ { std::unique_lock Lock(audio.mutex); while (audio.processing) { audio.cv_from_thread.wait(Lock); } } -#endif // OTRTODO: FIGURE OUT END FRAME POINT /* if (OTRGlobals::Instance->context->GetWindow()->lastScancode != -1) diff --git a/soh/src/code/graph.c b/soh/src/code/graph.c index cf0a53c76..f4dcf56e8 100644 --- a/soh/src/code/graph.c +++ b/soh/src/code/graph.c @@ -481,22 +481,6 @@ static void RunFrame() uint64_t ticksA, ticksB; ticksA = GetPerfCounter(); -#ifdef __SWITCH__ - #define SAMPLES_HIGH 752 - #define SAMPLES_LOW 720 - - #define AUDIO_FRAMES_PER_UPDATE (R_UPDATE_RATE > 0 ? R_UPDATE_RATE : 1 ) - #define NUM_AUDIO_CHANNELS 2 - int samples_left = AudioPlayer_Buffered(); - u32 num_audio_samples = samples_left < AudioPlayer_GetDesiredBuffered() ? SAMPLES_HIGH : SAMPLES_LOW; - - s16 audio_buffer[SAMPLES_HIGH * NUM_AUDIO_CHANNELS * 3]; - for (int i = 0; i < AUDIO_FRAMES_PER_UPDATE; i++) { - AudioMgr_CreateNextAudioBuffer(audio_buffer + i * (num_audio_samples * NUM_AUDIO_CHANNELS), num_audio_samples); - } - - AudioPlayer_Play((u8*)audio_buffer, num_audio_samples * (sizeof(int16_t) * NUM_AUDIO_CHANNELS * AUDIO_FRAMES_PER_UPDATE)); -#endif Graph_StartFrame(); // TODO: Workaround for rumble being too long. Implement os thread functions. diff --git a/soh/src/code/main.c b/soh/src/code/main.c index fd02b016a..ccfdc2019 100644 --- a/soh/src/code/main.c +++ b/soh/src/code/main.c @@ -36,12 +36,14 @@ void Main_LogSystemHeap(void) { osSyncPrintf(VT_RST); } -void main(int argc, char** argv) +int main(int argc, char** argv) { GameConsole_Init(); InitOTR(); BootCommands_Init(); Main(0); + DeinitOTR(); + return 0; } void Main(void* arg) {