From 5a4ea8193de16e942fdcbf00b59880f849fbcb20 Mon Sep 17 00:00:00 2001 From: Tau Date: Sat, 4 May 2019 18:55:32 -0400 Subject: [PATCH] idzio/shifter.c: Factor out shifter sim logic --- idzio/meson.build | 2 ++ idzio/shifter.c | 32 ++++++++++++++++++++++++++++++++ idzio/shifter.h | 8 ++++++++ idzio/xi.c | 28 +++++++++------------------- 4 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 idzio/shifter.c create mode 100644 idzio/shifter.h diff --git a/idzio/meson.build b/idzio/meson.build index 80bf6c7..9f181fb 100644 --- a/idzio/meson.build +++ b/idzio/meson.build @@ -12,6 +12,8 @@ idzio_dll = shared_library( 'backend.h', 'dllmain.c', 'idzio.h', + 'shifter.c', + 'shifter.h', 'xi.c', 'xi.h', ], diff --git a/idzio/shifter.c b/idzio/shifter.c new file mode 100644 index 0000000..6782409 --- /dev/null +++ b/idzio/shifter.c @@ -0,0 +1,32 @@ +#include +#include + +#include "idzio/shifter.h" + +static bool idz_shifter_shifting; +static uint8_t idz_shifter_gear; + +void idz_shifter_reset(void) +{ + idz_shifter_gear = 0; +} + +void idz_shifter_update(bool shift_dn, bool shift_up) +{ + if (!idz_shifter_shifting) { + if (shift_dn && idz_shifter_gear > 0) { + idz_shifter_gear--; + } + + if (shift_up && idz_shifter_gear < 6) { + idz_shifter_gear++; + } + } + + idz_shifter_shifting = shift_dn || shift_up; +} + +uint8_t idz_shifter_current_gear(void) +{ + return idz_shifter_gear; +} diff --git a/idzio/shifter.h b/idzio/shifter.h new file mode 100644 index 0000000..20fcab4 --- /dev/null +++ b/idzio/shifter.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include + +void idz_shifter_reset(void); +void idz_shifter_update(bool shift_dn, bool shift_up); +uint8_t idz_shifter_current_gear(void); diff --git a/idzio/xi.c b/idzio/xi.c index 0ef8d68..811a90f 100644 --- a/idzio/xi.c +++ b/idzio/xi.c @@ -7,6 +7,7 @@ #include "idzio/backend.h" #include "idzio/idzio.h" +#include "idzio/shifter.h" #include "idzio/xi.h" #include "util/dprintf.h" @@ -21,9 +22,6 @@ static const struct idz_io_backend idz_xi_backend = { .jvs_read_analogs = idz_xi_jvs_read_analogs, }; -static bool idz_xi_shifting; -static uint8_t idz_xi_gear; - HRESULT idz_xi_init(const struct idz_io_backend **backend) { assert(backend != NULL); @@ -77,8 +75,8 @@ static void idz_xi_jvs_read_buttons(uint8_t *gamebtn_out) static void idz_xi_jvs_read_shifter(uint8_t *gear) { - bool shift_inc; - bool shift_dec; + bool shift_dn; + bool shift_up; XINPUT_STATE xi; WORD xb; @@ -89,24 +87,16 @@ static void idz_xi_jvs_read_shifter(uint8_t *gear) xb = xi.Gamepad.wButtons; if (xb & XINPUT_GAMEPAD_START) { - idz_xi_gear = 0; /* Reset to Neutral when start is pressed */ + /* Reset to Neutral when start is pressed */ + idz_shifter_reset(); } - shift_inc = xb & (XINPUT_GAMEPAD_X | XINPUT_GAMEPAD_RIGHT_SHOULDER); - shift_dec = xb & (XINPUT_GAMEPAD_Y | XINPUT_GAMEPAD_LEFT_SHOULDER); + shift_dn = xb & (XINPUT_GAMEPAD_Y | XINPUT_GAMEPAD_LEFT_SHOULDER); + shift_up = xb & (XINPUT_GAMEPAD_X | XINPUT_GAMEPAD_RIGHT_SHOULDER); - if (!idz_xi_shifting) { - if (shift_inc && idz_xi_gear < 6) { - idz_xi_gear++; - } + idz_shifter_update(shift_dn, shift_up); - if (shift_dec && idz_xi_gear > 0) { - idz_xi_gear--; - } - } - - idz_xi_shifting = shift_inc || shift_dec; - *gear = idz_xi_gear; + *gear = idz_shifter_current_gear(); } static void idz_xi_jvs_read_analogs(struct idz_io_analog_state *out)