~mil/mepo

ref: e02895d949561f67251bb6eb57398f346d4c89d7 mepo/src/main.zig -rw-r--r-- 2.4 KiB
e02895d9Miles Alan Fix a minor bug relating to undefined colors for TTF_RenderText_Blended 4 months ago
                                                                                
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
const std = @import("std");
const builtin = @import("builtin");
const Mepo = @import("./Mepo.zig");
const Downloader = @import("./Downloader.zig");
const TileCache = @import("./TileCache.zig");
const sdl = @import("./sdlshim.zig");
const utildbg = @import("./util/utildbg.zig");

pub fn main() !void {
    comptime {
        const v = builtin.zig_version;
        if (v.major != 0 or v.minor != 9 or v.patch != 1)
            @panic("Must be built against Zig 0.9.1");
    }

    const allocator = std.heap.c_allocator;
    var stderr_on: bool = false;
    var mode: enum { Graphical, Download, Version } = .Graphical;
    var download_request: []const u8 = undefined;
    var cfg: ?[]const u8 = null;
    var use_sw_renderer = false;

    var args_it = std.process.args();
    var arg_w = args_it.next(allocator);
    while (arg_w != null) : (arg_w = args_it.next(allocator)) {
        const arg = try arg_w.?;
        if (std.mem.eql(u8, arg, "-i")) {
            // -i: Read from STDIN
            utildbg.log("Reading from STDIN", .{});
            cfg = try read_stdin(allocator);
        } else if (std.mem.startsWith(u8, arg, "-d")) {
            // -d: Downloader mode (non-video), runs cfg
            mode = .Download;
        } else if (std.mem.startsWith(u8, arg, "-e")) {
            // -e: Enable stderr debugging
            stderr_on = true;
        } else if (std.mem.startsWith(u8, arg, "-sw")) {
            // -sw: Use software renderer instead of default HW renderer
            use_sw_renderer = true;
        } else if (std.mem.startsWith(u8, arg, "-v")) {
            // -v: Version
            mode = .Version;
        }
    }

    utildbg.log("Boot Mepo in mode: {}\n", .{mode});
    switch (mode) {
        .Graphical => {
            var tile_cache = try TileCache.init(allocator);
            var mepo = try Mepo.init(allocator, &tile_cache, use_sw_renderer, stderr_on, cfg);
            mepo.sdl_event_loop() catch Mepo.graceful_terminate();
        },
        .Download => {
            var tile_cache = try TileCache.init(allocator);
            try Downloader.cache_dlbbox(&tile_cache, download_request);
        },
        .Version => {
            utildbg.log("Mepo - version {d}\n", .{0.4});
        },
    }
}

fn read_stdin(allocator: std.mem.Allocator) anyerror![]const u8 {
    const stream = &std.io.getStdIn().reader();
    const buffer = try allocator.alloc(u8, 1024 * 100);
    _ = try stream.read(buffer);
    return std.mem.trim(u8, buffer, " \n");
}