From 8f76ba782669bc986a5e450e5a50ac84232c1323 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 21 Sep 2022 23:19:45 +0200 Subject: Began rewriting the game as a wasm4 cartridge --- src/main.zig | 102 ++++++----------------------------------------------------- 1 file changed, 9 insertions(+), 93 deletions(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index 875b02c..29b7db5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,99 +1,15 @@ -const std = @import("std"); -const spoon = @import("spoon"); - const game = @import("game.zig"); +const std = @import("std"); +const utils = @import("utils.zig"); +const w4 = @import("wasm4.zig"); -var term: spoon.Term = undefined; -var done: bool = false; - -//----- Game State ----------------------------------------------------------- -var gs: game.Game = undefined; - -//----- Main ----------------------------------------------------------------- -pub fn main() !void { - try term.init(); - defer term.deinit(); - - std.os.sigaction(std.os.SIG.WINCH, &std.os.Sigaction{ - .handler = .{ .handler = handleSigWinch }, - .mask = std.os.empty_sigset, - .flags = 0, - }, null); - - var fds: [1]std.os.pollfd = undefined; - fds[0] = .{ - .fd = term.tty.handle, - .events = std.os.POLL.IN, - .revents = undefined, - }; - - try term.uncook(.{}); - defer term.cook() catch {}; - - try term.fetchSize(); - try term.setWindowTitle("Grenade Brothers", .{}); - - gs.reset(.left); - try renderAll(); - - var buf: [16]u8 = undefined; - while (!done) { - // 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")) { - gs.moveLeft(.right); - } else if (in.eqlDescription("arrow-right")) { - gs.moveRight(.right); - } else if (in.eqlDescription("arrow-up")) { - gs.moveJump(.right); - } else if (in.eqlDescription("a")) { - gs.moveLeft(.left); - } else if (in.eqlDescription("d")) { - gs.moveRight(.left); - } else if (in.eqlDescription("space")) { - gs.moveJump(.left); - } - } - } else { - gs.step(); - } - try renderAll(); - } -} - -fn renderAll() !void { - var rc = try term.getRenderContext(); - defer rc.done() catch {}; - - try rc.clear(); - - if (term.width < 80 or term.width < 24) { - try rc.setAttribute(.{ .fg = .red, .bold = true }); - try rc.writeAllWrapping("Terminal too small!"); - return; - } - - try gs.draw(&rc); -} +//----- Globals --------------------------------------------------------------- +var Game: game.Game = undefined; -fn handleSigWinch(_: c_int) callconv(.C) void { - term.fetchSize() catch {}; - renderAll() catch {}; +export fn start() void { + Game.reset(); } -/// Custom panic handler, so that we can try to cook the terminal on a crash, -/// as otherwise all messages will be mangled. -pub fn panic(msg: []const u8, trace: ?*std.builtin.StackTrace) noreturn { - @setCold(true); - term.cook() catch {}; - std.builtin.default_panic(msg, trace); +export fn update() void { + Game.draw(); } -- cgit v1.2.3