aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2025-09-27 08:21:33 +0000
committers-ol <s+removethis@s-ol.nu>2025-09-27 08:21:33 +0000
commit1de0e8c307a8593bf46cae36433bf770fafd24e2 (patch)
treed8379a5609dff113dee3bb7c9771e22cde696b6a /src
parentlog fps every 64 frames (diff)
downloadglsl-view-1de0e8c307a8593bf46cae36433bf770fafd24e2.tar.gz
glsl-view-1de0e8c307a8593bf46cae36433bf770fafd24e2.zip
add toggleable crop mode
Diffstat (limited to 'src')
-rw-r--r--src/output.zig64
1 files changed, 45 insertions, 19 deletions
diff --git a/src/output.zig b/src/output.zig
index 87dc311..d15c0d4 100644
--- a/src/output.zig
+++ b/src/output.zig
@@ -37,10 +37,19 @@ pub const WindowOutput = struct {
nearest = c.GL_NEAREST,
linear = c.GL_LINEAR,
};
+ const CropMode = enum(u32) {
+ contain,
+ cover,
+ center,
+ natural,
+
+ pub const last: CropMode = .natural;
+ };
width: i32,
height: i32,
filter: FilterMode,
+ crop: CropMode,
monitor: []const u8,
fullscreen: bool,
@@ -49,6 +58,7 @@ pub const WindowOutput = struct {
.width = 800,
.height = 600,
.filter = .linear,
+ .crop = .contain,
.monitor = "",
.fullscreen = false,
};
@@ -80,6 +90,8 @@ pub const WindowOutput = struct {
) orelse {
std.debug.panic("unable to create output window\n", .{});
},
+
+ .crop = config.crop,
};
c.glfwSetWindowUserPointer(self.window, @as(*anyopaque, @ptrCast(self)));
@@ -96,6 +108,7 @@ pub const WindowOutput = struct {
output: Output,
window: *c.GLFWwindow,
constants: *gl.Constants,
+ crop: Config.CropMode,
resized: bool = false,
@@ -112,26 +125,30 @@ pub const WindowOutput = struct {
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));
- }
+ var wwidth: c_int = undefined;
+ var wheight: c_int = undefined;
+ c.glfwGetFramebufferSize(self.window, &wwidth, &wheight);
+ const window_width: f32 = @floatFromInt(wwidth);
+ const window_height: f32 = @floatFromInt(wheight);
+
+ const width: f32 = @floatFromInt(self.constants.config.width);
+ const height: f32 = @floatFromInt(self.constants.config.height);
+
+ const width_scale = window_width / width;
+ const height_scale = window_height / height;
+
+ const scale: f32 = switch (self.crop) {
+ .contain => @min(width_scale, height_scale),
+ .cover => @max(width_scale, height_scale),
+ .center => 1.0,
+ .natural => 1.0 / @ceil(1.0 / @min(width_scale, height_scale)),
+ };
c.glViewport(
- @divFloor(width - scaled_width, 2),
- @divFloor(height - scaled_height, 2),
- scaled_width,
- scaled_height,
+ @intFromFloat((window_width - width * scale) / 2.0),
+ @intFromFloat((window_height - height * scale) / 2.0),
+ @intFromFloat(width * scale),
+ @intFromFloat(height * scale),
);
}
@@ -163,7 +180,6 @@ pub const WindowOutput = struct {
const userptr = c.glfwGetWindowUserPointer(win).?;
const self = @as(*WindowOutput, @ptrCast(@alignCast(userptr)));
- _ = self;
_ = scancode;
_ = mods;
@@ -171,6 +187,16 @@ pub const WindowOutput = struct {
// c.GLFW_KEY_F => // toggle fullscreen
// c.GLFW_KEY_LEFT => // cycle through monitors
// c.GLFW_KEY_RIGHT => // cycle through monitors
+ c.GLFW_KEY_C => {
+ self.resized = true;
+ self.crop = switch (self.crop) {
+ .contain => .cover,
+ .cover => .center,
+ .center => .natural,
+ .natural => .contain,
+ };
+ std.debug.print("crop mode: {}\n", .{self.crop});
+ },
else => {},
}
}