首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++ DirectInput示例从joypad检索即时数据。

c++ DirectInput示例从joypad检索即时数据。
EN

Stack Overflow用户
提问于 2013-10-05 13:33:44
回答 1查看 611关注 0票数 0

我试图通过客户端服务器应用程序发送一个joypad的代码。使用joypad API成功地显示了joypad数据之后,我无法确定哪些变量存储了来自joypad的数据,以便在缓冲区中传递数据并发送它们。任何帮助都将不胜感激。

显示joypad数据的代码块:

代码语言:javascript
复制
//--------------------------------------------------------------------------------------------------
// Name: UpdateInputState()
// Desc: Get the input device's state and display it.
//--------------------------------------------------------------------------------------------------
HRESULT UpdateInputState(HWND hDlg)
{
    HRESULT hr;
    TCHAR strText[512] = {0}; // Device state text.
    DIJOYSTATE2 js; // DirectInput joypad state. 

    if(NULL == g_pJoypad)
        return S_OK;

    // Poll the device to read the current state.
    hr = g_pJoypad->Poll();
    if(FAILED(hr))
    {
        // DirectInput is telling us that the input stream has been interrupted. We aren't tracking
        // any state between polls, so we don't have any special reset that needs to be done. We just
        // re-acquire and try again.
        hr = g_pJoypad->Acquire();
        while(hr == DIERR_INPUTLOST)
            hr = g_pJoypad->Acquire();

        // hr may be DIERR_OTHERAPPHASPRIO or other errors. This may occur when the application is
        // minimized or in the process of switching, so just try again later.
        return S_OK;
    }

    // Get the input's device state.
    // GetDeviceState() retrieves immediate data from the device. 
    if(FAILED(hr = g_pJoypad->GetDeviceState(sizeof(DIJOYSTATE2), &js)))
        return hr; // The device should have been acquired during the Poll().

    // Display joypad state to dialog.

    // Axes.
    _stprintf_s(strText, 512, TEXT("%ld"), js.lX);
    SetWindowText(GetDlgItem(hDlg, IDC_X_AXIS_L), strText);
    _stprintf_s(strText, 512, TEXT("%ld"), js.lY);
    SetWindowText(GetDlgItem(hDlg, IDC_Y_AXIS_L), strText);
    _stprintf_s(strText, 512, TEXT("%ld"), js.lZ);
    SetWindowText(GetDlgItem(hDlg, IDC_X_AXIS_R), strText);
    _stprintf_s(strText, 512, TEXT("%ld"), js.lRx);
    _stprintf_s(strText, 512, TEXT("%ld"), js.lRz);
    SetWindowText(GetDlgItem(hDlg, IDC_Y_AXIS_R), strText);

    // POV.
    _stprintf_s(strText, 512, TEXT("%lu"), js.rgdwPOV[0]);
    SetWindowText(GetDlgItem(hDlg, IDC_POV), strText);

    // Buttons.
    // Fill up text with which buttons are pressed.
    _tcscpy_s(strText, 512, TEXT(""));
    for(int i = 0; i < 128; i++)
    {
        if(js.rgbButtons[i] & 0x80)
        {
            TCHAR sz[128];
            _stprintf_s(sz, 128, TEXT("%02d"), i);
            _tcscat_s(strText, 512, sz);
        }
    }
    SetWindowText(GetDlgItem(hDlg, IDC_BUTTONS), strText);

    return S_OK;
}

将数据发送到服务器的代码块:

代码语言:javascript
复制
 // Send a message.
    if (send(ConnectSocket, sendbuf, sizeof(sendbuf), 0) == SOCKET_ERROR)
    {
        MessageBox(NULL, TEXT("The joypad data could not be sent."), TEXT("Client says"), 
                           MB_ICONERROR | MB_OK);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-05 21:34:30

比我想象的简单。数据存储在strText (轴和轴)和sz (按钮)中。但是,这两个缓冲区都已声明为TCHAR,为了将其作为参数传递给send() (而不是sendbuf),需要对CHAR进行转换。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19198394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档