c++ - 如何在不使用 UWP 的情况下在 C++ 中使用命名空间 Windows.Gaming.Input?

标签 c++ windows uwp xbox gamepad

我有一个大型 C++ 游戏项目,我想在其中添加对 Xbox One Controller 的支持。我尝试使用 Windows.Gaming.Input namespace .但是,这似乎仅适用于 UWP 项目。这是真的吗?

如果是这种情况,将现有的 SDL 引擎移植到 UWP 是否容易?

最佳答案

您可以从桌面应用程序很好地调用 Windows.Gaming.Input - 不确定您从哪里得知它仅适用于 UWP 应用程序。只需包含标题并使用它。以下是在所有游戏 handle 上打印按钮状态的示例代码:

#include <assert.h>
#include <cstdint>
#include <iostream>
#include <roapi.h>
#include <wrl.h>
#include "windows.gaming.input.h"

using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::Gaming::Input;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

#pragma comment(lib, "runtimeobject.lib")

int main()
{
    auto hr = RoInitialize(RO_INIT_MULTITHREADED);
    assert(SUCCEEDED(hr));

    ComPtr<IGamepadStatics> gamepadStatics;
    hr = RoGetActivationFactory(HStringReference(L"Windows.Gaming.Input.Gamepad").Get(), __uuidof(IGamepadStatics), &gamepadStatics);
    assert(SUCCEEDED(hr));

    ComPtr<IVectorView<Gamepad*>> gamepads;
    hr = gamepadStatics->get_Gamepads(&gamepads);
    assert(SUCCEEDED(hr));

    uint32_t gamepadCount;
    hr = gamepads->get_Size(&gamepadCount);
    assert(SUCCEEDED(hr));

    for (uint32_t i = 0; i < gamepadCount; i++)
    {
        ComPtr<IGamepad> gamepad;
        hr = gamepads->GetAt(i, &gamepad);
        assert(SUCCEEDED(hr));

        GamepadReading gamepadReading;
        hr = gamepad->GetCurrentReading(&gamepadReading);
        assert(SUCCEEDED(hr));

        std::cout << "Gamepad " << i + 1 << " buttons value is: " << gamepadReading.Buttons << std::endl;
    }

    return 0;
}

关于c++ - 如何在不使用 UWP 的情况下在 C++ 中使用命名空间 Windows.Gaming.Input?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41286503/

相关文章:

c++ - 将屏幕绘图翻译到屏幕的另一部分

windows - 用于 Windows 的 ANSI C 中的简单删除文件程序

在uwp中编译c文件

c# - 如何在 Windows 10 Universal App 中使用 WCF 服务?

c# - 两个用户控件还是两种样式?

c++ - C++ 中的 Bron Kerbosch 算法

c++ - 无法正确实现upper_bound()

c++ - 错误消息 "iostream: no such file or directory"

"empty"main 中的 c++ 段错误(在非空项目中)

python - 使用 Python3.X 在 Windows 10 上控制屏幕亮度