Skip to content

Commit dfe5ef1

Browse files
committed
- Remove usingnamespace usage
- Fixup build script / callconv deprecations
1 parent 21f5017 commit dfe5ef1

File tree

4 files changed

+161
-172
lines changed

4 files changed

+161
-172
lines changed

build.zig

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,31 @@ pub fn build(b: *std.Build) void {
2323
}
2424

2525
const options_module = options_step.createModule();
26-
27-
_ = b.addModule("root", .{
26+
const zmesh_module = b.addModule("root", .{
2827
.root_source_file = b.path("src/root.zig"),
28+
.target = target,
29+
.optimize = optimize,
2930
.imports = &.{
3031
.{ .name = "zmesh_options", .module = options_module },
3132
},
3233
});
3334

34-
const zmesh_lib = if (options.shared) blk: {
35-
const lib = b.addSharedLibrary(.{
36-
.name = "zmesh",
35+
const zmesh_lib = b.addLibrary(.{
36+
.name = "zmesh",
37+
.linkage = if (options.shared) .dynamic else .static,
38+
.root_module = b.createModule(.{
3739
.target = target,
3840
.optimize = optimize,
39-
});
41+
}),
42+
});
4043

41-
if (target.result.os.tag == .windows) {
42-
lib.root_module.addCMacro("PAR_SHAPES_API", "__declspec(dllexport)");
43-
lib.root_module.addCMacro("CGLTF_API", "__declspec(dllexport)");
44-
lib.root_module.addCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)");
45-
lib.root_module.addCMacro("ZMESH_API", "__declspec(dllexport)");
46-
}
44+
if (options.shared and target.result.os.tag == .windows) {
45+
zmesh_lib.root_module.addCMacro("PAR_SHAPES_API", "__declspec(dllexport)");
46+
zmesh_lib.root_module.addCMacro("CGLTF_API", "__declspec(dllexport)");
47+
zmesh_lib.root_module.addCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)");
48+
zmesh_lib.root_module.addCMacro("ZMESH_API", "__declspec(dllexport)");
49+
}
4750

48-
break :blk lib;
49-
} else b.addStaticLibrary(.{
50-
.name = "zmesh",
51-
.target = target,
52-
.optimize = optimize,
53-
});
5451
b.installArtifact(zmesh_lib);
5552

5653
zmesh_lib.linkLibC();
@@ -93,9 +90,7 @@ pub fn build(b: *std.Build) void {
9390

9491
const tests = b.addTest(.{
9592
.name = "zmesh-tests",
96-
.root_source_file = b.path("src/root.zig"),
97-
.target = target,
98-
.optimize = optimize,
93+
.root_module = zmesh_module,
9994
});
10095
b.installArtifact(tests);
10196

src/io.zig

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,11 @@
11
const std = @import("std");
22
const assert = std.debug.assert;
33

4-
const mem = @import("memory.zig");
5-
64
/// Deprecated. Use `zmesh.io.zcgltf.parseAndLoadFile` instead.
75
pub const parseAndLoadFile = zcgltf.parseAndLoadFile;
86
/// Deprecated. Use `zmesh.io.zcgltf.freeData` instead.
97
pub const freeData = zcgltf.freeData;
108
/// Deprecated. Use `zmesh.io.zcgltf.appendMeshPrimitive` instead.
119
pub const appendMeshPrimitive = zcgltf.appendMeshPrimitive;
1210

13-
pub const zcgltf = struct {
14-
const bindings = @import("zcgltf.zig");
15-
const Data = bindings.Data;
16-
17-
pub usingnamespace bindings;
18-
19-
pub fn parseAndLoadFile(pathname: [:0]const u8) bindings.Error!*Data {
20-
const options = bindings.Options{
21-
.memory = .{
22-
.alloc_func = mem.zmeshAllocUser,
23-
.free_func = mem.zmeshFreeUser,
24-
},
25-
};
26-
27-
const data = try bindings.parseFile(options, pathname);
28-
errdefer bindings.free(data);
29-
30-
try bindings.loadBuffers(options, data, pathname);
31-
32-
return data;
33-
}
34-
35-
pub fn freeData(data: *Data) void {
36-
bindings.free(data);
37-
}
38-
39-
pub fn appendMeshPrimitive(
40-
data: *Data,
41-
mesh_index: u32,
42-
prim_index: u32,
43-
indices: *std.ArrayList(u32),
44-
positions: *std.ArrayList([3]f32),
45-
normals: ?*std.ArrayList([3]f32),
46-
texcoords0: ?*std.ArrayList([2]f32),
47-
tangents: ?*std.ArrayList([4]f32),
48-
) !void {
49-
assert(mesh_index < data.meshes_count);
50-
assert(prim_index < data.meshes.?[mesh_index].primitives_count);
51-
52-
const mesh = &data.meshes.?[mesh_index];
53-
const prim = &mesh.primitives[prim_index];
54-
55-
const num_vertices: u32 = @as(u32, @intCast(prim.attributes[0].data.count));
56-
const num_indices: u32 = @as(u32, @intCast(prim.indices.?.count));
57-
58-
// Indices.
59-
{
60-
try indices.ensureTotalCapacity(indices.items.len + num_indices);
61-
62-
const accessor = prim.indices.?;
63-
const buffer_view = accessor.buffer_view.?;
64-
65-
assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0);
66-
assert(buffer_view.buffer.data != null);
67-
68-
const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) +
69-
accessor.offset + buffer_view.offset;
70-
71-
if (accessor.stride == 1) {
72-
if (accessor.component_type != .r_8u) {
73-
return error.InvalidIndicesAccessorComponentType;
74-
}
75-
const src = @as([*]const u8, @ptrCast(data_addr));
76-
var i: u32 = 0;
77-
while (i < num_indices) : (i += 1) {
78-
indices.appendAssumeCapacity(src[i]);
79-
}
80-
} else if (accessor.stride == 2) {
81-
if (accessor.component_type != .r_16u) {
82-
return error.InvalidIndicesAccessorComponentType;
83-
}
84-
const src = @as([*]const u16, @ptrCast(@alignCast(data_addr)));
85-
var i: u32 = 0;
86-
while (i < num_indices) : (i += 1) {
87-
indices.appendAssumeCapacity(src[i]);
88-
}
89-
} else if (accessor.stride == 4) {
90-
if (accessor.component_type != .r_32u) {
91-
return error.InvalidIndicesAccessorComponentType;
92-
}
93-
const src = @as([*]const u32, @ptrCast(@alignCast(data_addr)));
94-
var i: u32 = 0;
95-
while (i < num_indices) : (i += 1) {
96-
indices.appendAssumeCapacity(src[i]);
97-
}
98-
} else {
99-
return error.InvalidIndicesAccessorStride;
100-
}
101-
}
102-
103-
// Attributes.
104-
{
105-
const attributes = prim.attributes[0..prim.attributes_count];
106-
for (attributes) |attrib| {
107-
const accessor = attrib.data;
108-
assert(accessor.component_type == .r_32f);
109-
110-
const buffer_view = accessor.buffer_view.?;
111-
assert(buffer_view.buffer.data != null);
112-
113-
assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0);
114-
assert(accessor.stride * accessor.count == buffer_view.size);
115-
116-
const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) +
117-
accessor.offset + buffer_view.offset;
118-
119-
if (attrib.type == .position) {
120-
assert(accessor.type == .vec3);
121-
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
122-
try positions.appendSlice(slice);
123-
} else if (attrib.type == .normal) {
124-
if (normals) |n| {
125-
assert(accessor.type == .vec3);
126-
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
127-
try n.appendSlice(slice);
128-
}
129-
} else if (attrib.type == .texcoord) {
130-
if (texcoords0) |tc| {
131-
assert(accessor.type == .vec2);
132-
const slice = @as([*]const [2]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
133-
try tc.appendSlice(slice);
134-
}
135-
} else if (attrib.type == .tangent) {
136-
if (tangents) |tan| {
137-
assert(accessor.type == .vec4);
138-
const slice = @as([*]const [4]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
139-
try tan.appendSlice(slice);
140-
}
141-
}
142-
}
143-
}
144-
}
145-
};
11+
pub const zcgltf = @import("zcgltf.zig");

src/memory.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ pub fn init(alloc: std.mem.Allocator) void {
88
mem_allocations = std.AutoHashMap(usize, usize).init(alloc);
99
mem_allocations.?.ensureTotalCapacity(32) catch unreachable;
1010

11-
const zmeshMallocPtr = @extern(*?*const fn (size: usize) callconv(.C) ?*anyopaque, .{
11+
const zmeshMallocPtr = @extern(*?*const fn (size: usize) callconv(.c) ?*anyopaque, .{
1212
.name = "zmeshMallocPtr",
1313
.is_dll_import = options.shared,
1414
});
15-
const zmeshCallocPtr = @extern(*?*const fn (num: usize, size: usize) callconv(.C) ?*anyopaque, .{
15+
const zmeshCallocPtr = @extern(*?*const fn (num: usize, size: usize) callconv(.c) ?*anyopaque, .{
1616
.name = "zmeshCallocPtr",
1717
.is_dll_import = options.shared,
1818
});
19-
const zmeshReallocPtr = @extern(*?*const fn (ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque, .{
19+
const zmeshReallocPtr = @extern(*?*const fn (ptr: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque, .{
2020
.name = "zmeshReallocPtr",
2121
.is_dll_import = options.shared,
2222
});
23-
const zmeshFreePtr = @extern(*?*const fn (maybe_ptr: ?*anyopaque) callconv(.C) void, .{
23+
const zmeshFreePtr = @extern(*?*const fn (maybe_ptr: ?*anyopaque) callconv(.c) void, .{
2424
.name = "zmeshFreePtr",
2525
.is_dll_import = options.shared,
2626
});
@@ -38,8 +38,8 @@ pub fn deinit() void {
3838
mem_allocator = null;
3939
}
4040

41-
const MallocFn = *const fn (size: usize) callconv(.C) ?*anyopaque;
42-
const FreeFn = *const fn (ptr: ?*anyopaque) callconv(.C) void;
41+
const MallocFn = *const fn (size: usize) callconv(.c) ?*anyopaque;
42+
const FreeFn = *const fn (ptr: ?*anyopaque) callconv(.c) void;
4343

4444
extern fn meshopt_setAllocator(
4545
allocate: MallocFn,
@@ -51,7 +51,7 @@ var mem_allocations: ?std.AutoHashMap(usize, usize) = null;
5151
var mem_mutex: std.Thread.Mutex = .{};
5252
const mem_alignment: std.mem.Alignment = .@"16";
5353

54-
pub fn zmeshMalloc(size: usize) callconv(.C) ?*anyopaque {
54+
pub fn zmeshMalloc(size: usize) callconv(.c) ?*anyopaque {
5555
mem_mutex.lock();
5656
defer mem_mutex.unlock();
5757

@@ -66,7 +66,7 @@ pub fn zmeshMalloc(size: usize) callconv(.C) ?*anyopaque {
6666
return mem.ptr;
6767
}
6868

69-
fn zmeshCalloc(num: usize, size: usize) callconv(.C) ?*anyopaque {
69+
fn zmeshCalloc(num: usize, size: usize) callconv(.c) ?*anyopaque {
7070
const ptr = zmeshMalloc(num * size);
7171
if (ptr != null) {
7272
@memset(@as([*]u8, @ptrCast(ptr))[0 .. num * size], 0);
@@ -75,12 +75,12 @@ fn zmeshCalloc(num: usize, size: usize) callconv(.C) ?*anyopaque {
7575
return null;
7676
}
7777

78-
pub fn zmeshAllocUser(user: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
78+
pub fn zmeshAllocUser(user: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque {
7979
_ = user;
8080
return zmeshMalloc(size);
8181
}
8282

83-
fn zmeshRealloc(ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
83+
fn zmeshRealloc(ptr: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque {
8484
mem_mutex.lock();
8585
defer mem_mutex.unlock();
8686

@@ -103,7 +103,7 @@ fn zmeshRealloc(ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
103103
return mem.ptr;
104104
}
105105

106-
fn zmeshFree(maybe_ptr: ?*anyopaque) callconv(.C) void {
106+
fn zmeshFree(maybe_ptr: ?*anyopaque) callconv(.c) void {
107107
if (maybe_ptr) |ptr| {
108108
mem_mutex.lock();
109109
defer mem_mutex.unlock();
@@ -114,7 +114,7 @@ fn zmeshFree(maybe_ptr: ?*anyopaque) callconv(.C) void {
114114
}
115115
}
116116

117-
pub fn zmeshFreeUser(user: ?*anyopaque, ptr: ?*anyopaque) callconv(.C) void {
117+
pub fn zmeshFreeUser(user: ?*anyopaque, ptr: ?*anyopaque) callconv(.c) void {
118118
_ = user;
119119
zmeshFree(ptr);
120120
}

0 commit comments

Comments
 (0)