简体   繁体   中英

C++ XInput Compilation using g++

#include <Windows.h>
#include <XInput.h>

#include <iostream>

using namespace std;

struct Controller{
    XINPUT_STATE state;
};

class Joypad
{
public:
    int getJoystickPort()
    {
        DWORD dwResult;

        for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
        {
            XINPUT_STATE state;
            ZeroMemory(&state, sizeof(XINPUT_STATE));   

            // Simply get the state of the controller from XInput.
            dwResult = XInputGetState(i, &state);

            if (dwResult == ERROR_SUCCESS)
            {
                return((int) i);
                cout << "Joystick Port: " << i << " is connnected." << endl;
                cout << "Button " << (((int)state.Gamepad.wButtons == XINPUT_GAMEPAD_A)) << " pressed." << endl;
                cout << "LX:  " << state.Gamepad.sThumbLX << " LY: " << state.Gamepad.sThumbLY << endl;
                cout << "RX:  " << state.Gamepad.sThumbRX << " RY: " << state.Gamepad.sThumbRY << endl;
            }
            else
            {
                cout << "Joystick at Port: " << i << " is disconnected." << endl;
            }
        }
        return -1;
    }
};



void joystickStates(){
    Joypad* joypad = new Joypad;
    while (true){
        system("cls");      
        joypad->getJoystickPort();
        Sleep(1000);
    }
}

int main(){
    joystickStates();
    return 0;
}

im getting the __in & __out 's was not declared in this scope errors.

I used the syntax below g++ Joypad.cpp -IC:\\DirectSDK\\Include -LC:\\DirectSDK\\Lib\\x86 -lXinput -o Joypad

and I also tried g++ Joypad.cpp -IC:\\Windows SDK~um,shared, etc -LC:\\Windows SDK\\Lib -lXInput -o Joypad

Is there something I missed? I use mingw x86_64

Directories included: Windows Kits 8.1 (um,shared,winrt) Microsoft DirectX SDK

Libraries included: XInput.lib - C:\\Progra~2\\Micros~4\\Lib\\x86

The __in and __out are Microsoft-specific SAL annotations. They are understood by the MS Visual Studio C++ compiler but not by the GCC compiler.

A way to "delete" them would be to make yourself a header file, say, no_sal.h and every time your GCC compilation barfs on a SAL annotation __foo , add:

#define __foo

to no_sal.h . Then make sure that no_sal.h is included first in every compilation by passing g++ the option -include /path/to/no_sal.h .

However, I would not expect this to be the end of your problems in attempting to compile such very "deeply Microsoft" code as the DirectX SDK with GCC. If you don't want to use Visual Studio then consider switching your Windows GCC distribution from MinGW (I guess) to TDM GCC 64-bit , which bundles DirectX headers and libraries for GCC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM