aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: cc3ae265d1d3e3d803b8334499f3fdf7e0bedf86 (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
const std = @import("std");
const c = @import("c.zig");

pub fn main() !void {
    if (c.mgl_init() != 0)
        return error.MglInitFailed;
    defer c.mgl_deinit();

    var window: c.mgl_window = undefined;
    var create_params = std.mem.zeroes(c.mgl_window_create_params);
    create_params.size = .{ .x = 1280, .y = 720 };

    if (c.mgl_window_create(&window, "mgl", &create_params) != 0)
        return error.MglFailedToCreateWindow;
    defer c.mgl_window_deinit(&window);

    var font_file: c.mgl_memory_mapped_file = undefined;
    var file_load_options = c.mgl_memory_mapped_file_load_options{ .readable = true, .writable = false };
    if (c.mgl_mapped_file_load("/usr/share/fonts/noto/NotoSans-Regular.ttf", &font_file, &file_load_options) != 0)
        return error.MglFailedToLoadFontFile;
    defer c.mgl_mapped_file_unload(&font_file);

    var font: c.mgl_font = undefined;
    if (c.mgl_font_load_from_file(&font, &font_file, 26) != 0)
        return error.MglFailedToLoadFont;
    defer c.mgl_font_unload(&font);

    var text: c.mgl_text = undefined;
    c.mgl_text_init(&text, &font, "hello world", 11);
    defer c.mgl_text_deinit(&text);

    var event: c.mgl_event = undefined;
    while (c.mgl_window_is_open(&window)) {
        while (c.mgl_window_poll_event(&window, &event)) {
            switch (event.type) {
                c.MGL_EVENT_TEXT_ENTERED => {
                    const str = event.unnamed_0.text.str[0..@intCast(event.unnamed_0.text.size)];
                    std.debug.print("text event, codepoint: {any}, str: {s}\n", .{ event.unnamed_0.text.codepoint, str });
                },
                else => std.debug.print("unhandled event: {any}\n", .{ event.type }),
            }
        }

        c.mgl_window_clear(&window, c.mgl_color{ .r = 0, .g = 0, .b = 0, .a = 255});
        c.mgl_text_draw(c.mgl_get_context(), &text);
        c.mgl_window_display(&window);
    }
}