aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2022-09-25 23:06:32 +0200
committerJulien Dessaux2022-09-25 23:06:32 +0200
commitc9539b4cb407f5bd726d56c9c26c7677a56db4bf (patch)
tree4caa6928f600f5a6ceb35f691e78f5f59c2ab974
parentAdded brothers and ball collisions (diff)
downloadgrenade-brothers-c9539b4cb407f5bd726d56c9c26c7677a56db4bf.tar.gz
grenade-brothers-c9539b4cb407f5bd726d56c9c26c7677a56db4bf.tar.bz2
grenade-brothers-c9539b4cb407f5bd726d56c9c26c7677a56db4bf.zip
Committed forgotten file necessary for gamepad inputs
Diffstat (limited to '')
-rw-r--r--src/inputs.zig27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/inputs.zig b/src/inputs.zig
new file mode 100644
index 0000000..4d2afa0
--- /dev/null
+++ b/src/inputs.zig
@@ -0,0 +1,27 @@
+const std = @import("std");
+const w4 = @import("wasm4.zig");
+
+// This packed struct matches the buttons definitions in wasm4.zig
+// so that a bitCast can give us named fields
+pub const Buttons = packed struct {
+ x: bool = false,
+ y: bool = false,
+ _: u2,
+ left: bool = false,
+ right: bool = false,
+ up: bool = false,
+ down: bool = false,
+};
+
+pub const Gamepad = struct {
+ held: Buttons = .{},
+ pressed: Buttons = .{},
+ released: Buttons = .{},
+
+ pub fn update(self: *Gamepad, buttons: u8) void {
+ var previous = @bitCast(u8, self.held);
+ self.held = @bitCast(Buttons, buttons);
+ self.pressed = @bitCast(Buttons, buttons & (buttons ^ previous));
+ self.released = @bitCast(Buttons, previous & (buttons ^ previous));
+ }
+};