Переход на CpuRenderEngine

This commit is contained in:
2025-12-21 17:37:43 +03:00
parent 3ac35a5046
commit d640269cad
4 changed files with 52 additions and 55 deletions

View File

@@ -35,10 +35,43 @@ pub fn exampleReset(self: *CpuRenderEngine) void {
}
pub fn example(self: CpuRenderEngine, canvas_size: ImageSize, visible_rect: ImageRect) !?dvui.Texture {
_ = self;
_ = canvas_size;
_ = visible_rect;
return null;
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 });
var y: u32 = 0;
while (y < height) : (y += 1) {
var x: u32 = 0;
while (x < width) : (x += 1) {
const gx: u32 = visible_rect.x + x;
const gy: u32 = visible_rect.y + y;
const denom_x: f32 = if (full_w > 1) @as(f32, @floatFromInt(full_w - 1)) else 1;
const denom_y: f32 = if (full_h > 1) @as(f32, @floatFromInt(full_h - 1)) else 1;
const fx: f32 = @as(f32, @floatFromInt(gx)) / denom_x;
const fy: f32 = @as(f32, @floatFromInt(gy)) / denom_y;
const factor: f32 = std.math.clamp((fx + fy) / 2, 0, 1);
const r_f: f32 = @as(f32, @floatFromInt(self.gradient_start.r)) + factor * (@as(f32, @floatFromInt(self.gradient_end.r)) - @as(f32, @floatFromInt(self.gradient_start.r)));
const g_f: f32 = @as(f32, @floatFromInt(self.gradient_start.g)) + factor * (@as(f32, @floatFromInt(self.gradient_end.g)) - @as(f32, @floatFromInt(self.gradient_start.g)));
const b_f: f32 = @as(f32, @floatFromInt(self.gradient_start.b)) + factor * (@as(f32, @floatFromInt(self.gradient_end.b)) - @as(f32, @floatFromInt(self.gradient_start.b)));
const r: u8 = @intFromFloat(std.math.clamp(r_f, 0, 255));
const g: u8 = @intFromFloat(std.math.clamp(g_f, 0, 255));
const b: u8 = @intFromFloat(std.math.clamp(b_f, 0, 255));
pixels[y * width + x] = .{ .r = r, .g = g, .b = b, .a = 255 };
}
}
return try dvui.textureCreate(pixels, width, height, .nearest);
}
pub fn renderEngine(self: *CpuRenderEngine) RenderEngine {