Do something useful with the resize callback

This commit is contained in:
Brandon Dyck 2022-01-09 18:06:56 -07:00
parent 48cf77be08
commit 68e1402260

View File

@ -5,6 +5,7 @@ const max = std.math.max;
const Width = 800;
const Height = 600;
const AspectRatio = @intToFloat(comptime_float, Width) / @intToFloat(comptime_float, Height);
fn grey(value: u8) mfb.Rgb {
return mfb.Rgb{ .r = value, .g = value, .b = value };
@ -107,12 +108,25 @@ fn handleActive(win: mfb.Window(State), isActive: bool) callconv(.C) void {
}
fn handleResize(win: mfb.Window(State), width: i32, height: i32) callconv(.C) void {
const win_aspect_ratio = @intToFloat(f32, width) / @intToFloat(f32, height);
var vp_height: i32 = height;
var vp_width: i32 = width;
var vp_offset_x: i32 = 0;
var vp_offset_y: i32 = 0;
if (win_aspect_ratio > AspectRatio) {
vp_width = @floatToInt(i32, @intToFloat(f32, height) * AspectRatio);
vp_offset_x = @divTrunc(width - vp_width, 2);
} else {
vp_height = @floatToInt(i32, @intToFloat(f32, width) / AspectRatio);
vp_offset_y = @divTrunc(height - vp_height, 2);
}
const did_set = win.setViewport(
@intCast(u32, width) / 4,
@intCast(u32, height) / 4,
@intCast(u32, width) / 2,
@intCast(u32, height) / 2,
);
@intCast(u32, vp_offset_x),
@intCast(u32, vp_offset_y),
@intCast(u32, vp_width),
@intCast(u32, vp_height),
);
if (!did_set) {
std.log.warn("did not set viewport!", .{});
}