游戏编程入门之测试Xbox360控制输入
代码:
1 #include<Windows.h> 2 #include<d3d9.h> 3 #include<d3dx9.h> 4 #include<Xinput.h> 5 #include<time.h> 6 #include<iostream> 7 using namespace std; 8 #pragma comment(lib,"d3d9.lib") 9 #pragma comment(lib,"d3dx9.lib") 10 #pragma comment(lib,"xinput.lib") 11 const string APPTITLE = "XInput Test Program"; 12 const int SCREENW = 1024; 13 const int SCREENH = 768; 14 LPDIRECT3D9 d3d = NULL; 15 LPDIRECT3DDEVICE9 d3ddev = NULL; 16 bool gameover = false; 17 #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) 18 19 bool XInput_Init(int contNum = 0) 20 { 21 XINPUT_CAPABILITIES caps; 22 ZeroMemory(&caps, sizeof(XINPUT_CAPABILITIES)); 23 XInputGetCapabilities(contNum, XINPUT_FLAG_GAMEPAD, &caps); 24 if (caps.Type != XINPUT_DEVTYPE_GAMEPAD) 25 return FALSE; 26 return TRUE; 27 } 28 29 void XInput_Vibrate(int contNum = 0, int left = 65535, int right = 65535) 30 { 31 XINPUT_VIBRATION vibration; 32 ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION)); 33 vibration.wLeftMotorSpeed = left; 34 vibration.wRightMotorSpeed = right; 35 XInputSetState(contNum, &vibration); 36 } 37 38 void XInput_Update() 39 { 40 XINPUT_STATE state; 41 string message = ""; 42 for (int i = 0; i < 4; i++) 43 { 44 ZeroMemory(&state, sizeof(XINPUT_STATE)); 45 message = ""; 46 DWORD result = XInputGetState(1, &state); 47 if (result == 0) 48 { 49 if (state.Gamepad.bLeftTrigger) 50 message = "Left Trigger"; 51 else if (state.Gamepad.bRightTrigger) 52 message = "Right Trigger"; 53 else if (state.Gamepad.sThumbLX < -10000 || state.Gamepad.sThumbLX>10000) 54 message = "Left Thumb Stick"; 55 else if (state.Gamepad.sThumbRX < -10000 || state.Gamepad.sThumbRX>10000) 56 message = "Right Thumb Stick"; 57 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_UP) 58 message = "DPAD Up"; 59 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_DOWN) 60 message = "DPAD Down"; 61 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_LEFT) 62 message = "DPAD Left"; 63 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_DPAD_RIGHT) 64 message = "DPAD Right"; 65 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_START) 66 message = "Start Button"; 67 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_LEFT_THUMB) 68 message = "Left Thumb"; 69 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_RIGHT_THUMB) 70 message = "Right Thumb"; 71 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_LEFT_SHOULDER) 72 message = "Left Shoulder"; 73 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_RIGHT_SHOULDER) 74 message = "Right Shoulder"; 75 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_A) 76 { 77 XInput_Vibrate(0, 65535, 65535); 78 message = "A Button"; 79 } 80 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_B) 81 { 82 XInput_Vibrate(0, 0, 0); 83 message = "B Button"; 84 } 85 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_X) 86 message = "X Button"; 87 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_Y) 88 message = "X Button"; 89 else if (state.Gamepad.wButtons&XINPUT_GAMEPAD_BACK) 90 gameover = true; 91 if (message.length() > 0) 92 MessageBox(0, message.c_str(), "Controler", 0); 93 } 94 else { 95 96 } 97 } 98 } 99 100 bool Game_Init(HWND hwnd) 101 { 102 d3d = Direct3DCreate9(D3D_SDK_VERSION); 103 if (d3d == NULL) 104 { 105 MessageBox(hwnd, "Error initializing Direct3D", "ERROR", MB_OK); 106 return FALSE; 107 } 108 D3DPRESENT_PARAMETERS d3dpp; 109 ZeroMemory(&d3dpp, sizeof(d3dpp)); 110 d3dpp.Windowed = TRUE; 111 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 112 d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 113 d3dpp.BackBufferCount = 1; 114 d3dpp.BackBufferWidth = SCREENW; 115 d3dpp.BackBufferHeight = SCREENH; 116 d3dpp.hDeviceWindow = hwnd; 117 d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); 118 if (d3ddev == NULL) 119 { 120 MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK); 121 return FALSE; 122 } 123 if (!d3ddev) 124 { 125 MessageBox(hwnd, "Error creating Direct3D device", "ERROR", MB_OK); 126 return FALSE; 127 } 128 XInput_Init(); 129 return TRUE; 130 } 131 132 void Game_Run(HWND hwnd) 133 { 134 if (!d3ddev) 135 return; 136 d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 150), 1.0f, 0); 137 if (d3ddev->BeginScene()) 138 { 139 d3ddev->EndScene(); 140 d3ddev->Present(NULL, NULL, NULL, NULL); 141 } 142 if (KEY_DOWN(VK_ESCAPE)) 143 PostMessage(hwnd, WM_DESTROY, 0, 0); 144 XInput_Update(); 145 } 146 147 void Game_End(HWND hwnd) 148 { 149 if (d3ddev) d3ddev->Release(); 150 if (d3d) d3d->Release(); 151 } 152 153 LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 154 { 155 switch (msg) 156 { 157 case WM_DESTROY: 158 gameover = true; 159 break; 160 } 161 return DefWindowProc(hWnd, msg, wParam, lParam); 162 } 163 164 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 165 { 166 WNDCLASSEX wc; 167 MSG msg; 168 wc.cbSize = sizeof(WNDCLASSEX); 169 wc.lpfnWndProc = (WNDPROC)WinProc; 170 wc.style = 0; 171 wc.cbClsExtra = 0; 172 wc.cbWndExtra = 0; 173 wc.hIcon = NULL; 174 wc.hIconSm = NULL; 175 wc.hInstance = hInstance; 176 wc.lpszMenuName = NULL; 177 wc.hCursor = LoadCursor(NULL, IDC_ARROW); 178 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 179 wc.lpszClassName = "MainWindowClass"; 180 if (!RegisterClassEx(&wc)) 181 return FALSE; 182 HWND hwnd = CreateWindow("MainWindowClass", APPTITLE.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREENW, SCREENH, (HWND)NULL, (HMENU)NULL, 183 hInstance, (LPVOID)NULL); 184 if (hwnd == 0) 185 return 0; 186 ShowWindow(hwnd, nCmdShow); 187 UpdateWindow(hwnd); 188 if (!Game_Init(hwnd)) 189 return 0; 190 while (!gameover) 191 { 192 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 193 { 194 TranslateMessage(&msg); 195 DispatchMessage(&msg); 196 } 197 Game_Run(hwnd); 198 } 199 Game_End(hwnd); 200 return msg.wParam; 201 }
结果(运行后按下手柄的键位会在红框框中出现相应的提示,博主穷所以就没买手柄,哈哈):