aboutsummaryrefslogtreecommitdiffstats
path: root/src/output.zig
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-01-26 19:49:25 +0000
committers-ol <s-ol@users.noreply.github.com>2020-01-26 19:49:25 +0000
commit885a8652cb8a433a18811d90048d11c1ec67b60e (patch)
tree52b845a825c91ec878983eb1078b4bd48f27ec73 /src/output.zig
parentremove offset default uniform (diff)
downloadglsl-view-885a8652cb8a433a18811d90048d11c1ec67b60e.tar.gz
glsl-view-885a8652cb8a433a18811d90048d11c1ec67b60e.zip
break OSC in favor of uniform caching
Diffstat (limited to 'src/output.zig')
-rw-r--r--src/output.zig49
1 files changed, 38 insertions, 11 deletions
diff --git a/src/output.zig b/src/output.zig
index c31c191..770c0ba 100644
--- a/src/output.zig
+++ b/src/output.zig
@@ -27,6 +27,8 @@ pub const WindowOutput = struct {
window: *c.GLFWwindow,
constants: *gl.Constants,
+ resized: bool = false,
+
pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, constants: *gl.Constants) *Output {
const self = allocator.create(WindowOutput) catch unreachable;
@@ -40,38 +42,58 @@ pub const WindowOutput = struct {
},
};
- c.glfwSetWindowUserPointer(self.*.window, @ptrCast(*c_void, self));
- _ = c.glfwSetKeyCallback(self.*.window, keyCallback);
+ c.glfwSetWindowUserPointer(self.window, @ptrCast(*c_void, self));
+ _ = c.glfwSetKeyCallback(self.window, keyCallback);
+ _ = c.glfwSetFramebufferSizeCallback(self.window, sizeCallback);
- c.glfwMakeContextCurrent(self.*.window);
- constants.normalizedQuad.bind(0);
+ c.glfwMakeContextCurrent(self.window);
+ constants.normalized_quad.bind(0);
- return &self.*.output;
+ return &self.output;
}
fn update(output: *Output, texture_id: c.GLuint) bool {
const self = @fieldParentPtr(WindowOutput, "output", output);
- if (c.glfwWindowShouldClose(self.*.window) == c.GL_TRUE)
+ if (c.glfwWindowShouldClose(self.window) == c.GL_TRUE)
return true;
- c.glfwMakeContextCurrent(self.*.window);
+ c.glfwMakeContextCurrent(self.window);
c.glClear(c.GL_COLOR_BUFFER_BIT);
+ 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 = @intToFloat(f32, width) / @intToFloat(f32, height);
+ if (window_aspect >= self.constants.aspect) {
+ scaled_height = height;
+ scaled_width = @floatToInt(c_int, @intToFloat(f32, height) * self.constants.aspect);
+ } else {
+ scaled_width = width;
+ scaled_height = @floatToInt(c_int, @intToFloat(f32, 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.textureShader.bind();
- self.constants.normalizedQuad.draw();
+ self.constants.texture_shader.bind();
+ self.constants.normalized_quad.draw();
- c.glfwSwapBuffers(self.*.window);
+ c.glfwSwapBuffers(self.window);
return false;
}
fn destroy(output: *Output) void {
const self = @fieldParentPtr(WindowOutput, "output", output);
- c.glfwDestroyWindow(self.*.window);
+ c.glfwDestroyWindow(self.window);
}
fn keyCallback(win: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void {
@@ -85,4 +107,9 @@ pub const WindowOutput = struct {
else => {},
}
}
+
+ fn sizeCallback(win: ?*c.GLFWwindow, width: c_int, height: c_int) callconv(.C) void {
+ const self = @ptrCast(*WindowOutput, @alignCast(@alignOf(WindowOutput), c.glfwGetWindowUserPointer(win).?));
+ self.resized = true;
+ }
};