diff --git a/.vscode/launch.json b/.vscode/launch.json index 91be301..c5017d3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,9 +12,10 @@ }, { "name": "Zig: Debug (gdb)", - "type": "gdb", + "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/zig-out/bin/Zivro", + "cwd": "${workspaceFolder}", "preLaunchTask": "zig: build" }, ] diff --git a/.vscode/tasks.json b/.vscode/tasks.json index fc2ef3f..f4a8325 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,12 +4,11 @@ { "label": "zig: build", "type": "shell", - "command": "zig build", + "command": "zig build -Doptimize=Debug", "group": { "kind": "build", "isDefault": true }, - "problemMatcher": ["$gcc"], } ] } diff --git a/src/Tool.zig b/src/Tool.zig index 64f2379..ad8f188 100644 --- a/src/Tool.zig +++ b/src/Tool.zig @@ -7,5 +7,5 @@ pub const ToolContext = struct { }; pub const Tool = struct { - onCanvasClick: *const fn (*const ToolContext) void, + onCanvasClick: *const fn (*const ToolContext) anyerror!void, }; diff --git a/src/WindowContext.zig b/src/WindowContext.zig index eedb590..77776de 100644 --- a/src/WindowContext.zig +++ b/src/WindowContext.zig @@ -3,7 +3,7 @@ const Canvas = @import("Canvas.zig"); const CpuRenderEngine = @import("render/CpuRenderEngine.zig"); const RenderEngine = @import("render/RenderEngine.zig").RenderEngine; const Document = @import("models/Document.zig"); -const random_document = @import("models/random_document.zig"); +const random_document = @import("random_document.zig"); const basic_models = @import("models/basic_models.zig"); const WindowContext = @This(); @@ -65,7 +65,7 @@ pub fn addNewDocument(self: *WindowContext) !void { const ptr = try self.allocator.create(OpenDocument); errdefer self.allocator.destroy(ptr); OpenDocument.init(self.allocator, ptr); - try random_document.addRandomShapes(&ptr.document, std.crypto.random); + //try random_document.addRandomShapes(&ptr.document, std.crypto.random); try self.documents.append(self.allocator, ptr); self.active_document_index = self.documents.items.len - 1; } diff --git a/src/models/Object.zig b/src/models/Object.zig index 51d6657..0c6b56f 100644 --- a/src/models/Object.zig +++ b/src/models/Object.zig @@ -17,7 +17,7 @@ const default_common_data = [_]PropertyData{ .{ .visible = true }, .{ .opacity = 1.0 }, .{ .locked = false }, - .{ .stroke_rgba = 0x000000FF }, + .{ .stroke_rgba = 0x000000FF }, // 0xRRGGBBAA: чёрный, полная непрозрачность .{ .thickness = 2.0 }, }; diff --git a/src/models/Property.zig b/src/models/Property.zig index eb30471..20ff542 100644 --- a/src/models/Property.zig +++ b/src/models/Property.zig @@ -19,7 +19,9 @@ pub const Data = union(enum) { points: std.ArrayList(Point2_f), + /// Цвет заливки: u32 в формате 0xRRGGBBAA (R старший байт, A младший). fill_rgba: u32, + /// Цвет обводки: u32 в формате 0xRRGGBBAA (R старший байт, A младший). stroke_rgba: u32, thickness: f32, diff --git a/src/models/random_document.zig b/src/random_document.zig similarity index 93% rename from src/models/random_document.zig rename to src/random_document.zig index 2a3e1f6..f98834f 100644 --- a/src/models/random_document.zig +++ b/src/random_document.zig @@ -1,8 +1,8 @@ const std = @import("std"); -const Document = @import("Document.zig"); +const Document = @import("models/Document.zig"); const Object = Document.Object; -const shape = @import("shape/shape.zig"); -const basic_models = @import("basic_models.zig"); +const shape = @import("models/shape/shape.zig"); +const basic_models = @import("models/basic_models.zig"); const Size_f = basic_models.Size_f; const Point2_f = basic_models.Point2_f; const Scale2_f = basic_models.Scale2_f; @@ -12,12 +12,13 @@ fn randFloat(rng: std.Random, min: f32, max: f32) f32 { return min + (max - min) * rng.float(f32); } +/// Цвет в u32, порядок 0xRRGGBBAA (R старший, A младший; чёрный = 0x000000FF). fn randRgba(rng: std.Random) u32 { const r = rng.int(u8); const g = rng.int(u8); const b = rng.int(u8); const a: u8 = @intCast(rng.intRangeLessThan(usize, 128, 256)); - return r | (@as(u32, g) << 8) | (@as(u32, b) << 16) | (@as(u32, a) << 24); + return (@as(u32, r) << 24) | (@as(u32, g) << 16) | (@as(u32, b) << 8) | a; } fn randomShapeKind(rng: std.Random) Object.ShapeKind { diff --git a/src/render/cpu/pipeline.zig b/src/render/cpu/pipeline.zig index ba282f2..49f3c0b 100644 --- a/src/render/cpu/pipeline.zig +++ b/src/render/cpu/pipeline.zig @@ -142,11 +142,12 @@ pub const DrawContext = struct { } }; +/// u32 в порядке 0xRRGGBBAA: старший байт R, младший A (чёрный = 0x000000FF). pub fn rgbaToPma(rgba: u32) Color.PMA { - const r: u8 = @intCast((rgba >> 0) & 0xFF); - const g: u8 = @intCast((rgba >> 8) & 0xFF); - const b: u8 = @intCast((rgba >> 16) & 0xFF); - const a: u8 = @intCast((rgba >> 24) & 0xFF); + const r: u8 = @intCast((rgba >> 24) & 0xFF); + const g: u8 = @intCast((rgba >> 16) & 0xFF); + const b: u8 = @intCast((rgba >> 8) & 0xFF); + const a: u8 = @intCast((rgba >> 0) & 0xFF); if (a == 0) return .{ .r = 0, .g = 0, .b = 0, .a = 0 }; const af: f32 = @as(f32, @floatFromInt(a)) / 255.0; return .{ diff --git a/src/tools/line.zig b/src/tools/line.zig index f3598d1..8e8bae5 100644 --- a/src/tools/line.zig +++ b/src/tools/line.zig @@ -1,19 +1,14 @@ +const std = @import("std"); const Canvas = @import("../Canvas.zig"); const Tool = @import("../Tool.zig"); const shape = @import("../models/shape/shape.zig"); -fn onCanvasClick(ctx: *const Tool.ToolContext) void { +fn onCanvasClick(ctx: *const Tool.ToolContext) !void { const canvas = ctx.canvas; var obj = shape.createObject(canvas.document.allocator, .line) catch return; defer obj.deinit(canvas.allocator); - obj.setProperty(canvas.document.allocator, .{ .data = .{ .position = ctx.document_point } }) catch { - obj.deinit(canvas.document.allocator); - return; - }; - canvas.document.addObject(obj) catch { - return; - }; + try obj.setProperty(canvas.document.allocator, .{ .data = .{ .position = ctx.document_point } }); + try canvas.document.addObject(obj); canvas.requestRedraw(); } - pub const tool = Tool.Tool{ .onCanvasClick = onCanvasClick }; diff --git a/src/ui/canvas_view.zig b/src/ui/canvas_view.zig index 5fa3c1e..e2e6141 100644 --- a/src/ui/canvas_view.zig +++ b/src/ui/canvas_view.zig @@ -167,7 +167,9 @@ fn handleCanvasMouse(canvas: *Canvas, scroll: anytype) void { .canvas = canvas, .document_point = point, }; - desc.implementation.onCanvasClick(&ctx); + desc.implementation.onCanvasClick(&ctx) catch |err| { + std.debug.print("onCanvasClick error: {}\n", .{err}); + }; } } },