Commit b23aa5f7 authored by Tatsuya79's avatar Tatsuya79
Browse files

Add analog support.

parent 0d9a1d1b
......@@ -31,6 +31,7 @@ void Input::init(int pad_id, int* key_config, int* pad_config, int analog, int*
this->wheel_zone = analog_settings[0];
this->wheel_dead = analog_settings[1];
this->pedals_dead = analog_settings[2];
this->gamepad = true;
a_wheel = CENTRE;
}
......@@ -127,127 +128,48 @@ void Input::handle_key(const int key, const bool is_pressed)
#endif
}
#if 0
void Input::handle_joy_axis(SDL_JoyAxisEvent* evt)
{
int16_t value = evt->value;
// Digital Controls
if (!analog)
{
// X-Axis
if (evt->axis == 0)
{
// Neural
if ( (value > -DIGITAL_DEAD ) && (value < DIGITAL_DEAD ) )
{
keys[LEFT] = false;
keys[RIGHT] = false;
}
else if (value < 0)
{
keys[LEFT] = true;
}
else if (value > 0)
{
keys[RIGHT] = true;
}
}
// Y-Axis
else if (evt->axis == 1)
{
// Neural
if ( (value > -DIGITAL_DEAD ) && (value < DIGITAL_DEAD ) )
{
keys[UP] = false;
keys[DOWN] = false;
}
else if (value < 0)
{
keys[UP] = true;
}
else if (value > 0)
{
keys[DOWN] = true;
}
}
}
// Analog Controls
else
{
//std::cout << "Axis: " << (int) evt->axis << " Value: " << (int) evt->value << std::endl;
// Steering
// OutRun requires values between 0x48 and 0xb8.
if (evt->axis == axis[0])
{
int percentage_adjust = ((wheel_zone) << 8) / 100;
int adjusted = value + ((value * percentage_adjust) >> 8);
// Make 0 hard left, and 0x80 centre value.
adjusted = ((adjusted + (1 << 15)) >> 9);
adjusted += 0x40; // Centre
if (adjusted < 0x40)
adjusted = 0x40;
else if (adjusted > 0xC0)
adjusted = 0xC0;
// Remove Dead Zone
if (wheel_dead)
{
if (std::abs(CENTRE - adjusted) <= wheel_dead)
adjusted = CENTRE;
}
//std::cout << "wheel zone : " << wheel_zone << " : " << std::hex << " : " << (int) adjusted << std::endl;
a_wheel = adjusted;
}
// Accelerator and Brake [Combined Axis]
else if (axis[1] == axis[2] && (evt->axis == axis[1] || evt->axis == axis[2]))
{
// Accelerator
if (value < -pedals_dead)
{
// Scale input to be in the range of 0 to 0x7F
value = -value;
int adjusted = value / 258;
adjusted += (adjusted >> 2);
a_accel = adjusted;
}
// Brake
else if (value > pedals_dead)
{
// Scale input to be in the range of 0 to 0x7F
int adjusted = value / 258;
adjusted += (adjusted >> 2);
a_brake = adjusted;
}
else
{
a_accel = 0;
a_brake = 0;
}
}
// Accelerator [Single Axis]
else if (evt->axis == axis[1])
{
// Scale input to be in the range of 0 to 0x7F
int adjusted = 0x7F - ((value + (1 << 15)) >> 9);
adjusted += (adjusted >> 2);
a_accel = adjusted;
}
// Brake [Single Axis]
else if (evt->axis == axis[2])
{
// Scale input to be in the range of 0 to 0x7F
int adjusted = 0x7F - ((value + (1 << 15)) >> 9);
adjusted += (adjusted >> 2);
a_brake = adjusted;
}
}
void Input::handle_joy_axis(int wheel_axis, int accel_axis, int brake_axis)
{
// Analog Controls
//std::cout << "Axis: " << (int) evt->axis << " Value: " << (int) evt->value << std::endl;
// Steering
// OutRun requires values between 0x48 and 0xb8.
int percentage_adjust = ((wheel_zone) << 8) / 100;
int adjustedw = wheel_axis + ((wheel_axis * percentage_adjust) >> 8);
// Make 0 hard left, and 0x80 centre value.
adjustedw = ((adjustedw + (1 << 15)) >> 9);
adjustedw += 0x40; // Centre
if (adjustedw < 0x40)
adjustedw = 0x40;
else if (adjustedw > 0xC0)
adjustedw = 0xC0;
// Remove Dead Zone
if (wheel_dead)
{
if (std::abs(CENTRE - adjustedw) <= wheel_dead)
adjustedw = CENTRE;
}
//std::cout << "wheel zone : " << wheel_zone << " : " << std::hex << " : " << (int) adjustedw << std::endl;
a_wheel = adjustedw;
// Accelerator [Single Axis]
// Scale input to be in the range of 0 to 0x7F
int adjusteda = 0x7F - ((-accel_axis + (1 << 15)) >> 9);
adjusteda += (adjusteda >> 2);
// Accelerates slightly for an unknown reason, add a deadzone
a_accel = (adjusteda < 80) ? 0 : adjusteda;
// Brake [Single Axis]
// Scale input to be in the range of 0 to 0x7F
int adjustedb = 0x7F - ((-brake_axis + (1 << 15)) >> 9);
adjustedb += (adjustedb >> 2);
a_brake = adjustedb;
}
#endif
void Input::handle_joy(const uint8_t button, const bool is_pressed)
{
......
......@@ -61,9 +61,8 @@ public:
void init(int, int*, int*, const int, int*, int*);
void close();
#if 0
void handle_joy_axis(SDL_JoyAxisEvent*);
#endif
void handle_joy_axis(int, int, int);
void frame_done();
bool is_pressed(presses p);
bool is_pressed_clear(presses p);
......
......@@ -138,7 +138,7 @@ static void config_init(void)
config.controls.padconfig[5] = 4; /* coin */
config.controls.padconfig[6] = 5; /* padconfig menu */
config.controls.padconfig[7] = 6; /* padconfig view */
config.controls.analog = 0;
config.controls.analog = 1;
config.controls.pad_id = 0; /* pad_id */
config.controls.axis[0] = 0; /* wheel */
config.controls.axis[1] = 2; /* accelerate */
......@@ -669,6 +669,8 @@ bool retro_load_game(const struct retro_game_info *info)
{0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Coin"},
{0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "Adjust View"},
{0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Go Back To Menu"},
{0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Analog X" },
{0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Analog Y" },
{0},
};
......@@ -810,11 +812,11 @@ static struct button_bind binds[] = {
{274, RETRO_DEVICE_ID_JOYPAD_DOWN},
{276, RETRO_DEVICE_ID_JOYPAD_LEFT},
{275, RETRO_DEVICE_ID_JOYPAD_RIGHT},
{122, RETRO_DEVICE_ID_JOYPAD_B}, /* Accelerate */
{120, RETRO_DEVICE_ID_JOYPAD_Y}, /* Brake */
{32, RETRO_DEVICE_ID_JOYPAD_X}, /* Gear 1 */
{32, RETRO_DEVICE_ID_JOYPAD_A}, /* Gear 2 */
{49, RETRO_DEVICE_ID_JOYPAD_START}, /* Start */
{122, RETRO_DEVICE_ID_JOYPAD_B}, /* Accelerate */
{120, RETRO_DEVICE_ID_JOYPAD_Y}, /* Brake */
{32, RETRO_DEVICE_ID_JOYPAD_X}, /* Gear 1 */
{32, RETRO_DEVICE_ID_JOYPAD_A}, /* Gear 2 */
{49, RETRO_DEVICE_ID_JOYPAD_START}, /* Start */
{53, RETRO_DEVICE_ID_JOYPAD_SELECT}, /* Coin */
{304, RETRO_DEVICE_ID_JOYPAD_L}, /* View */
{286, RETRO_DEVICE_ID_JOYPAD_R}, /* Menu */
......@@ -823,6 +825,7 @@ static struct button_bind binds[] = {
static void process_events(void)
{
unsigned i;
int analog_left_x, analog_r2, analog_l2;
input_poll_cb();
for (i = 0; i < (sizeof(binds) / sizeof(binds[0])); i++)
......@@ -832,6 +835,26 @@ static void process_events(void)
else
input.handle_key(binds[i].id, false);
}
analog_left_x = input_state_cb(0, RETRO_DEVICE_ANALOG,
RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
analog_r2 = input_state_cb(0, RETRO_DEVICE_ANALOG,
RETRO_DEVICE_INDEX_ANALOG_BUTTON, RETRO_DEVICE_ID_JOYPAD_R2);
analog_l2 = input_state_cb(0, RETRO_DEVICE_ANALOG,
RETRO_DEVICE_INDEX_ANALOG_BUTTON, RETRO_DEVICE_ID_JOYPAD_L2);
// Fallback to digital to avoid the need of an analog/digital core option
if (analog_r2 == 0)
analog_r2 = input_state_cb( 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B ) ? 0x7FFF : 0;
if (analog_l2 == 0)
analog_l2 = input_state_cb( 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y ) ? 0x7FFF : 0;
if (analog_left_x == 0)
{
analog_left_x += input_state_cb( 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT ) ? -0x7FFF : 0;
analog_left_x += input_state_cb( 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT ) ? 0x7FFF : 0;
}
input.handle_joy_axis(analog_left_x, analog_r2, analog_l2);
}
void retro_run(void)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment