idzio/shifter.c: Factor out shifter sim logic

This commit is contained in:
Tau 2019-05-04 18:55:32 -04:00
parent 04189be8f0
commit 5a4ea8193d
4 changed files with 51 additions and 19 deletions

View File

@ -12,6 +12,8 @@ idzio_dll = shared_library(
'backend.h',
'dllmain.c',
'idzio.h',
'shifter.c',
'shifter.h',
'xi.c',
'xi.h',
],

32
idzio/shifter.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdbool.h>
#include <stdint.h>
#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;
}

8
idzio/shifter.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
void idz_shifter_reset(void);
void idz_shifter_update(bool shift_dn, bool shift_up);
uint8_t idz_shifter_current_gear(void);

View File

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