aboutsummaryrefslogtreecommitdiff
path: root/src/inputs.zig
blob: a2ab59e7003f41ecd75c971741ff7400ec5c5b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 = 0,
    left: bool = false,
    right: bool = false,
    up: bool = false,
    down: bool = false,
};

pub const Gamepad = struct {
    held: Buttons = .{},
    pressed: Buttons = .{},
    released: Buttons = .{},

    pub fn reset(self: *Gamepad) void {
        self.held = @bitCast(Buttons, @bitCast(u8, self.held) ^ @bitCast(u8, self.held));
        self.pressed = @bitCast(Buttons, @bitCast(u8, self.pressed) ^ @bitCast(u8, self.pressed));
        self.released = @bitCast(Buttons, @bitCast(u8, self.released) ^ @bitCast(u8, self.released));
    }
    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));
    }
};