代码:
#include<Windows.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<Xinput.h>
#include<time.h>
#include<iostream>
using namespace std;
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"xinput.lib")
const string APPTITLE = "XInput Test Program";
const int SCREENW = ;
const int SCREENH = ;
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
bool gameover = false;
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) bool XInput_Init(int contNum = )
{
XINPUT_CAPABILITIES caps;
ZeroMemory(&caps, sizeof(XINPUT_CAPABILITIES));
XInputGetCapabilities(contNum, XINPUT_FLAG_GAMEPAD, &caps);
if (caps.Type != XINPUT_DEVTYPE_GAMEPAD)
return FALSE;
return TRUE;
} void XInput_Vibrate(int contNum = , int left = , int right = )
{
XINPUT_VIBRATION vibration;
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
vibration.wLeftMotorSpeed = left;
vibration.wRightMotorSpeed = right;
XInputSetState(contNum, &vibration);
} void XInput_Update()
{
XINPUT_STATE state;
string message = "";
for (int i = ; i < ; i++)
{
ZeroMemory(&state, sizeof(XINPUT_STATE));
message = "";
DWORD result = XInputGetState(, &state);
if (result == )
{
if (state.Gamepad.bLeftTrigger)
message = "Left Trigger";
else if (state.Gamepad.bRightTrigger)
message = "Right Trigger";
else if (state.Gamepad.sThumbLX < - || state.Gamepad.sThumbLX>)
message = "Left Thumb Stick";
else if (state.Gamepad.sThumbRX < - || state.Gamepad.sThumbRX>)
message = "Right Thumb Stick";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_UP)
message = "DPAD Up";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_DOWN)
message = "DPAD Down";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_LEFT)
message = "DPAD Left";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_RIGHT)
message = "DPAD Right";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_START)
message = "Start Button";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_LEFT_THUMB)
message = "Left Thumb";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_RIGHT_THUMB)
message = "Right Thumb";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_LEFT_SHOULDER)
message = "Left Shoulder";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_RIGHT_SHOULDER)
message = "Right Shoulder";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_A)
{
XInput_Vibrate(, , );
message = "A Button";
}
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_B)
{
XInput_Vibrate(, , );
message = "B Button";
}
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_X)
message = "X Button";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_Y)
message = "X Button";
else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_BACK)
gameover = true;
if (message.length() > )
MessageBox(, message.c_str(), "Controler", );
}
else { }
}
} bool Game_Init(HWND hwnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)
{
MessageBox(hwnd, "Error initializing Direct3D", "ERROR", MB_OK);
return FALSE;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = ;
d3dpp.BackBufferWidth = SCREENW;
d3dpp.BackBufferHeight = SCREENH;
d3dpp.hDeviceWindow = hwnd;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
if (d3ddev == NULL)
{
MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
return FALSE;
}
if (!d3ddev)
{
MessageBox(hwnd, "Error creating Direct3D device", "ERROR", MB_OK);
return FALSE;
}
XInput_Init();
return TRUE;
} void Game_Run(HWND hwnd)
{
if (!d3ddev)
return;
d3ddev->Clear(, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(, , ), 1.0f, );
if (d3ddev->BeginScene())
{
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
if (KEY_DOWN(VK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, , );
XInput_Update();
} void Game_End(HWND hwnd)
{
if (d3ddev) d3ddev->Release();
if (d3d) d3d->Release();
} LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
gameover = true;
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
} int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.style = ;
wc.cbClsExtra = ;
wc.cbWndExtra = ;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.lpszMenuName = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = "MainWindowClass";
if (!RegisterClassEx(&wc))
return FALSE;
HWND hwnd = CreateWindow("MainWindowClass", APPTITLE.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREENW, SCREENH, (HWND)NULL, (HMENU)NULL,
hInstance, (LPVOID)NULL);
if (hwnd == )
return ;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
if (!Game_Init(hwnd))
return ;
while (!gameover)
{
if (PeekMessage(&msg, NULL, , , PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Game_Run(hwnd);
}
Game_End(hwnd);
return msg.wParam;
}
结果(运行后按下手柄的键位会在红框框中出现相应的提示,博主穷所以就没买手柄,哈哈):