diff options
| author | s-ol <s+removethis@s-ol.nu> | 2026-04-13 10:54:29 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2026-04-14 18:10:05 +0000 |
| commit | 0c0154b352c66d57ced9f4fd648106919c007572 (patch) | |
| tree | c057008b52011eeb7e4344b5611d51f8d0f7c0a0 /src/output.zig | |
| parent | fix some memory leaks (diff) | |
| download | glsl-view-main.tar.gz glsl-view-main.zip | |
Diffstat (limited to '')
| -rw-r--r-- | src/output.zig | 67 |
1 files changed, 62 insertions, 5 deletions
diff --git a/src/output.zig b/src/output.zig index d15c0d4..e9785d2 100644 --- a/src/output.zig +++ b/src/output.zig @@ -5,7 +5,7 @@ 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, + update_fn: *const fn (output: *Output, fbo: gl.FramebufferObject, fresh: bool) anyerror!?bool, destroy_fn: *const fn (output: *Output, allocator: std.mem.Allocator) void, pub fn create( @@ -22,8 +22,8 @@ pub const Output = struct { } } - pub fn update(self: *Output, texture_id: c.GLuint) bool { - return self.update_fn(self, texture_id); + pub fn update(self: *Output, fbo: gl.FramebufferObject, fresh: bool) anyerror!?bool { + return self.update_fn(self, fbo, fresh); } pub fn destroy(self: *Output, allocator: std.mem.Allocator) void { @@ -112,7 +112,7 @@ pub const WindowOutput = struct { resized: bool = false, - fn update(output: *Output, texture_id: c.GLuint) bool { + fn update(output: *Output, fbo: gl.FramebufferObject, _: bool) !?bool { const self: *WindowOutput = @fieldParentPtr("output", output); if (c.glfwWindowShouldClose(self.window) == c.GL_TRUE) @@ -152,7 +152,7 @@ pub const WindowOutput = struct { ); } - c.glBindTexture(c.GL_TEXTURE_2D, texture_id); + c.glBindTexture(c.GL_TEXTURE_2D, fbo.texture_id); c.glDrawArrays(c.GL_TRIANGLE_STRIP, 0, 4); self.constants.texture_shader.bind(); @@ -210,3 +210,60 @@ pub const WindowOutput = struct { _ = height; } }; + +pub const StdoutOutput = struct { + pub const Config = struct { + pub const default: Config = .{}; + + pub fn create( + _: *const Config, + allocator: std.mem.Allocator, + constants: *gl.Constants, + ) !*Output { + const self = allocator.create(StdoutOutput) catch unreachable; + + const buffer = try allocator.alloc(u8, @intCast(constants.config.width * constants.config.height * 4)); + errdefer allocator.free(buffer); + + const stdout = std.fs.File.stdout(); + + self.* = .{ + .output = .{ + .update_fn = update, + .destroy_fn = destroy, + }, + .config = constants.config, + .buffer = buffer, + .file = stdout, + }; + + return &self.output; + } + }; + + output: Output, + config: *cfg.Config, + buffer: []u8, + file: std.fs.File, + + fn update(output: *Output, fbo: gl.FramebufferObject, fresh: bool) !?bool { + const self: *StdoutOutput = @fieldParentPtr("output", output); + + if (fresh) { + fbo.bind(); + defer fbo.unbind(); + + fbo.read(self.config.width, self.config.height, self.buffer); + _ = try self.file.write(self.buffer); + } + + return null; + } + + fn destroy(output: *Output, allocator: std.mem.Allocator) void { + const self: *StdoutOutput = @fieldParentPtr("output", output); + + allocator.free(self.buffer); + allocator.destroy(self); + } +}; |
