From fb0d5435a624142c7e4573ad1c8450fecea43431 Mon Sep 17 00:00:00 2001 From: Darien Johnson <84008186+CADIndie@users.noreply.github.com> Date: Thu, 16 Jan 2025 01:18:15 -0500 Subject: [PATCH] Add Local Multiplayer --- .../Windows.Xbox.Input.Gamepad.cpp | 69 ++++++++++++------- .../Windows.Xbox.Input.Gamepad.h | 3 + .../Windows.Xbox.System.User.cpp | 21 +++--- .../Implementation/Windows.Xbox.System.User.h | 2 + .../Windows.Xbox.System.UserDisplayInfo.cpp | 8 ++- .../Windows.Xbox.System.UserDisplayInfo.h | 2 + .../Windows.Xbox.UI.AccountPickerResult.cpp | 3 +- .../Windows.Xbox.UI.AccountPickerResult.h | 1 + 8 files changed, 72 insertions(+), 37 deletions(-) diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.cpp b/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.cpp index 0a63994..3913210 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.cpp +++ b/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.cpp @@ -11,30 +11,50 @@ namespace winrt::Windows::Xbox::Input::implementation { winrt::Windows::Foundation::Collections::IVectorView Gamepad::Gamepads() { - IGamepad dummyGamepad = winrt::make( ); - auto vector = winrt::single_threaded_vector( ); - vector.Append(dummyGamepad); - return vector.GetView( ); + wprintf(L"Gamepad || Gamepads Queried!\n"); + + if (staticGamepads == Foundation::Collections::IVector(nullptr) || staticGamepads.Size( ) == 0) { + staticGamepads = winrt::single_threaded_vector( ); + + for (DWORD gamepad = 0; gamepad < XUSER_MAX_COUNT; gamepad++) + { + XINPUT_CAPABILITIES capabilities; + if (XInputGetCapabilities(gamepad, XINPUT_FLAG_GAMEPAD, &capabilities) == ERROR_SUCCESS) + { + wprintf(L"Gamepad || Gamepad %d Created!\n", gamepad); + IGamepad newGamepad = winrt::make(gamepad); + staticGamepads.Append(newGamepad); + continue; + } + } + } + + return staticGamepads.GetView( ); } winrt::event_token Gamepad::GamepadAdded(winrt::Windows::Foundation::EventHandler const& handler) { + wprintf(L"Gamepad || Gamepad Added!\n"); return {}; } void Gamepad::GamepadAdded(winrt::event_token const& token) noexcept { + wprintf(L"Gamepad || Gamepad Added!\n"); throw hresult_not_implemented(); } winrt::event_token Gamepad::GamepadRemoved(winrt::Windows::Foundation::EventHandler const& handler) { + wprintf(L"Gamepad || Gamepad Removed!\n"); return {}; } void Gamepad::GamepadRemoved(winrt::event_token const& token) noexcept { + wprintf(L"Gamepad || Gamepad Removed!\n"); throw hresult_not_implemented(); } uint64_t Gamepad::Id() { - return 1; + wprintf(L"Gamepad || Gamepad ID ( %d ) Queried!\n", m_id); + return m_id; } hstring Gamepad::Type() { @@ -42,7 +62,8 @@ namespace winrt::Windows::Xbox::Input::implementation } winrt::Windows::Xbox::System::User Gamepad::User() { - return System::implementation::User::Users( ).GetAt(0); + wprintf(L"Gamepad || User Queried!\n"); + return System::implementation::User::Users( ).GetAt(Id()); } winrt::Windows::Xbox::Input::INavigationReading Gamepad::GetNavigationReading() { @@ -69,7 +90,7 @@ namespace winrt::Windows::Xbox::Input::implementation ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION)); vibration.wLeftMotorSpeed = value.LeftMotorLevel * 65535; vibration.wRightMotorSpeed = value.RightMotorLevel * 65535; - XInputSetState(0, &vibration); + XInputSetState(m_id, &vibration); } winrt::Windows::Xbox::Input::IGamepadReading Gamepad::GetCurrentReading() { @@ -99,28 +120,26 @@ namespace winrt::Windows::Xbox::Input::implementation { XINPUT_GAMEPAD_Y, GamepadButtons::Y }, }; - for (DWORD user = 0; user < XUSER_MAX_COUNT; user++) { - if (XInputGetState(user, &xiState) == ERROR_SUCCESS) + if (XInputGetState(m_id, &xiState) == ERROR_SUCCESS) + { + for (int i = 0; i < ARRAYSIZE(buttons); i++) { - for (int i = 0; i < ARRAYSIZE(buttons); i++) + if (xiState.Gamepad.wButtons & buttons[ i ].first) { - if (xiState.Gamepad.wButtons & buttons[ i ].first) - { - reading.Buttons |= buttons[ i ].second; - } + reading.Buttons |= buttons[ i ].second; } - - reading.LeftTrigger = xiState.Gamepad.bLeftTrigger / 255.f; - reading.RightTrigger = xiState.Gamepad.bRightTrigger / 255.f; - reading.LeftThumbstickX = xiState.Gamepad.sThumbLX / 32768.f; - reading.LeftThumbstickY = xiState.Gamepad.sThumbLY / 32768.f; - reading.RightThumbstickX = xiState.Gamepad.sThumbRX / 32768.f; - reading.RightThumbstickY = xiState.Gamepad.sThumbRY / 32768.f; } - //else { - // printf("Controller input failure: %x\n", XInputGetState(0, &xiState)); - //} - } + + reading.LeftTrigger = xiState.Gamepad.bLeftTrigger / 255.f; + reading.RightTrigger = xiState.Gamepad.bRightTrigger / 255.f; + reading.LeftThumbstickX = xiState.Gamepad.sThumbLX / 32768.f; + reading.LeftThumbstickY = xiState.Gamepad.sThumbLY / 32768.f; + reading.RightThumbstickX = xiState.Gamepad.sThumbRX / 32768.f; + reading.RightThumbstickY = xiState.Gamepad.sThumbRY / 32768.f; + } + //else { + // printf("Gamepad input failure: %x\n", XInputGetState(0, &xiState)); + //} if (GetAsyncKeyState('A')) reading.Buttons |= GamepadButtons::A; diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.h b/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.h index 76e2985..fe0cb86 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.h +++ b/dlls/winrt_x/Implementation/Windows.Xbox.Input.Gamepad.h @@ -6,6 +6,7 @@ namespace winrt::Windows::Xbox::Input::implementation struct Gamepad : GamepadT { Gamepad() = default; + Gamepad(uint64_t id) : m_id(id) {} static winrt::Windows::Foundation::Collections::IVectorView Gamepads(); static winrt::event_token GamepadAdded(winrt::Windows::Foundation::EventHandler const& handler); @@ -25,6 +26,8 @@ namespace winrt::Windows::Xbox::Input::implementation winrt::event_token ReadingChanged(winrt::Windows::Foundation::TypedEventHandler const& handler); void ReadingChanged(winrt::event_token const& token) noexcept; bool IsTrusted(); + inline static winrt::Windows::Foundation::Collections::IVector staticGamepads = { nullptr }; + uint64_t m_id{ 0 }; }; } namespace winrt::Windows::Xbox::Input::factory_implementation diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.System.User.cpp b/dlls/winrt_x/Implementation/Windows.Xbox.System.User.cpp index 30aed23..00d0bb8 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.System.User.cpp +++ b/dlls/winrt_x/Implementation/Windows.Xbox.System.User.cpp @@ -20,13 +20,17 @@ namespace winrt::Windows::Xbox::System::implementation } winrt::Windows::Foundation::Collections::IVectorView User::Users() { - if (staticUser == System::User(nullptr)) - staticUser = winrt::make( ); - - if (staticUsers == Foundation::Collections::IVector(nullptr) || staticUsers.Size() == 0) - { + wprintf(L"User || Users Queried!\n"); + if (staticUsers == Foundation::Collections::IVector(nullptr) || staticUsers.Size( ) == 0) { staticUsers = winrt::single_threaded_vector( ); - staticUsers.Append(staticUser); + + for (int i = 0; i < 4; i++) + { + wprintf(L"User || User %d Created!\n", i); + staticUser = winrt::make(i); + staticUsers.Append(staticUser); + continue; + } } return staticUsers.GetView(); @@ -137,7 +141,7 @@ namespace winrt::Windows::Xbox::System::implementation } uint32_t User::Id() { - return 1; + return m_id; } winrt::Windows::Foundation::Collections::IVectorView User::AudioDevices() { @@ -151,7 +155,8 @@ namespace winrt::Windows::Xbox::System::implementation } winrt::Windows::Xbox::System::UserDisplayInfo User::DisplayInfo() { - return winrt::make( ); + hstring gamertag = to_hstring(m_id); + return winrt::make(gamertag); } bool User::IsGuest() { diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.System.User.h b/dlls/winrt_x/Implementation/Windows.Xbox.System.User.h index 5c20523..99d5a70 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.System.User.h +++ b/dlls/winrt_x/Implementation/Windows.Xbox.System.User.h @@ -25,6 +25,7 @@ namespace winrt::Windows::Xbox::System::implementation struct User : UserT { User() = default; + User(uint64_t id) : m_id(id) {} static winrt::Windows::Xbox::System::UserOnlineState OnlineState(); static winrt::event_token OnlineStateChanged(winrt::Windows::Foundation::EventHandler const& handler); @@ -66,6 +67,7 @@ namespace winrt::Windows::Xbox::System::implementation winrt::Windows::Foundation::IAsyncOperation GetTokenAndSignatureAsync(hstring httpMethod, hstring url, hstring headers); winrt::Windows::Foundation::IAsyncOperation GetTokenAndSignatureAsync(hstring httpMethod, hstring url, hstring headers, array_view body); winrt::Windows::Foundation::IAsyncOperation GetTokenAndSignatureAsync(hstring httpMethod, hstring url, hstring headers, hstring body); + uint64_t m_id {0}; private: inline static winrt::event> m_userAddedEvent; inline static winrt::event> m_userRemovedEvent; diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.cpp b/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.cpp index 61b1622..ffe0ddf 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.cpp +++ b/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.cpp @@ -26,7 +26,8 @@ namespace winrt::Windows::Xbox::System::implementation hstring UserDisplayInfo::Gamertag() { printf("!!!!! Windows.Xbox.System.UserDisplayInfo [Gamertag] NOT IMPLEMENTED !!!!\n"); - return winrt::to_hstring("durangler"); + hstring gamertag = L"durangler" + m_gamertag; + return gamertag; } uint32_t UserDisplayInfo::GamerScore() { @@ -41,7 +42,8 @@ namespace winrt::Windows::Xbox::System::implementation hstring UserDisplayInfo::GameDisplayName() { printf("!!!!! Windows.Xbox.System.UserDisplayInfo [GameDisplayName] NOT IMPLEMENTED !!!!\n"); - return winrt::to_hstring("durangler"); + hstring gamertag = L"durangler" + m_gamertag; + return gamertag; } int32_t UserDisplayInfo::Reputation() { @@ -54,7 +56,7 @@ namespace winrt::Windows::Xbox::System::implementation return UserAgeGroup::Unknown; } winrt::Windows::Foundation::Collections::IVectorView UserDisplayInfo::Privileges() - { + { printf("!!!!! Windows.Xbox.System.UserDisplayInfo [Privileges] NOT IMPLEMENTED !!!!\n"); auto vector = winrt::single_threaded_vector(); diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.h b/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.h index 5e78ced..8e8d95f 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.h +++ b/dlls/winrt_x/Implementation/Windows.Xbox.System.UserDisplayInfo.h @@ -25,6 +25,7 @@ namespace winrt::Windows::Xbox::System::implementation struct UserDisplayInfo : UserDisplayInfoT { UserDisplayInfo() = default; + UserDisplayInfo(hstring gamertag) : m_gamertag(gamertag) {} hstring Gamertag(); uint32_t GamerScore(); @@ -33,5 +34,6 @@ namespace winrt::Windows::Xbox::System::implementation int32_t Reputation(); winrt::Windows::Xbox::System::UserAgeGroup AgeGroup(); winrt::Windows::Foundation::Collections::IVectorView Privileges(); + hstring m_gamertag{ 0 }; }; } diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.cpp b/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.cpp index 0e6d675..be8b589 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.cpp +++ b/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.cpp @@ -25,6 +25,7 @@ namespace winrt::Windows::Xbox::UI::implementation { winrt::Windows::Xbox::System::IUser AccountPickerResult::User() { - return System::User::Users( ).GetAt(0); + uint64_t count = signedInUsersCount + 1; + return System::User::Users( ).GetAt(count); } } diff --git a/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.h b/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.h index 5cd8db8..dd50fdb 100644 --- a/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.h +++ b/dlls/winrt_x/Implementation/Windows.Xbox.UI.AccountPickerResult.h @@ -26,6 +26,7 @@ namespace winrt::Windows::Xbox::UI::implementation { AccountPickerResult() = default; + uint64_t signedInUsersCount = 1; winrt::Windows::Xbox::System::IUser User(); }; }