tiny.game.input
Defined in tiny.game.
The input a game receives for one step: the edges of a button, and a fixed-size array of bytes that holds the step's input for recording.
API (3)
Actions
Public operations.
Button.transition: Returns a button's state from whether it was down on the previous step and whether it is down now.Snapshot: Returns the type of one step's input as an array ofbyte_countbytes, which the game fills before the step runs.
Types and contracts
Public types and contracts.
Button: The state of one button on one step: whether it is down, and whether it went down or came up on this step.
Source
Source: fun/game/src/input.zig
zig
//! The input a game receives for one step: the edges of a button, and a fixed-size array of bytes//! that holds the step's input for recording. A game wants to know whether a button went down or//! came up on this step, besides whether it is down. A developer wants to record the input each//! step received and replay it later, step for step. Whether a button changed depends on the//! previous step as well as this one, so the edges come from two steps of state. Input held as an//! in-memory struct carries padding bytes, pointers and the machine's byte order, and all three can//! differ from one run or machine to the next. The transition function `Button.transition` takes//! whether the button was down on the previous step and whether it is down now, and returns the//! button's state with its two edges. The game encodes each step's input as a fixed-size array of//! bytes (a *snapshot*) before the step runs, so a recording holds only bytes the game chose. The//! type constructor `Snapshot(n)` makes the type for n bytes, and n is at least 1.const std = @import("std");/// The state of one button on one step: whether it is down, and whether it went down or came up on/// this step. A game reads the edges from it, so an action that happens once per press happens on/// the pressing step alone. The struct has a C-compatible layout (`extern`), so it can sit inside/// input records that a host passes across a C call into a game library. Every field is false by/// default.pub const Button = extern struct { /// True while the button is held on this step. down: bool = false, /// True on the step the button went down: down now and up on the previous step. pressed: bool = false, /// True on the step the button came up: up now and down on the previous step. released: bool = false, /// Returns a button's state from whether it was down on the previous step and whether it is /// down now. A game calls it once per step for each button, with the level it kept from the /// step before. The edges depend on those two levels alone, so a button held across steps /// reports `pressed` on its first step only. pub fn transition(previous_down: bool, down: bool) Button { return .{ .down = down, .pressed = down and !previous_down, .released = !down and previous_down }; }};/// Returns the type of one step's input as an array of `byte_count` bytes, which the game fills/// before the step runs. A recording stores one of these per step, and a replay hands the same/// bytes back to the game. The game chooses the encoding, and an explicit encoding in a fixed byte/// order keeps padding bytes, pointers and the machine's byte order out of the recorded input. A/// `byte_count` of zero is a compile error.pub fn Snapshot(comptime byte_count: usize) type { if (byte_count == 0) @compileError("input snapshots need at least one byte"); return struct { /// The encoded input: `byte_count` bytes that the game writes before the step and reads /// during it. bytes: [byte_count]u8, /// Returns true when two snapshots hold the same bytes. pub fn eql(a: @This(), b: @This()) bool { return std.mem.eql(u8, &a.bytes, &b.bytes); } };}test "button edges derive only from adjacent levels" { const pressed = Button.transition(false, true); try std.testing.expect(pressed.pressed); try std.testing.expect(!Button.transition(true, true).pressed); try std.testing.expect(Button.transition(true, false).released);}Source: fun/game/src/root.zig:115
zig
pub const input = @import("input.zig");Audit
| Definitions | 4 |
|---|---|
| Public names | 4 |
| Members | 3 |
| Version | 26.7.0 |
| Revision | daab053ee433 |