Keep joystick movement until rendering was performed.
[mercenary-reloaded.git] / src / libjoystick / joystick.c
index 12abea5..e004d69 100644 (file)
@@ -27,6 +27,11 @@ static int joystick_right = 0;
 static int joystick_up = 0;
 static int joystick_down = 0;
 static int joystick_fire = 0;
+static int joystick_left_reset = 0;
+static int joystick_right_reset = 0;
+static int joystick_up_reset = 0;
+static int joystick_down_reset = 0;
+static int joystick_fire_reset = 0;
 
 #define CIAAPRA                0xbfe000
 #define JOYDAT1                0xdff00c
@@ -68,18 +73,39 @@ uint16_t emulate_joystick_read(uint32_t address)
 }
 
 /* use -1 to keep state unchanged */
+/* NOTE: a pressed and then released button/stick will sustain until reset_joystick() is called after rendering was done.
+ * this ensures that a short press (during rendering one frame) will not get lost.
+ */
 void set_joystick(int left, int right, int up, int down, int fire)
 {
+       if (left == 1)
+               joystick_left = 1;
        if (left >= 0)
-               joystick_left = left;
+               joystick_left_reset = left;
+       if (right == 1)
+               joystick_right = 1;
        if (right >= 0)
-               joystick_right = right;
+               joystick_right_reset = right;
+       if (up == 1)
+               joystick_up = 1;
        if (up >= 0)
-               joystick_up = up;
+               joystick_up_reset = up;
+       if (down == 1)
+               joystick_down = 1;
        if (down >= 0)
-               joystick_down = down;
+               joystick_down_reset = down;
+       if (fire == 1)
+               joystick_fire = 1;
        if (fire >= 0)
-               joystick_fire = fire;
+               joystick_fire_reset = fire;
 }
 
+void reset_joystick(void)
+{
+       joystick_left = joystick_left_reset;
+       joystick_right = joystick_right_reset;
+       joystick_up = joystick_up_reset;
+       joystick_down = joystick_down_reset;
+       joystick_fire = joystick_fire_reset;
+}