【问题标题】:How to use the namespace Windows.Gaming.Input in C++ without using UWP?如何在不使用 UWP 的情况下在 C++ 中使用命名空间 Windows.Gaming.Input?
【发布时间】:2017-05-08 06:39:33
【问题描述】:

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

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

【问题讨论】:

  • 除非您非常需要为此使用 UWP API,否则我建议您直接转至 XInput

标签: c++ windows uwp xbox gamepad


【解决方案1】:

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

#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!)
  • 非常感谢您发布此示例。我将uint32_t 更改为unsigned int 以匹配这些方法的实际原型,并且我使用RuntimeClass_Windows_Gaming_Input_Gamepad 而不是字符串文字。
猜你喜欢
  • 1970-01-01
  • 2018-09-13
  • 2011-08-12
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多