From 2154ee5aea783d4bf138cdd5c31cd072b5133c46 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sat, 30 Oct 2021 11:13:53 -0700 Subject: [PATCH] glfw: ziggify gamepad action enumerations Helps hexops/mach#37 Signed-off-by: Stephen Gutekanst --- glfw/src/Joystick.zig | 9 +++++++++ glfw/src/action.zig | 4 +--- glfw/src/gamepad_axis.zig | 23 ++++++++++++----------- glfw/src/main.zig | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/glfw/src/Joystick.zig b/glfw/src/Joystick.zig index 7e3493ce..cfdd34f9 100644 --- a/glfw/src/Joystick.zig +++ b/glfw/src/Joystick.zig @@ -9,6 +9,7 @@ const c = @import("c.zig").c; const Window = @import("Window.zig"); const Error = @import("errors.zig").Error; const getError = @import("errors.zig").getError; +const GamepadAxis = @import("gamepad_axis.zig").GamepadAxis; const Joystick = @This(); @@ -46,7 +47,14 @@ const GamepadState = extern struct { buttons: [15]u8, /// The states of each gamepad axis (see gamepad_axes), in the range -1.0 to 1.0 inclusive. + /// + /// Use the enumeration helper e.g. `.getAxis(.left_x)` to access these indices. axes: [6]f32, + + /// Returns the status of the specified gamepad axis, in the range -1.0 to 1.0 inclusive. + pub fn getAxis(self: @This(), which: GamepadAxis) f32 { + return self.axis[which]; + } }; /// Returns whether the specified joystick is present. @@ -540,4 +548,5 @@ test "getGamepadState" { const joystick = glfw.Joystick{ .jid = glfw.Joystick.one }; _ = joystick.getGamepadState() catch |err| std.debug.print("failed to get gamepad state, joysticks not supported? error={}\n", .{err}); + _ = (GamepadState{}).getAxis(.left_x); } diff --git a/glfw/src/action.zig b/glfw/src/action.zig index 4ce11e2e..59314709 100644 --- a/glfw/src/action.zig +++ b/glfw/src/action.zig @@ -1,8 +1,6 @@ -//! Key and button actions - const c = @import("c.zig").c; -/// Holds all GLFW C action enumerations in their raw form. +/// Key and button actions pub const Action = enum(c_int) { /// The key or mouse button was released. release = c.GLFW_RELEASE, diff --git a/glfw/src/gamepad_axis.zig b/glfw/src/gamepad_axis.zig index aaa813f5..24a524d8 100644 --- a/glfw/src/gamepad_axis.zig +++ b/glfw/src/gamepad_axis.zig @@ -1,13 +1,14 @@ -//! Gamepad axes. -//! -//! See glfw.getGamepadState for how these are used. - const c = @import("c.zig").c; -pub const left_x = c.GLFW_GAMEPAD_AXIS_LEFT_X; -pub const left_y = c.GLFW_GAMEPAD_AXIS_LEFT_Y; -pub const right_x = c.GLFW_GAMEPAD_AXIS_RIGHT_X; -pub const right_y = c.GLFW_GAMEPAD_AXIS_RIGHT_Y; -pub const left_trigger = c.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER; -pub const right_trigger = c.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER; -pub const left = right_trigger; +/// Gamepad axes. +/// +/// See glfw.getGamepadState for how these are used. +pub const GamepadAxis = enum(c_int) { + left_x = c.GLFW_GAMEPAD_AXIS_LEFT_X, + left_y = c.GLFW_GAMEPAD_AXIS_LEFT_Y, + right_x = c.GLFW_GAMEPAD_AXIS_RIGHT_X, + right_y = c.GLFW_GAMEPAD_AXIS_RIGHT_Y, + left_trigger = c.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, + right_trigger = c.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, + last = .right_trigger, +}; diff --git a/glfw/src/main.zig b/glfw/src/main.zig index c9229adc..feeeb63c 100644 --- a/glfw/src/main.zig +++ b/glfw/src/main.zig @@ -10,7 +10,7 @@ pub const Error = @import("errors.zig").Error; const getError = @import("errors.zig").getError; pub const Action = @import("action.zig").Action; -pub const gamepad_axis = @import("gamepad_axis.zig"); +pub const GamepadAxis = @import("gamepad_axis.zig").GamepadAxis; pub const gamepad_button = @import("gamepad_button.zig"); pub const GammaRamp = @import("GammaRamp.zig"); pub const hat = @import("hat.zig");