Skip to content

Commit 6cb068d

Browse files
committed
[libretro] Improve directional input handling consistency
1 parent 2d34b96 commit 6cb068d

1 file changed

Lines changed: 99 additions & 18 deletions

File tree

platforms/libretro/libretro.cpp

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ static int audio_sample_count = 0;
5656

5757
static unsigned input_device[2];
5858
static bool allow_up_down = false;
59+
static int8_t dpad_vertical_latch[2] = { 0, 0 };
60+
static int8_t dpad_horizontal_latch[2] = { 0, 0 };
5961
static bool lightgun_touchscreen = false;
6062
static bool lightgun_crosshair = false;
6163
static Video::LightPhaserCrosshairShape lightgun_crosshair_shape = Video::LightPhaserCrosshairCross;
@@ -493,32 +495,111 @@ static void update_input(void)
493495
ib |= input_state_cb(player, RETRO_DEVICE_JOYPAD, 0, i) ? (1 << i) : 0;
494496
}
495497

496-
if (ib & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
498+
bool raw_up = (ib & (1 << RETRO_DEVICE_ID_JOYPAD_UP)) != 0;
499+
bool raw_down = (ib & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN)) != 0;
500+
bool raw_left = (ib & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT)) != 0;
501+
bool raw_right = (ib & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT)) != 0;
502+
bool up = raw_up;
503+
bool down = raw_down;
504+
bool left = raw_left;
505+
bool right = raw_right;
506+
507+
if (!allow_up_down)
497508
{
498-
if (allow_up_down || !(ib & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN)))
499-
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Up);
509+
if (raw_up && raw_down)
510+
{
511+
if (dpad_vertical_latch[player] > 0)
512+
{
513+
up = true;
514+
down = false;
515+
}
516+
else if (dpad_vertical_latch[player] < 0)
517+
{
518+
up = false;
519+
down = true;
520+
}
521+
else
522+
{
523+
up = true;
524+
down = false;
525+
dpad_vertical_latch[player] = 1;
526+
}
527+
}
528+
else if (raw_up)
529+
{
530+
up = true;
531+
down = false;
532+
dpad_vertical_latch[player] = 1;
533+
}
534+
else if (raw_down)
535+
{
536+
up = false;
537+
down = true;
538+
dpad_vertical_latch[player] = -1;
539+
}
540+
else
541+
{
542+
up = false;
543+
down = false;
544+
dpad_vertical_latch[player] = 0;
545+
}
546+
547+
if (raw_left && raw_right)
548+
{
549+
if (dpad_horizontal_latch[player] > 0)
550+
{
551+
left = true;
552+
right = false;
553+
}
554+
else if (dpad_horizontal_latch[player] < 0)
555+
{
556+
left = false;
557+
right = true;
558+
}
559+
else
560+
{
561+
left = true;
562+
right = false;
563+
dpad_horizontal_latch[player] = 1;
564+
}
565+
}
566+
else if (raw_left)
567+
{
568+
left = true;
569+
right = false;
570+
dpad_horizontal_latch[player] = 1;
571+
}
572+
else if (raw_right)
573+
{
574+
left = false;
575+
right = true;
576+
dpad_horizontal_latch[player] = -1;
577+
}
578+
else
579+
{
580+
left = false;
581+
right = false;
582+
dpad_horizontal_latch[player] = 0;
583+
}
500584
}
585+
586+
if (up)
587+
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Up);
501588
else
502589
core->KeyReleased(static_cast<GS_Joypads>(player), Key_Up);
503-
if (ib & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
504-
{
505-
if (allow_up_down || !(ib & (1 << RETRO_DEVICE_ID_JOYPAD_UP)))
506-
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Down);
507-
}
590+
591+
if (down)
592+
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Down);
508593
else
509594
core->KeyReleased(static_cast<GS_Joypads>(player), Key_Down);
510-
if (ib & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
511-
{
512-
if (allow_up_down || !(ib & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT)))
513-
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Left);
514-
}
595+
596+
if (left)
597+
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Left);
515598
else
516599
core->KeyReleased(static_cast<GS_Joypads>(player), Key_Left);
517-
if (ib & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
518-
{
519-
if (allow_up_down || !(ib & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT)))
520-
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Right);
521-
}
600+
601+
if (right)
602+
core->KeyPressed(static_cast<GS_Joypads>(player), Key_Right);
522603
else
523604
core->KeyReleased(static_cast<GS_Joypads>(player), Key_Right);
524605

0 commit comments

Comments
 (0)