glfw: add Joystick.getHats

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-21 23:26:27 -07:00 committed by Stephen Gutekanst
parent 20ce5c841b
commit 02c26bde30

View file

@ -129,60 +129,54 @@ pub inline fn getButtons(self: Joystick) Error!?[]const u8 {
return buttons[0..@intCast(usize, count)]; return buttons[0..@intCast(usize, count)];
} }
// TODO(joystick) /// Returns the state of all hats of the specified joystick.
// /// Returns the state of all hats of the specified joystick. ///
// /// /// This function returns the state of all hats of the specified joystick. Each element in the array
// /// This function returns the state of all hats of the specified joystick. /// is one of the following values:
// /// Each element in the array is one of the following values: ///
// /// /// | Name | Value |
// /// Name | Value /// |-----------------------|-------------------------------------|
// /// ---- | ----- /// | `glfw.hat.centered` | 0 |
// /// `GLFW_HAT_CENTERED` | 0 /// | `glfw.hat.u[` | 1 |
// /// `GLFW_HAT_UP` | 1 /// | `glfw.hat.right` | 2 |
// /// `GLFW_HAT_RIGHT` | 2 /// | `glfw.hat.down` | 4 |
// /// `GLFW_HAT_DOWN` | 4 /// | `glfw.hat.left` | 8 |
// /// `GLFW_HAT_LEFT` | 8 /// | `glfw.hat.right_up` | `glfw.hat.right` \| `glfw.hat.up` |
// /// `GLFW_HAT_RIGHT_UP` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_UP` /// | `glfw.hat.right_down` | `glfw.hat.right` \| `glfw.hat.down` |
// /// `GLFW_HAT_RIGHT_DOWN` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_DOWN` /// | `glfw.hat.left_up` | `glfw.hat.left` \| `glfw.hat.up` |
// /// `GLFW_HAT_LEFT_UP` | `GLFW_HAT_LEFT` \| `GLFW_HAT_UP` /// | `glfw.hat.left_down` | `glfw.hat.left` \| `glfw.hat.down` |
// /// `GLFW_HAT_LEFT_DOWN` | `GLFW_HAT_LEFT` \| `GLFW_HAT_DOWN` ///
// /// /// The diagonal directions are bitwise combinations of the primary (up, right, down and left)
// /// The diagonal directions are bitwise combinations of the primary (up, right, /// directions and you can test for these individually by ANDing it with the corresponding
// /// down and left) directions and you can test for these individually by ANDing /// direction.
// /// it with the corresponding direction. ///
// /// /// ```
// /// @code /// if (hats[2] & glfw.hat.right) {
// /// if (hats[2] & GLFW_HAT_RIGHT) /// // State of hat 2 could be right-up, right, or right-down.
// /// { /// }
// /// // State of hat 2 could be right-up, right or right-down /// ```
// /// } ///
// /// @endcode /// If the specified joystick is not present this function will return null but will not generate an
// /// /// error. This can be used instead of first calling glfw.Joystick.present.
// /// If the specified joystick is not present this function will return null ///
// /// but will not generate an error. This can be used instead of first calling /// @return An array of hat states, or null if the joystick is not present.
// /// @ref glfwJoystickPresent. ///
// /// /// Possible errors include glfw.Error.NotInitialized, glfw.Error.InvalidEnum and glfw.Error.PlatformError.
// /// @param[in] jid The [joystick](@ref joysticks) to query. ///
// /// @param[out] count Where to store the number of hat states in the returned /// @pointer_lifetime The returned array is allocated and freed by GLFW. You should not free it
// /// array. This is set to zero if the joystick is not present or an error /// yourself. It is valid until the specified joystick is disconnected, this function is called
// /// occurred. /// again for that joystick or the library is terminated.
// /// @return An array of hat states, or null if the joystick is not present ///
// /// or an error occurred. /// @thread_safety This function must only be called from the main thread.
// /// ///
// /// Possible errors include glfw.Error.NotInitialized, glfw.Error.InvalidEnum and glfw.Error.PlatformError. /// see also: joystick_hat
// /// pub inline fn getHats(self: Joystick) Error!?[]const u8 {
// /// @pointer_lifetime The returned array is allocated and freed by GLFW. You var count: c_int = undefined;
// /// should not free it yourself. It is valid until the specified joystick is const hats = c.glfwGetJoystickHats(self.jid, &count);
// /// disconnected, this function is called again for that joystick or the library try getError();
// /// is terminated. if (hats == null) return null;
// /// return hats[0..@intCast(usize, count)];
// /// @thread_safety This function must only be called from the main thread. }
// ///
// /// see also: joystick_hat
// ///
// ///
// /// @ingroup input
// GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count);
// TODO(joystick) // TODO(joystick)
// /// Returns the name of the specified joystick. // /// Returns the name of the specified joystick.
@ -462,6 +456,16 @@ test "getButtons" {
_ = joystick.getButtons() catch |err| std.debug.print("failed to get joystick buttons, joysticks not supported? error={}\n", .{err}); _ = joystick.getButtons() catch |err| std.debug.print("failed to get joystick buttons, joysticks not supported? error={}\n", .{err});
} }
test "getHats" {
const glfw = @import("main.zig");
try glfw.init();
defer glfw.terminate();
const joystick = glfw.Joystick{ .jid = glfw.Joystick.one };
_ = joystick.getHats() catch |err| std.debug.print("failed to get joystick hats, joysticks not supported? error={}\n", .{err});
}
test "setUserPointer_syntax" { test "setUserPointer_syntax" {
const glfw = @import("main.zig"); const glfw = @import("main.zig");
try glfw.init(); try glfw.init();