papyrus compiles

This commit is contained in:
peterino2 2025-10-15 22:19:03 -07:00
parent cd27fcdb76
commit 1916c46e14
16 changed files with 96 additions and 75 deletions

View File

@ -87,6 +87,7 @@ pub const createNameRegistry = p2.createNameRegistry;
pub const destroyNameRegistry = p2.destroyNameRegistry;
pub const setNameRegistry = p2.setNameRegistry;
pub const NameRegistry = p2.NameRegistry;
pub const NameInvalid = p2.NameInvalid;
pub const MergedSpans = p2.MergedSpans;
pub const Span = p2.Span;
@ -97,6 +98,7 @@ pub const math = @import("math.zig");
pub const Mat = math.Mat;
pub const Transform = math.Transform;
pub const Quat = math.Quat;
pub const Vector2i = math.Vector2i;
pub const Vectorf = math.Vectorf;
pub const Vector = math.Vector;
pub const Vector4 = math.Vector4;

View File

@ -24,9 +24,11 @@ pub fn build(b: *std.Build) void {
const test_step = b.step("test", "run unit tests for net");
const tests = b.addTest(.{
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("tests/tests.zig"),
}),
});
tests.root_module.addImport("net", mod);

View File

@ -8,4 +8,5 @@
.paths = .{
"",
},
.fingerprint = 0xf2ea15fffd5f7fbc,
}

View File

@ -21,10 +21,12 @@ pub fn build(b: *std.Build) void {
// Creates a step for unit testing.
const main_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("tests/testing.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
main_tests.root_module.addImport("core", core_dep.module("core"));

View File

@ -53,9 +53,9 @@ pub fn render(self: *@This()) !void {
var timer = try std.time.Timer.start();
const tstart = timer.read();
var drawList = papyrus.DrawList.init(self.ui.allocator);
try self.ui.makeDrawList(&drawList, &self.arena);
defer drawList.deinit();
var drawList: papyrus.DrawList = .{};
try self.ui.makeDrawList(self.allocator, &drawList, &self.arena);
defer drawList.deinit(self.allocator);
const tend = timer.read();
const duration = (@as(f64, @floatFromInt(tend - tstart)) / 1000);
@ -266,10 +266,9 @@ pub const BmpWriter = struct {
cwd.makePath("Saved") catch unreachable;
var logFile = try cwd.createFile(outFile, .{});
defer logFile.close();
var writer = logFile.writer();
try writer.writeAll(std.mem.asBytes(&header));
try writer.writeAll(std.mem.asBytes(&info));
try writer.writeAll(self.pixelBuffer);
try logFile.writeAll(std.mem.asBytes(&header));
try logFile.writeAll(std.mem.asBytes(&info));
try logFile.writeAll(self.pixelBuffer);
}
pub fn deinit(self: *@This()) void {

View File

@ -2,12 +2,14 @@ ctx: *Context,
node: NodeHandle,
n: *const papyrus.Node,
drawList: *DrawList,
allocator: std.mem.Allocator,
parentInfo: LayoutInfo,
resolvedSize: Vector2f = .{},
resolvedPos: Vector2f = .{},
const papyrus = @import("papyrus.zig");
const core = @import("core");
const std = @import("std");
const Context = papyrus.Context;
const LayoutInfo = papyrus.LayoutInfo;

View File

@ -1,11 +1,13 @@
const std = @import("std");
const c = @import("c.zig");
const c = @import("c.zig").c;
const core = @import("core");
const Vector2i = core.Vector2i;
const Vector2f = core.Vector2f;
const Name = core.Name;
const loadFileAlloc = utils.loadFileAlloc;
const colors = core.colors;
const ColorRGBA8 = colors.ColorRGBA8;
const Color = colors.Color;
@ -13,7 +15,6 @@ const Color = colors.Color;
const LocText = @import("localization.zig").LocText;
const utils = @import("utils.zig");
const loadFileAlloc = utils.loadFileAlloc;
const BmpWriter = @import("BmpRenderer.zig").BmpWriter;
name: Name,
@ -84,7 +85,7 @@ pub const FontAtlas = struct {
const rv = try initEmbeddedFont(allocator, bytes, fontSize, opts);
core.engine_log("[Fontcache] creating font cache for {s} font", .{fontName});
var cachedFontArchive = std.ArrayList(u8).init(allocator);
var cachedFontArchive = std.io.Writer.Allocating.init(allocator);
defer cachedFontArchive.deinit();
try rv.saveToArchive(&cachedFontArchive);
@ -93,12 +94,16 @@ pub const FontAtlas = struct {
const file = try std.fs.cwd().createFile(cacheFile, .{});
defer file.close();
try file.writeAll(cachedFontArchive.items);
var list = cachedFontArchive.toArrayList();
defer list.deinit(allocator);
try file.writeAll(list.items);
return rv;
};
core.engine_log("[Fontcache] loading font cache for {s} font", .{fontName});
const archiveBytes = try loadFileAlloc(cacheFile, 8, allocator);
const archiveBytes = try utils.loadFileAlloc(cacheFile, .@"8", allocator); //(cacheFile, 8, allocator);
defer allocator.free(archiveBytes);
var rv = try initFromArchive(allocator, archiveBytes);
@ -149,7 +154,7 @@ pub const FontAtlas = struct {
var self = @This(){
.allocator = allocator,
.filePath = file,
.fileContent = try loadFileAlloc(file, 8, allocator),
.fileContent = try loadFileAlloc(file, .@"8", allocator),
.atlasBuffer = null,
.fontSize = fontSize,
.isSDF = true,
@ -182,7 +187,7 @@ pub const FontAtlas = struct {
var self = @This(){
.allocator = allocator,
.filePath = file,
.fileContent = try loadFileAlloc(file, 8, allocator),
.fileContent = try loadFileAlloc(file, .@"8", allocator),
.atlasBuffer = null,
.fontSize = fontSize,
.isMonospace = opts.isMonospace,
@ -211,27 +216,26 @@ pub const FontAtlas = struct {
return self;
}
pub fn saveToArchive(self: @This(), out: *std.ArrayList(u8)) !void {
var writer = out.writer();
pub fn saveToArchive(self: @This(), o: *std.io.Writer.Allocating) !void {
const writer = &o.writer;
try writer.writeInt(u8, @intFromBool(self.isSDF), .little); // isSDF
try writer.writeInt(u32, @bitCast(self.fontSize), .little); // fontSize: f32 = 0,
try writer.writeStruct(self.atlasSize); // atlasSize: Vector2i = .{},
try writer.writeStruct(self.glyphMax); // glyphMax: Vector2i = .{},
try writer.writeStruct(self.atlasSize, .little); // atlasSize: Vector2i = .{},
try writer.writeStruct(self.glyphMax, .little); // glyphMax: Vector2i = .{},
try writer.writeInt(i32, self.glyphStride, .little); // glyphStride: i32 = 0,
try writer.writeInt(u32, @bitCast(self.scale), .little); // scale: f32 = 0,
try writer.writeInt(u32, @bitCast(self.lineSize), .little); // lineSize: f32 = 0,
try writer.writeInt(u8, @intFromBool(self.isMonospace), .little); // isMonospace: bool = false,
for (self.glyphMetrics) |x| { // glyphMetrics: [256]Vector2i = undefined,
try writer.writeStruct(x);
try writer.writeStruct(x, .little);
}
for (self.glyphBox0) |x| { // glyphBox0: [256]Vector2i = undefined,
try writer.writeStruct(x);
try writer.writeStruct(x, .little);
}
for (self.glyphBox1) |x| { // glyphBox1: [256]Vector2i = undefined,
try writer.writeStruct(x);
try writer.writeStruct(x, .little);
}
for (self.hasGlyph) |x| { // hasGlyph: [256]bool = undefined,
@ -240,13 +244,13 @@ pub const FontAtlas = struct {
for (self.meshes) |x| { // meshes: [256][4]Vector2f = undefined,
for (x) |y| {
try writer.writeStruct(y);
try writer.writeStruct(y, .little);
}
}
for (self.glyphCoordinates) |x| { // glyphCoordinates: [256][2]Vector2f = undefined,
for (x) |y| {
try writer.writeStruct(y);
try writer.writeStruct(y, .little);
}
}

View File

@ -66,13 +66,14 @@ pub fn deinit(self: *@This()) void {
pub fn addMousePickInfo(
self: @This(),
drawListAllocator: std.mem.Allocator,
papyrusCtx: *const Context,
drawList: *papyrus.DrawList,
) !void {
const selected_node = self.selected_node orelse return;
const layoutInfo = papyrusCtx._displayLayout.items[selected_node.index];
try drawList.append(.{
try drawList.append(drawListAllocator, .{
.node = .{},
.primitive = .{
.Rect = .{

View File

@ -1,3 +1,3 @@
pub usingnamespace @cImport({
pub const c = @cImport({
@cInclude("stb_ttf.h");
});

View File

@ -478,13 +478,13 @@ pub const Context = struct {
.defaultBitmapFont = ctx.fontCache.defaultBitmapFont,
.textEntry = try TextEntrySystem.create(self, allocator),
._drawOrder = DrawOrderList.init(allocator),
._drawOrder = .{},
._layout = .{},
._layoutNodes = .{},
._horizontalLayout = .{},
.debugText = std.ArrayList(DebugLogTextEntry).init(allocator),
.debugText = .{},
.rootNodes = .{},
.mousePick = Layout.init(allocator),
.rootNodes = std.ArrayList(NodeHandle).init(allocator),
.stringArena = std.heap.ArenaAllocator.init(allocator),
};
@ -503,7 +503,7 @@ pub const Context = struct {
pub fn pushDebugTextSlice(self: *@This(), slice: []const u8, _color: ?Color) !void {
const color = if (_color == null) Color.Yellow else _color.?;
if (self.debugText.items.len < debugTextMax) {
try self.debugText.append(.{
try self.debugText.append(self.allocator, .{
.text = self.stringArena.allocator().dupe(slice) catch "",
.color = color,
});
@ -514,7 +514,7 @@ pub const Context = struct {
const color = if (_color == null) Color.Yellow else _color.?;
if (self.debugText.items.len < debugTextMax) {
try self.debugText.append(.{
try self.debugText.append(self.allocator, .{
.text = std.fmt.allocPrint(self.stringArena.allocator(), fmt, args) catch "",
.color = color,
});
@ -538,7 +538,7 @@ pub const Context = struct {
}
}
}
self.rootNodes.deinit();
self.rootNodes.deinit(self.allocator);
self.mousePick.deinit();
@ -546,7 +546,7 @@ pub const Context = struct {
self.nodes.deinit();
self.events.deinit();
self._drawOrder.deinit();
self._drawOrder.deinit(self.allocator);
self._layout.deinit(self.allocator);
self._layoutNodes.deinit(self.allocator);
@ -558,7 +558,7 @@ pub const Context = struct {
for (self.debugText.items) |text| {
_ = text;
}
self.debugText.deinit();
self.debugText.deinit(self.allocator);
self.allocator.destroy(self);
}
@ -722,7 +722,8 @@ pub const Context = struct {
pub fn addTextEntry_experimental(self: *@This(), parent: NodeHandle, text: ?[]const u8) !NodeHandle {
var textProperty = NodeProperty_TextEntry{
.editText = std.ArrayList(u8).init(self.allocator),
.editText = .{},
.allcator = self.allocator,
.font = self.defaultFont,
};
@ -780,8 +781,8 @@ pub const Context = struct {
try assertf(node.index != 0, "removeFromParent CANNOT be called on the root node", .{});
var killList = std.ArrayList(NodeHandle).init(self.allocator);
defer killList.deinit();
var killList = std.ArrayList(NodeHandle){};
defer killList.deinit(self.allocator);
self.walkNodesToRemove(node, &killList) catch {
for (killList.items) |n| {
_ = n;
@ -818,7 +819,7 @@ pub const Context = struct {
}
fn walkNodesToRemove(self: @This(), root: NodeHandle, killList: *std.ArrayList(NodeHandle)) !void {
try killList.append(root);
try killList.append(self.allocator, root);
var next = self.getRead(root).child;
@ -837,7 +838,7 @@ pub const Context = struct {
if (self.getRead(next).state != .Visible) {
continue;
}
try list.append(next);
try list.append(self.allocator, next);
try self.assembleDrawOrderListForNode(next, list);
}
}
@ -850,7 +851,7 @@ pub const Context = struct {
if (self.getRead(next).state != .Visible) {
continue;
}
try self.rootNodes.append(next);
try self.rootNodes.append(self.allocator, next);
}
const SortFunc = struct {
@ -867,7 +868,7 @@ pub const Context = struct {
);
for (self.rootNodes.items) |rootNode| {
try list.append(rootNode);
try list.append(self.allocator, rootNode);
try self.assembleDrawOrderListForNode(rootNode, list);
}
}
@ -1016,7 +1017,7 @@ pub const Context = struct {
};
}
pub fn makeDrawList(self: *@This(), drawList: *DrawList, stringArena: *std.heap.ArenaAllocator) !void {
pub fn makeDrawList(self: *@This(), drawlistAllocator: std.mem.Allocator, drawList: *DrawList, stringArena: *std.heap.ArenaAllocator) !void {
_ = stringArena.reset(.retain_capacity);
// do not re allocate these, instead use a preallocated pool
@ -1050,6 +1051,7 @@ pub const Context = struct {
var dlb = DrawListBuilder{
.ctx = self,
.allocator = drawlistAllocator,
.node = node,
.drawList = drawList,
.n = n,
@ -1093,7 +1095,7 @@ pub const Context = struct {
if (panel.hasTitle) {
// draw the main image.
try drawList.append(.{ .node = node, .primitive = .{
try drawList.append(drawlistAllocator, .{ .node = node, .primitive = .{
.Rect = .{
.tl = dlb.resolvedPos.add(.{ .y = panel.titleSize }),
.size = dlb.resolvedSize.sub(.{ .y = panel.titleSize }),
@ -1108,7 +1110,7 @@ pub const Context = struct {
} });
// draw the title bar
try drawList.append(.{ .node = node, .primitive = .{
try drawList.append(drawlistAllocator, .{ .node = node, .primitive = .{
.Rect = .{
.tl = dlb.resolvedPos,
.size = .{ .x = dlb.resolvedSize.x, .y = panel.titleSize },
@ -1121,7 +1123,7 @@ pub const Context = struct {
},
} });
try drawList.append(.{
try drawList.append(drawlistAllocator, .{
.node = node,
.primitive = .{
.Text = .{
@ -1149,7 +1151,7 @@ pub const Context = struct {
} else {
// draw the main image
try drawList.append(.{ .node = node, .primitive = .{
try drawList.append(drawlistAllocator, .{ .node = node, .primitive = .{
.Rect = .{
.tl = dlb.resolvedPos,
.size = dlb.resolvedSize,
@ -1174,7 +1176,7 @@ pub const Context = struct {
}
},
.DisplayText => |txt| {
try drawList.append(.{
try drawList.append(drawlistAllocator, .{
.node = node,
.primitive = .{
.Text = .{
@ -1212,11 +1214,11 @@ pub const Context = struct {
if (self.debugEnabled) {
if (self.drawDebug) {
try self.addDebugInfo(drawList);
try self.addDebugInfo(drawlistAllocator, drawList);
}
if (self.debugMousePick) {
try self.mousePick.addMousePickInfo(self, drawList);
try self.mousePick.addMousePickInfo(drawlistAllocator, self, drawList);
}
// if (self.debugLogText.items.len) {
@ -1227,7 +1229,7 @@ pub const Context = struct {
for (0..drawList.items.len) |i| {
switch (drawList.items[i].primitive) {
.Text => |*text| {
text.text.utf8 = try core.dupeString(stringArena.allocator(), text.text.utf8);
text.text.utf8 = try stringArena.allocator().dupe(u8, text.text.utf8);
},
.Rect => {},
}
@ -1261,7 +1263,7 @@ pub const Context = struct {
// });
// }
fn addDebugInfo(self: @This(), drawList: *DrawList) !void {
fn addDebugInfo(self: @This(), drawlistAllocator: std.mem.Allocator, drawList: *DrawList) !void {
const defaultHeight = self.debugFontSize;
const yOffsetPerLine: f32 = defaultHeight + 1;
const sizePerLine: f32 = yOffsetPerLine + (yOffsetPerLine / 4);
@ -1270,7 +1272,7 @@ pub const Context = struct {
const fontHandle = self.fontCache.defaultMonoFont.atlas.fontHandle;
try drawList.append(.{
try drawList.append(drawlistAllocator, .{
.node = .{},
.primitive = .{
.Rect = .{
@ -1307,7 +1309,7 @@ pub const Context = struct {
break;
}
try drawList.append(.{
try drawList.append(drawlistAllocator, .{
.node = .{ .index = @intCast(i) },
.primitive = .{
.Text = .{

View File

@ -72,6 +72,7 @@ pub fn addToDrawList(dlb: DrawListBuilder) !void {
}
try drawlist.append(
dlb.allocator,
.{ .node = dlb.node, .primitive = .{ .Rect = .{
.tl = dlb.resolvedPos,
.size = dlb.resolvedSize,
@ -81,7 +82,7 @@ pub fn addToDrawList(dlb: DrawListBuilder) !void {
} } },
);
try drawlist.append(.{
try drawlist.append(dlb.allocator, .{
.node = dlb.node,
.primitive = .{
.Text = .{

View File

@ -5,6 +5,7 @@ entryState: TextEntryState = .Normal,
entryStubPosition: u32 = 0,
textEntryStyle: TextEntryStyle = .{},
editText: std.ArrayList(u8),
allocator: std.mem.Allocator,
enterSendsNewline: bool = true,
pub const TextEntryStyle = struct {
@ -57,7 +58,7 @@ pub fn addToDrawList(dlb: DrawListBuilder) !void {
}
// 1. draw the main rect for Text.
try drawlist.append(.{
try drawlist.append(dlb.allocator, .{
.node = dlb.node,
.primitive = .{ .Rect = .{
.tl = dlb.resolvedPos,
@ -68,7 +69,7 @@ pub fn addToDrawList(dlb: DrawListBuilder) !void {
} },
});
try drawlist.append(.{
try drawlist.append(dlb.allocator, .{
.node = dlb.node,
.primitive = .{
.Text = .{
@ -120,7 +121,7 @@ pub fn addToDrawList(dlb: DrawListBuilder) !void {
const geo = hr.characterGeo;
const width: f32 = 2.0;
try drawlist.append(.{
try drawlist.append(dlb.allocator, .{
.node = dlb.node,
.primitive = .{
.Rect = .{
@ -138,7 +139,7 @@ pub fn addToDrawList(dlb: DrawListBuilder) !void {
pub fn tearDown(ctx: *papyrus.Context, node: papyrus.NodeHandle) void {
var textEntry = ctx.getTextEntry(node);
textEntry.editText.deinit();
textEntry.editText.deinit(textEntry.allocator);
}
// ======

View File

@ -4,11 +4,11 @@ const core = @import("core");
const Name = core.Name;
const MakeName = core.Name;
pub fn loadFileAlloc(filename: []const u8, comptime alignment: usize, allocator: std.mem.Allocator) ![]u8 {
pub fn loadFileAlloc(filename: []const u8, comptime alignment: std.mem.Alignment, allocator: std.mem.Allocator) ![]u8 {
var file = try std.fs.cwd().openFile(filename, .{});
const filesize = (try file.stat()).size;
const buffer: []u8 = try allocator.alignedAlloc(u8, alignment, filesize);
try file.reader().readNoEof(buffer);
_ = try file.readAll(buffer);
return buffer;
}
@ -38,15 +38,14 @@ pub const FileLog = struct {
pub fn init(allocator: std.mem.Allocator, fileName: []const u8) !@This() {
return .{
.buffer = std.ArrayList(u8).init(allocator),
.buffer = .{},
.allocator = allocator,
.fileName = try dupeString(allocator, fileName),
};
}
pub fn write(self: *@This(), comptime fmt: []const u8, args: anytype) !void {
var writer = self.buffer.writer();
try writer.print(fmt, args);
try self.buffer.print(self.allocator, fmt, args);
}
pub fn writeOut(self: @This()) !void {
@ -59,7 +58,7 @@ pub const FileLog = struct {
pub fn deinit(self: *@This()) void {
self.allocator.free(self.fileName);
self.buffer.deinit();
self.buffer.deinit(self.allocator);
}
};

View File

@ -271,7 +271,7 @@ test "sdf texture generation" {
const allocator = std.testing.allocator;
var font: c.stbtt_fontinfo = undefined;
const fileContent = try utils.loadFileAlloc("fonts/FiraMono-Medium.ttf", 8, allocator);
const fileContent = try utils.loadFileAlloc("fonts/FiraMono-Medium.ttf", .@"8", allocator);
defer allocator.free(fileContent);
_ = c.stbtt_InitFont(&font, fileContent.ptr, c.stbtt_GetFontOffsetForIndex(fileContent.ptr, 0));

View File

@ -36,9 +36,11 @@ pub fn build(b: *std.Build) void {
// ========== tests ==========
const tests = b.addTest(.{
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("tests/tests.zig"),
}),
});
const test_step = b.step("test", "run unit tests for ui");

3
lib/zgltf/build.zig vendored
View File

@ -4,6 +4,9 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const static_build = b.option(bool, "static_build", "builds backlog dependencies for static linking") orelse false;
_ = static_build;
const mod = b.addModule("zgltf", .{
.root_source_file = b.path("src/zgltf.zig"),
.target = target,