diff --git a/cmake/dependencies/common.cmake b/cmake/dependencies/common.cmake index 6d53277c4..4bc792577 100644 --- a/cmake/dependencies/common.cmake +++ b/cmake/dependencies/common.cmake @@ -9,15 +9,14 @@ if (CMAKE_GENERATOR MATCHES "Visual Studio") endif() #=================== ImGui =================== -set(sdl_gamepad_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/patches/sdl-gamepad-fix.patch) +# set(sdl_gamepad_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/patches/sdl-gamepad-fix.patch) # Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. -set(sdl_apply_patch_if_needed git apply ${sdl_gamepad_patch_file} ${git_hide_output} || git apply --reverse --check ${sdl_gamepad_patch_file}) +# set(sdl_apply_patch_if_needed git apply ${sdl_gamepad_patch_file} ${git_hide_output} || git apply --reverse --check ${sdl_gamepad_patch_file}) FetchContent_Declare( ImGui GIT_REPOSITORY https://github.com/ocornut/imgui.git - GIT_TAG v1.90.6-docking - PATCH_COMMAND ${sdl_apply_patch_if_needed} + GIT_TAG v1.91.6-docking ) FetchContent_MakeAvailable(ImGui) list(APPEND ADDITIONAL_LIB_INCLUDES ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends) @@ -37,7 +36,7 @@ target_sources(ImGui target_sources(ImGui PRIVATE ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp - ${imgui_SOURCE_DIR}/backends/imgui_impl_sdl2.cpp + ${imgui_SOURCE_DIR}/backends/imgui_impl_sdl3.cpp ) target_include_directories(ImGui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends PRIVATE ${SDL2_INCLUDE_DIRS}) diff --git a/cmake/dependencies/linux.cmake b/cmake/dependencies/linux.cmake index 02301f130..855a5be68 100644 --- a/cmake/dependencies/linux.cmake +++ b/cmake/dependencies/linux.cmake @@ -1,6 +1,6 @@ #=================== ImGui =================== -find_package(SDL2 REQUIRED) -target_link_libraries(ImGui PUBLIC SDL2::SDL2) +find_package(SDL3 REQUIRED) +target_link_libraries(ImGui PUBLIC SDL3::SDL3) if (USE_OPENGLES) target_link_libraries(ImGui PUBLIC ${OPENGL_GLESv2_LIBRARY}) diff --git a/include/libultraship/classes.h b/include/libultraship/classes.h index 7b75f8241..dbc046e4c 100644 --- a/include/libultraship/classes.h +++ b/include/libultraship/classes.h @@ -35,7 +35,7 @@ #include "audio/WasapiAudioPlayer.h" #endif #include "audio/SDLAudioPlayer.h" -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #include "utils/AppleFolderManager.h" #endif #endif diff --git a/src/Context.cpp b/src/Context.cpp index 362769ce3..a793c30a7 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -11,7 +11,7 @@ #include #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #include "utils/AppleFolderManager.h" #include #include @@ -375,7 +375,7 @@ std::string Context::GetShortName() { std::string Context::GetAppBundlePath() { #if defined(__ANDROID__) - const char* externaldir = SDL_AndroidGetExternalStoragePath(); + const char* externaldir = SDL_GetAndroidExternalStoragePath(); if (externaldir != NULL) { return externaldir; } @@ -389,7 +389,7 @@ std::string Context::GetAppBundlePath() { #ifdef NON_PORTABLE return CMAKE_INSTALL_PREFIX; #else -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE FolderManager folderManager; return folderManager.getMainBundlePath(); #endif @@ -416,7 +416,7 @@ std::string Context::GetAppBundlePath() { std::string Context::GetAppDirectoryPath(std::string appName) { #if defined(__ANDROID__) - const char* externaldir = SDL_AndroidGetExternalStoragePath(); + const char* externaldir = SDL_GetAndroidExternalStoragePath(); if (externaldir != NULL) { return externaldir; } @@ -427,7 +427,7 @@ std::string Context::GetAppDirectoryPath(std::string appName) { return std::string(home) + "/Documents"; #endif -#if defined(__APPLE__) +#if defined(SDL_PLATFORM_APPLE) if (char* fpath = std::getenv("SHIP_HOME")) { if (fpath[0] == '~') { const char* home = getenv("HOME") ? getenv("HOME") : getpwuid(getuid())->pw_dir; diff --git a/src/audio/SDLAudioPlayer.cpp b/src/audio/SDLAudioPlayer.cpp index 2184e280d..ef39ca910 100644 --- a/src/audio/SDLAudioPlayer.cpp +++ b/src/audio/SDLAudioPlayer.cpp @@ -1,6 +1,8 @@ #include "SDLAudioPlayer.h" #include +// todo: https://wiki.libsdl.org/SDL3/README/migration#sdl_audioh + namespace Ship { SDLAudioPlayer::~SDLAudioPlayer() { @@ -9,35 +11,36 @@ SDLAudioPlayer::~SDLAudioPlayer() { } bool SDLAudioPlayer::DoInit() { - if (SDL_Init(SDL_INIT_AUDIO) != 0) { - SPDLOG_ERROR("SDL init error: %s\n", SDL_GetError()); - return false; - } - SDL_AudioSpec want, have; - SDL_zero(want); - want.freq = this->GetSampleRate(); - want.format = AUDIO_S16SYS; - want.channels = 2; - want.samples = this->GetSampleLength(); - want.callback = NULL; - mDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); - if (mDevice == 0) { - SPDLOG_ERROR("SDL_OpenAudio error: {}", SDL_GetError()); - return false; - } - SDL_PauseAudioDevice(mDevice, 0); + // if (SDL_Init(SDL_INIT_AUDIO) != 0) { + // SPDLOG_ERROR("SDL init error: %s\n", SDL_GetError()); + // return false; + // } + // SDL_AudioSpec want, have; + // SDL_zero(want); + // want.freq = this->GetSampleRate(); + // want.format = SDL_AUDIO_S16; + // want.channels = 2; + // want.samples = this->GetSampleLength(); + // want.callback = NULL; + // mDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); + // if (mDevice == 0) { + // SPDLOG_ERROR("SDL_OpenAudio error: {}", SDL_GetError()); + // return false; + // } + // SDL_PauseAudioDevice(mDevice, 0); return true; } int SDLAudioPlayer::Buffered() { // 4 is sizeof(int16_t) * num_channels (2 for stereo) - return SDL_GetQueuedAudioSize(mDevice) / 4; + // return SDL_GetQueuedAudioSize(mDevice) / 4; + return 0; } void SDLAudioPlayer::Play(const uint8_t* buf, size_t len) { - if (Buffered() < 6000) { - // Don't fill the audio buffer too much in case this happens - SDL_QueueAudio(mDevice, buf, len); - } + // if (Buffered() < 6000) { + // // Don't fill the audio buffer too much in case this happens + // SDL_QueueAudio(mDevice, buf, len); + // } } } // namespace Ship diff --git a/src/audio/SDLAudioPlayer.h b/src/audio/SDLAudioPlayer.h index 148b15c72..77e794776 100644 --- a/src/audio/SDLAudioPlayer.h +++ b/src/audio/SDLAudioPlayer.h @@ -1,6 +1,6 @@ #pragma once #include "AudioPlayer.h" -#include +#include namespace Ship { class SDLAudioPlayer : public AudioPlayer { diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 4fdf06619..a1b7f9306 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -8,7 +8,7 @@ #include "utils/StringHelper.h" #include "Context.h" -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #include "graphic/Fast3D/gfx_metal.h" #endif @@ -252,7 +252,7 @@ WindowBackend Config::GetWindowBackend() { #ifdef ENABLE_DX11 return WindowBackend::FAST3D_DXGI_DX11; #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE if (Metal_IsSupported()) { return WindowBackend::FAST3D_SDL_METAL; } diff --git a/src/controller/controldevice/controller/Controller.cpp b/src/controller/controldevice/controller/Controller.cpp index 9d4e72a57..16fd85770 100644 --- a/src/controller/controldevice/controller/Controller.cpp +++ b/src/controller/controldevice/controller/Controller.cpp @@ -2,10 +2,10 @@ #include #include #include "public/bridge/consolevariablebridge.h" -#if __APPLE__ -#include +#if SDL_PLATFORM_APPLE +#include #else -#include +#include #endif #include #include "utils/StringHelper.h" diff --git a/src/controller/controldevice/controller/mapping/factories/AxisDirectionMappingFactory.cpp b/src/controller/controldevice/controller/mapping/factories/AxisDirectionMappingFactory.cpp index 2fe627d22..4d0dea7cf 100644 --- a/src/controller/controldevice/controller/mapping/factories/AxisDirectionMappingFactory.cpp +++ b/src/controller/controldevice/controller/mapping/factories/AxisDirectionMappingFactory.cpp @@ -158,7 +158,7 @@ AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(ShipDeviceInd std::shared_ptr AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t portIndex, StickIndex stickIndex, Direction direction) { - std::unordered_map sdlControllers; + std::unordered_map sdlControllers; std::shared_ptr mapping = nullptr; for (auto [lusIndex, indexMapping] : Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) { @@ -171,17 +171,17 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex(); - if (!SDL_IsGameController(sdlIndex)) { + if (!SDL_IsGamepad(sdlIndex)) { // this SDL device isn't a game controller continue; } - sdlControllers[lusIndex] = SDL_GameControllerOpen(sdlIndex); + sdlControllers[lusIndex] = SDL_OpenGamepad(sdlIndex); } for (auto [lusIndex, controller] : sdlControllers) { - for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) { - if (SDL_GameControllerGetButton(controller, static_cast(button))) { + for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) { + if (SDL_GetGamepadButton(controller, static_cast(button))) { mapping = std::make_shared(lusIndex, portIndex, stickIndex, direction, button); break; @@ -192,9 +192,9 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port break; } - for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { - const auto axis = static_cast(i); - const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f; + for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) { + const auto axis = static_cast(i); + const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f; int32_t axisDirection = 0; if (axisValue < -0.7f) { axisDirection = NEGATIVE; @@ -213,7 +213,7 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port } for (auto [i, controller] : sdlControllers) { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } return mapping; diff --git a/src/controller/controldevice/controller/mapping/factories/ButtonMappingFactory.cpp b/src/controller/controldevice/controller/mapping/factories/ButtonMappingFactory.cpp index f707f98f3..09b410988 100644 --- a/src/controller/controldevice/controller/mapping/factories/ButtonMappingFactory.cpp +++ b/src/controller/controldevice/controller/mapping/factories/ButtonMappingFactory.cpp @@ -172,73 +172,73 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(ShipDeviceIndex shipDeviceI switch (bitmask) { case BTN_A: mappings.push_back( - std::make_shared(shipDeviceIndex, portIndex, BTN_A, SDL_CONTROLLER_BUTTON_A)); + std::make_shared(shipDeviceIndex, portIndex, BTN_A, SDL_GAMEPAD_BUTTON_SOUTH)); break; case BTN_B: mappings.push_back( - std::make_shared(shipDeviceIndex, portIndex, BTN_B, SDL_CONTROLLER_BUTTON_B)); + std::make_shared(shipDeviceIndex, portIndex, BTN_B, SDL_GAMEPAD_BUTTON_EAST)); break; case BTN_L: if (!isGameCube) { mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_L, - SDL_CONTROLLER_BUTTON_LEFTSHOULDER)); + SDL_GAMEPAD_BUTTON_LEFT_SHOULDER)); } break; case BTN_R: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_R, - SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 1)); + SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 1)); break; case BTN_Z: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_Z, - SDL_CONTROLLER_AXIS_TRIGGERLEFT, 1)); + SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 1)); break; case BTN_START: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_START, - SDL_CONTROLLER_BUTTON_START)); + SDL_GAMEPAD_BUTTON_START)); break; case BTN_CUP: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_CUP, - SDL_CONTROLLER_AXIS_RIGHTY, -1)); + SDL_GAMEPAD_AXIS_RIGHTY, -1)); break; case BTN_CDOWN: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_CDOWN, - SDL_CONTROLLER_AXIS_RIGHTY, 1)); + SDL_GAMEPAD_AXIS_RIGHTY, 1)); if (isGameCube) { mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_CDOWN, - SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)); + SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER)); } break; case BTN_CLEFT: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_CLEFT, - SDL_CONTROLLER_AXIS_RIGHTX, -1)); + SDL_GAMEPAD_AXIS_RIGHTX, -1)); if (isGameCube) { mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_CLEFT, - SDL_CONTROLLER_BUTTON_Y)); + SDL_GAMEPAD_BUTTON_NORTH)); } break; case BTN_CRIGHT: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_CRIGHT, - SDL_CONTROLLER_AXIS_RIGHTX, 1)); + SDL_GAMEPAD_AXIS_RIGHTX, 1)); if (isGameCube) { mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_CRIGHT, - SDL_CONTROLLER_BUTTON_X)); + SDL_GAMEPAD_BUTTON_WEST)); } break; case BTN_DUP: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_DUP, - SDL_CONTROLLER_BUTTON_DPAD_UP)); + SDL_GAMEPAD_BUTTON_DPAD_UP)); break; case BTN_DDOWN: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_DDOWN, - SDL_CONTROLLER_BUTTON_DPAD_DOWN)); + SDL_GAMEPAD_BUTTON_DPAD_DOWN)); break; case BTN_DLEFT: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_DLEFT, - SDL_CONTROLLER_BUTTON_DPAD_LEFT)); + SDL_GAMEPAD_BUTTON_DPAD_LEFT)); break; case BTN_DRIGHT: mappings.push_back(std::make_shared(shipDeviceIndex, portIndex, BTN_DRIGHT, - SDL_CONTROLLER_BUTTON_DPAD_RIGHT)); + SDL_GAMEPAD_BUTTON_DPAD_RIGHT)); break; } @@ -247,7 +247,7 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(ShipDeviceIndex shipDeviceI std::shared_ptr ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROLLERBUTTONS_T bitmask) { - std::unordered_map sdlControllers; + std::unordered_map sdlControllers; std::shared_ptr mapping = nullptr; for (auto [lusIndex, indexMapping] : Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) { @@ -260,17 +260,17 @@ ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROL auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex(); - if (!SDL_IsGameController(sdlIndex)) { + if (!SDL_IsGamepad(sdlIndex)) { // this SDL device isn't a game controller continue; } - sdlControllers[lusIndex] = SDL_GameControllerOpen(sdlIndex); + sdlControllers[lusIndex] = SDL_OpenGamepad(sdlIndex); } for (auto [lusIndex, controller] : sdlControllers) { - for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) { - if (SDL_GameControllerGetButton(controller, static_cast(button))) { + for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) { + if (SDL_GetGamepadButton(controller, static_cast(button))) { mapping = std::make_shared(lusIndex, portIndex, bitmask, button); break; } @@ -280,9 +280,9 @@ ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROL break; } - for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { - const auto axis = static_cast(i); - const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f; + for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) { + const auto axis = static_cast(i); + const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f; int32_t axisDirection = 0; if (axisValue < -0.7f) { axisDirection = NEGATIVE; @@ -301,7 +301,7 @@ ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROL } for (auto [i, controller] : sdlControllers) { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } return mapping; diff --git a/src/controller/controldevice/controller/mapping/factories/GyroMappingFactory.cpp b/src/controller/controldevice/controller/mapping/factories/GyroMappingFactory.cpp index 78d6e183c..4b20e5f9b 100644 --- a/src/controller/controldevice/controller/mapping/factories/GyroMappingFactory.cpp +++ b/src/controller/controldevice/controller/mapping/factories/GyroMappingFactory.cpp @@ -45,7 +45,7 @@ std::shared_ptr GyroMappingFactory::CreateGyroMappingFrom } std::shared_ptr GyroMappingFactory::CreateGyroMappingFromSDLInput(uint8_t portIndex) { - std::unordered_map sdlControllersWithGyro; + std::unordered_map sdlControllersWithGyro; std::shared_ptr mapping = nullptr; for (auto [lusIndex, indexMapping] : Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) { @@ -58,22 +58,22 @@ std::shared_ptr GyroMappingFactory::CreateGyroMappingFrom auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex(); - if (!SDL_IsGameController(sdlIndex)) { + if (!SDL_IsGamepad(sdlIndex)) { // this SDL device isn't a game controller continue; } - auto controller = SDL_GameControllerOpen(sdlIndex); - if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO)) { - sdlControllersWithGyro[lusIndex] = SDL_GameControllerOpen(sdlIndex); + auto controller = SDL_OpenGamepad(sdlIndex); + if (SDL_GamepadHasSensor(controller, SDL_SENSOR_GYRO)) { + sdlControllersWithGyro[lusIndex] = SDL_OpenGamepad(sdlIndex); } else { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } } for (auto [lusIndex, controller] : sdlControllersWithGyro) { - for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) { - if (SDL_GameControllerGetButton(controller, static_cast(button))) { + for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) { + if (SDL_GetGamepadButton(controller, static_cast(button))) { mapping = std::make_shared(lusIndex, portIndex, 1.0f, 0.0f, 0.0f, 0.0f); mapping->Recalibrate(); break; @@ -84,9 +84,9 @@ std::shared_ptr GyroMappingFactory::CreateGyroMappingFrom break; } - for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { - const auto axis = static_cast(i); - const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f; + for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) { + const auto axis = static_cast(i); + const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f; int32_t axisDirection = 0; if (axisValue < -0.7f) { axisDirection = NEGATIVE; @@ -105,7 +105,7 @@ std::shared_ptr GyroMappingFactory::CreateGyroMappingFrom } for (auto [i, controller] : sdlControllersWithGyro) { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } return mapping; diff --git a/src/controller/controldevice/controller/mapping/factories/LEDMappingFactory.cpp b/src/controller/controldevice/controller/mapping/factories/LEDMappingFactory.cpp index 1a8138d38..e05cba28a 100644 --- a/src/controller/controldevice/controller/mapping/factories/LEDMappingFactory.cpp +++ b/src/controller/controldevice/controller/mapping/factories/LEDMappingFactory.cpp @@ -43,7 +43,7 @@ std::shared_ptr LEDMappingFactory::CreateLEDMappingFromCon } std::shared_ptr LEDMappingFactory::CreateLEDMappingFromSDLInput(uint8_t portIndex) { - std::unordered_map sdlControllersWithLEDs; + std::unordered_map sdlControllersWithLEDs; std::shared_ptr mapping = nullptr; for (auto [lusIndex, indexMapping] : Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) { @@ -56,22 +56,24 @@ std::shared_ptr LEDMappingFactory::CreateLEDMappingFromSDL auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex(); - if (!SDL_IsGameController(sdlIndex)) { + if (!SDL_IsGamepad(sdlIndex)) { // this SDL device isn't a game controller continue; } - auto controller = SDL_GameControllerOpen(sdlIndex); - if (SDL_GameControllerHasLED(controller)) { - sdlControllersWithLEDs[lusIndex] = SDL_GameControllerOpen(sdlIndex); + auto controller = SDL_OpenGamepad(sdlIndex); + // todo: SDL_GameControllerHasLED() - replaced with SDL_PROP_GAMEPAD_CAP_RGB_LED_BOOLEAN + // if (SDL_GameControllerHasLED(controller)) { + if (false) { + sdlControllersWithLEDs[lusIndex] = SDL_OpenGamepad(sdlIndex); } else { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } } for (auto [lusIndex, controller] : sdlControllersWithLEDs) { - for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) { - if (SDL_GameControllerGetButton(controller, static_cast(button))) { + for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) { + if (SDL_GetGamepadButton(controller, static_cast(button))) { mapping = std::make_shared(lusIndex, portIndex, 0, Color_RGB8({ 0, 0, 0 })); break; } @@ -81,9 +83,9 @@ std::shared_ptr LEDMappingFactory::CreateLEDMappingFromSDL break; } - for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { - const auto axis = static_cast(i); - const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f; + for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) { + const auto axis = static_cast(i); + const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f; int32_t axisDirection = 0; if (axisValue < -0.7f) { axisDirection = NEGATIVE; @@ -101,7 +103,7 @@ std::shared_ptr LEDMappingFactory::CreateLEDMappingFromSDL } for (auto [i, controller] : sdlControllersWithLEDs) { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } return mapping; diff --git a/src/controller/controldevice/controller/mapping/factories/RumbleMappingFactory.cpp b/src/controller/controldevice/controller/mapping/factories/RumbleMappingFactory.cpp index c50d4a449..bfb6e2a61 100644 --- a/src/controller/controldevice/controller/mapping/factories/RumbleMappingFactory.cpp +++ b/src/controller/controldevice/controller/mapping/factories/RumbleMappingFactory.cpp @@ -63,7 +63,7 @@ RumbleMappingFactory::CreateDefaultSDLRumbleMappings(ShipDeviceIndex shipDeviceI } std::shared_ptr RumbleMappingFactory::CreateRumbleMappingFromSDLInput(uint8_t portIndex) { - std::unordered_map sdlControllersWithRumble; + std::unordered_map sdlControllersWithRumble; std::shared_ptr mapping = nullptr; for (auto [lusIndex, indexMapping] : Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) { @@ -76,24 +76,26 @@ std::shared_ptr RumbleMappingFactory::CreateRumbleMappi auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex(); - if (!SDL_IsGameController(sdlIndex)) { + if (!SDL_IsGamepad(sdlIndex)) { // this SDL device isn't a game controller continue; } - auto controller = SDL_GameControllerOpen(sdlIndex); - bool hasRumble = SDL_GameControllerHasRumble(controller); + auto controller = SDL_OpenGamepad(sdlIndex); + // todo: SDL_GameControllerHasRumble() - replaced with SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN + // bool hasRumble = SDL_GameControllerHasRumble(controller); + bool hasRumble = false; if (hasRumble) { - sdlControllersWithRumble[lusIndex] = SDL_GameControllerOpen(sdlIndex); + sdlControllersWithRumble[lusIndex] = SDL_OpenGamepad(sdlIndex); } else { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } } for (auto [lusIndex, controller] : sdlControllersWithRumble) { - for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) { - if (SDL_GameControllerGetButton(controller, static_cast(button))) { + for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) { + if (SDL_GetGamepadButton(controller, static_cast(button))) { mapping = std::make_shared(lusIndex, portIndex, DEFAULT_LOW_FREQUENCY_RUMBLE_PERCENTAGE, DEFAULT_HIGH_FREQUENCY_RUMBLE_PERCENTAGE); @@ -105,9 +107,9 @@ std::shared_ptr RumbleMappingFactory::CreateRumbleMappi break; } - for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { - const auto axis = static_cast(i); - const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f; + for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) { + const auto axis = static_cast(i); + const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f; int32_t axisDirection = 0; if (axisValue < -0.7f) { axisDirection = NEGATIVE; @@ -126,7 +128,7 @@ std::shared_ptr RumbleMappingFactory::CreateRumbleMappi } for (auto [i, controller] : sdlControllersWithRumble) { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } return mapping; diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.cpp index 3228dd3c2..d1e9b9959 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.cpp @@ -7,7 +7,7 @@ namespace Ship { SDLAxisDirectionToAnyMapping::SDLAxisDirectionToAnyMapping(ShipDeviceIndex shipDeviceIndex, int32_t sdlControllerAxis, int32_t axisDirection) : ControllerInputMapping(shipDeviceIndex), SDLMapping(shipDeviceIndex) { - mControllerAxis = static_cast(sdlControllerAxis); + mControllerAxis = static_cast(sdlControllerAxis); mAxisDirection = static_cast(axisDirection); } @@ -16,19 +16,19 @@ SDLAxisDirectionToAnyMapping::~SDLAxisDirectionToAnyMapping() { std::string SDLAxisDirectionToAnyMapping::GetPhysicalInputName() { switch (mControllerAxis) { - case SDL_CONTROLLER_AXIS_LEFTX: + case SDL_GAMEPAD_AXIS_LEFTX: return StringHelper::Sprintf(UsesGameCubeLayout() ? "Analog Stick %s" : "Left Stick %s", mAxisDirection == NEGATIVE ? ICON_FA_ARROW_LEFT : ICON_FA_ARROW_RIGHT); - case SDL_CONTROLLER_AXIS_LEFTY: + case SDL_GAMEPAD_AXIS_LEFTY: return StringHelper::Sprintf(UsesGameCubeLayout() ? "Analog Stick %s" : "Left Stick %s", mAxisDirection == NEGATIVE ? ICON_FA_ARROW_UP : ICON_FA_ARROW_DOWN); - case SDL_CONTROLLER_AXIS_RIGHTX: + case SDL_GAMEPAD_AXIS_RIGHTX: return StringHelper::Sprintf(UsesGameCubeLayout() ? "C Stick %s" : "Right Stick %s", mAxisDirection == NEGATIVE ? ICON_FA_ARROW_LEFT : ICON_FA_ARROW_RIGHT); - case SDL_CONTROLLER_AXIS_RIGHTY: + case SDL_GAMEPAD_AXIS_RIGHTY: return StringHelper::Sprintf(UsesGameCubeLayout() ? "C Stick %s" : "Right Stick %s", mAxisDirection == NEGATIVE ? ICON_FA_ARROW_UP : ICON_FA_ARROW_DOWN); - case SDL_CONTROLLER_AXIS_TRIGGERLEFT: + case SDL_GAMEPAD_AXIS_LEFT_TRIGGER: if (UsesPlaystationLayout()) { return "L2"; } @@ -42,7 +42,7 @@ std::string SDLAxisDirectionToAnyMapping::GetPhysicalInputName() { return "L"; } break; - case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: + case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER: if (UsesPlaystationLayout()) { return "R2"; } @@ -73,11 +73,11 @@ bool SDLAxisDirectionToAnyMapping::PhysicalDeviceIsConnected() { } bool SDLAxisDirectionToAnyMapping::AxisIsTrigger() { - return mControllerAxis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || mControllerAxis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT; + return mControllerAxis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER || mControllerAxis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER; } bool SDLAxisDirectionToAnyMapping::AxisIsStick() { - return mControllerAxis == SDL_CONTROLLER_AXIS_LEFTX || mControllerAxis == SDL_CONTROLLER_AXIS_LEFTY || - mControllerAxis == SDL_CONTROLLER_AXIS_RIGHTX || mControllerAxis == SDL_CONTROLLER_AXIS_RIGHTY; + return mControllerAxis == SDL_GAMEPAD_AXIS_LEFTX || mControllerAxis == SDL_GAMEPAD_AXIS_LEFTY || + mControllerAxis == SDL_GAMEPAD_AXIS_RIGHTX || mControllerAxis == SDL_GAMEPAD_AXIS_RIGHTY; } } // namespace Ship diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.h b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.h index 21f8a792c..6496c927b 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.h +++ b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAnyMapping.h @@ -15,7 +15,7 @@ class SDLAxisDirectionToAnyMapping : virtual public ControllerInputMapping, publ bool AxisIsStick(); protected: - SDL_GameControllerAxis mControllerAxis; + SDL_GamepadAxis mControllerAxis; AxisDirection mAxisDirection; }; } // namespace Ship diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAxisDirectionMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAxisDirectionMapping.cpp index f94c307cb..e51bf53fb 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAxisDirectionMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToAxisDirectionMapping.cpp @@ -27,7 +27,7 @@ float SDLAxisDirectionToAxisDirectionMapping::GetNormalizedAxisDirectionValue() return 0.0f; } - const auto axisValue = SDL_GameControllerGetAxis(mController, mControllerAxis); + const auto axisValue = SDL_GetGamepadAxis(mController, mControllerAxis); if ((mAxisDirection == POSITIVE && axisValue < 0) || (mAxisDirection == NEGATIVE && axisValue > 0)) { return 0.0f; diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToButtonMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToButtonMapping.cpp index de57fe4b9..9d37a04f9 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToButtonMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLAxisDirectionToButtonMapping.cpp @@ -22,7 +22,7 @@ void SDLAxisDirectionToButtonMapping::UpdatePad(CONTROLLERBUTTONS_T& padButtons) return; } - const auto axisValue = SDL_GameControllerGetAxis(mController, mControllerAxis); + const auto axisValue = SDL_GetGamepadAxis(mController, mControllerAxis); int32_t axisThresholdPercentage = 25; auto indexMapping = Context::GetInstance() ->GetControlDeck() diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.cpp index da74565b9..dc65ce6c0 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.cpp @@ -6,7 +6,7 @@ namespace Ship { SDLButtonToAnyMapping::SDLButtonToAnyMapping(ShipDeviceIndex shipDeviceIndex, int32_t sdlControllerButton) : ControllerInputMapping(shipDeviceIndex), SDLMapping(shipDeviceIndex) { - mControllerButton = static_cast(sdlControllerButton); + mControllerButton = static_cast(sdlControllerButton); } SDLButtonToAnyMapping::~SDLButtonToAnyMapping() { @@ -38,25 +38,25 @@ std::string SDLButtonToAnyMapping::GetGenericButtonName() { std::string SDLButtonToAnyMapping::GetGameCubeButtonName() { switch (mControllerButton) { - case SDL_CONTROLLER_BUTTON_A: + case SDL_GAMEPAD_BUTTON_SOUTH: return "A"; - case SDL_CONTROLLER_BUTTON_B: + case SDL_GAMEPAD_BUTTON_EAST: return "B"; - case SDL_CONTROLLER_BUTTON_X: + case SDL_GAMEPAD_BUTTON_WEST: return "X"; - case SDL_CONTROLLER_BUTTON_Y: + case SDL_GAMEPAD_BUTTON_NORTH: return "Y"; - case SDL_CONTROLLER_BUTTON_START: + case SDL_GAMEPAD_BUTTON_START: return "Start"; - case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: return "Z"; - case SDL_CONTROLLER_BUTTON_DPAD_UP: + case SDL_GAMEPAD_BUTTON_DPAD_UP: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_UP); - case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_DOWN); - case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_LEFT); - case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_RIGHT); default: break; @@ -67,50 +67,50 @@ std::string SDLButtonToAnyMapping::GetGameCubeButtonName() { std::string SDLButtonToAnyMapping::GetPlaystationButtonName() { switch (mControllerButton) { - case SDL_CONTROLLER_BUTTON_A: + case SDL_GAMEPAD_BUTTON_SOUTH: return StringHelper::Sprintf("%s", ICON_FA_TIMES); - case SDL_CONTROLLER_BUTTON_B: + case SDL_GAMEPAD_BUTTON_EAST: return StringHelper::Sprintf("%s", ICON_FA_CIRCLE_O); - case SDL_CONTROLLER_BUTTON_X: + case SDL_GAMEPAD_BUTTON_WEST: return StringHelper::Sprintf("%s", ICON_FA_SQUARE_O); - case SDL_CONTROLLER_BUTTON_Y: + case SDL_GAMEPAD_BUTTON_NORTH: return "Triangle"; // imgui default font doesn't have Δ, and font-awesome 4 doesn't have a triangle - case SDL_CONTROLLER_BUTTON_BACK: - if (GetSDLControllerType() == SDL_CONTROLLER_TYPE_PS3) { + case SDL_GAMEPAD_BUTTON_BACK: + if (GetSDLControllerType() == SDL_GAMEPAD_TYPE_PS3) { return "Select"; } - if (GetSDLControllerType() == SDL_CONTROLLER_TYPE_PS4) { + if (GetSDLControllerType() == SDL_GAMEPAD_TYPE_PS4) { return "Share"; } return "Create"; - case SDL_CONTROLLER_BUTTON_GUIDE: + case SDL_GAMEPAD_BUTTON_GUIDE: return "PS"; - case SDL_CONTROLLER_BUTTON_START: - if (GetSDLControllerType() == SDL_CONTROLLER_TYPE_PS3) { + case SDL_GAMEPAD_BUTTON_START: + if (GetSDLControllerType() == SDL_GAMEPAD_TYPE_PS3) { return "Start"; } - if (GetSDLControllerType() == SDL_CONTROLLER_TYPE_PS4) { + if (GetSDLControllerType() == SDL_GAMEPAD_TYPE_PS4) { return "Options"; } return StringHelper::Sprintf("%s", ICON_FA_BARS); - case SDL_CONTROLLER_BUTTON_LEFTSTICK: + case SDL_GAMEPAD_BUTTON_LEFT_STICK: return "L3"; // it seems the official term is just long for these - case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + case SDL_GAMEPAD_BUTTON_RIGHT_STICK: return "R3"; // it seems the official term is just long for these - case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: return "L1"; - case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: return "R1"; - case SDL_CONTROLLER_BUTTON_DPAD_UP: + case SDL_GAMEPAD_BUTTON_DPAD_UP: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_UP); - case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_DOWN); - case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_LEFT); - case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_RIGHT); - case SDL_CONTROLLER_BUTTON_MISC1: - if (GetSDLControllerType() == SDL_CONTROLLER_TYPE_PS5) { + case SDL_GAMEPAD_BUTTON_MISC1: + if (GetSDLControllerType() == SDL_GAMEPAD_TYPE_PS5) { return StringHelper::Sprintf("%s", ICON_FA_MICROPHONE_SLASH); } break; @@ -123,37 +123,37 @@ std::string SDLButtonToAnyMapping::GetPlaystationButtonName() { std::string SDLButtonToAnyMapping::GetSwitchButtonName() { switch (mControllerButton) { - case SDL_CONTROLLER_BUTTON_A: + case SDL_GAMEPAD_BUTTON_SOUTH: return "A"; - case SDL_CONTROLLER_BUTTON_B: + case SDL_GAMEPAD_BUTTON_EAST: return "B"; - case SDL_CONTROLLER_BUTTON_X: + case SDL_GAMEPAD_BUTTON_WEST: return "X"; - case SDL_CONTROLLER_BUTTON_Y: + case SDL_GAMEPAD_BUTTON_NORTH: return "Y"; - case SDL_CONTROLLER_BUTTON_BACK: + case SDL_GAMEPAD_BUTTON_BACK: return StringHelper::Sprintf("%s", ICON_FA_MINUS); - case SDL_CONTROLLER_BUTTON_GUIDE: + case SDL_GAMEPAD_BUTTON_GUIDE: return StringHelper::Sprintf("%s", ICON_FA_HOME); - case SDL_CONTROLLER_BUTTON_START: + case SDL_GAMEPAD_BUTTON_START: return StringHelper::Sprintf("%s", ICON_FA_PLUS); - case SDL_CONTROLLER_BUTTON_LEFTSTICK: + case SDL_GAMEPAD_BUTTON_LEFT_STICK: return "Left Stick Press"; // it seems the official term is just long for these - case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + case SDL_GAMEPAD_BUTTON_RIGHT_STICK: return "Right Stick Press"; // it seems the official term is just long for these - case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: return "L"; - case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: return "R"; - case SDL_CONTROLLER_BUTTON_DPAD_UP: + case SDL_GAMEPAD_BUTTON_DPAD_UP: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_UP); - case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_DOWN); - case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_LEFT); - case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_RIGHT); - case SDL_CONTROLLER_BUTTON_MISC1: + case SDL_GAMEPAD_BUTTON_MISC1: return "Capture"; /* Xbox Series X share button, PS5 microphone button, Nintendo Switch Pro capture button, Amazon Luna microphone button */ default: @@ -165,52 +165,52 @@ std::string SDLButtonToAnyMapping::GetSwitchButtonName() { std::string SDLButtonToAnyMapping::GetXboxButtonName() { switch (mControllerButton) { - case SDL_CONTROLLER_BUTTON_A: + case SDL_GAMEPAD_BUTTON_SOUTH: return "A"; - case SDL_CONTROLLER_BUTTON_B: + case SDL_GAMEPAD_BUTTON_EAST: return "B"; - case SDL_CONTROLLER_BUTTON_X: + case SDL_GAMEPAD_BUTTON_WEST: return "X"; - case SDL_CONTROLLER_BUTTON_Y: + case SDL_GAMEPAD_BUTTON_NORTH: return "Y"; - case SDL_CONTROLLER_BUTTON_BACK: - if (GetSDLControllerType() == SDL_CONTROLLER_TYPE_XBOX360) { + case SDL_GAMEPAD_BUTTON_BACK: + if (GetSDLControllerType() == SDL_GAMEPAD_TYPE_XBOX360) { return "Back"; } return "View"; - case SDL_CONTROLLER_BUTTON_GUIDE: + case SDL_GAMEPAD_BUTTON_GUIDE: return "Xbox"; - case SDL_CONTROLLER_BUTTON_START: - if (GetSDLControllerType() == SDL_CONTROLLER_TYPE_XBOX360) { + case SDL_GAMEPAD_BUTTON_START: + if (GetSDLControllerType() == SDL_GAMEPAD_TYPE_XBOX360) { return "Start"; } return StringHelper::Sprintf("%s", ICON_FA_BARS); - case SDL_CONTROLLER_BUTTON_LEFTSTICK: + case SDL_GAMEPAD_BUTTON_LEFT_STICK: return "LS"; - case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + case SDL_GAMEPAD_BUTTON_RIGHT_STICK: return "RS"; - case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: return "LB"; - case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: return "RB"; - case SDL_CONTROLLER_BUTTON_DPAD_UP: + case SDL_GAMEPAD_BUTTON_DPAD_UP: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_UP); - case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_DOWN); - case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_LEFT); - case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_RIGHT); - case SDL_CONTROLLER_BUTTON_MISC1: + case SDL_GAMEPAD_BUTTON_MISC1: return "Share"; /* Xbox Series X share button, PS5 microphone button, Nintendo Switch Pro capture button, Amazon Luna microphone button */ - case SDL_CONTROLLER_BUTTON_PADDLE1: + case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1: return "P1"; - case SDL_CONTROLLER_BUTTON_PADDLE2: + case SDL_GAMEPAD_BUTTON_LEFT_PADDLE1: return "P2"; - case SDL_CONTROLLER_BUTTON_PADDLE3: + case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2: return "P3"; - case SDL_CONTROLLER_BUTTON_PADDLE4: + case SDL_GAMEPAD_BUTTON_LEFT_PADDLE2: return "P4"; default: break; diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.h b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.h index 1c8c8c4d3..8751effa8 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.h +++ b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAnyMapping.h @@ -13,7 +13,7 @@ class SDLButtonToAnyMapping : virtual public ControllerInputMapping, public SDLM bool PhysicalDeviceIsConnected() override; protected: - SDL_GameControllerButton mControllerButton; + SDL_GamepadButton mControllerButton; private: std::string GetPlaystationButtonName(); diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAxisDirectionMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAxisDirectionMapping.cpp index 5044f4839..aacc1a512 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAxisDirectionMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToAxisDirectionMapping.cpp @@ -18,7 +18,7 @@ SDLButtonToAxisDirectionMapping::SDLButtonToAxisDirectionMapping(ShipDeviceIndex float SDLButtonToAxisDirectionMapping::GetNormalizedAxisDirectionValue() { if (ControllerLoaded() && !Context::GetInstance()->GetControlDeck()->GamepadGameInputBlocked() && - SDL_GameControllerGetButton(mController, mControllerButton)) { + SDL_GetGamepadButton(mController, mControllerButton)) { return MAX_AXIS_RANGE; } diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToButtonMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToButtonMapping.cpp index 1c3d730be..cb427d676 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLButtonToButtonMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLButtonToButtonMapping.cpp @@ -21,7 +21,7 @@ void SDLButtonToButtonMapping::UpdatePad(CONTROLLERBUTTONS_T& padButtons) { return; } - if (SDL_GameControllerGetButton(mController, mControllerButton)) { + if (SDL_GetGamepadButton(mController, mControllerButton)) { padButtons |= mBitmask; } } diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLGyroMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLGyroMapping.cpp index e7b139984..6b43e977b 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLGyroMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLGyroMapping.cpp @@ -22,8 +22,8 @@ void SDLGyroMapping::Recalibrate() { } float gyroData[3]; - SDL_GameControllerSetSensorEnabled(mController, SDL_SENSOR_GYRO, SDL_TRUE); - SDL_GameControllerGetSensorData(mController, SDL_SENSOR_GYRO, gyroData, 3); + SDL_SetGamepadSensorEnabled(mController, SDL_SENSOR_GYRO, true); + SDL_GetGamepadSensorData(mController, SDL_SENSOR_GYRO, gyroData, 3); mNeutralPitch = gyroData[0]; mNeutralYaw = gyroData[1]; @@ -38,8 +38,8 @@ void SDLGyroMapping::UpdatePad(float& x, float& y) { } float gyroData[3]; - SDL_GameControllerSetSensorEnabled(mController, SDL_SENSOR_GYRO, SDL_TRUE); - SDL_GameControllerGetSensorData(mController, SDL_SENSOR_GYRO, gyroData, 3); + SDL_SetGamepadSensorEnabled(mController, SDL_SENSOR_GYRO, true); + SDL_GetGamepadSensorData(mController, SDL_SENSOR_GYRO, gyroData, 3); x = (gyroData[0] - mNeutralPitch) * mSensitivity; y = (gyroData[1] - mNeutralYaw) * mSensitivity; diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLLEDMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLLEDMapping.cpp index 4dbf5c43b..ccd82346d 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLLEDMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLLEDMapping.cpp @@ -14,7 +14,9 @@ void SDLLEDMapping::SetLEDColor(Color_RGB8 color) { return; } - if (!SDL_GameControllerHasLED(mController)) { + // todo: SDL_GameControllerHasLED() - replaced with SDL_PROP_GAMEPAD_CAP_RGB_LED_BOOLEAN + // if (!SDL_GameControllerHasLED(mController)) { + if (true) { return; } @@ -26,7 +28,7 @@ void SDLLEDMapping::SetLEDColor(Color_RGB8 color) { color = mSavedColor; } - SDL_JoystickSetLED(SDL_GameControllerGetJoystick(mController), color.r, color.g, color.b); + SDL_SetJoystickLED(SDL_GetGamepadJoystick(mController), color.r, color.g, color.b); } std::string SDLLEDMapping::GetLEDMappingId() { diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLMapping.cpp index 4e5a35de5..82d2cfb6c 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLMapping.cpp @@ -25,7 +25,7 @@ bool SDLMapping::OpenController() { return false; } - const auto newCont = SDL_GameControllerOpen(deviceIndexMapping->GetSDLDeviceIndex()); + const auto newCont = SDL_OpenGamepad(deviceIndexMapping->GetSDLDeviceIndex()); // We failed to load the controller if (newCont == nullptr) { @@ -37,8 +37,8 @@ bool SDLMapping::OpenController() { } bool SDLMapping::CloseController() { - if (mController != nullptr && SDL_WasInit(SDL_INIT_GAMECONTROLLER)) { - SDL_GameControllerClose(mController); + if (mController != nullptr && SDL_WasInit(SDL_INIT_GAMEPAD)) { + SDL_CloseGamepad(mController); } mController = nullptr; @@ -46,10 +46,10 @@ bool SDLMapping::CloseController() { } bool SDLMapping::ControllerLoaded(bool closeIfDisconnected) { - SDL_GameControllerUpdate(); + SDL_UpdateGamepads(); // If the controller is disconnected - if (mController != nullptr && !SDL_GameControllerGetAttached(mController)) { + if (mController != nullptr && !SDL_GamepadConnected(mController)) { if (!closeIfDisconnected) { return false; } @@ -68,12 +68,12 @@ bool SDLMapping::ControllerLoaded(bool closeIfDisconnected) { return true; } -SDL_GameControllerType SDLMapping::GetSDLControllerType() { +SDL_GamepadType SDLMapping::GetSDLControllerType() { if (!ControllerLoaded()) { - return SDL_CONTROLLER_TYPE_UNKNOWN; + return SDL_GAMEPAD_TYPE_STANDARD; } - return SDL_GameControllerGetType(mController); + return SDL_GetGamepadType(mController); } uint16_t SDLMapping::GetSDLControllerVendorId() { @@ -81,7 +81,7 @@ uint16_t SDLMapping::GetSDLControllerVendorId() { return 0; } - return SDL_GameControllerGetVendor(mController); + return SDL_GetGamepadVendor(mController); } uint16_t SDLMapping::GetSDLControllerProductId() { @@ -89,22 +89,22 @@ uint16_t SDLMapping::GetSDLControllerProductId() { return 0; } - return SDL_GameControllerGetProduct(mController); + return SDL_GetGamepadProduct(mController); } bool SDLMapping::UsesPlaystationLayout() { auto type = GetSDLControllerType(); - return type == SDL_CONTROLLER_TYPE_PS3 || type == SDL_CONTROLLER_TYPE_PS4 || type == SDL_CONTROLLER_TYPE_PS5; + return type == SDL_GAMEPAD_TYPE_PS3 || type == SDL_GAMEPAD_TYPE_PS4 || type == SDL_GAMEPAD_TYPE_PS5; } bool SDLMapping::UsesSwitchLayout() { auto type = GetSDLControllerType(); - return type == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO || type == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR; + return type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO || type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR; } bool SDLMapping::UsesXboxLayout() { auto type = GetSDLControllerType(); - return type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE; + return type == SDL_GAMEPAD_TYPE_XBOX360 || type == SDL_GAMEPAD_TYPE_XBOXONE; } bool SDLMapping::UsesGameCubeLayout() { @@ -147,7 +147,7 @@ int32_t SDLMapping::GetJoystickInstanceId() { return -1; } - return SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(mController)); + return SDL_GetJoystickID(SDL_GetGamepadJoystick(mController)); } int32_t SDLMapping::GetCurrentSDLDeviceIndex() { @@ -155,13 +155,20 @@ int32_t SDLMapping::GetCurrentSDLDeviceIndex() { return -1; } - for (int32_t i = 0; i < SDL_NumJoysticks(); i++) { - SDL_Joystick* joystick = SDL_JoystickOpen(i); - if (SDL_JoystickInstanceID(joystick) == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(mController))) { - SDL_JoystickClose(joystick); - return i; + int i, numJoysticks; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); + if (joysticks) { + for (i = 0; i < numJoysticks; ++i) { + SDL_JoystickID instanceId = joysticks[i]; + SDL_Joystick* joystick = SDL_OpenJoystick(instanceId); + if (SDL_GetJoystickID(joystick) == SDL_GetJoystickID(SDL_GetGamepadJoystick(mController))) { + SDL_CloseJoystick(joystick); + SDL_free(joysticks); + return i; + } + SDL_CloseJoystick(joystick); } - SDL_JoystickClose(joystick); + SDL_free(joysticks); } // didn't find one diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLMapping.h b/src/controller/controldevice/controller/mapping/sdl/SDLMapping.h index bc0837609..2f8489ed5 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLMapping.h +++ b/src/controller/controldevice/controller/mapping/sdl/SDLMapping.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include "controller/controldevice/controller/mapping/ControllerMapping.h" @@ -20,7 +20,7 @@ class SDLMapping : public ControllerMapping { bool ControllerLoaded(bool closeIfDisconnected = false); protected: - SDL_GameControllerType GetSDLControllerType(); + SDL_GamepadType GetSDLControllerType(); uint16_t GetSDLControllerVendorId(); uint16_t GetSDLControllerProductId(); bool UsesPlaystationLayout(); @@ -30,7 +30,7 @@ class SDLMapping : public ControllerMapping { std::string GetSDLDeviceName(); int32_t GetSDLDeviceIndex(); - SDL_GameController* mController; + SDL_Gamepad* mController; private: bool OpenController(); diff --git a/src/controller/controldevice/controller/mapping/sdl/SDLRumbleMapping.cpp b/src/controller/controldevice/controller/mapping/sdl/SDLRumbleMapping.cpp index ad23de79a..3a3236658 100644 --- a/src/controller/controldevice/controller/mapping/sdl/SDLRumbleMapping.cpp +++ b/src/controller/controldevice/controller/mapping/sdl/SDLRumbleMapping.cpp @@ -18,7 +18,7 @@ void SDLRumbleMapping::StartRumble() { return; } - SDL_GameControllerRumble(mController, mLowFrequencyIntensity, mHighFrequencyIntensity, 0); + SDL_RumbleGamepad(mController, mLowFrequencyIntensity, mHighFrequencyIntensity, 0); } void SDLRumbleMapping::StopRumble() { @@ -26,7 +26,7 @@ void SDLRumbleMapping::StopRumble() { return; } - SDL_GameControllerRumble(mController, 0, 0, 0); + SDL_RumbleGamepad(mController, 0, 0, 0); } void SDLRumbleMapping::SetLowFrequencyIntensity(uint8_t intensityPercentage) { diff --git a/src/controller/deviceindex/ControllerDisconnectedWindow.cpp b/src/controller/deviceindex/ControllerDisconnectedWindow.cpp index b706f8a25..23bc6ec2d 100644 --- a/src/controller/deviceindex/ControllerDisconnectedWindow.cpp +++ b/src/controller/deviceindex/ControllerDisconnectedWindow.cpp @@ -1,6 +1,6 @@ #include "ControllerDisconnectedWindow.h" #include "utils/StringHelper.h" -#include +#include #include #include "Context.h" @@ -16,16 +16,16 @@ void ControllerDisconnectedWindow::InitElement() { void ControllerDisconnectedWindow::UpdateElement() { SDL_PumpEvents(); SDL_Event event; - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED) > 0) { - // from https://wiki.libsdl.org/SDL2/SDL_ControllerDeviceEvent: which - the joystick device index for - // the SDL_CONTROLLERDEVICEADDED event + while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_GAMEPAD_ADDED, SDL_EVENT_GAMEPAD_ADDED) > 0) { + // from https://wiki.libsdl.org/SDL2/SDL_GamepadDeviceEvent: which - the joystick device index for + // the SDL_EVENT_GAMEPAD_ADDED event Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->HandlePhysicalDeviceConnect( event.cdevice.which); } - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMOVED) > 0) { - // from https://wiki.libsdl.org/SDL2/SDL_ControllerDeviceEvent: which - the [...] instance id for the - // SDL_CONTROLLERDEVICEREMOVED [...] event + while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_GAMEPAD_REMOVED, SDL_EVENT_GAMEPAD_REMOVED) > 0) { + // from https://wiki.libsdl.org/SDL2/SDL_GamepadDeviceEvent: which - the [...] instance id for the + // SDL_EVENT_GAMEPAD_REMOVED [...] event Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->HandlePhysicalDeviceDisconnect( event.cdevice.which); } @@ -34,16 +34,23 @@ void ControllerDisconnectedWindow::UpdateElement() { int32_t ControllerDisconnectedWindow::GetSDLIndexFromSDLInput() { int32_t sdlDeviceIndex = -1; - std::unordered_map sdlControllers; - for (auto i = 0; i < SDL_NumJoysticks(); i++) { - if (SDL_IsGameController(i)) { - sdlControllers[i] = SDL_GameControllerOpen(i); + std::unordered_map sdlControllers; + + int i, numJoysticks; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); + if (joysticks) { + for (i = 0; i < numJoysticks; ++i) { + SDL_JoystickID instanceId = joysticks[i]; + if (SDL_IsGamepad(instanceId)) { + sdlControllers[i] = SDL_OpenGamepad(instanceId); + } } + SDL_free(joysticks); } for (auto [controllerIndex, controller] : sdlControllers) { - for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) { - if (SDL_GameControllerGetButton(controller, static_cast(button))) { + for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) { + if (SDL_GetGamepadButton(controller, static_cast(button))) { sdlDeviceIndex = controllerIndex; break; } @@ -53,9 +60,9 @@ int32_t ControllerDisconnectedWindow::GetSDLIndexFromSDLInput() { break; } - for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { - const auto axis = static_cast(i); - const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f; + for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) { + const auto axis = static_cast(i); + const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f; if (axisValue < -0.7f || axisValue > 0.7f) { sdlDeviceIndex = controllerIndex; break; @@ -64,7 +71,7 @@ int32_t ControllerDisconnectedWindow::GetSDLIndexFromSDLInput() { } for (auto [i, controller] : sdlControllers) { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } return sdlDeviceIndex; @@ -94,11 +101,17 @@ void ControllerDisconnectedWindow::DrawKnownControllerDisconnected() { Hide(); } + int i, numJoysticks; uint8_t connectedSdlControllerCount = 0; - for (auto i = 0; i < SDL_NumJoysticks(); i++) { - if (SDL_IsGameController(i)) { - connectedSdlControllerCount++; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); + if (joysticks) { + for (i = 0; i < numJoysticks; ++i) { + SDL_JoystickID instanceId = joysticks[i]; + if (SDL_IsGamepad(instanceId)) { + connectedSdlControllerCount++; + } } + SDL_free(joysticks); } if (connectedSdlControllerCount != 0 && @@ -115,11 +128,17 @@ void ControllerDisconnectedWindow::DrawKnownControllerDisconnected() { void ControllerDisconnectedWindow::DrawUnknownOrMultipleControllersDisconnected() { ImGui::Text("Controller(s) disconnected."); + int i, numJoysticks; uint8_t connectedSdlControllerCount = 0; - for (auto i = 0; i < SDL_NumJoysticks(); i++) { - if (SDL_IsGameController(i)) { - connectedSdlControllerCount++; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); + if (joysticks) { + for (i = 0; i < numJoysticks; ++i) { + SDL_JoystickID instanceId = joysticks[i]; + if (SDL_IsGamepad(instanceId)) { + connectedSdlControllerCount++; + } } + SDL_free(joysticks); } if (connectedSdlControllerCount != 0 && diff --git a/src/controller/deviceindex/ControllerReorderingWindow.cpp b/src/controller/deviceindex/ControllerReorderingWindow.cpp index 0fd340279..f664bd63a 100644 --- a/src/controller/deviceindex/ControllerReorderingWindow.cpp +++ b/src/controller/deviceindex/ControllerReorderingWindow.cpp @@ -1,6 +1,6 @@ #include "ControllerReorderingWindow.h" #include "utils/StringHelper.h" -#include +#include #include #include "Context.h" @@ -20,16 +20,23 @@ void ControllerReorderingWindow::UpdateElement() { int32_t ControllerReorderingWindow::GetSDLIndexFromSDLInput() { int32_t sdlDeviceIndex = -1; - std::unordered_map sdlControllers; - for (auto i = 0; i < SDL_NumJoysticks(); i++) { - if (SDL_IsGameController(i)) { - sdlControllers[i] = SDL_GameControllerOpen(i); + std::unordered_map sdlControllers; + + int i, numJoysticks; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); + if (joysticks) { + for (i = 0; i < numJoysticks; ++i) { + SDL_JoystickID instanceId = joysticks[i]; + if (SDL_IsGamepad(instanceId)) { + sdlControllers[i] = SDL_OpenGamepad(instanceId); + } } + SDL_free(joysticks); } for (auto [controllerIndex, controller] : sdlControllers) { - for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) { - if (SDL_GameControllerGetButton(controller, static_cast(button))) { + for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) { + if (SDL_GetGamepadButton(controller, static_cast(button))) { sdlDeviceIndex = controllerIndex; break; } @@ -39,9 +46,9 @@ int32_t ControllerReorderingWindow::GetSDLIndexFromSDLInput() { break; } - for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) { - const auto axis = static_cast(i); - const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f; + for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) { + const auto axis = static_cast(i); + const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f; if (axisValue < -0.7f || axisValue > 0.7f) { sdlDeviceIndex = controllerIndex; break; @@ -50,7 +57,7 @@ int32_t ControllerReorderingWindow::GetSDLIndexFromSDLInput() { } for (auto [i, controller] : sdlControllers) { - SDL_GameControllerClose(controller); + SDL_CloseGamepad(controller); } return sdlDeviceIndex; @@ -65,11 +72,19 @@ void ControllerReorderingWindow::DrawElement() { // if we don't have more than one controller, just close the window std::vector connectedSdlControllerIndices; - for (auto i = 0; i < SDL_NumJoysticks(); i++) { - if (SDL_IsGameController(i)) { - connectedSdlControllerIndices.push_back(i); + + int i, numJoysticks; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); + if (joysticks) { + for (i = 0; i < numJoysticks; ++i) { + SDL_JoystickID instanceId = joysticks[i]; + if (SDL_IsGamepad(instanceId)) { + connectedSdlControllerIndices.push_back(i); + } } + SDL_free(joysticks); } + if (connectedSdlControllerIndices.size() <= 1) { Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->InitializeMappingsMultiplayer( connectedSdlControllerIndices); diff --git a/src/controller/deviceindex/ShipDeviceIndexMappingManager.cpp b/src/controller/deviceindex/ShipDeviceIndexMappingManager.cpp index 8fbf3183c..d37227f1a 100644 --- a/src/controller/deviceindex/ShipDeviceIndexMappingManager.cpp +++ b/src/controller/deviceindex/ShipDeviceIndexMappingManager.cpp @@ -1,5 +1,5 @@ #include "ShipDeviceIndexMappingManager.h" -#include +#include #include "utils/StringHelper.h" #include "public/bridge/consolevariablebridge.h" #include @@ -41,15 +41,19 @@ void ShipDeviceIndexMappingManager::InitializeMappingsMultiplayer(std::vector matchingGuidLusIndices; @@ -173,10 +177,17 @@ void ShipDeviceIndexMappingManager::InitializeMappingsSinglePlayer() { } std::vector connectedSdlControllerIndices; - for (auto i = 0; i < SDL_NumJoysticks(); i++) { - if (SDL_IsGameController(i)) { - connectedSdlControllerIndices.push_back(i); + + int i, numJoysticks; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); + if (joysticks) { + for (i = 0; i < numJoysticks; ++i) { + SDL_JoystickID instanceId = joysticks[i]; + if (SDL_IsGamepad(instanceId)) { + connectedSdlControllerIndices.push_back(i); + } } + SDL_free(joysticks); } mShipDeviceIndexToPhysicalDeviceIndexMappings.clear(); @@ -288,7 +299,7 @@ void ShipDeviceIndexMappingManager::HandlePhysicalDeviceConnect(int32_t sdlDevic return; } - if (!SDL_IsGameController(sdlDeviceIndex)) { + if (!SDL_IsGamepad(sdlDeviceIndex)) { return; } diff --git a/src/debug/CrashHandler.h b/src/debug/CrashHandler.h index 23f5bed51..8bee6ab4d 100644 --- a/src/debug/CrashHandler.h +++ b/src/debug/CrashHandler.h @@ -10,7 +10,7 @@ #include // for dladdr #include #include -#include +#include #endif #if _WIN32 diff --git a/src/graphic/Fast3D/Fast3dWindow.cpp b/src/graphic/Fast3D/Fast3dWindow.cpp index 44ba7a416..23dc384e7 100644 --- a/src/graphic/Fast3D/Fast3dWindow.cpp +++ b/src/graphic/Fast3D/Fast3dWindow.cpp @@ -24,7 +24,7 @@ Fast3dWindow::Fast3dWindow(std::vector> guiWind #ifdef _WIN32 AddAvailableWindowBackend(Ship::WindowBackend::FAST3D_DXGI_DX11); #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE if (Metal_IsSupported()) { AddAvailableWindowBackend(Ship::WindowBackend::FAST3D_SDL_METAL); } @@ -119,7 +119,7 @@ void Fast3dWindow::InitWindowManager() { mWindowManagerApi = &gfx_sdl; break; #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE case Ship::WindowBackend::FAST3D_SDL_METAL: mRenderingApi = &gfx_metal_api; mWindowManagerApi = &gfx_sdl; diff --git a/src/graphic/Fast3D/gfx_metal.cpp b/src/graphic/Fast3D/gfx_metal.cpp index d8c972a70..eed769fab 100644 --- a/src/graphic/Fast3D/gfx_metal.cpp +++ b/src/graphic/Fast3D/gfx_metal.cpp @@ -5,7 +5,7 @@ // Created by David Chavez on 16.08.22. // -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #include "gfx_metal.h" @@ -27,7 +27,7 @@ #define CA_PRIVATE_IMPLEMENTATION #define MTL_PRIVATE_IMPLEMENTATION #include -#include +#include #include #include #include @@ -214,7 +214,7 @@ bool Metal_Init(SDL_Renderer* renderer) { mctx.renderer = renderer; NS::AutoreleasePool* autorelease_pool = NS::AutoreleasePool::alloc()->init(); - mctx.layer = (CA::MetalLayer*)SDL_RenderGetMetalLayer(renderer); + mctx.layer = (CA::MetalLayer*)SDL_GetRenderMetalLayer(renderer); mctx.layer->setPixelFormat(MTL::PixelFormatBGRA8Unorm); mctx.device = mctx.layer->device(); @@ -236,7 +236,7 @@ static void gfx_metal_setup_screen_framebuffer(uint32_t width, uint32_t height); void Metal_SetupFrame(SDL_Renderer* renderer) { int width, height; - SDL_GetRendererOutputSize(renderer, &width, &height); + SDL_GetCurrentRenderOutputSize(renderer, &width, &height); gfx_metal_setup_screen_framebuffer(width, height); } @@ -812,7 +812,7 @@ static void gfx_metal_update_framebuffer_parameters(int fb_id, uint32_t width, u // see `gfx_metal_setup_screen_framebuffer`. if (fb_id == 0) { int width, height; - SDL_GetRendererOutputSize(mctx.renderer, &width, &height); + SDL_GetCurrentRenderOutputSize(mctx.renderer, &width, &height); mctx.layer->setDrawableSize({ CGFloat(width), CGFloat(height) }); return; diff --git a/src/graphic/Fast3D/gfx_metal.h b/src/graphic/Fast3D/gfx_metal.h index c014b234e..b788b25d7 100644 --- a/src/graphic/Fast3D/gfx_metal.h +++ b/src/graphic/Fast3D/gfx_metal.h @@ -5,13 +5,13 @@ // Created by David Chavez on 16.08.22. // -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #ifndef GFX_METAL_H #define GFX_METAL_H #include "gfx_rendering_api.h" -#include +#include extern struct GfxRenderingAPI gfx_metal_api; diff --git a/src/graphic/Fast3D/gfx_metal_shader.cpp b/src/graphic/Fast3D/gfx_metal_shader.cpp index b9e282c6c..fb097629a 100644 --- a/src/graphic/Fast3D/gfx_metal_shader.cpp +++ b/src/graphic/Fast3D/gfx_metal_shader.cpp @@ -5,7 +5,7 @@ // Created by David Chavez on 16.08.22. // -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #include #include diff --git a/src/graphic/Fast3D/gfx_metal_shader.h b/src/graphic/Fast3D/gfx_metal_shader.h index 6394d58fd..b06c31e78 100644 --- a/src/graphic/Fast3D/gfx_metal_shader.h +++ b/src/graphic/Fast3D/gfx_metal_shader.h @@ -5,7 +5,7 @@ // Created by David Chavez on 16.08.22. // -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #ifndef GFX_METAL_SHADER_H #define GFX_METAL_SHADER_H diff --git a/src/graphic/Fast3D/gfx_opengl.cpp b/src/graphic/Fast3D/gfx_opengl.cpp index a29c6508c..dddda6936 100644 --- a/src/graphic/Fast3D/gfx_opengl.cpp +++ b/src/graphic/Fast3D/gfx_opengl.cpp @@ -19,24 +19,24 @@ #endif #ifdef _MSC_VER -#include +#include // #define GL_GLEXT_PROTOTYPES 1 #include #elif FOR_WINDOWS #include -#include "SDL.h" +#include #define GL_GLEXT_PROTOTYPES 1 -#include "SDL_opengl.h" -#elif __APPLE__ -#include +#include +#elif SDL_PLATFORM_APPLE +#include #include #elif USE_OPENGLES -#include +#include #include #else -#include +#include #define GL_GLEXT_PROTOTYPES 1 -#include +#include #endif #include "gfx_cc.h" @@ -70,7 +70,7 @@ struct Framebuffer { static map, struct ShaderProgram> shader_program_pool; static GLuint opengl_vbo; -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) static GLuint opengl_vao; #endif @@ -279,7 +279,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad size_t num_floats = 4; // Vertex shader -#if defined(__APPLE__) +#if defined(SDL_PLATFORM_APPLE) append_line(vs_buf, &vs_len, "#version 410 core"); append_line(vs_buf, &vs_len, "in vec4 aVtxPos;"); #elif defined(USE_OPENGLES) @@ -291,7 +291,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad #endif for (int i = 0; i < 2; i++) { if (cc_features.used_textures[i]) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) vs_len += sprintf(vs_buf + vs_len, "in vec2 aTexCoord%d;\n", i); vs_len += sprintf(vs_buf + vs_len, "out vec2 vTexCoord%d;\n", i); #else @@ -301,7 +301,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad num_floats += 2; for (int j = 0; j < 2; j++) { if (cc_features.clamp[i][j]) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) vs_len += sprintf(vs_buf + vs_len, "in float aTexClamp%s%d;\n", j == 0 ? "S" : "T", i); vs_len += sprintf(vs_buf + vs_len, "out float vTexClamp%s%d;\n", j == 0 ? "S" : "T", i); #else @@ -314,7 +314,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad } } if (cc_features.opt_fog) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(vs_buf, &vs_len, "in vec4 aFog;"); append_line(vs_buf, &vs_len, "out vec4 vFog;"); #else @@ -325,7 +325,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad } if (cc_features.opt_grayscale) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(vs_buf, &vs_len, "in vec4 aGrayscaleColor;"); append_line(vs_buf, &vs_len, "out vec4 vGrayscaleColor;"); #else @@ -336,7 +336,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad } for (int i = 0; i < cc_features.num_inputs; i++) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) vs_len += sprintf(vs_buf + vs_len, "in vec%d aInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1); vs_len += sprintf(vs_buf + vs_len, "out vec%d vInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1); #else @@ -373,7 +373,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad append_line(vs_buf, &vs_len, "}"); // Fragment shader -#if defined(__APPLE__) +#if defined(SDL_PLATFORM_APPLE) append_line(fs_buf, &fs_len, "#version 410 core"); #elif defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "#version 300 es"); @@ -383,14 +383,14 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad #endif for (int i = 0; i < 2; i++) { if (cc_features.used_textures[i]) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) fs_len += sprintf(fs_buf + fs_len, "in vec2 vTexCoord%d;\n", i); #else fs_len += sprintf(fs_buf + fs_len, "varying vec2 vTexCoord%d;\n", i); #endif for (int j = 0; j < 2; j++) { if (cc_features.clamp[i][j]) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) fs_len += sprintf(fs_buf + fs_len, "in float vTexClamp%s%d;\n", j == 0 ? "S" : "T", i); #else fs_len += sprintf(fs_buf + fs_len, "varying float vTexClamp%s%d;\n", j == 0 ? "S" : "T", i); @@ -400,21 +400,21 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad } } if (cc_features.opt_fog) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "in vec4 vFog;"); #else append_line(fs_buf, &fs_len, "varying vec4 vFog;"); #endif } if (cc_features.opt_grayscale) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "in vec4 vGrayscaleColor;"); #else append_line(fs_buf, &fs_len, "varying vec4 vGrayscaleColor;"); #endif } for (int i = 0; i < cc_features.num_inputs; i++) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) fs_len += sprintf(fs_buf + fs_len, "in vec%d vInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1); #else fs_len += sprintf(fs_buf + fs_len, "varying vec%d vInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1); @@ -448,7 +448,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad append_line(fs_buf, &fs_len, "}"); if (current_filter_mode == FILTER_THREE_POINT) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "#define TEX_OFFSET(off) texture(tex, texCoord - (off)/texSize)"); #else append_line(fs_buf, &fs_len, "#define TEX_OFFSET(off) texture2D(tex, texCoord - (off)/texSize)"); @@ -466,7 +466,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad append_line(fs_buf, &fs_len, "}"); } else { append_line(fs_buf, &fs_len, "vec4 hookTexture2D(in sampler2D tex, in vec2 uv, in vec2 texSize) {"); -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, " return texture(tex, uv);"); #else append_line(fs_buf, &fs_len, " return texture2D(tex, uv);"); @@ -474,7 +474,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad append_line(fs_buf, &fs_len, "}"); } -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "out vec4 outColor;"); #endif @@ -616,13 +616,13 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad if (cc_features.opt_invisible) { append_line(fs_buf, &fs_len, "texel.a = 0.0;"); } -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "outColor = texel;"); #else append_line(fs_buf, &fs_len, "gl_FragColor = texel;"); #endif } else { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "outColor = vec4(texel, 1.0);"); #else append_line(fs_buf, &fs_len, "gl_FragColor = vec4(texel, 1.0);"); @@ -630,7 +630,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad } if (srgb_mode) { -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) append_line(fs_buf, &fs_len, "outColor = fromLinear(outColor);"); #else append_line(fs_buf, &fs_len, "gl_FragColor = fromLinear(gl_FragColor);"); @@ -920,7 +920,7 @@ static void gfx_opengl_init() { glGenBuffers(1, &opengl_vbo); glBindBuffer(GL_ARRAY_BUFFER, opengl_vbo); -#if defined(__APPLE__) || defined(USE_OPENGLES) +#if defined(SDL_PLATFORM_APPLE) || defined(USE_OPENGLES) glGenVertexArrays(1, &opengl_vao); glBindVertexArray(opengl_vao); #endif diff --git a/src/graphic/Fast3D/gfx_pc.cpp b/src/graphic/Fast3D/gfx_pc.cpp index 4b6fcb3e0..9a3c5db00 100644 --- a/src/graphic/Fast3D/gfx_pc.cpp +++ b/src/graphic/Fast3D/gfx_pc.cpp @@ -4078,7 +4078,7 @@ void gfx_init(struct GfxWindowManagerAPI* wapi, struct GfxRenderingAPI* rapi, co gfx_wapi->init(game_name, rapi->get_name(), start_in_fullscreen, width, height, posX, posY); gfx_rapi->init(); gfx_rapi->update_framebuffer_parameters(0, width, height, 1, false, true, true, true); -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE gfx_current_dimensions.internal_mul = 1; #else gfx_current_dimensions.internal_mul = CVarGetFloat(CVAR_INTERNAL_RESOLUTION, 1); diff --git a/src/graphic/Fast3D/gfx_sdl2.cpp b/src/graphic/Fast3D/gfx_sdl2.cpp index a4027d647..bbf682c16 100644 --- a/src/graphic/Fast3D/gfx_sdl2.cpp +++ b/src/graphic/Fast3D/gfx_sdl2.cpp @@ -1,6 +1,6 @@ #include -#if defined(ENABLE_OPENGL) || defined(__APPLE__) +#if defined(ENABLE_OPENGL) || defined(SDL_PLATFORM_APPLE) #ifdef __MINGW32__ #define FOR_WINDOWS 1 @@ -13,16 +13,16 @@ #if FOR_WINDOWS #include -#include "SDL.h" +#include #define GL_GLEXT_PROTOTYPES 1 -#include "SDL_opengl.h" -#elif __APPLE__ -#include +#include +#elif SDL_PLATFORM_APPLE +#include #include "gfx_metal.h" #else -#include +#include #define GL_GLEXT_PROTOTYPES 1 -#include +#include #endif #include "window/gui/Gui.h" @@ -33,7 +33,7 @@ #ifdef _WIN32 #include #include -#include +#include #endif #define GFX_BACKEND_NAME "SDL" @@ -224,7 +224,7 @@ static void set_fullscreen(bool on, bool call_callback) { if (fullscreen_state == on) { return; } - int display_in_use = SDL_GetWindowDisplayIndex(wnd); + int display_in_use = SDL_GetDisplayForWindow(wnd); if (display_in_use < 0) { SPDLOG_WARN("Can't detect on which monitor we are. Probably out of display area?"); SPDLOG_WARN(SDL_GetError()); @@ -232,9 +232,9 @@ static void set_fullscreen(bool on, bool call_callback) { if (on) { // OTRTODO: Get mode from config. - SDL_DisplayMode mode; - if (SDL_GetDesktopDisplayMode(display_in_use, &mode) >= 0) { - SDL_SetWindowDisplayMode(wnd, &mode); + auto mode = SDL_GetDesktopDisplayMode(display_in_use); + if (mode != nullptr) { + SDL_SetWindowFullscreenMode(wnd, mode); } else { SPDLOG_ERROR(SDL_GetError()); } @@ -251,10 +251,19 @@ static void set_fullscreen(bool on, bool call_callback) { SDL_SetWindowPosition(wnd, posX, posY); SDL_SetWindowSize(wnd, window_width, window_height); } - if (SDL_SetWindowFullscreen(wnd, - on ? (CVarGetInteger(CVAR_SDL_WINDOWED_FULLSCREEN, 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP - : SDL_WINDOW_FULLSCREEN) - : 0) >= 0) { + + /* todo: + + SDL_WINDOW_FULLSCREEN_DESKTOP has been removed, and you can call SDL_GetWindowFullscreenMode() + to see whether an exclusive fullscreen mode will be used or the borderless fullscreen desktop mode + will be used when the window is fullscreen. + + */ + + if (SDL_SetWindowFullscreen(wnd, on)) { + // on ? (CVarGetInteger(CVAR_SDL_WINDOWED_FULLSCREEN, 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP + // : SDL_WINDOW_FULLSCREEN) + // : 0) >= 0) { fullscreen_state = on; } else { SPDLOG_ERROR("Failed to switch from or to fullscreen mode."); @@ -267,11 +276,10 @@ static void set_fullscreen(bool on, bool call_callback) { } static void gfx_sdl_get_active_window_refresh_rate(uint32_t* refresh_rate) { - int display_in_use = SDL_GetWindowDisplayIndex(wnd); + int display_in_use = SDL_GetDisplayForWindow(wnd); - SDL_DisplayMode mode; - SDL_GetCurrentDisplayMode(display_in_use, &mode); - *refresh_rate = mode.refresh_rate; + auto mode = SDL_GetCurrentDisplayMode(display_in_use); + *refresh_rate = mode->refresh_rate; } static uint64_t previous_time; @@ -314,16 +322,11 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s window_width = width; window_height = height; -#if SDL_VERSION_ATLEAST(2, 24, 0) - /* fix DPI scaling issues on Windows */ - SDL_SetHint(SDL_HINT_WINDOWS_DPI_AWARENESS, "permonitorv2"); -#endif - SDL_Init(SDL_INIT_VIDEO); - SDL_EventState(SDL_DROPFILE, SDL_ENABLE); + SDL_SetEventEnabled(SDL_EVENT_DROP_FILE, true); -#if defined(__APPLE__) +#if defined(SDL_PLATFORM_APPLE) bool use_opengl = strcmp(gfx_api_name, "OpenGL") == 0; #else bool use_opengl = true; @@ -337,7 +340,7 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal"); } -#if defined(__APPLE__) +#if defined(SDL_PLATFORM_APPLE) SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); @@ -359,7 +362,7 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s #ifdef __IOS__ Uint32 flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_SHOWN; #else - Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; + Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; #endif if (use_opengl) { @@ -368,7 +371,15 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s flags = flags | SDL_WINDOW_METAL; } - wnd = SDL_CreateWindow(title, posX, posY, window_width, window_height, flags); + SDL_PropertiesID props = SDL_CreateProperties(); + SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, posX); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, posY); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, window_width); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, window_height); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, flags); + wnd = SDL_CreateWindowWithProperties(props); + SDL_DestroyProperties(props); #ifdef _WIN32 // Get Windows window handle and use it to subclass the window procedure. // Needed to circumvent SDLs DPI scaling problems under windows (original does only scale *sometimes*). @@ -380,14 +391,14 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s #endif Ship::GuiWindowInitData window_impl; - int display_in_use = SDL_GetWindowDisplayIndex(wnd); + int display_in_use = SDL_GetDisplayForWindow(wnd); if (display_in_use < 0) { // Fallback to default if out of bounds posX = 100; posY = 100; } if (use_opengl) { - SDL_GL_GetDrawableSize(wnd, &window_width, &window_height); + SDL_GetWindowSizeInPixels(wnd, &window_width, &window_height); if (start_in_fullscreen) { set_fullscreen(true, false); @@ -400,17 +411,26 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s window_impl.Opengl = { wnd, ctx }; } else { - uint32_t flags = SDL_RENDERER_ACCELERATED; - if (vsync_enabled) { - flags |= SDL_RENDERER_PRESENTVSYNC; - } - renderer = SDL_CreateRenderer(wnd, -1, flags); + // uint32_t flags = SDL_RENDERER_ACCELERATED; + // if (vsync_enabled) { + // flags |= SDL_RENDERER_PRESENTVSYNC; + // } + /* todo: + + SDL_RENDERER_ACCELERATED - all renderers except SDL_SOFTWARE_RENDERER are accelerated + SDL_RENDERER_PRESENTVSYNC - replaced with SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER + during renderer creation and SDL_PROP_RENDERER_VSYNC_NUMBER + after renderer creation + + */ + + renderer = SDL_CreateRenderer(wnd, nullptr); if (renderer == NULL) { SPDLOG_ERROR("Error creating renderer: {}", SDL_GetError()); return; } - SDL_GetRendererOutputSize(renderer, &window_width, &window_height); + SDL_GetCurrentRenderOutputSize(renderer, &window_width, &window_height); window_impl.Metal = { wnd, renderer }; } @@ -440,9 +460,9 @@ static void gfx_sdl_set_fullscreen(bool enable) { static void gfx_sdl_set_cursor_visibility(bool visible) { if (visible) { - SDL_ShowCursor(SDL_ENABLE); + SDL_ShowCursor(); } else { - SDL_ShowCursor(SDL_DISABLE); + SDL_HideCursor(); } } @@ -451,11 +471,31 @@ static void gfx_sdl_set_mouse_pos(int32_t x, int32_t y) { } static void gfx_sdl_get_mouse_pos(int32_t* x, int32_t* y) { - SDL_GetMouseState(x, y); + /* todo: + + SDL_GetMouseState(), SDL_GetGlobalMouseState(), SDL_GetRelativeMouseState(), + SDL_WarpMouseInWindow(), and SDL_WarpMouseGlobal() all use floating point mouse + positions, to provide sub-pixel precision on platforms that support it. + + */ + static float fx, fy; + SDL_GetMouseState(&fx, &fy); + *x = static_cast(fx); + *y = static_cast(fy); } static void gfx_sdl_get_mouse_delta(int32_t* x, int32_t* y) { - SDL_GetRelativeMouseState(x, y); + /* todo: + + SDL_GetMouseState(), SDL_GetGlobalMouseState(), SDL_GetRelativeMouseState(), + SDL_WarpMouseInWindow(), and SDL_WarpMouseGlobal() all use floating point mouse + positions, to provide sub-pixel precision on platforms that support it. + + */ + static float fx, fy; + SDL_GetRelativeMouseState(&fx, &fy); + *x = static_cast(fx); + *y = static_cast(fy); } static void gfx_sdl_get_mouse_wheel(float* x, float* y) { @@ -470,11 +510,11 @@ static bool gfx_sdl_get_mouse_state(uint32_t btn) { } static void gfx_sdl_set_mouse_capture(bool capture) { - SDL_SetRelativeMouseMode(static_cast(capture)); + SDL_SetWindowRelativeMouseMode(wnd, static_cast(capture)); } static bool gfx_sdl_is_mouse_captured() { - return (SDL_GetRelativeMouseMode() == SDL_TRUE); + return (SDL_GetWindowRelativeMouseMode(wnd) == true); } static void gfx_sdl_set_keyboard_callbacks(bool (*on_key_down)(int scancode), bool (*on_key_up)(int scancode), @@ -490,7 +530,7 @@ static void gfx_sdl_set_mouse_callbacks(bool (*on_btn_down)(int btn), bool (*on_ } static void gfx_sdl_get_dimensions(uint32_t* width, uint32_t* height, int32_t* posX, int32_t* posY) { - SDL_GL_GetDrawableSize(wnd, static_cast((void*)width), static_cast((void*)height)); + SDL_GetWindowSizeInPixels(wnd, static_cast((void*)width), static_cast((void*)height)); SDL_GetWindowPosition(wnd, static_cast(posX), static_cast(posY)); } @@ -547,43 +587,39 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) { switch (event.type) { #ifndef TARGET_WEB // Scancodes are broken in Emscripten SDL2: https://bugzilla.libsdl.org/show_bug.cgi?id=3259 - case SDL_KEYDOWN: - gfx_sdl_onkeydown(event.key.keysym.scancode); + case SDL_EVENT_KEY_DOWN: + gfx_sdl_onkeydown(event.key.scancode); break; - case SDL_KEYUP: - gfx_sdl_onkeyup(event.key.keysym.scancode); + case SDL_EVENT_KEY_UP: + gfx_sdl_onkeyup(event.key.scancode); break; - case SDL_MOUSEBUTTONDOWN: + case SDL_EVENT_MOUSE_BUTTON_DOWN: gfx_sdl_on_mouse_button_down(event.button.button - 1); break; - case SDL_MOUSEBUTTONUP: + case SDL_EVENT_MOUSE_BUTTON_UP: gfx_sdl_on_mouse_button_up(event.button.button - 1); break; - case SDL_MOUSEWHEEL: + case SDL_EVENT_MOUSE_WHEEL: mouse_wheel_x = event.wheel.x; mouse_wheel_y = event.wheel.y; break; #endif - case SDL_WINDOWEVENT: - switch (event.window.event) { - case SDL_WINDOWEVENT_SIZE_CHANGED: - SDL_GL_GetDrawableSize(wnd, &window_width, &window_height); - break; - case SDL_WINDOWEVENT_CLOSE: - if (event.window.windowID == SDL_GetWindowID(wnd)) { - // We listen specifically for main window close because closing main window - // on macOS does not trigger SDL_Quit. - gfx_sdl_close(); - } - break; + case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: + SDL_GetWindowSizeInPixels(wnd, &window_width, &window_height); + break; + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + if (event.window.windowID == SDL_GetWindowID(wnd)) { + // We listen specifically for main window close because closing main window + // on macOS does not trigger SDL_Quit. + gfx_sdl_close(); } break; - case SDL_DROPFILE: - Ship::Context::GetInstance()->GetConsoleVariables()->SetString(CVAR_DROPPED_FILE, event.drop.file); + case SDL_EVENT_DROP_FILE: + Ship::Context::GetInstance()->GetConsoleVariables()->SetString(CVAR_DROPPED_FILE, event.drop.data); Ship::Context::GetInstance()->GetConsoleVariables()->SetInteger(CVAR_NEW_FILE_DROPPED, 1); Ship::Context::GetInstance()->GetConsoleVariables()->Save(); break; - case SDL_QUIT: + case SDL_EVENT_QUIT: gfx_sdl_close(); break; } @@ -592,10 +628,10 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) { static void gfx_sdl_handle_events() { SDL_Event event; SDL_PumpEvents(); - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_CONTROLLERDEVICEADDED - 1) > 0) { + while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_GAMEPAD_ADDED - 1) > 0) { gfx_sdl_handle_single_event(event); } - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEREMOVED + 1, SDL_LASTEVENT) > 0) { + while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_GAMEPAD_REMOVED + 1, SDL_EVENT_LAST) > 0) { gfx_sdl_handle_single_event(event); } } diff --git a/src/port/mobile/MobileImpl.cpp b/src/port/mobile/MobileImpl.cpp index 572168e0b..8a54362d0 100644 --- a/src/port/mobile/MobileImpl.cpp +++ b/src/port/mobile/MobileImpl.cpp @@ -1,6 +1,6 @@ #if defined(__ANDROID__) || defined(__IOS__) #include "MobileImpl.h" -#include +#include #include "public/bridge/consolevariablebridge.h" #include diff --git a/src/public/libultra/os.cpp b/src/public/libultra/os.cpp index 7ba54ddf2..bf10b1ffc 100644 --- a/src/public/libultra/os.cpp +++ b/src/public/libultra/os.cpp @@ -1,5 +1,5 @@ #include "libultraship/libultraship.h" -#include +#include #include // Establish a chrono duration for the N64 46.875MHz clock rate @@ -14,13 +14,13 @@ int32_t osContInit(OSMesgQueue* mq, uint8_t* controllerBits, OSContStatus* statu *controllerBits = 0; status->status |= 1; - if (SDL_Init(SDL_INIT_GAMECONTROLLER) != 0) { + if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) { SPDLOG_ERROR("Failed to initialize SDL game controllers ({})", SDL_GetError()); exit(EXIT_FAILURE); } std::string controllerDb = Ship::Context::LocateFileAcrossAppDirs("gamecontrollerdb.txt"); - int mappingsAdded = SDL_GameControllerAddMappingsFromFile(controllerDb.c_str()); + int mappingsAdded = SDL_AddGamepadMappingsFromFile(controllerDb.c_str()); if (mappingsAdded >= 0) { SPDLOG_INFO("Added SDL game controllers from \"{}\" ({})", controllerDb, mappingsAdded); } else { diff --git a/src/public/libultra/os_vi.cpp b/src/public/libultra/os_vi.cpp index 882ae0d90..947743c07 100644 --- a/src/public/libultra/os_vi.cpp +++ b/src/public/libultra/os_vi.cpp @@ -13,7 +13,15 @@ Uint32 __lusViCallback(Uint32 interval, void* param) { } void osCreateViManager(OSPri pri) { - SDL_AddTimer(16, &__lusViCallback, NULL); + /* todo: + + The callback passed to SDL_AddTimer() has changed parameters to: + + Uint32 SDLCALL TimerCallback(void *userdata, SDL_TimerID timerID, Uint32 interval); + + */ + + // SDL_AddTimer(16, &__lusViCallback, NULL); } void osViSetEvent(OSMesgQueue* queue, OSMesg mesg, uint32_t c) { diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 0fafc9027..e310f2ac7 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -6,7 +6,7 @@ #include "controller/controldevice/controller/mapping/keyboard/KeyboardScancodes.h" #include "Context.h" -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE #include "utils/AppleFolderManager.h" #endif diff --git a/src/window/gui/ConsoleWindow.cpp b/src/window/gui/ConsoleWindow.cpp index f76055083..b96a0f9c4 100644 --- a/src/window/gui/ConsoleWindow.cpp +++ b/src/window/gui/ConsoleWindow.cpp @@ -387,12 +387,12 @@ void ConsoleWindow::DrawElement() { ImGui::PushStyleColor(ImGuiCol_FrameBgActive, ImVec4(.3f, .3f, .3f, 1.0f)); if (ImGui::BeginTable("History", 1)) { - if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_DownArrow))) { + if (ImGui::IsKeyPressed(ImGuiKey_DownArrow)) { if (mSelectedId < (int32_t)mLog.size() - 1) { ++mSelectedId; } } - if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_UpArrow))) { + if (ImGui::IsKeyPressed(ImGuiKey_UpArrow)) { if (mSelectedId > 0) { --mSelectedId; } @@ -415,7 +415,7 @@ void ConsoleWindow::DrawElement() { std::find(mSelectedEntries.begin(), mSelectedEntries.end(), i) != mSelectedEntries.end(); ImGui::PushStyleColor(ImGuiCol_Text, mPriorityColours[line.Priority]); if (ImGui::Selectable(id.c_str(), isSelected)) { - if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_LeftCtrl)) && !isSelected) { + if (ImGui::IsKeyDown(ImGuiKey_LeftCtrl) && !isSelected) { mSelectedEntries.push_back(i); } else { diff --git a/src/window/gui/Gui.cpp b/src/window/gui/Gui.cpp index 281dadad6..db048097b 100644 --- a/src/window/gui/Gui.cpp +++ b/src/window/gui/Gui.cpp @@ -20,16 +20,16 @@ #include "window/gui/GfxDebuggerWindow.h" -#ifdef __APPLE__ -#include -#include +#ifdef SDL_PLATFORM_APPLE +#include +#include #include "graphic/Fast3D/gfx_metal.h" #include -#include +#include #else -#include -#include +#include +#include #endif #if defined(__ANDROID__) || defined(__IOS__) @@ -38,7 +38,7 @@ #ifdef ENABLE_OPENGL #include -#include +#include #endif @@ -164,9 +164,9 @@ void Gui::ImGuiWMInit() { case WindowBackend::FAST3D_SDL_OPENGL: SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); - ImGui_ImplSDL2_InitForOpenGL(static_cast(mImpl.Opengl.Window), mImpl.Opengl.Context); + ImGui_ImplSDL3_InitForOpenGL(static_cast(mImpl.Opengl.Window), mImpl.Opengl.Context); break; -#if __APPLE__ +#if SDL_PLATFORM_APPLE case WindowBackend::FAST3D_SDL_METAL: SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); @@ -187,7 +187,7 @@ void Gui::ImGuiBackendInit() { switch (Context::GetInstance()->GetWindow()->GetWindowBackend()) { #ifdef ENABLE_OPENGL case WindowBackend::FAST3D_SDL_OPENGL: -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE ImGui_ImplOpenGL3_Init("#version 410 core"); #elif USE_OPENGLES ImGui_ImplOpenGL3_Init("#version 300 es"); @@ -197,7 +197,7 @@ void Gui::ImGuiBackendInit() { break; #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE case WindowBackend::FAST3D_SDL_METAL: Metal_Init(mImpl.Metal.Renderer); break; @@ -261,7 +261,7 @@ void Gui::HandleWindowEvents(WindowEvent event) { switch (Context::GetInstance()->GetWindow()->GetWindowBackend()) { case WindowBackend::FAST3D_SDL_OPENGL: case WindowBackend::FAST3D_SDL_METAL: - ImGui_ImplSDL2_ProcessEvent(static_cast(event.Sdl.Event)); + ImGui_ImplSDL3_ProcessEvent(static_cast(event.Sdl.Event)); #if defined(__ANDROID__) || defined(__IOS__) Mobile::ImGuiProcessEvent(mImGuiIo->WantTextInput); #endif @@ -318,7 +318,7 @@ void Gui::ImGuiBackendNewFrame() { break; #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE case WindowBackend::FAST3D_SDL_METAL: Metal_NewFrame(mImpl.Metal.Renderer); break; @@ -332,7 +332,7 @@ void Gui::ImGuiWMNewFrame() { switch (Context::GetInstance()->GetWindow()->GetWindowBackend()) { case WindowBackend::FAST3D_SDL_OPENGL: case WindowBackend::FAST3D_SDL_METAL: - ImGui_ImplSDL2_NewFrame(); + ImGui_ImplSDL3_NewFrame(); break; #ifdef ENABLE_DX11 case WindowBackend::FAST3D_DXGI_DX11: @@ -504,7 +504,7 @@ void Gui::DrawMenu() { } } -#if __APPLE__ +#if SDL_PLATFORM_APPLE if ((ImGui::IsKeyDown(ImGuiKey_LeftSuper) || ImGui::IsKeyDown(ImGuiKey_RightSuper)) && ImGui::IsKeyPressed(ImGuiKey_R, false)) { std::reinterpret_pointer_cast( @@ -654,7 +654,7 @@ void Gui::DrawGame() { } if (gfxFramebuffer) { ImGui::SetCursorPos(pos); - ImGui::Image(reinterpret_cast(gfxFramebuffer), size); + ImGui::Image((ImTextureID)gfxFramebuffer, size); } ImGui::End(); @@ -675,7 +675,7 @@ void Gui::DrawFloatingWindows() { // Set back the GL context for next frame SDL_GL_MakeCurrent(backupCurrentWindow, backupCurrentContext); } else { -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE // Metal requires additional frame setup to get ImGui ready for drawing floating windows if (backend == WindowBackend::FAST3D_SDL_METAL) { Metal_SetupFloatingFrame(); @@ -712,7 +712,7 @@ void Gui::Draw() { void Gui::SetupRendererFrame() { switch (Context::GetInstance()->GetWindow()->GetWindowBackend()) { -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE case WindowBackend::FAST3D_SDL_METAL: Metal_SetupFrame(mImpl.Metal.Renderer); break; @@ -728,13 +728,13 @@ ImTextureID Gui::GetTextureById(int32_t id) { return gfx_d3d11_get_texture_by_id(id); } #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE if (Context::GetInstance()->GetWindow()->GetWindowBackend() == WindowBackend::FAST3D_SDL_METAL) { return gfx_metal_get_texture_by_id(id); } #endif - return reinterpret_cast(id); + return (ImTextureID)id; } bool Gui::HasTextureByName(const std::string& name) { @@ -743,7 +743,7 @@ bool Gui::HasTextureByName(const std::string& name) { ImTextureID Gui::GetTextureByName(const std::string& name) { if (!Gui::HasTextureByName(name)) { - return nullptr; + return (ImTextureID)nullptr; } return GetTextureById(mGuiTextures[name].RendererTextureId); } @@ -764,7 +764,7 @@ void Gui::ImGuiRenderDrawData(ImDrawData* data) { break; #endif -#ifdef __APPLE__ +#ifdef SDL_PLATFORM_APPLE case WindowBackend::FAST3D_SDL_METAL: Metal_RenderDrawData(data); break; diff --git a/src/window/gui/Gui.h b/src/window/gui/Gui.h index 10d09c865..2dfa8e22f 100644 --- a/src/window/gui/Gui.h +++ b/src/window/gui/Gui.h @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include "window/gui/ConsoleWindow.h" #include "window/gui/InputEditorWindow.h" #include "controller/deviceindex/ControllerDisconnectedWindow.h" diff --git a/src/window/gui/StatsWindow.cpp b/src/window/gui/StatsWindow.cpp index 0723ce224..bff527939 100644 --- a/src/window/gui/StatsWindow.cpp +++ b/src/window/gui/StatsWindow.cpp @@ -23,7 +23,7 @@ void StatsWindow::DrawElement() { ImGui::Text("Platform: Windows"); #elif defined(__IOS__) ImGui::Text("Platform: iOS"); -#elif defined(__APPLE__) +#elif defined(SDL_PLATFORM_APPLE) ImGui::Text("Platform: macOS"); #elif defined(__linux__) ImGui::Text("Platform: Linux");