From 5b1b3a8c5e45f18d01644b9623ce3a88bf4c5ee0 Mon Sep 17 00:00:00 2001 From: Roman Pytkov Date: Tue, 3 Mar 2026 15:26:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BD=D0=B0=20i32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/render/cpu/ellipse.zig | 2 +- src/render/cpu/line.zig | 5 ++--- src/render/cpu/pipeline.zig | 35 +++++++++++++++++++---------------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/render/cpu/ellipse.zig b/src/render/cpu/ellipse.zig index 82d59c3..7fbcf7e 100644 --- a/src/render/cpu/ellipse.zig +++ b/src/render/cpu/ellipse.zig @@ -89,7 +89,7 @@ pub fn draw(ctx: *DrawContext, obj: *const Object) void { const ny = loc_y * inv_ry; const d = nx * nx + ny * ny; if (d >= d_inner_sq and d <= d_outer_sq) { - ctx.blendPixelAtBuffer(@intCast(bx), @intCast(by), stroke); + ctx.blendPixelAtBuffer(bx, by, stroke); } } } diff --git a/src/render/cpu/line.zig b/src/render/cpu/line.zig index bc871a6..5860eaa 100644 --- a/src/render/cpu/line.zig +++ b/src/render/cpu/line.zig @@ -162,9 +162,8 @@ fn drawLineInBuffer(ctx: *DrawContext, bx0: i32, by0: i32, bx1: i32, by1: i32, c while (thick <= half_thickness) { const x = if (use_vertical) x0 + thick else x0; const y = if (use_vertical) y0 else y0 + thick; - if (x >= 0 and y >= 0) { - ctx.blendPixelAtBuffer(@intCast(x), @intCast(y), color); - } + ctx.blendPixelAtBuffer(x, y, color); + thick += 1; } diff --git a/src/render/cpu/pipeline.zig b/src/render/cpu/pipeline.zig index 129d757..c00c138 100644 --- a/src/render/cpu/pipeline.zig +++ b/src/render/cpu/pipeline.zig @@ -134,9 +134,12 @@ pub const DrawContext = struct { /// Смешивает цвет в пикселе буфера с учётом opacity трансформа. В replace_mode просто перезаписывает пиксель. /// Если активен fill canvas, каждый записанный пиксель помечается как граница для заливки. - pub fn blendPixelAtBuffer(self: *DrawContext, bx: u32, by: u32, color: Color.PMA) void { - if (bx >= self.buf_width or by >= self.buf_height) return; - if (self._fill_canvas) |fc| fc.setBorder(bx, by); + pub fn blendPixelAtBuffer(self: *DrawContext, bx_i32: i32, by_i32: i32, color: Color.PMA) void { + if (self._fill_canvas) |fc| fc.setBorder(bx_i32, by_i32); + + if (bx_i32 < 0 or by_i32 < 0 or bx_i32 >= self.buf_width or by_i32 >= self.buf_height) return; + const bx: u32 = @intCast(bx_i32); + const by: u32 = @intCast(by_i32); const idx = by * self.buf_width + bx; const dst = &self.pixels[idx]; if (self.replace_mode) { @@ -184,7 +187,7 @@ pub const DrawContext = struct { const vw = @as(i32, @intCast(self.visible_rect.w)); const vh = @as(i32, @intCast(self.visible_rect.h)); if (bx < 0 or bx >= vw or by < 0 or by >= vh) return; - self.blendPixelAtBuffer(@intCast(bx), @intCast(by), color); + self.blendPixelAtBuffer(bx, by, color); } /// Начинает сбор границ для заливки: создаёт FillCanvas и при последующих вызовах blendPixelAtBuffer помечает пиксели как границу. @@ -238,8 +241,8 @@ const FillCanvas = struct { const Row = struct { pixels: []PixelType, - first_border_x: ?u32 = null, - last_border_x: ?u32 = null, + first_border_x: ?i32 = null, + last_border_x: ?i32 = null, }; pub fn init(allocator: std.mem.Allocator, width: u32, height: u32) !FillCanvas { @@ -273,26 +276,26 @@ const FillCanvas = struct { allocator.free(self._pixels); } - pub fn setBorder(self: *FillCanvas, x: u32, y: u32) void { - if (x >= self.buf_width or y >= self.buf_height) return; + pub fn setBorder(self: *FillCanvas, x: i32, y: i32) void { + if (x < 0 or y < 0 or x >= self.buf_width or y >= self.buf_height) return; - const row = &self.rows[y]; - row.pixels[x] = .Border; + const row = &self.rows[@intCast(y)]; + row.pixels[@intCast(x)] = .Border; if (row.first_border_x) |first| { if (x < first) { - row.first_border_x = x; + row.first_border_x = @intCast(x); } } else { - row.first_border_x = x; + row.first_border_x = @intCast(x); } if (row.last_border_x) |last| { if (x > last) { - row.last_border_x = x; + row.last_border_x = @intCast(x); } } else { - row.last_border_x = x; + row.last_border_x = @intCast(x); } } @@ -310,7 +313,7 @@ const FillCanvas = struct { var on_border = true; var segment_index: usize = 0; // индекс "промежутка" (счет с 0) - var x = first; + var x: u32 = @intCast(first); while (x <= last) : (x += 1) { if (row.pixels[x] == .Border) { on_border = true; @@ -321,7 +324,7 @@ const FillCanvas = struct { } if (!on_border and row.pixels[x] == .Empty and segment_index % 2 == 1) { row.pixels[x] = .Fill; - draw_ctx.blendPixelAtBuffer(x, y, color); + draw_ctx.blendPixelAtBuffer(@intCast(x), @intCast(y), color); } } }