add xinput

This commit is contained in:
Zack Buhman 2026-01-13 19:21:17 -06:00
parent efa08063c0
commit cdf49949c3
4 changed files with 115 additions and 6 deletions

View File

@ -24,10 +24,11 @@ CFLAGS += -Wno-unknown-pragmas
CFLAGS += -I./include
CFLAGS += -municode
LDFLAGS += -municode
LIBS += -ld3d10
CXXFLAGS += -fno-exceptions
LIBS = -ld3d10
all: $(BUILD_TYPE)/d3d10.exe
$(BUILD_TYPE)/%.fxo: src/%.fx
@ -60,6 +61,7 @@ OBJS = \
$(BUILD_TYPE)/main.obj \
$(BUILD_TYPE)/print.obj \
$(BUILD_TYPE)/render_state.obj \
$(BUILD_TYPE)/input.obj \
$(BUILD_TYPE)/main.res
$(BUILD_TYPE)/d3d10.exe: $(OBJS)

20
include/input.hpp Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#ifndef _INPUT_HPP_
#define _INPUT_HPP_
HRESULT InitInput(HINSTANCE hInstance);
void UpdateInput();
struct Joystate {
int buttons;
float triggerL;
float triggerR;
float thumbLX;
float thumbLY;
float thumbRX;
float thumbRY;
};
extern Joystate g_Joystate;
#endif

68
src/input.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <windows.h>
#include <assert.h>
#include <XInput.h>
#include "print.hpp"
#include "input.hpp"
Joystate g_Joystate;
typedef DWORD (WINAPI * lpXInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES *);
typedef DWORD (WINAPI * lpXInputGetState)(DWORD, XINPUT_STATE *);
//lpXInputGetCapabilities g_XInputGetCapabilities;
lpXInputGetState g_XInputGetState;
HRESULT InitInput(HINSTANCE hInstance)
{
const char * libs[] = {
"xinput1_4.dll",
"xinput1_3.dll",
"xinput9_1_0.dll",
"xinput1_2.dll",
"xinput1_1.dll",
};
const int libs_length = (sizeof (libs)) / (sizeof (libs[0]));
HMODULE hLib;
for (int i = 0; i < libs_length; i++) {
hLib = LoadLibraryA(libs[i]);
if (hLib != NULL) {
print("using xinput: %s\n", libs[i]);
break;
}
}
if (hLib == NULL) {
print("no xinput module found\n");
return E_FAIL;
}
//g_XInputGetCapabilities = (lpXInputGetCapabilities)GetProcAddress(hLib, "XInputGetCapabilities");
//assert(g_XInputGetCapabilities != NULL);
g_XInputGetState = (lpXInputGetState)GetProcAddress(hLib, "XInputGetState");
assert(g_XInputGetState != NULL);
return S_OK;
}
void UpdateInput()
{
XINPUT_STATE state;
for (DWORD i = 0; i < 4; i++) {
DWORD ret = g_XInputGetState(i, &state);
if (ret != ERROR_SUCCESS) {
continue;
}
break;
}
const float trigger = 1.0f / 255.0f;
const float thumb = 1.0f / 32768.0f;
g_Joystate.buttons = state.Gamepad.wButtons;
g_Joystate.triggerL = ((float)state.Gamepad.bLeftTrigger) * trigger;
g_Joystate.triggerR = ((float)state.Gamepad.bRightTrigger) * trigger;
g_Joystate.thumbLX = ((float)state.Gamepad.sThumbLX) * thumb;
g_Joystate.thumbLY = ((float)state.Gamepad.sThumbLY) * thumb;
g_Joystate.thumbRX = ((float)state.Gamepad.sThumbRX) * thumb;
g_Joystate.thumbRY = ((float)state.Gamepad.sThumbRY) * thumb;
}

View File

@ -8,6 +8,7 @@
#include "globals.hpp"
#include "print.hpp"
#include "render_state.hpp"
#include "input.hpp"
#include "gltf.hpp"
#include "gltf_instance.hpp"
@ -168,6 +169,11 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
InitializeNodeInstances();
if (FAILED(InitInput(hInstance))) {
print("InitInput\n");
return 0;
}
MSG msg = {};
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
@ -189,6 +195,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
UpdateInput();
if (Resize())
Render();
}
@ -1439,10 +1446,22 @@ void RenderFont()
print("g_pVertexBuffersFont->Map");
}
int length = sprint("bloomPasses: %d\n"
" exposure: %f",
g_bloomPasses,
g_exposure);
sprint("bloomPasses: %d\n"
" exposure: %f\n\n"
"triggerL: % 5.3f\n"
"triggerR: % 5.3f\n"
" thumbLX: % 5.3f\n"
" thumbLY: % 5.3f\n"
" thumbRX: % 5.3f\n"
" thumbRY: % 5.3f",
g_bloomPasses,
g_exposure,
g_Joystate.triggerL,
g_Joystate.triggerR,
g_Joystate.thumbLX,
g_Joystate.thumbLY,
g_Joystate.thumbRX,
g_Joystate.thumbRY);
const char start_advance = 10;
int hadvance = start_advance;
@ -1509,7 +1528,7 @@ void RenderFont()
for (UINT p = 0; p < techDesc.Passes; p++) {
g_pTechniqueFont->GetPassByIndex(p)->Apply(0);
g_pd3dDevice->Draw(length, 0);
g_pd3dDevice->Draw(ix, 0);
}
}