From 55062cba987367d6bed7b8a2ff6de94813de8560 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sun, 21 Dec 2025 21:10:31 -0600 Subject: [PATCH] add small directinput demo --- Makefile | 2 +- main.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3668573..94d8d87 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ CFLAGS += -Wno-error=unused-function CFLAGS += -Wno-error=unused-variable CFLAGS += -DUNICODE -LDFLAGS += -Wl,--subsystem,windows -mwindows -mconsole -municode -ld3d9 -ld3dx9 -lwinmm +LDFLAGS += -Wl,--subsystem,windows -mwindows -mconsole -municode -ld3d9 -ld3dx9 -lwinmm -ldinput8 -ldxguid OPT = -Og diff --git a/main.cpp b/main.cpp index f49a830..fe4ef7e 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include struct CUSTOMVERTEX { @@ -25,6 +26,9 @@ IDirect3DVertexDeclaration9 * g_pVertexDeclaration = NULL; ID3DXConstantTable * g_pConstantTable = NULL; IDirect3DVertexShader9 * g_pVertexShader = NULL; +IDirectInput8 * g_pDI = NULL; +IDirectInputDevice8 * g_pdiDevice; + HRESULT InitDirect3D(HWND hwnd) { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); @@ -52,6 +56,50 @@ HRESULT InitDirect3D(HWND hwnd) return S_OK; } +BOOL CALLBACK EnumDevicesCallback(const DIDEVICEINSTANCE * pdidInstance, + void * pvRef) +{ + HRESULT hr; + + hr = g_pDI->CreateDevice(pdidInstance->guidInstance, &g_pdiDevice, NULL ); + if( FAILED(hr) ) + return DIENUM_CONTINUE; + + return DIENUM_STOP; +} + +HRESULT InitDirectInput(HINSTANCE hInstance) +{ + HRESULT hr; + + hr = DirectInput8Create(hInstance, + DIRECTINPUT_VERSION, + IID_IDirectInput8, + (void **)&g_pDI, + NULL); + if (FAILED(hr)) + return hr; + + hr = g_pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, + EnumDevicesCallback, + NULL, + DIEDFL_ATTACHEDONLY); + if (FAILED(hr)) + return hr; + + if (g_pdiDevice == NULL) { + fprintf(stderr, "no game controller found\n"); + fflush(stderr); + return E_FAIL; + } + + hr = g_pdiDevice->SetDataFormat(&c_dfDIJoystick); + if (FAILED(hr)) + return hr; + + return S_OK; +} + void Cleanup() { if (g_pd3dDevice != NULL) @@ -103,8 +151,59 @@ void Render() g_pd3dDevice->Present(NULL, NULL, NULL, NULL); } +HRESULT UpdateInput() +{ + HRESULT hr; + + hr = g_pdiDevice->Poll(); + if (FAILED(hr)) { + hr = g_pdiDevice->Acquire(); + while (hr == DIERR_INPUTLOST) + hr = g_pdiDevice->Acquire(); + return S_OK; + } + + DIJOYSTATE js; + + g_pdiDevice->GetDeviceState((sizeof (DIJOYSTATE)), &js); + + printf("[lx %ld] ", js.lX); // left stick + printf("[ly %ld] ", js.lY); + printf("[lz %ld] ", js.lZ); // trigger + printf("[lrx %ld] ", js.lRx); // right stick + printf("[lry %ld] ", js.lRy); + printf("[lrz %ld] ", js.lRz); // zero + + printf("[pov %ld] ", js.rgdwPOV[0]); + static int max = -1; + for (int i = 0; i < 8; i++) { + if (js.rgbButtons[i] != 0 && i > max) { + max = i; + } + } + printf("[max %d] ", max); + printf("\n"); + fflush(stdout); + + return S_OK; +} + int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { + HRESULT hr; + hr = InitDirectInput(hInstance); + if (FAILED(hr)) + return 0; + + if (0) { + while (true) { + hr = UpdateInput(); + if (FAILED(hr)) + return 0; + Sleep(100); + } + } + const wchar_t CLASS_NAME[] = L"Sample Window Class"; WNDCLASS wc = {};