aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 120e926b57e57a156624aca77db7612d1228d4b3 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const std = @import("std");
const spoon = @import("spoon");

const game = @import("game.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") or in.eqlDescription("a")) {
                    gs.moveLeft();
                } else if (in.eqlDescription("arrow-right") or in.eqlDescription("d")) {
                    gs.moveRight();
                }
            }
        } 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);
}

fn handleSigWinch(_: c_int) callconv(.C) void {
    term.fetchSize() catch {};
    renderAll() catch {};
}

/// 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);
}