Created
January 7, 2012 00:49
-
-
Save t-mat/1573282 to your computer and use it in GitHub Desktop.
XInput test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <windows.h> | |
| #include <xinput.h> | |
| #include <stdio.h> | |
| // Compatibility for old XInput.h | |
| #ifndef XUSER_MAX_COUNT | |
| #define XUSER_MAX_COUNT 4 | |
| #endif | |
| #pragma comment(lib, "xinput.lib") | |
| class XiJoyStick { | |
| enum { | |
| ENTRY_MAX = XUSER_MAX_COUNT | |
| }; | |
| public: | |
| struct Entry { | |
| bool connected; | |
| XINPUT_STATE xiState; | |
| }; | |
| XiJoyStick() { | |
| memset(&entry[0], 0, sizeof(entry)); | |
| } | |
| ~XiJoyStick() { | |
| clear(); | |
| } | |
| void clear() { | |
| } | |
| void update() { | |
| for(int i = 0; i < ENTRY_MAX; ++i) { | |
| Entry* e = &entry[i]; | |
| memset(&e->xiState, 0, sizeof(e->xiState)); | |
| DWORD r = XInputGetState((DWORD)i, &e->xiState); | |
| if(r == ERROR_SUCCESS) { | |
| e->connected = true; | |
| } else { | |
| e->connected = false; | |
| if(r == ERROR_DEVICE_NOT_CONNECTED) { | |
| // not connected | |
| } else { | |
| // r : windows error (winerror.h) code | |
| } | |
| } | |
| } | |
| } | |
| int getEntryCount() const { | |
| return ENTRY_MAX; | |
| } | |
| const Entry* getEntry(int index) const { | |
| const Entry* e = 0; | |
| if(index >= 0 && index < ENTRY_MAX) { | |
| e = &entry[index]; | |
| } | |
| return e; | |
| } | |
| protected: | |
| Entry entry[ENTRY_MAX]; | |
| }; | |
| int main(int, const char*) { | |
| XiJoyStick xjs; | |
| for(;;) { | |
| xjs.update(); | |
| const XiJoyStick::Entry* e = xjs.getEntry(0); | |
| if(e && e->connected) { | |
| const XINPUT_GAMEPAD* xg = &e->xiState.Gamepad; | |
| printf("wButtons=%04x, lt=%02x, rt=%02x, l(%+6d,%+6d), r(%+6d,%+6d)\r" | |
| , xg->wButtons, xg->bLeftTrigger, xg->bRightTrigger | |
| , xg->sThumbLX, xg->sThumbLY, xg->sThumbRX, xg->sThumbRY | |
| ); | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment