Переход к RenderEngine

This commit is contained in:
2025-12-21 17:26:19 +03:00
parent 6f58967049
commit 3ac35a5046
11 changed files with 138 additions and 33 deletions

View File

@@ -1,18 +1,14 @@
const std = @import("std");
const builtin = @import("builtin");
const dvui = @import("dvui");
const Document = @import("models/Document.zig");
const RenderEngine = @import("render/RenderEngine.zig").RenderEngine;
const ImageRect = @import("models/rasterization_models.zig").ImageRect;
const Size = dvui.Size;
const Color = dvui.Color;
const Canvas = @This();
pub const ImageRect = struct {
x: u32,
y: u32,
w: u32,
h: u32,
};
allocator: std.mem.Allocator,
texture: ?dvui.Texture = null,
size: Size = .{ .w = 800, .h = 600 },
@@ -24,11 +20,16 @@ scroll: dvui.ScrollInfo = .{
native_scaling: bool = false,
gradient_start: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 255 },
gradient_end: Color.PMA = .{ .r = 255, .g = 255, .b = 255, .a = 255 },
document: ?*Document = null,
render_engine: RenderEngine,
_visible_rect: ?ImageRect = null,
_zoom: f32 = 1,
pub fn init(allocator: std.mem.Allocator) Canvas {
return .{ .allocator = allocator };
pub fn init(allocator: std.mem.Allocator, engine: RenderEngine) Canvas {
return .{
.allocator = allocator,
.render_engine = engine,
};
}
pub fn deinit(self: *Canvas) void {
@@ -39,7 +40,7 @@ pub fn deinit(self: *Canvas) void {
}
/// Заполнить canvas градиентом
pub fn redrawGradient(self: *Canvas) !void {
pub fn redrawExample(self: *Canvas) !void {
const full = self.getScaledImageSize();
const full_w: u32 = full.w;
const full_h: u32 = full.h;
@@ -57,10 +58,14 @@ pub fn redrawGradient(self: *Canvas) !void {
const width: u32 = vis.w;
const height: u32 = vis.h;
// 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;
@@ -95,14 +100,15 @@ pub fn redrawGradient(self: *Canvas) !void {
}
/// Заполнить canvas случайным градиентом
pub fn fillRandomGradient(self: *Canvas) !void {
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 };
try self.redrawGradient();
self.render_engine.exampleReset();
try self.redrawExample();
}
pub fn setZoom(self: *Canvas, value: f32) void {
@@ -136,7 +142,7 @@ pub fn updateVisibleImageRect(self: *Canvas, viewport: dvui.Rect, scroll_offset:
}
self._visible_rect = next;
if (changed or self.texture == null) {
try self.redrawGradient();
try self.redrawExample();
}
}
@@ -194,10 +200,3 @@ fn floatToClampedU32(value: f32, max_inclusive: u32) u32 {
if (value >= max_f) return max_inclusive;
return @intFromFloat(value);
}
/// Отобразить canvas в UI
pub fn render(self: Canvas, rect: dvui.RectScale) !void {
if (self.texture) |texture| {
try dvui.renderTexture(texture, rect, .{});
}
}