82 lines
2.1 KiB
Zig
82 lines
2.1 KiB
Zig
const Gfx = @import("./graphics.zig");
|
|
|
|
const Math = @import("lib").Math;
|
|
const Vec2 = Math.Vec2;
|
|
const Vec4 = Math.Vec4;
|
|
const rgb = Math.rgb;
|
|
|
|
const Lib = @import("lib");
|
|
const Frame = Lib.Frame;
|
|
|
|
const ScreenScalar = @This();
|
|
|
|
// TODO: Implement a fractional pixel perfect scalar
|
|
// Based on this video: https://www.youtube.com/watch?v=d6tp43wZqps
|
|
// And this blog: https://colececil.dev/blog/2017/scaling-pixel-art-without-destroying-it/
|
|
|
|
translation: Vec2,
|
|
scale: f32,
|
|
|
|
pub fn init(window_size: Vec2, canvas_size: Vec2) ScreenScalar {
|
|
// TODO: Render to a lower resolution instead of scaling.
|
|
// To avoid pixel bleeding in spritesheet artifacts
|
|
const scale = @floor(@min(
|
|
window_size.x / canvas_size.x,
|
|
window_size.y / canvas_size.y,
|
|
));
|
|
|
|
var translation: Vec2 = Vec2.sub(window_size, canvas_size.multiplyScalar(scale)).multiplyScalar(0.5);
|
|
translation.x = @round(translation.x);
|
|
translation.y = @round(translation.y);
|
|
|
|
return ScreenScalar{
|
|
.translation = translation,
|
|
.scale = scale
|
|
};
|
|
}
|
|
|
|
pub fn push(self: ScreenScalar, frame: *Frame) void {
|
|
const scale = self.scale;
|
|
const translation = self.translation;
|
|
|
|
frame.pushTransform(translation, .init(scale, scale));
|
|
}
|
|
|
|
pub fn pop(self: ScreenScalar, frame: *Frame, color: Vec4) void {
|
|
const translation = self.translation;
|
|
|
|
frame.popTransform();
|
|
|
|
frame.drawRectangle(.{
|
|
.rect = .{
|
|
.pos = .init(0, 0),
|
|
.size = .init(frame.screen_size.x, translation.y),
|
|
},
|
|
.color = color
|
|
});
|
|
|
|
frame.drawRectangle(.{
|
|
.rect = .{
|
|
.pos = .init(0, frame.screen_size.y - translation.y),
|
|
.size = .init(frame.screen_size.x, translation.y),
|
|
},
|
|
.color = color
|
|
});
|
|
|
|
frame.drawRectangle(.{
|
|
.rect = .{
|
|
.pos = .init(0, 0),
|
|
.size = .init(translation.x, frame.screen_size.y),
|
|
},
|
|
.color = color
|
|
});
|
|
|
|
frame.drawRectangle(.{
|
|
.rect = .{
|
|
.pos = .init(frame.screen_size.x - translation.x, 0),
|
|
.size = .init(translation.x, frame.screen_size.y),
|
|
},
|
|
.color = color
|
|
});
|
|
}
|