правильное определение видимой области

This commit is contained in:
2025-12-21 19:22:38 +03:00
parent d640269cad
commit 070c8bad2d
4 changed files with 64 additions and 60 deletions

View File

@@ -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);
}