diff options
Diffstat (limited to 'src/output.zig')
| -rw-r--r-- | src/output.zig | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/src/output.zig b/src/output.zig index 8303107..077387c 100644 --- a/src/output.zig +++ b/src/output.zig @@ -1,21 +1,22 @@ const c = @import("c.zig"); const std = @import("std"); const cfg = @import("config.zig"); +const gl = @import("gl.zig"); pub const Output = struct { - update: fn (self: *Output) bool, - destroy: fn (self: *Output) void, + update: fn (*Output, c.GLuint) bool, + destroy: fn (*Output) void, - pub fn init(update: fn (*Output) bool, destroy: fn (*Output) void) Output { + pub fn init(update: fn (*Output, c.GLuint) 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 { + pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, constants: *gl.Constants) *Output { return switch (config.type) { - cfg.OutputType.window => WindowOutput.create(allocator, config, share_window), + cfg.OutputType.window => WindowOutput.create(allocator, config, constants), else => unreachable, }; } @@ -24,15 +25,17 @@ pub const Output = struct { pub const WindowOutput = struct { output: Output, window: *c.GLFWwindow, + constants: *gl.Constants, - pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, share_window: *c.GLFWwindow) *Output { + pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, constants: *gl.Constants) *Output { const self = allocator.create(WindowOutput) catch unreachable; c.glfwDefaultWindowHints(); self.* = WindowOutput{ + .constants = constants, .output = Output.init(update, destroy), - .window = c.glfwCreateWindow(config.width, config.height, "glsl-view output", null, share_window) orelse { + .window = c.glfwCreateWindow(config.width, config.height, "glsl-view output", null, constants.main_window) orelse { std.debug.panic("unable to create output window\n", .{}); }, }; @@ -40,17 +43,27 @@ pub const WindowOutput = struct { c.glfwSetWindowUserPointer(self.*.window, @ptrCast(*c_void, self)); _ = c.glfwSetKeyCallback(self.*.window, keyCallback); + c.glfwMakeContextCurrent(self.*.window); + constants.normalizedQuad.bind(0); + return &self.*.output; } - fn update(output: *Output) bool { + fn update(output: *Output, texture_id: c.GLuint) 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.glfwMakeContextCurrent(self.*.window); + c.glClear(c.GL_COLOR_BUFFER_BIT); + + c.glBindTexture(c.GL_TEXTURE_2D, texture_id); + c.glDrawArrays(c.GL_TRIANGLE_STRIP, 0, 4); + self.constants.textureShader.bind(); + self.constants.normalizedQuad.draw(); + c.glfwSwapBuffers(self.*.window); return false; } |
