aboutsummaryrefslogtreecommitdiffstats
path: root/src/output.zig
blob: c236f4adb51e5dd3ea0faca74553c6d52a210c01 (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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const std = @import("std");
const c = @import("c.zig");
const build_config = @import("build_config");
const cfg = @import("config.zig");
const gl = @import("gl.zig");

pub const Output = struct {
    update_fn: *const fn (output: *Output, texture_id: c.GLuint) bool,
    destroy_fn: *const fn (output: *Output, allocator: std.mem.Allocator) void,

    pub fn create(
        allocator: std.mem.Allocator,
        config: *const cfg.OutputConfig,
        constants: *gl.Constants,
    ) !*Output {
        switch (config.*) {
            inline else => |*con| {
                const ConfigType = @TypeOf(con.*);
                if (ConfigType == void) return error.supportDisabled;
                return con.create(allocator, constants);
            },
        }
    }

    pub fn update(self: *Output, texture_id: c.GLuint) bool {
        return self.update_fn(self, texture_id);
    }

    pub fn destroy(self: *Output, allocator: std.mem.Allocator) void {
        return self.destroy_fn(self, allocator);
    }
};

pub const WindowOutput = struct {
    pub const Config = struct {
        const FilterMode = enum(c.GLuint) {
            nearest = c.GL_NEAREST,
            linear = c.GL_LINEAR,
        };

        width: i32,
        height: i32,
        filter: FilterMode,

        monitor: []const u8,
        fullscreen: bool,

        pub const default: Config = .{
            .width = 800,
            .height = 600,
            .filter = .linear,
            .monitor = "",
            .fullscreen = false,
        };

        pub fn create(
            config: *const Config,
            allocator: std.mem.Allocator,
            constants: *gl.Constants,
        ) *Output {
            const self = allocator.create(WindowOutput) catch unreachable;

            c.glfwDefaultWindowHints();
            c.glfwWindowHint(c.GLFW_FOCUSED, c.GLFW_FALSE);
            c.glfwWindowHint(c.GLFW_FLOATING, c.GLFW_TRUE);
            c.glfwWindowHint(c.GLFW_TRANSPARENT_FRAMEBUFFER, c.GL_TRUE);

            self.* = .{
                .constants = constants,
                .output = .{
                    .update_fn = update,
                    .destroy_fn = destroy,
                },
                .window = c.glfwCreateWindow(
                    config.width,
                    config.height,
                    "glsl-view output",
                    null,
                    constants.main_window,
                ) orelse {
                    std.debug.panic("unable to create output window\n", .{});
                },
            };

            c.glfwSetWindowUserPointer(self.window, @as(*anyopaque, @ptrCast(self)));
            _ = c.glfwSetKeyCallback(self.window, keyCallback);
            _ = c.glfwSetFramebufferSizeCallback(self.window, sizeCallback);

            c.glfwMakeContextCurrent(self.window);
            constants.normalized_quad.bind(0);

            return &self.output;
        }
    };

    output: Output,
    window: *c.GLFWwindow,
    constants: *gl.Constants,

    resized: bool = false,

    fn update(output: *Output, texture_id: c.GLuint) bool {
        const self: *WindowOutput = @fieldParentPtr("output", output);

        if (c.glfwWindowShouldClose(self.window) == c.GL_TRUE)
            return true;

        c.glfwMakeContextCurrent(self.window);
        c.glClearColor(0, 0, 0, 1);
        c.glClear(c.GL_COLOR_BUFFER_BIT);
        c.glfwWindowHint(c.GLFW_RESIZABLE, c.GLFW_FALSE);
        c.glfwWindowHint(c.GLFW_DECORATED, c.GLFW_FALSE);

        if (self.resized) {
            var width: c_int = undefined;
            var height: c_int = undefined;
            var scaled_width: c_int = undefined;
            var scaled_height: c_int = undefined;

            c.glfwGetFramebufferSize(self.window, &width, &height);
            const window_aspect = @as(f32, @floatFromInt(width)) / @as(f32, @floatFromInt(height));
            if (window_aspect >= self.constants.aspect) {
                scaled_height = height;
                scaled_width = @as(c_int, @intFromFloat(@as(f32, @floatFromInt(height)) * self.constants.aspect));
            } else {
                scaled_width = width;
                scaled_height = @as(c_int, @intFromFloat(@as(f32, @floatFromInt(width)) / self.constants.aspect));
            }

            c.glViewport(
                @divFloor(width - scaled_width, 2),
                @divFloor(height - scaled_height, 2),
                scaled_width,
                scaled_height,
            );
        }

        c.glBindTexture(c.GL_TEXTURE_2D, texture_id);
        c.glDrawArrays(c.GL_TRIANGLE_STRIP, 0, 4);

        self.constants.texture_shader.bind();
        self.constants.normalized_quad.draw();

        c.glfwSwapBuffers(self.window);
        return false;
    }

    fn destroy(output: *Output, allocator: std.mem.Allocator) void {
        const self: *WindowOutput = @fieldParentPtr("output", output);

        c.glfwDestroyWindow(self.window);
        allocator.destroy(self);
    }

    fn keyCallback(
        win: ?*c.GLFWwindow,
        key: c_int,
        scancode: c_int,
        action: c_int,
        mods: c_int,
    ) callconv(.C) void {
        if (action != c.GLFW_PRESS) return;
        const userptr = c.glfwGetWindowUserPointer(win).?;
        const self = @as(*WindowOutput, @ptrCast(@alignCast(userptr)));

        _ = self;
        _ = scancode;
        _ = mods;

        switch (key) {
            //        c.GLFW_KEY_F => // toggle fullscreen
            //        c.GLFW_KEY_LEFT => // cycle through monitors
            //        c.GLFW_KEY_RIGHT => // cycle through monitors
            else => {},
        }
    }

    fn sizeCallback(win: ?*c.GLFWwindow, width: c_int, height: c_int) callconv(.C) void {
        const userptr = c.glfwGetWindowUserPointer(win).?;
        const self = @as(*WindowOutput, @ptrCast(@alignCast(userptr)));
        self.resized = true;

        _ = width;
        _ = height;
    }
};