From d4bc5f7f1098c40c0e88b8e2b07bd335dc0fa137 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 21 Jan 2024 11:48:15 +0200 Subject: [PATCH] center chip8 screen in the middle --- src/main-scene.zig | 19 ++++++++++++++++++- src/raylib-chip.zig | 23 +++++++++++++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main-scene.zig b/src/main-scene.zig index 6c72bcd..aa6c459 100644 --- a/src/main-scene.zig +++ b/src/main-scene.zig @@ -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 + ); } diff --git a/src/raylib-chip.zig b/src/raylib-chip.zig index ae763c3..c710b74 100644 --- a/src/raylib-chip.zig +++ b/src/raylib-chip.zig @@ -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(); }