add small directinput demo

This commit is contained in:
Zack Buhman 2025-12-21 21:10:31 -06:00
parent be143930c8
commit 55062cba98
2 changed files with 100 additions and 1 deletions

View File

@ -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

View File

@ -3,6 +3,7 @@
#include <d3dx9.h>
#include <stdint.h>
#include <stdio.h>
#include <dinput.h>
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 = {};