Skip to content

Commit d8a744f

Browse files
committed
compiling and working for zig 0.14.0
1 parent 6d7a976 commit d8a744f

33 files changed

+282
-233
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
zig-cache
2-
zig-out
1+
.zig-cache
2+
3+
vcpkg_installed
4+
zig-out

build.zig

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ pub fn build(b: *std.Build) !void {
1818
const nanovg_dep = b.dependency("nanovg", .{ .target = target, .optimize = optimize });
1919

2020
const nanovg_mod = nanovg_dep.module("nanovg");
21-
const gui_mod = b.createModule(.{ .root_source_file = .{ .path = "src/gui/gui.zig" } });
21+
const gui_mod = b.createModule(.{ .root_source_file = b.path("src/gui/gui.zig") });
2222
gui_mod.addImport("nanovg", nanovg_mod);
23-
const data_mod = b.createModule(.{ .root_source_file = .{ .path = "data/data.zig" } });
24-
23+
const data_mod = b.createModule(.{ .root_source_file = b.path("data/data.zig") });
2524

2625
const exe = b.addExecutable(.{
2726
.name = "minipixel",
28-
.root_source_file = .{ .path = "src/main.zig" },
27+
.root_source_file = b.path("src/main.zig"),
2928
.target = target,
3029
.optimize = optimize,
3130
});
@@ -34,44 +33,39 @@ pub fn build(b: *std.Build) !void {
3433
exe.root_module.addOptions("build_options", exe_options);
3534
exe_options.addOption(bool, "automated_testing", automated_testing);
3635

37-
exe.addIncludePath(.{ .path = "lib/gl2/include" });
36+
exe.addIncludePath(b.path("lib/gl2/include"));
3837
if (target.result.os.tag == .windows) {
39-
// exe.addVcpkgPaths(.dynamic) catch @panic("vcpkg not installed");
40-
// if (exe.vcpkg_bin_path) |bin_path| {
41-
// for (&[_][]const u8{ "libpng16.dll", "zlib1.dll" }) |dll| {
42-
// const src_dll = try std.fs.path.join(b.allocator, &.{ bin_path, dll });
43-
// b.installBinFile(src_dll, dll);
44-
// }
45-
// }
4638
exe.subsystem = .Windows;
4739
exe.linkSystemLibrary("shell32");
4840
std.fs.cwd().access("minipixel.o", .{}) catch {
4941
std.log.err("minipixel.o not found. Please use VS Developer Prompt and run\n\n" ++
5042
"\trc /fo minipixel.o minipixel.rc\n\nbefore continuing\n", .{});
5143
return error.FileNotFound;
5244
};
53-
exe.addObjectFile(.{ .path = "minipixel.o" }); // add icon
45+
exe.addObjectFile(b.path("minipixel.o")); // add icon
5446
} else if (target.result.os.tag == .macos) {
55-
exe.addCSourceFile(.{ .file = .{ .path = "src/c/sdl_hacks.m" }, .flags = &.{} });
47+
exe.addCSourceFile(.{ .file = b.path("src/c/sdl_hacks.m"), .flags = &.{} });
5648
}
5749
const c_flags: []const []const u8 = if (optimize == .Debug)
5850
&.{ "-std=c99", "-D_CRT_SECURE_NO_WARNINGS", "-O0", "-g" }
5951
else
6052
&.{ "-std=c99", "-D_CRT_SECURE_NO_WARNINGS" };
61-
exe.addCSourceFile(.{ .file = .{ .path = "src/c/png_image.c" }, .flags = &.{"-std=c99"} });
62-
exe.addCSourceFile(.{ .file = .{ .path = "lib/gl2/src/glad.c" }, .flags = c_flags });
63-
exe.root_module.addImport("win32", zigwin32_dep.module("zigwin32"));
53+
exe.addCSourceFile(.{ .file = b.path("src/c/png_image.c"), .flags = &.{"-std=c99"} });
54+
exe.addCSourceFile(.{ .file = b.path("lib/gl2/src/glad.c"), .flags = c_flags });
55+
exe.root_module.addImport("win32", zigwin32_dep.module("win32"));
6456
exe.root_module.addImport("nfd", nfd_dep.module("nfd"));
6557
exe.root_module.addImport("nanovg", nanovg_mod);
6658
exe.root_module.addImport("gui", gui_mod);
6759
exe.root_module.addImport("data", data_mod);
6860
exe.linkLibrary(sdl_dep.artifact("SDL2"));
6961
if (target.result.os.tag == .windows) {
7062
// Workaround for CI: Zig detects pkg-config and resolves -lpng16 which doesn't exist
63+
exe.addIncludePath(b.path("vcpkg_installed/x64-windows/include"));
64+
exe.addLibraryPath(b.path("vcpkg_installed/x64-windows/bin"));
7165
exe.linkSystemLibrary("libpng16");
7266
} else if (target.result.os.tag == .macos) {
73-
exe.addIncludePath(.{ .path = "/opt/homebrew/include" });
74-
exe.addLibraryPath(.{ .path = "/opt/homebrew/lib" });
67+
exe.addIncludePath(b.path("/opt/homebrew/include"));
68+
exe.addLibraryPath(b.path("/opt/homebrew/lib"));
7569
exe.linkSystemLibrary("png");
7670
} else {
7771
exe.linkSystemLibrary("libpng16");
@@ -90,7 +84,7 @@ pub fn build(b: *std.Build) !void {
9084
installPalFiles(b);
9185

9286
const test_cmd = b.addTest(.{
93-
.root_source_file = .{ .path = "src/tests.zig" },
87+
.root_source_file = b.path("src/tests.zig"),
9488
.optimize = optimize,
9589
});
9690
const test_step = b.step("test", "Run tests");

build.zig.zon

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.{
2-
.name = "Mini Pixel",
2+
.name = .minipixel,
33
.version = "0.2.1",
4+
.fingerprint = 0xc96373d8301039f5,
45
.paths = .{
56
"data",
67
"lib",
@@ -10,21 +11,21 @@
1011
"LICENSE",
1112
},
1213
.dependencies = .{
14+
.nanovg = .{
15+
.url = "git+https://github.com/fabioarnold/nanovg-zig.git#5a26c03f3acc3c3d7610108bd2c6663500296718",
16+
.hash = "nanovg_zig-0.0.0-dfizXhAgNgB1frMIoQM_XTPT1RbVBLZJ-DvxVi-BnQbD",
17+
},
1318
.nfd = .{
14-
.url = "https://github.com/fabioarnold/nfd-zig/archive/91b199d3cbb314c52c630b7bf6050c4d665c0628.tar.gz",
15-
.hash = "122020466a938b490e0d4963eae2157d976990aa0acc53a1e32f9ac39d63a14cf977",
19+
.url = "git+https://github.com/fabioarnold/nfd-zig.git#ad81729d33da30d5f4fd23718debec48245121ca",
20+
.hash = "N-V-__8AAPOHBgCmeTgIR1EyYsjFxHTUpBX57MSSHIxq7729",
1621
},
1722
.sdl = .{
18-
.url = "https://github.com/andrewrk/SDL/archive/db4a162db2f6f59f737d03f441455dc9524d5793.tar.gz",
19-
.hash = "1220c5360c9c71c215baa41b46ec18d0711059b48416a2b1cf96c7c2d87b2e8e4cf6",
23+
.url = "git+https://github.com/andrewrk/SDL.git#35d2548e41d5c3ea1e6bd22631b1ff1d352fd174",
24+
.hash = "SDL-2.32.6-JToi3zqUEgG6r4ADMT5x-s1d5htGsWVMCJorr3GWEvLw",
2025
},
2126
.zigwin32 = .{
22-
.url = "https://github.com/marlersoft/zigwin32/archive/6777f1db221d0cb50322842f558f03e3c3a4099f.tar.gz",
23-
.hash = "1220f8d5028adceac0c6fc8ef554d82efef49ca48913393e9c2f5d73a66b58c8aaad",
24-
},
25-
.nanovg = .{
26-
.url = "https://github.com/fabioarnold/nanovg-zig/archive/b3396d4e65da5862c6ff088f64b3301aa50f3a9f.tar.gz",
27-
.hash = "1220908cb7b3e907526744c1ab23d21d312a6062e3813dc1facc6f03980fdd3c04f6",
27+
.url = "git+https://github.com/marlersoft/zigwin32.git#d21b419d808215e1f82605fdaddc49750bfa3bca",
28+
.hash = "zigwin32-25.0.28-preview-AAAAAI0J-wP-8_KKo4Yjr33XLhVhlJmeagGUedByaOnX",
2829
},
2930
},
3031
}

src/AboutDialogWidget.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn onMouseDown(widget: *gui.Widget, mouse_event: *gui.MouseEvent) void {
6969
}
7070

7171
fn onMouseMove(widget: *gui.Widget, mouse_event: *gui.MouseEvent) void {
72-
const self = @fieldParentPtr(Self, "widget", widget);
72+
const self: *Self = @fieldParentPtr("widget", widget);
7373
_ = self;
7474
const link_itchio_rect = Rect(f32).make(
7575
link_itchio_bounds[0],
@@ -88,7 +88,7 @@ fn onMouseMove(widget: *gui.Widget, mouse_event: *gui.MouseEvent) void {
8888
}
8989

9090
fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void {
91-
var self = @fieldParentPtr(Self, "widget", widget);
91+
var self: *Self = @fieldParentPtr("widget", widget);
9292
switch (event.key) {
9393
.Return, .Escape => self.close(),
9494
else => event.event.ignore(),
@@ -97,7 +97,7 @@ fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void {
9797

9898
fn onCloseButtonClick(button: *gui.Button) void {
9999
if (button.widget.parent) |parent| {
100-
var self = @fieldParentPtr(Self, "widget", parent);
100+
var self: *Self = @fieldParentPtr("widget", parent);
101101
self.close();
102102
}
103103
}

src/BlendModeWidget.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn setActive(self: *Self, active: BlendMode) void {
6060
fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void {
6161
if (!widget.enabled) return;
6262
if (event.button == .left) {
63-
var self = @fieldParentPtr(Self, "widget", widget);
63+
var self: *Self = @fieldParentPtr("widget", widget);
6464
const point = Point(f32).make(event.x, event.y);
6565
for (self.rects, 0..) |rect, i| {
6666
if (rect.contains(point)) {
@@ -72,7 +72,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void {
7272
}
7373

7474
pub fn draw(widget: *gui.Widget, vg: nvg) void {
75-
const self = @fieldParentPtr(Self, "widget", widget);
75+
const self: *Self = @fieldParentPtr("widget", widget);
7676

7777
const rect = widget.relative_rect;
7878
vg.save();

src/CanvasWidget.zig

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -851,14 +851,14 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document, vg: nvg)
851851

852852
self.horizontal_scrollbar.onChangedFn = struct {
853853
fn changed(scrollbar: *gui.Scrollbar) void {
854-
const canvas = @fieldParentPtr(Self, "widget", scrollbar.widget.parent.?);
854+
const canvas: *Self = @fieldParentPtr("widget", scrollbar.widget.parent.?);
855855
const client_w = canvas.getClientRect().w;
856856
canvas.translation.x = @round(0.5 * client_w - scrollbar.value);
857857
}
858858
}.changed;
859859
self.vertical_scrollbar.onChangedFn = struct {
860860
fn changed(scrollbar: *gui.Scrollbar) void {
861-
const canvas = @fieldParentPtr(Self, "widget", scrollbar.widget.parent.?);
861+
const canvas: *Self = @fieldParentPtr("widget", scrollbar.widget.parent.?);
862862
const client_h = canvas.getClientRect().h;
863863
canvas.translation.y = @round(0.5 * client_h - scrollbar.value);
864864
}
@@ -959,13 +959,13 @@ pub fn centerAndZoomDocument(self: *Self) void {
959959

960960
fn onResize(widget: *gui.Widget, event: *const gui.ResizeEvent) void {
961961
_ = event;
962-
const self = @fieldParentPtr(Self, "widget", widget);
962+
const self: *Self = @fieldParentPtr("widget", widget);
963963
self.updateLayout();
964964
self.updateScrollbars();
965965
}
966966

967967
fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void {
968-
var self = @fieldParentPtr(Self, "widget", widget);
968+
var self: *Self = @fieldParentPtr("widget", widget);
969969

970970
// translate view
971971
if (event.isButtonPressed(.middle)) {
@@ -988,7 +988,7 @@ fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void {
988988
}
989989

990990
fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void {
991-
var self = @fieldParentPtr(Self, "widget", widget);
991+
var self: *Self = @fieldParentPtr("widget", widget);
992992

993993
if (event.isButtonPressed(.middle)) {
994994
self.scroll_offset = self.toDocumentSpace(event.x, event.y);
@@ -1005,7 +1005,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void {
10051005
}
10061006

10071007
fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void {
1008-
var self = @fieldParentPtr(Self, "widget", widget);
1008+
var self: *Self = @fieldParentPtr("widget", widget);
10091009

10101010
if (event.button == .middle) {
10111011
self.scroll_offset = null;
@@ -1028,7 +1028,7 @@ fn onMouseWheel(widget: *gui.Widget, event: *const gui.MouseEvent) void {
10281028
const up = event.wheel_y < 0;
10291029
const down = event.wheel_y > 0;
10301030

1031-
var self = @fieldParentPtr(Self, "widget", widget);
1031+
var self: *Self = @fieldParentPtr("widget", widget);
10321032

10331033
if (event.isModifierPressed(.ctrl)) {
10341034
if (up) {
@@ -1052,20 +1052,20 @@ fn onMouseWheel(widget: *gui.Widget, event: *const gui.MouseEvent) void {
10521052
}
10531053

10541054
fn onTouchPan(widget: *gui.Widget, event: *const gui.TouchEvent) void {
1055-
var self = @fieldParentPtr(Self, "widget", widget);
1055+
var self: *Self = @fieldParentPtr("widget", widget);
10561056
self.setTranslation(self.translation.x + event.dx, self.translation.y + event.dy);
10571057
self.updateToolMousePreview(event.x, event.y);
10581058
}
10591059

10601060
fn onTouchZoom(widget: *gui.Widget, event: *const gui.TouchEvent) void {
1061-
var self = @fieldParentPtr(Self, "widget", widget);
1061+
var self: *Self = @fieldParentPtr("widget", widget);
10621062
const factor = 1.0 + event.zoom;
10631063
self.zoom(factor, event.x, event.y);
10641064
self.updateToolMousePreview(event.x, event.y);
10651065
}
10661066

10671067
fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void {
1068-
var self = @fieldParentPtr(Self, "widget", widget);
1068+
var self: *Self = @fieldParentPtr("widget", widget);
10691069
self.baseOnKeyDownFn(widget, event);
10701070
if (event.event.is_accepted) return;
10711071
switch (self.tool) {
@@ -1077,20 +1077,20 @@ fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void {
10771077
}
10781078

10791079
fn onKeyUp(widget: *gui.Widget, event: *gui.KeyEvent) void {
1080-
var self = @fieldParentPtr(Self, "widget", widget);
1080+
var self: *Self = @fieldParentPtr("widget", widget);
10811081
switch (self.tool) {
10821082
.draw => self.draw_tool.onKeyUp(self, event),
10831083
else => event.event.ignore(),
10841084
}
10851085
}
10861086

10871087
fn onEnter(widget: *gui.Widget) void {
1088-
var self = @fieldParentPtr(Self, "widget", widget);
1088+
var self: *Self = @fieldParentPtr("widget", widget);
10891089
self.hovered = true;
10901090
}
10911091

10921092
fn onLeave(widget: *gui.Widget) void {
1093-
var self = @fieldParentPtr(Self, "widget", widget);
1093+
var self: *Self = @fieldParentPtr("widget", widget);
10941094
self.hovered = false;
10951095

10961096
if (self.tool == .draw) self.draw_tool.onLeave(self);
@@ -1177,7 +1177,7 @@ fn notifyColorPicked(self: *Self) void {
11771177
}
11781178

11791179
fn draw(widget: *gui.Widget, vg: nvg) void {
1180-
const self = @fieldParentPtr(Self, "widget", widget);
1180+
const self: *Self = @fieldParentPtr("widget", widget);
11811181
const rect = widget.relative_rect;
11821182
vg.save();
11831183
vg.scissor(rect.x, rect.y, rect.w, rect.h);
@@ -1329,7 +1329,7 @@ fn drawSelection(self: Self, selection: Document.Selection, rect: Rect(f32), vg:
13291329

13301330
fn updateStatusBar(self: Self) void {
13311331
if (self.widget.parent) |parent| {
1332-
var editor = @fieldParentPtr(EditorWidget, "widget", parent);
1332+
var editor: *EditorWidget = @fieldParentPtr("widget", parent);
13331333

13341334
editor.tool_status_label.text = switch (self.tool) {
13351335
.crop => self.crop_tool.getStatusText(editor.tool_text[0..]),
@@ -1342,7 +1342,7 @@ fn updateStatusBar(self: Self) void {
13421342

13431343
fn updateImageStatus(self: Self) void {
13441344
if (self.widget.parent) |parent| {
1345-
var editor = @fieldParentPtr(EditorWidget, "widget", parent);
1345+
var editor: *EditorWidget = @fieldParentPtr("widget", parent);
13461346
editor.updateImageStatus();
13471347
}
13481348
}

src/ColorBitmap.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const col = @import("color.zig");
66
const Color = col.Color;
77
const IndexedBitmap = @import("IndexedBitmap.zig");
88

9+
const Point = @import("Point.zig");
10+
911
width: u32,
1012
height: u32,
1113
pixels: []u8,
@@ -218,39 +220,39 @@ pub fn floodFill(self: ColorBitmap, allocator: Allocator, x: i32, y: i32, color:
218220
const old_color = self.getPixel(x, y) orelse return;
219221
if (col.eql(old_color, color)) return;
220222

221-
const start_coords = .{ .x = @as(u32, @intCast(x)), .y = @as(u32, @intCast(y)) };
223+
const start_coords: Point = .{ .x = @as(u32, @intCast(x)), .y = @as(u32, @intCast(y)) };
222224
self.setPixelUnchecked(start_coords.x, start_coords.y, color);
223225

224-
var stack = std.ArrayList(struct { x: u32, y: u32 }).init(allocator);
226+
var stack = std.ArrayList(Point).init(allocator);
225227
try stack.ensureTotalCapacity(self.width * self.height / 2);
226228
defer stack.deinit();
227229
try stack.append(start_coords);
228230

229231
while (stack.items.len > 0) {
230-
const coords = stack.pop();
232+
const coords = stack.pop().?;
231233
if (coords.y > 0) {
232-
const new_coords = .{ .x = coords.x, .y = coords.y - 1 };
234+
const new_coords: Point = .{ .x = coords.x, .y = coords.y - 1 };
233235
if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) {
234236
self.setPixelUnchecked(new_coords.x, new_coords.y, color);
235237
stack.appendAssumeCapacity(new_coords);
236238
}
237239
}
238240
if (coords.y < self.height - 1) {
239-
const new_coords = .{ .x = coords.x, .y = coords.y + 1 };
241+
const new_coords: Point = .{ .x = coords.x, .y = coords.y + 1 };
240242
if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) {
241243
self.setPixelUnchecked(new_coords.x, new_coords.y, color);
242244
stack.appendAssumeCapacity(new_coords);
243245
}
244246
}
245247
if (coords.x > 0) {
246-
const new_coords = .{ .x = coords.x - 1, .y = coords.y };
248+
const new_coords: Point = .{ .x = coords.x - 1, .y = coords.y };
247249
if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) {
248250
self.setPixelUnchecked(new_coords.x, new_coords.y, color);
249251
stack.appendAssumeCapacity(new_coords);
250252
}
251253
}
252254
if (coords.x < self.width - 1) {
253-
const new_coords = .{ .x = coords.x + 1, .y = coords.y };
255+
const new_coords: Point = .{ .x = coords.x + 1, .y = coords.y };
254256
if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) {
255257
self.setPixelUnchecked(new_coords.x, new_coords.y, color);
256258
stack.appendAssumeCapacity(new_coords);

src/ColorForegroundBackgroundWidget.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn setActiveRgba(self: *Self, color: []const u8) void {
103103

104104
fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void {
105105
if (event.button == .left) {
106-
var self = @fieldParentPtr(Self, "widget", widget);
106+
var self: *Self = @fieldParentPtr("widget", widget);
107107
const point = Point(f32).make(event.x, event.y);
108108
for (self.rects, 0..) |rect, i| {
109109
if (rect.contains(point)) {
@@ -116,7 +116,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void {
116116

117117
fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void {
118118
if (event.button == .left) {
119-
var self = @fieldParentPtr(Self, "widget", widget);
119+
var self: *Self = @fieldParentPtr("widget", widget);
120120
const point = Point(f32).make(event.x, event.y);
121121
const swap_rect = Rect(f32).make(10, 42, 14, 14);
122122
if (swap_rect.contains(point)) {
@@ -145,7 +145,7 @@ fn drawSwapArrows(vg: nvg) void {
145145
}
146146

147147
pub fn draw(widget: *gui.Widget, vg: nvg) void {
148-
const self = @fieldParentPtr(Self, "widget", widget);
148+
const self: *Self = @fieldParentPtr("widget", widget);
149149

150150
const rect = widget.relative_rect;
151151
vg.save();

0 commit comments

Comments
 (0)