diff options
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/src/main.zig b/src/main.zig index cec8688..120e926 100644 --- a/src/main.zig +++ b/src/main.zig @@ -33,21 +33,32 @@ pub fn main() !void { try term.fetchSize(); try term.setWindowTitle("Grenade Brothers", .{}); - gs.reset(); + gs.reset(.left); try renderAll(); var buf: [16]u8 = undefined; while (!done) { - _ = try std.os.poll(&fds, 100); - - const read = try term.readInput(&buf); - var it = spoon.inputParser(buf[0..read]); - while (it.next()) |in| { - if (in.eqlDescription("escape") or in.eqlDescription("q")) { - done = true; - break; + // TODO We need to measure how long it took before a key was hit so that we can adjust the timeout on the next loop + // otherwise we will get inconsistent ticks for movement steps + const timeout = try std.os.poll(&fds, @floatToInt(u64, 1000 / 60.0)); + + if (timeout > 0) { // if timeout if not 0 then some fds we are polling have events for us + const read = try term.readInput(&buf); + var it = spoon.inputParser(buf[0..read]); + while (it.next()) |in| { + if (in.eqlDescription("escape") or in.eqlDescription("q")) { + done = true; + break; + } else if (in.eqlDescription("arrow-left") or in.eqlDescription("a")) { + gs.moveLeft(); + } else if (in.eqlDescription("arrow-right") or in.eqlDescription("d")) { + gs.moveRight(); + } } + } else { + gs.step(); } + try renderAll(); } } |