Created
March 14, 2025 09:04
-
-
Save 0riginaln0/2b82323eee4404d07d001955758eb7ce to your computer and use it in GitHub Desktop.
Input handling "library" in Zig for WASM-4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const w4 = @import("wasm4.zig"); | |
pub const Input = struct { | |
previous_frame_gamepad: u8, | |
current_frame_gamepad: u8, | |
previous_frame_mouse: u8, | |
current_frame_mouse: u8, | |
previous_frame_mouse_x: i16, | |
current_frame_mouse_x: i16, | |
previous_frame_mouse_y: i16, | |
current_frame_mouse_y: i16, | |
pub fn init() Input { | |
return Input{ | |
.previous_frame_gamepad = 0, | |
.current_frame_gamepad = 0, | |
.previous_frame_mouse = 0, | |
.current_frame_mouse = 0, | |
.previous_frame_mouse_x = 0, | |
.current_frame_mouse_x = 0, | |
.previous_frame_mouse_y = 0, | |
.current_frame_mouse_y = 0, | |
}; | |
} | |
pub fn update(self: *Input) void { | |
self.previous_frame_gamepad = self.current_frame_gamepad; | |
self.current_frame_gamepad = w4.GAMEPAD1.*; | |
self.previous_frame_mouse = self.current_frame_mouse; | |
self.current_frame_mouse = w4.MOUSE_BUTTONS.*; | |
self.previous_frame_mouse_x = self.current_frame_mouse_x; | |
self.current_frame_mouse_x = w4.MOUSE_X.*; | |
self.previous_frame_mouse_y = self.current_frame_mouse_y; | |
self.current_frame_mouse_y = w4.MOUSE_Y.*; | |
} | |
pub fn getMouseX(self: Input) i16 { | |
return self.current_frame_mouse_x; | |
} | |
pub fn getMouseY(self: Input) i16 { | |
return self.current_frame_mouse_y; | |
} | |
pub fn getMouseDx(self: Input) i16 { | |
return -(self.previous_frame_mouse_x - self.current_frame_mouse_x); | |
} | |
pub fn getMouseDy(self: Input) i16 { | |
return -(self.previous_frame_mouse_y - self.current_frame_mouse_y); | |
} | |
pub fn isLeftMousePressed(self: Input) bool { | |
return (self.current_frame_mouse & w4.MOUSE_LEFT) != 0; | |
} | |
pub fn isRightMousePressed(self: Input) bool { | |
return (self.current_frame_mouse & w4.MOUSE_RIGHT) != 0; | |
} | |
pub fn isMiddleMousePressed(self: Input) bool { | |
return (self.current_frame_mouse & w4.MOUSE_MIDDLE) != 0; | |
} | |
pub fn isLeftMouseJustPressed(self: Input) bool { | |
return ((self.previous_frame_mouse & w4.MOUSE_LEFT) == 0) and self.isLeftMousePressed(); | |
} | |
pub fn isRightMouseJustPressed(self: Input) bool { | |
return ((self.previous_frame_mouse & w4.MOUSE_RIGHT) == 0) and self.isRightMousePressed(); | |
} | |
pub fn isMiddleMouseJustPressed(self: Input) bool { | |
return ((self.previous_frame_mouse & w4.MOUSE_MIDDLE) == 0) and self.isMiddleMousePressed(); | |
} | |
pub fn isLeftMouseJustReleased(self: Input) bool { | |
return ((self.previous_frame_mouse & w4.MOUSE_LEFT) != 0) and !self.isLeftMousePressed(); | |
} | |
pub fn isRightMouseJustReleased(self: Input) bool { | |
return ((self.previous_frame_mouse & w4.MOUSE_RIGHT) != 0) and !self.isRightMousePressed(); | |
} | |
pub fn isMiddleMouseJustReleased(self: Input) bool { | |
return ((self.previous_frame_mouse & w4.MOUSE_MIDDLE) != 0) and !self.isMiddleMousePressed(); | |
} | |
pub fn isUpPressed(self: Input) bool { | |
return (self.current_frame_gamepad & w4.BUTTON_UP) != 0; | |
} | |
pub fn isDownPressed(self: Input) bool { | |
return (self.current_frame_gamepad & w4.BUTTON_DOWN) != 0; | |
} | |
pub fn isLeftPressed(self: Input) bool { | |
return (self.current_frame_gamepad & w4.BUTTON_LEFT) != 0; | |
} | |
pub fn isRightPressed(self: Input) bool { | |
return (self.current_frame_gamepad & w4.BUTTON_RIGHT) != 0; | |
} | |
pub fn isXPressed(self: Input) bool { | |
return (self.current_frame_gamepad & w4.BUTTON_1) != 0; | |
} | |
pub fn isZPressed(self: Input) bool { | |
return (self.current_frame_gamepad & w4.BUTTON_2) != 0; | |
} | |
pub fn isUpJustPressed(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_UP) == 0) and self.isUpPressed(); | |
} | |
pub fn isDownJustPressed(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_DOWN) == 0) and self.isDownPressed(); | |
} | |
pub fn isLeftJustPressed(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_LEFT) == 0) and self.isLeftPressed(); | |
} | |
pub fn isRightJustPressed(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_RIGHT) == 0) and self.isRightPressed(); | |
} | |
pub fn isXJustPressed(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_1) == 0) and self.isXPressed(); | |
} | |
pub fn isZJustPressed(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_2) == 0) and self.isZPressed(); | |
} | |
pub fn isUpJustReleased(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_UP) != 0) and !self.isUpPressed(); | |
} | |
pub fn isDownJustReleased(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_DOWN) != 0) and !self.isDownPressed(); | |
} | |
pub fn isLeftJustReleased(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_LEFT) != 0) and !self.isLeftPressed(); | |
} | |
pub fn isRightJustReleased(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_RIGHT) != 0) and !self.isRightPressed(); | |
} | |
pub fn isXJustReleased(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_1) != 0) and !self.isXPressed(); | |
} | |
pub fn isZJustReleased(self: Input) bool { | |
return ((self.previous_frame_gamepad & w4.BUTTON_2) != 0) and !self.isZPressed(); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const w4 = @import("wasm4.zig"); | |
const Input = @import("input.zig").Input; | |
const smiley = [8]u8{ | |
0b11000011, | |
0b10000001, | |
0b00100100, | |
0b00100100, | |
0b00000000, | |
0b00100100, | |
0b10011001, | |
0b11000011, | |
}; | |
export fn start() void {} | |
var input = Input.init(); | |
export fn update() void { | |
input.update(); | |
w4.DRAW_COLORS.* = 2; | |
w4.text("Hello from Zig!", 10, 10); | |
if (input.isXPressed()) { | |
w4.DRAW_COLORS.* = 4; | |
} | |
w4.blit(&smiley, 76, 76, 8, 8, w4.BLIT_1BPP); | |
w4.text("Press X to blink", 16, 90); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment