ref: 890d7844c5bdbbea6785da24d0a315a13afed92b snake-zig/src/main.zig -rw-r--r-- 5.1 KiB View raw
                                                                                
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const std = @import("std");
const ArrayList = std.ArrayList;
const clap = @import("clap");
const c = @import("c.zig");
const panic = std.debug.panic;
const AllShaders = @import("shaders.zig").AllShaders;
const Spritesheet = @import("spritesheet.zig").Spritesheet;
const State = @import("game_state.zig").State;
const PlayingState = @import("game_state.zig").PlayingState;
const MainMenuState = @import("game_state.zig").MainMenuState;
const c_allocator = std.heap.c_allocator;
const graphics = @import("graphics.zig");
const Context = @import("context.zig").Context;
const Key = @import("key.zig").Key;

// global for the callback
var pressed_keys: ArrayList(Key) = ArrayList(Key).init(c_allocator);
extern fn keyCallback(window: ?*c.GLFWwindow, glfw_key: c_int, scancode: c_int, action: c_int, mods: c_int) void {
    if (action != c.GLFW_PRESS) return;
    if (Key.fromGLFW(glfw_key)) |key| {
        pressed_keys.append(key) catch panic("Ran out of memory!");
    }
}

const default_grid_width = 8;
const default_grid_height = 8;
const default_window_width = 800;
const default_window_height = 800;

pub fn main() anyerror!void {
    // there is a compiler error with zig when compiling clap's ComptimeClap struct.
    // will implement later. Most of this is probably wrong
    //
    // const params = [_]clap.Param([]const u8){
    //     clap.Param([]const u8){
    //         .id = "Show help and exit",
    //         .names = clap.Names{ .short = 'h', .long = "help" },
    //     },
    //     clap.Param([]const u8){
    //         .id = "Window width",
    //         .names = clap.Names{ .long = "window-width" },
    //         .takes_value = true,
    //     },
    //     clap.Param([]const u8){
    //         .id = "Window height",
    //         .names = clap.Names{ .long = "window-height" },
    //         .takes_value = true,
    //     },
    //     clap.Param([]const u8){
    //         .id = "Grid width",
    //         .names = clap.Names{ .short = 'w', .long = "grid-width" },
    //         .takes_value = true,
    //     },
    //     clap.Param([]const u8){
    //         .id = "Grid height",
    //         .names = clap.Names{ .short = 'h', .long = "grid-height" },
    //         .takes_value = true,
    //     },
    // };

    // const args = init: {
    //     var iter = clap.args.OsIterator.init(c_allocator);
    //     defer iter.deinit();
    //     // skip exe path
    //     _ = try iter.next();
    //     break :init clap.ComptimeClap([]const u8, params).parse(c_allocator, clap.args.OsIterator, &iter);
    // };
    // defer args.deinit();

    // if (args.flag("--help")) {
    //     const stdout = init: {
    //         const file = try std.io.getStdOut();
    //         break :init file.outStream().stream;
    //     };
    //     try clap.help(stdout, params);
    //     return;
    // }

    // const grid_width = if (args.option("--grid-width")) |width| {
    //     std.fmt.parseInt(usize, width, 10) catch |err| {
    //         std.debug.warn("Could not parse grid-width, goind with default width {}", default_grid_width);
    //         default_grid_width;
    //     };
    // } else default_grid_width;

    // const grid_height = if (args.option("--grid-height")) |height| {
    //     std.fmt.parseInt(usize, height, 10) catch |err| {
    //         std.debug.warn("Could not parse grid-height, goind with default height {}", default_grid_height);
    //         default_grid_height;
    //     };
    // } else default_grid_height;

    // const window_width = if (args.option("--grid-height")) |height| {
    //     std.fmt.parseInt(usize, height, 10) catch |err| {
    //         std.debug.warn("Could not parse grid-height, goind with default width {}", default_window_width);
    //         default_window_width;
    //     };
    // } else default_window_width;

    // const window_height = if (args.option("--window-height")) |height| {
    //     std.fmt.parseInt(usize, height, 10) catch |err| {
    //         std.debug.warn("Could not parse window-height, goind with default height {}", default_window_height);
    //         default_window_height;
    //     };
    // } else default_window_height;

    // var ctx = Context.init(grid_width, grid_height, window_width, window_height);
    var ctx = Context.init(8, 8, 800, 800);
    defer ctx.deinit();
    _ = c.glfwSetKeyCallback(ctx.window, keyCallback);

    var game_state = State{ .MainMenu = MainMenuState{ .time = 0.0 } };
    defer game_state.deinit();

    c.glClearColor(0.2, 0.4, 0.3, 1.0);

    var prev_time = c.glfwGetTime();
    while (c.glfwWindowShouldClose(ctx.window) == c.GL_FALSE) {
        c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT | c.GL_STENCIL_BUFFER_BIT);
        const now = c.glfwGetTime();
        ctx.dt = now - prev_time;
        prev_time = now;

        for (pressed_keys.toSlice()) |key| if (game_state.handleKey(&ctx, key)) |state| {
            game_state = state;
        };
        pressed_keys.shrink(0);

        if (try game_state.update(&ctx)) |state| {
            game_state = state;
        }
        game_state.draw(&ctx);
        c.glfwSwapBuffers(ctx.window);
        c.glfwPollEvents();
    }
}