From 9614c0b97b6ca35cab84c6be80553c5bba0a5f34 Mon Sep 17 00:00:00 2001 From: Kylie C Date: Tue, 14 Oct 2025 22:32:30 -0400 Subject: [PATCH] audio & correct mouse/keyboard indexing Create the mutexes properly --- 3rdparty/sdl3-shim/SDL3/SDL.h | 11 ++- 3rdparty/sdl3-shim/SDL3/SDL_audio.h | 96 ++++++++++--------- 3rdparty/sdl3-shim/SDL3/SDL_timer.h | 2 +- .../legoomni/src/input/legoinputmanager.cpp | 6 +- 4 files changed, 67 insertions(+), 48 deletions(-) diff --git a/3rdparty/sdl3-shim/SDL3/SDL.h b/3rdparty/sdl3-shim/SDL3/SDL.h index 0debbc81..838e8a6f 100644 --- a/3rdparty/sdl3-shim/SDL3/SDL.h +++ b/3rdparty/sdl3-shim/SDL3/SDL.h @@ -96,10 +96,19 @@ inline SDL_DisplayMode* SDL_GetCurrentDisplayMode(SDL_DisplayID displayID) #define SDL_RenderTexture(...) (SDL_RenderCopyF(__VA_ARGS__) == 0) +#define SDL_CreateWindowAndRenderer(title, width, height, flags, window, renderer) (SDL_CreateWindowAndRenderer(width, height, flags, window, renderer) == 0) + +#define SDL_SetRenderScale(...) (SDL_RenderSetScale(__VA_ARGS__) == 0) + +// https://wiki.libsdl.org/SDL3/README-migration#sdl_keyboardh + +typedef int SDL_KeyboardID; + +#define SDL_GetKeyFromScancode(scancode, modstate, event) SDL_GetKeyFromScancode(scancode) + // https://wiki.libsdl.org/SDL3/README-migration#sdl_haptich // SDL_MouseID/SDL_KeyboardID are new -typedef int SDL_KeyboardID; #define SDL_GetKeyboardState (const bool*)SDL_GetKeyboardState typedef int SDL_HapticID; diff --git a/3rdparty/sdl3-shim/SDL3/SDL_audio.h b/3rdparty/sdl3-shim/SDL3/SDL_audio.h index d66cb2f9..3520469f 100644 --- a/3rdparty/sdl3-shim/SDL3/SDL_audio.h +++ b/3rdparty/sdl3-shim/SDL3/SDL_audio.h @@ -7,8 +7,6 @@ // https://wiki.libsdl.org/SDL3/README-migration#sdl_audioh -#define SDL_DestroyAudioStream SDL_FreeAudioStream - #define SDL_AUDIO_F32 AUDIO_F32SYS #define SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK NULL @@ -16,40 +14,31 @@ typedef void SDL_AudioStreamCallback(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount); struct ShimAudioCtx { + SDL_AudioDeviceID device; SDL_AudioStream* stream; SDL_AudioStreamCallback* callback; void *userdata; SDL_AudioSpec obtained; }; -static std::map g_audioCtxs; -static SDL_mutex *g_audioMutex = NULL; +static std::map g_audioCtxs; +static SDL_mutex *g_audioMutex = SDL_CreateMutex(); -static void SDLCALL shim_audio_callback(void *userdata, Uint8 *stream, int len) { - SDL_AudioDeviceID devid = reinterpret_cast(userdata); +static void shim_audio_callback(void *userdata, Uint8 *data_stream, int len) { + const auto ctx = static_cast(userdata); + SDL_AudioStream* audio_stream = ctx->stream; - SDL_LockMutex(g_audioMutex); - const auto it = g_audioCtxs.find(devid); - if (it == g_audioCtxs.end()) { - SDL_UnlockMutex(g_audioMutex); - SDL_memset(stream, 0, len); - return; - } + int available = SDL_AudioStreamAvailable(audio_stream); - ShimAudioCtx &ctx = it->second; - SDL_UnlockMutex(g_audioMutex); + int additional_amount = len - available; + if (additional_amount < 0) + additional_amount = 0; - // How many sample frames the device is asking for - const int frame_size = (SDL_AUDIO_BITSIZE(ctx.obtained.format) / 8) * ctx.obtained.channels; - const int needed_frames = len / frame_size; + ctx->callback(ctx->userdata, audio_stream, additional_amount, len); - int total = (SDL_AudioStreamAvailable(ctx.stream) / frame_size) + needed_frames; - // SDL3 callback pushes more into the stream - ctx.callback(ctx.userdata, ctx.stream, needed_frames, total); - - int got = SDL_AudioStreamGet(ctx.stream, stream, len); - if (got < len) { - SDL_memset(stream + got, 0, len - got); + int retrieved = SDL_AudioStreamGet(audio_stream, data_stream, len); + if (retrieved < len) { + memset(data_stream + retrieved, ctx->obtained.silence, len - retrieved); } } @@ -59,43 +48,64 @@ static void SDLCALL shim_audio_callback(void *userdata, Uint8 *stream, int len) inline SDL_AudioDeviceID SDL_GetAudioStreamDevice(SDL_AudioStream* stream) { SDL_LockMutex(g_audioMutex); - const auto it = g_audioCtxs.find(reinterpret_cast(stream)); + const auto it = g_audioCtxs.find(stream); if (it == g_audioCtxs.end()) { return 0; } - return it->first; + SDL_UnlockMutex(g_audioMutex); + return it->second->device; } inline SDL_AudioStream * SDL_OpenAudioDeviceStream(const char* devid, const SDL_AudioSpec* desired, SDL_AudioStreamCallback callback, void* userdata) { SDL_AudioSpec obtained{}; SDL_AudioSpec desired2 = *desired; + + auto ctx = new ShimAudioCtx(); + ctx->callback = callback; + ctx->userdata = userdata; + ctx->stream = nullptr; + desired2.callback = shim_audio_callback; - desired2.userdata = reinterpret_cast(static_cast(0)); - SDL_AudioDeviceID device = SDL_OpenAudioDevice(devid, 0, &desired2, &obtained, 0); + desired2.userdata = ctx; + const SDL_AudioDeviceID device = SDL_OpenAudioDevice(devid, 0, &desired2, &obtained, 0); + if (device <= 0) { + delete ctx; + return NULL; + } + SDL_AudioStream* stream = SDL_NewAudioStream( desired->format, desired->channels, desired->freq, obtained.format, obtained.channels, obtained.freq ); if (!stream) { SDL_CloseAudioDevice(device); - return nullptr; + delete ctx; + return NULL; } - { - SDL_LockMutex(g_audioMutex); - ShimAudioCtx ctx; - ctx.stream = stream; - ctx.callback = callback; - ctx.userdata = userdata; - ctx.obtained = obtained; - g_audioCtxs[device] = ctx; - SDL_UnlockMutex(g_audioMutex); - } + ctx->device = device; + ctx->stream = stream; + ctx->obtained = obtained; - SDL_LockAudioDevice(device); - desired2.userdata = reinterpret_cast(static_cast(device)); - SDL_UnlockAudioDevice(device); + SDL_LockMutex(g_audioMutex); + g_audioCtxs[stream] = ctx; + SDL_UnlockMutex(g_audioMutex); return stream; } + +inline SDL_bool SDL_DestroyAudioStream(SDL_AudioStream* stream) +{ + SDL_LockMutex(g_audioMutex); + if (const auto it = g_audioCtxs.find(stream); it != g_audioCtxs.end()) { + SDL_CloseAudioDevice(it->second->device); + delete it->second; + g_audioCtxs.erase(it); + SDL_UnlockMutex(g_audioMutex); + } + SDL_UnlockMutex(g_audioMutex); + + SDL_FreeAudioStream(stream); + return true; +} diff --git a/3rdparty/sdl3-shim/SDL3/SDL_timer.h b/3rdparty/sdl3-shim/SDL3/SDL_timer.h index 5cdff813..57ff2d62 100644 --- a/3rdparty/sdl3-shim/SDL3/SDL_timer.h +++ b/3rdparty/sdl3-shim/SDL3/SDL_timer.h @@ -22,7 +22,7 @@ struct SDL2TimerShimData { }; static std::map g_timers; -static SDL_mutex *g_timerMutex = NULL; +static SDL_mutex *g_timerMutex = SDL_CreateMutex(); inline Uint32 shim_timer_callback (Uint32 interval, void *param) { diff --git a/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp b/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp index 82963f58..52f448f3 100644 --- a/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp +++ b/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp @@ -785,7 +785,7 @@ void LegoInputManager::UpdateLastInputMethod(SDL_Event* p_event) #if SDL_MAJOR_VERSION >= 3 m_lastInputMethod = SDL_KeyboardID_v{p_event->key.which}; #else - m_lastInputMethod = SDL_KeyboardID_v{0}; + m_lastInputMethod = SDL_KeyboardID_v{1}; #endif break; case SDL_EVENT_MOUSE_BUTTON_DOWN: @@ -793,14 +793,14 @@ void LegoInputManager::UpdateLastInputMethod(SDL_Event* p_event) #if SDL_MAJOR_VERSION >= 3 m_lastInputMethod = SDL_MouseID_v{p_event->button.which}; #else - m_lastInputMethod = SDL_MouseID_v{0}; + m_lastInputMethod = SDL_MouseID_v{1}; #endif break; case SDL_EVENT_MOUSE_MOTION: #if SDL_MAJOR_VERSION >= 3 m_lastInputMethod = SDL_MouseID_v{p_event->motion.which}; #else - m_lastInputMethod = SDL_MouseID_v{0}; + m_lastInputMethod = SDL_MouseID_v{1}; #endif break; case SDL_EVENT_GAMEPAD_BUTTON_DOWN: