Переход на 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

@@ -42,8 +42,6 @@ pub fn deinit(self: *Canvas) void {
/// Заполнить canvas градиентом
pub fn redrawExample(self: *Canvas) !void {
const full = self.getScaledImageSize();
const full_w: u32 = full.w;
const full_h: u32 = full.h;
const vis: ImageRect = self._visible_rect orelse ImageRect{ .x = 0, .y = 0, .w = 0, .h = 0 };
@@ -55,58 +53,20 @@ pub fn redrawExample(self: *Canvas) !void {
return;
}
const width: u32 = vis.w;
const height: u32 = vis.h;
const new_texture = self.render_engine.example(.{ .w = full.w, .h = full.h }, vis) catch null;
// const new_texture = self.render_engine.example(width, height);
// Выделить буфер пиксельных данных
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 = vis.x + x;
const gy: u32 = vis.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 };
if (new_texture) |tex| {
// Удалить старую текстуру
if (self.texture) |old_tex| {
dvui.Texture.destroyLater(old_tex);
}
}
// Удалить старую текстуру
if (self.texture) |tex| {
dvui.Texture.destroyLater(tex);
self.texture = tex;
}
// Создать новую текстуру из пиксельных данных
self.texture = try dvui.textureCreate(pixels, width, height, .nearest);
}
/// Заполнить canvas случайным градиентом
// Ресетнуть example изображение в renderEngine
pub fn exampleReset(self: *Canvas) !void {
// Сгенерировать случайные цвета градиента
var prng = std.Random.DefaultPrng.init(@intCast(std.time.microTimestamp()));
const random = prng.random();
self.gradient_start = Color.PMA{ .r = random.int(u8), .g = random.int(u8), .b = random.int(u8), .a = 255 };
self.gradient_end = Color.PMA{ .r = random.int(u8), .g = random.int(u8), .b = random.int(u8), .a = 255 };
self.render_engine.exampleReset();
try self.redrawExample();
}