1
0

center chip8 screen in the middle

This commit is contained in:
Rokas Puzonas 2024-01-21 11:48:15 +02:00
parent 3fb9ea38eb
commit d4bc5f7f10
2 changed files with 35 additions and 7 deletions

View File

@ -84,5 +84,22 @@ pub fn update(self: *Self, dt: f32) void {
pub fn draw(self: *Self) void {
rl.ClearBackground(rl.Color{ .r = 33, .g = 33, .b = 33 });
self.raylib_chip.render();
const display_width = self.chip.display_width;
const display_height = self.chip.display_height;
const screen_width = rl.GetScreenWidth();
const screen_height = rl.GetScreenHeight();
const x_scale = @as(f32, @floatFromInt(screen_width)) / @as(f32, @floatFromInt(display_width));
const y_scale = @as(f32, @floatFromInt(screen_height)) / @as(f32, @floatFromInt(display_height));
const max_scale = @min(x_scale, y_scale);
const render_width: i32 = @intFromFloat(@as(f32, @floatFromInt(display_width)) * max_scale);
const render_height: i32 = @intFromFloat(@as(f32, @floatFromInt(display_height)) * max_scale);
self.raylib_chip.render(
@divTrunc(screen_width - render_width, 2),
@divTrunc(screen_height - render_height, 2),
render_width,
render_height
);
}

View File

@ -83,14 +83,25 @@ pub fn update(self: *Self, dt: f32) void {
}
}
pub fn render(self: *const Self) void {
rl.DrawRectangle(0, 0, self.chip.display_width, self.chip.display_height, self.off_color);
pub fn render(self: *const Self, x: i32, y: i32, width: i32, height: i32) void {
const display_width = self.chip.display_width;
const display_height = self.chip.display_height;
const x_scale = @as(f32,@floatFromInt(width)) / @as(f32, @floatFromInt(display_width));
const y_scale = @as(f32,@floatFromInt(height)) / @as(f32, @floatFromInt(display_height));
for (0..self.chip.display_height) |y| {
for (0..self.chip.display_width) |x| {
if (self.chip.display_get(@intCast(x), @intCast(y))) {
rl.DrawRectangle(@intCast(x), @intCast(y), 1, 1, self.on_color);
rl.rlPushMatrix();
rl.rlTranslatef(@floatFromInt(x), @floatFromInt(y), 0);
rl.rlScalef(x_scale, y_scale, 1);
rl.DrawRectangle(0, 0, display_width, display_height, self.off_color);
for (0..display_height) |iy| {
for (0..display_width) |ix| {
if (self.chip.display_get(@intCast(ix), @intCast(iy))) {
rl.DrawRectangle(@intCast(ix), @intCast(iy), 1, 1, self.on_color);
}
}
}
rl.rlPopMatrix();
}