summaryrefslogtreecommitdiff
path: root/src/render/window.zig
blob: 28c22d178b8f6f75ed3825d0a948caf5e99c6020 (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
const c = @import("../c.zig");
const std = @import("std");

pub const Error = error{
    platform_unavailable,
    platform_error,
};

pub fn getExtensions() [][*c]const u8 {
    var extension_count: u32 = undefined;
    const raw: [*c][*c]const u8 = c.glfwGetRequiredInstanceExtensions(&extension_count);
    const extensions = raw[0..extension_count];

    return extensions;
}

pub const Window = struct {
    title: []const u8,
    width: usize,
    height: usize,
    raw: *c.GLFWwindow,

    pub fn create(width: usize, height: usize, title: []const u8) !Window {
        if (c.glfwInit() != c.GLFW_TRUE) {
            const status = c.glfwGetError(null);

            return switch (status) {
                c.GLFW_PLATFORM_UNAVAILABLE => Error.platform_unavailable,
                c.GLFW_PLATFORM_ERROR => Error.platform_error,
                else => unreachable,
            };
        }

        c.glfwWindowHint(c.GLFW_RESIZABLE, c.GLFW_FALSE);
        c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
        const raw = c.glfwCreateWindow(@intCast(width), @intCast(height), title.ptr, null, null);
        c.glfwShowWindow(raw);

        return Window{
            .title = title,
            .width = width,
            .height = height,
            .raw = raw.?,
        };
    }

    pub fn shouldClose(self: Window) bool {
        return c.glfwWindowShouldClose(self.raw) == c.GLFW_TRUE;
    }

    pub fn size(self: Window) struct { usize, usize } {
        var width: u32 = undefined;
        var height: u32 = undefined;

        c.glfwGetFramebufferSize(self.raw, @ptrCast(&width), @ptrCast(&height));

        return .{ @intCast(width), @intCast(height) };
    }

    pub fn destroy(self: Window) void {
        c.glfwDestroyWindow(self.raw);
        c.glfwTerminate();
    }
};