diff --git a/src/Canvas.zig b/src/Canvas.zig index 77b422d..cccb20f 100644 --- a/src/Canvas.zig +++ b/src/Canvas.zig @@ -109,54 +109,24 @@ pub fn updateVisibleImageRect(self: *Canvas, viewport: dvui.Rect, scroll_offset: fn computeVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) ImageRect { const image_rect = self.getScaledImageSize(); - const img_w_f: f32 = @floatFromInt(image_rect.w); - const img_h_f: f32 = @floatFromInt(image_rect.h); + const img_w: u32 = image_rect.w; + const img_h: u32 = image_rect.h; - const view_left: f32 = scroll_offset.x; - const view_top: f32 = scroll_offset.y; - const view_right: f32 = scroll_offset.x + viewport.w; - const view_bottom: f32 = scroll_offset.y + viewport.h; + // Видимый размер всегда равен размеру viewport, но не больше холста + const vis_w: u32 = @min(@as(u32, @intFromFloat(viewport.w)), img_w); + const vis_h: u32 = @min(@as(u32, @intFromFloat(viewport.h)), img_h); - const img_left: f32 = @floatFromInt(image_rect.x); - const img_top: f32 = @floatFromInt(image_rect.y); - const img_right: f32 = img_left + img_w_f; - const img_bottom: f32 = img_top + img_h_f; + // Вычисляем x и y на основе scroll_offset, clamped чтобы не выходить за границы + const raw_x: i64 = @intFromFloat(scroll_offset.x - @as(f32, @floatFromInt(image_rect.x))); + const raw_y: i64 = @intFromFloat(scroll_offset.y - @as(f32, @floatFromInt(image_rect.y))); - const inter_left: f32 = @max(view_left, img_left); - const inter_top: f32 = @max(view_top, img_top); - const inter_right: f32 = @min(view_right, img_right); - const inter_bottom: f32 = @min(view_bottom, img_bottom); + const vis_x: u32 = @intCast(std.math.clamp(raw_x, 0, @as(i64, img_w) - @as(i64, vis_w))); + const vis_y: u32 = @intCast(std.math.clamp(raw_y, 0, @as(i64, img_h) - @as(i64, vis_h))); - if (inter_right <= inter_left or inter_bottom <= inter_top) { - return .{ .x = 0, .y = 0, .w = 0, .h = 0 }; - } - - // Переводим из координат контента в локальные координаты картинки - const local_x0: f32 = inter_left - img_left; - const local_y0: f32 = inter_top - img_top; - const local_x1: f32 = inter_right - img_left; - const local_y1: f32 = inter_bottom - img_top; - - // Консервативно округляем до пикселей: начало вниз (floor), конец вверх (ceil) - const x0_u32: u32 = floatToClampedU32(@floor(local_x0), image_rect.w); - const y0_u32: u32 = floatToClampedU32(@floor(local_y0), image_rect.h); - const x1_u32: u32 = floatToClampedU32(@ceil(local_x1), image_rect.w); - const y1_u32: u32 = floatToClampedU32(@ceil(local_y1), image_rect.h); - - const out: ImageRect = .{ - .x = x0_u32, - .y = y0_u32, - .w = if (x1_u32 > x0_u32) x1_u32 - x0_u32 else 0, - .h = if (y1_u32 > y0_u32) y1_u32 - y0_u32 else 0, + return .{ + .x = vis_x, + .y = vis_y, + .w = vis_w, + .h = vis_h, }; - - return out; -} - -fn floatToClampedU32(value: f32, max_inclusive: u32) u32 { - if (max_inclusive == 0) return 0; - if (value <= 0) return 0; - const max_f: f32 = @floatFromInt(max_inclusive); - if (value >= max_f) return max_inclusive; - return @intFromFloat(value); } diff --git a/src/WindowContext.zig b/src/WindowContext.zig index da4a22b..84b3525 100644 --- a/src/WindowContext.zig +++ b/src/WindowContext.zig @@ -15,7 +15,7 @@ pub fn init(allocator: std.mem.Allocator) !WindowContext { self.cpu_render = try allocator.create(CpuRenderEngine); errdefer allocator.destroy(self.cpu_render); - self.cpu_render.* = CpuRenderEngine.init(allocator, .Gradient); + self.cpu_render.* = CpuRenderEngine.init(allocator, .Squares); self.canvas = Canvas.init(allocator, self.cpu_render.renderEngine()); diff --git a/src/main.zig b/src/main.zig index 3a4df5a..b86b9c6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -93,7 +93,7 @@ fn gui_frame(ctx: *WindowContext) bool { std.debug.print("Error filling canvas: {}\n", .{err}); }; ctx.canvas.pos = .{ .x = 400, .y = 400 }; - ctx.canvas.setZoom(dvui.windowNaturalScale()); + //ctx.canvas.setZoom(dvui.windowNaturalScale()); } if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {} } diff --git a/src/render/CpuRenderEngine.zig b/src/render/CpuRenderEngine.zig index 5b916c5..7aa9ba2 100644 --- a/src/render/CpuRenderEngine.zig +++ b/src/render/CpuRenderEngine.zig @@ -34,19 +34,7 @@ pub fn exampleReset(self: *CpuRenderEngine) void { self.gradient_end = Color.PMA{ .r = random.int(u8), .g = random.int(u8), .b = random.int(u8), .a = 255 }; } -pub fn example(self: CpuRenderEngine, canvas_size: ImageSize, visible_rect: ImageRect) !?dvui.Texture { - const full_w = canvas_size.w; - const full_h = canvas_size.h; - - const width = visible_rect.w; - const height = visible_rect.h; - - // Выделить буфер пиксельных данных - const pixels = try self._allocator.alloc(Color.PMA, @as(usize, width) * height); - defer self._allocator.free(pixels); - - std.debug.print("w={any}, fw={any};\th={any}, fh={any}\n", .{ width, full_w, height, full_h }); - +fn renderGradient(self: CpuRenderEngine, pixels: []Color.PMA, width: u32, height: u32, full_w: u32, full_h: u32, visible_rect: ImageRect) void { var y: u32 = 0; while (y < height) : (y += 1) { var x: u32 = 0; @@ -70,6 +58,52 @@ pub fn example(self: CpuRenderEngine, canvas_size: ImageSize, visible_rect: Imag pixels[y * width + x] = .{ .r = r, .g = g, .b = b, .a = 255 }; } } +} + +fn renderSquares(self: CpuRenderEngine, pixels: []Color.PMA, canvas_size: ImageSize, visible_rect: ImageRect) void { + _ = self; + + const squares_num = 5; + const thikness: u32 = @intFromFloat(@as(f32, @floatFromInt(canvas_size.w + canvas_size.h)) / 2 * 0.03); + const squares_sum_height = canvas_size.h - thikness * (squares_num + 1); + const square_height = squares_sum_height / squares_num; + + var y: u32 = 0; + while (y < visible_rect.h) { + const canvas_y = y + visible_rect.y; + var start_line_index = canvas_y / square_height; + + start_line_index = start_line_index * square_height + thikness * (if (start_line_index > 0) start_line_index - 1 else 0); + const draw_line = canvas_y < start_line_index + thikness and canvas_y >= start_line_index; + if (draw_line) { + var x: u32 = 0; + while (x < visible_rect.w) { + pixels[y * visible_rect.w + x] = .{ .r = 255, .b = 0, .g = 0, .a = 255 }; + + x += 1; + } + } + y += 1; + } +} + +pub fn example(self: CpuRenderEngine, canvas_size: ImageSize, visible_rect: ImageRect) !?dvui.Texture { + const full_w = canvas_size.w; + const full_h = canvas_size.h; + + const width = visible_rect.w; + const height = visible_rect.h; + + // Выделить буфер пиксельных данных + const pixels = try self._allocator.alloc(Color.PMA, @as(usize, width) * height); + defer self._allocator.free(pixels); + + std.debug.print("w={any}, fw={any};\th={any}, fh={any}\n", .{ width, full_w, height, full_h }); + + switch (self.type) { + .Gradient => self.renderGradient(pixels, width, height, full_w, full_h, visible_rect), + .Squares => self.renderSquares(pixels, canvas_size, visible_rect), + } return try dvui.textureCreate(pixels, width, height, .nearest); }