From 84d8766124379d10aa44bb8db2600edd52d7f7ec Mon Sep 17 00:00:00 2001 From: geemili Date: Wed, 28 Feb 2024 13:46:14 -0700 Subject: [PATCH] send one move input per joystick button press This makes it easier to control on the RG351M, as inputting a single move no longer requires a frame perfect press and release. --- src/main.zig | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main.zig b/src/main.zig index 8141333..74c4b0f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -220,6 +220,15 @@ pub fn main() !void { std.log.info("detected joystick = \"{?s}\" {?s}", .{ seizer.backend.glfw.c.glfwGetJoystickName(seizer.backend.glfw.c.GLFW_JOYSTICK_1), seizer.backend.glfw.c.glfwGetJoystickGUID(seizer.backend.glfw.c.GLFW_JOYSTICK_1) }); } + var prev_controller_input_state = InputState{ + .left = false, + .right = false, + .up = false, + .down = false, + .action = false, + .undo = false, + }; + while (seizer.backend.glfw.c.glfwWindowShouldClose(window) != seizer.backend.glfw.c.GLFW_TRUE) { input_state = .{ .left = false, @@ -240,6 +249,15 @@ pub fn main() !void { input_state.down = input_state.down or gamepad_state.buttons[seizer.backend.glfw.c.GLFW_GAMEPAD_BUTTON_DPAD_DOWN] != 0; } } else if (seizer.backend.glfw.c.glfwJoystickPresent(seizer.backend.glfw.c.GLFW_JOYSTICK_1) != 0) { + var controller_input_state = InputState{ + .left = false, + .right = false, + .up = false, + .down = false, + .action = false, + .undo = false, + }; + var hats_count: c_int = undefined; const hats_ptr = seizer.backend.glfw.c.glfwGetJoystickHats(seizer.backend.glfw.c.GLFW_JOYSTICK_1, &hats_count); const hats = hats_ptr[0..@intCast(hats_count)]; @@ -249,16 +267,26 @@ pub fn main() !void { const HAT_DOWN = seizer.backend.glfw.c.GLFW_HAT_DOWN; const HAT_LEFT = seizer.backend.glfw.c.GLFW_HAT_LEFT; - input_state.left = input_state.left or hats[0] & HAT_LEFT != 0; - input_state.right = input_state.right or hats[0] & HAT_RIGHT != 0; - input_state.up = input_state.up or hats[0] & HAT_UP != 0; - input_state.down = input_state.down or hats[0] & HAT_DOWN != 0; + controller_input_state.left = hats[0] & HAT_LEFT != 0; + controller_input_state.right = hats[0] & HAT_RIGHT != 0; + controller_input_state.up = hats[0] & HAT_UP != 0; + controller_input_state.down = hats[0] & HAT_DOWN != 0; var buttons_count: c_int = undefined; const buttons_ptr = seizer.backend.glfw.c.glfwGetJoystickButtons(seizer.backend.glfw.c.GLFW_JOYSTICK_1, &buttons_count); const buttons = buttons_ptr[0..@intCast(buttons_count)]; - input_state.action = input_state.action or buttons[0] != 0; - input_state.undo = input_state.undo or buttons[1] != 0; + controller_input_state.action = buttons[0] != 0; + controller_input_state.undo = buttons[1] != 0; + + // detect rising for controller input + input_state.left = input_state.left or (!prev_controller_input_state.left and controller_input_state.left); + input_state.right = input_state.right or (!prev_controller_input_state.right and controller_input_state.right); + input_state.up = input_state.up or (!prev_controller_input_state.up and controller_input_state.up); + input_state.down = input_state.down or (!prev_controller_input_state.down and controller_input_state.down); + input_state.action = input_state.action or (!prev_controller_input_state.action and controller_input_state.action); + input_state.undo = input_state.undo or (!prev_controller_input_state.undo and controller_input_state.undo); + + prev_controller_input_state = controller_input_state; } var request_command: ?[]const u8 = null;