0

How to convert the following code to C++ WinRT ?

Platform::Array<bool>^ currentButtonReading =
ref new Platform::Array<bool>(buttonCount);

Platform::Array<GameControllerSwitchPosition>^ currentSwitchReading =
ref new Platform::Array<GameControllerSwitchPosition>(switchCount);

Platform::Array<double>^ currentAxisReading = ref new Platform::Array<double>(axisCount);

rawGameController->GetCurrentReading(
    currentButtonReading,
    currentSwitchReading,
    currentAxisReading);

It is from the article:

https://learn.microsoft.com/en-us/windows/uwp/gaming/raw-game-controller

Please advise.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
Ricky Gai
  • 31
  • 8
  • This is not C++ code. – SergeyA Jun 11 '19 at 14:41
  • [C++/WinRT accepts](https://learn.microsoft.com/en-us/uwp/cpp-ref-for-winrt/array-view#array_viewarray_view-constructor) a `std::array`, a `std::vector` a classic conformant array, or a pair of pointers [begin, end). – Raymond Chen Jun 11 '19 at 17:51
  • Raymond, thx for the hint, I came across that page u shared but cannot figure up how to convert it to C++ WinRT format. If u know how, can share how do you do it based on the code above ? – Ricky Gai Jun 12 '19 at 00:03
  • SergeyA, not sure about ur comment, but if u know how to convert the code above to C++ WinRT, do share it here. TQ :) – Ricky Gai Jun 12 '19 at 00:05
  • Are you just trying to use the game controller API (in which case, the generated headers have the correct signatures, based on `array_view`) or is this just an example, and you're actually trying to use something else? – Peter Torr - MSFT Jun 12 '19 at 01:03
  • Peter, yes I am using RawGameController to retrieve gamepad states. The code sample above seems to be C++ /CX and I am developing for C++ WinRT UWP which I cannot compile the sample code above. – Ricky Gai Jun 12 '19 at 01:49
  • I means, from the sample above, the array cannot accept non-static N, where N is suppose to be buttonCount, or I purposely set size larger than buttonCount to declare the array will do in this case ? – Ricky Gai Jun 12 '19 at 03:45
  • If your array is not a fixed size, then don't use the fixed-size array version. Use the `std::vector` version or (harder) the two-pointer version. – Raymond Chen Jun 12 '19 at 14:48

1 Answers1

0

Here is the answer after some study.

The C++ WinRT converted source:

struct _GAMEPAD
{     
    std::vector<RawGameController> db;
    RawGameController              controller{ nullptr };

    std::array<bool, 16>           abtn{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
    array_view<bool>               avbtn{ abtn };

    std::array<double, 6>          aaxi{ 0,0,0,0,0,0 };
    array_view<double>             avaxi{ aaxi };

    array_view<GameControllerSwitchPosition> avswp{};
};      

struct MainPage : MainPageT<MainPage>
{
...
    _GAMEPAD m_gamepad;
...
};

int MainPage::OnProc ()
{
...
    for (auto e : RawGameController::RawGameControllers() ) 
    {
        auto it = std::find( m_gamepad.db.begin(), m_gamepad.db.end(), e );

        if (it == m_gamepad.db.end())
        {
            m_gamepad.db.insert( m_gamepad.db.begin()+m_io.gamepad, e );
            m_io.gamepad++;
        } 
    }
    // assumed there is one gamepad.
    n = m_io.gamepad - 1;
    m_gamepad.controller = m_gamepad.db.at(n);

    m_gamepad.controller.GetCurrentReading( m_gamepad.avbtn,
                                            m_gamepad.avswp,
                                            m_gamepad.avaxi);
...
}

Hopes, this help.

Ricky Gai
  • 31
  • 8
  • 1
    You don't need to create `array_view` objects manually. Just let the projection do it. `m_gamepad.controller.GetCurrentReading(m_gamepad.abtn, {}, m_gamepad.aaxi)`. – Raymond Chen Jun 12 '19 at 14:47