diff options
Diffstat (limited to 'src/output.zig')
| -rw-r--r-- | src/output.zig | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/output.zig b/src/output.zig new file mode 100644 index 0000000..8303107 --- /dev/null +++ b/src/output.zig @@ -0,0 +1,76 @@ +const c = @import("c.zig"); +const std = @import("std"); +const cfg = @import("config.zig"); + +pub const Output = struct { + update: fn (self: *Output) bool, + destroy: fn (self: *Output) void, + + pub fn init(update: fn (*Output) bool, destroy: fn (*Output) void) Output { + return Output{ + .update = update, + .destroy = destroy, + }; + } + + pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, share_window: *c.GLFWwindow) *Output { + return switch (config.type) { + cfg.OutputType.window => WindowOutput.create(allocator, config, share_window), + else => unreachable, + }; + } +}; + +pub const WindowOutput = struct { + output: Output, + window: *c.GLFWwindow, + + pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, share_window: *c.GLFWwindow) *Output { + const self = allocator.create(WindowOutput) catch unreachable; + + c.glfwDefaultWindowHints(); + + self.* = WindowOutput{ + .output = Output.init(update, destroy), + .window = c.glfwCreateWindow(config.width, config.height, "glsl-view output", null, share_window) orelse { + std.debug.panic("unable to create output window\n", .{}); + }, + }; + + c.glfwSetWindowUserPointer(self.*.window, @ptrCast(*c_void, self)); + _ = c.glfwSetKeyCallback(self.*.window, keyCallback); + + return &self.*.output; + } + + fn update(output: *Output) bool { + const self = @fieldParentPtr(WindowOutput, "output", output); + + if (c.glfwWindowShouldClose(self.*.window) == c.GL_TRUE) + return true; + + c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT | c.GL_STENCIL_BUFFER_BIT); + + c.glfwSwapBuffers(self.*.window); + return false; + } + + fn destroy(output: *Output) void { + const self = @fieldParentPtr(WindowOutput, "output", output); + + c.glfwDestroyWindow(self.*.window); + } + + extern 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 self = @ptrCast(*WindowOutput, @alignCast(@alignOf(WindowOutput), c.glfwGetWindowUserPointer(win).?)); + + std.debug.warn("key pressed: {}\n", .{ key }); + switch (key) { + // c.GLFW_KEY_F => // toggle fullscreen + // c.GLFW_KEY_LEFT => // cycle through monitors + // c.GLFW_KEY_RIGHT => // cycle through monitors + else => {}, + } + } +}; |
