Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Zig 0.11 #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
89 changes: 71 additions & 18 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,33 +1,79 @@
const std = @import("std");
const path = std.fs.path;
const ArrayList = std.ArrayList;
const Builder = std.build.Builder;
const LibExeObjStep = std.build.LibExeObjStep;
const Module = std.build.Module;

const ig_build = @import("zig-imgui/imgui_build.zig");

const glslc_command = if (std.builtin.os.tag == .windows) "tools/win/glslc.exe" else "glslc";

pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
var buffer: [1024]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer);
var allocator = fba.allocator();
var module_names = ArrayList([]const u8).init(allocator);
var module_list = ArrayList(*Module).init(allocator);

const mode = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

// Add modules to the builder
const mod_vk = b.addModule("vk", .{
.source_file = .{ .path = "include/vk.zig" },
.dependencies = &.{},
});
module_names.append("vk") catch @panic("");
module_list.append(mod_vk) catch @panic("");

const mod_glfw = b.addModule("glfw", .{
.source_file = .{ .path = "include/glfw.zig" },
.dependencies = &.{
.{ .name = "vk", .module = mod_vk },
},
});
module_names.append("glfw") catch @panic("");
module_list.append(mod_glfw) catch @panic("");

const mod_cgltf = b.addModule("cgltf", .{
.source_file = .{ .path = "include/cgltf.zig" },
.dependencies = &.{},
});
module_names.append("cgltf") catch @panic("");
module_list.append(mod_cgltf) catch @panic("");

ig_build.addTestStep(b, "imgui:test", "zig-imgui/imgui", mode, target);

const exe = b.addExecutable("zig-gltf", "src/main.zig");
setDependencies(exe, mode, target);
exe.install();
const exe = b.addExecutable(.{
.name = "zig-gltf",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = mode,
});
setDependencies(exe, module_names, module_list, target);
b.installArtifact(exe);

const run_step = b.step("run", "Run the project");
const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);

const tests = b.addTest("src/all_tests.zig");
setDependencies(tests, mode, target);
const tests = b.addTest(.{
.root_source_file = .{ .path = "src/all_tests.zig" },
.target = target,
.optimize = mode,
});
setDependencies(tests, module_names, module_list, target);

const vscode_exe = b.addExecutable("vscode", "src/main.zig");
setDependencies(vscode_exe, mode, target);
const vscode_exe = b.addExecutable(.{
.name = "vscode",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = mode,
});
setDependencies(vscode_exe, module_names, module_list, target);

const vscode_install = b.addInstallArtifact(vscode_exe);
const vscode_install = b.addInstallArtifact(vscode_exe, .{});

const vscode_step = b.step("vscode", "Build for VSCode");
vscode_step.dependOn(&vscode_install.step);
Expand All @@ -36,18 +82,21 @@ pub fn build(b: *Builder) void {
run_tests.dependOn(&tests.step);
}

fn setDependencies(step: *LibExeObjStep, mode: std.builtin.Mode, target: std.zig.CrossTarget) void {
step.setBuildMode(mode);
fn setDependencies(step: *LibExeObjStep, module_names: ArrayList([]const u8), module_list: ArrayList(*Module), target: std.zig.CrossTarget) void {
step.linkLibCpp();

ig_build.link(step, "zig-imgui/imgui");
step.addPackagePath("vk", "include/vk.zig");
step.addPackagePath("glfw", "include/glfw.zig");
step.addPackagePath("cgltf", "include/cgltf.zig");

// Add modules to the compilation step
for (module_names.items, module_list.items) |name, mod| {
step.addModule(name, mod);
}

// Windows libraries
if (target.getOs().tag == .windows) {
step.addObjectFile(if (target.getAbi() == .msvc) "lib/win/glfw3.lib" else "lib/win/libglfw3.a");
step.addObjectFile("lib/win/vulkan-1.lib");
const glfw = if (target.getAbi() == .msvc) "lib/win/glfw3.lib" else "lib/win/libglfw3.a";
step.addObjectFile(.{ .path = glfw });
step.addObjectFile(.{ .path = "lib/win/vulkan-1.lib" });
step.linkSystemLibrary("gdi32");
step.linkSystemLibrary("shell32");
if (step.kind == .exe) {
Expand All @@ -58,7 +107,11 @@ fn setDependencies(step: *LibExeObjStep, mode: std.builtin.Mode, target: std.zig
step.linkSystemLibrary("vulkan");
}

step.addCSourceFile("c_src/cgltf.c", &[_][]const u8{ "-std=c99", "-DCGLTF_IMPLEMENTATION", "-D_CRT_SECURE_NO_WARNINGS" });
// C source code and flags
step.addCSourceFile(.{
.file = .{ .path = "c_src/cgltf.c" },
.flags = &[_][]const u8{ "-std=c99", "-DCGLTF_IMPLEMENTATION", "-D_CRT_SECURE_NO_WARNINGS" },
});
}

fn addShader(b: *Builder, exe: anytype, in_file: []const u8, out_file: []const u8) !void {
Expand Down
68 changes: 35 additions & 33 deletions include/cgltf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ pub const MutCString = [*:0]u8;

pub const Bool32 = i32;

pub const FileType = enum (u32) {
pub const FileType = enum(u32) {
invalid,
gltf,
glb,
_,
};

pub const Result = enum (u32) {
pub const Result = enum(u32) {
success,
data_too_short,
unknown_format,
Expand All @@ -27,14 +27,14 @@ pub const Result = enum (u32) {
};

pub const MemoryOptions = extern struct {
alloc: ?fn (?*anyopaque, usize) callconv(.C) ?*anyopaque = null,
free: ?fn (?*anyopaque, ?*anyopaque) callconv(.C) void = null,
alloc: ?*const fn (?*anyopaque, usize) callconv(.C) ?*anyopaque = null,
free: ?*const fn (?*anyopaque, ?*anyopaque) callconv(.C) void = null,
user_data: ?*anyopaque = null,
};

pub const FileOptions = extern struct {
read: ?fn (*const MemoryOptions, *const FileOptions, CString, *usize, *(?*anyopaque)) callconv(.C) Result = null,
release: ?fn (*const MemoryOptions, *const FileOptions, ?*anyopaque) callconv(.C) void = null,
read: ?*const fn (*const MemoryOptions, *const FileOptions, CString, *usize, *(?*anyopaque)) callconv(.C) Result = null,
release: ?*const fn (*const MemoryOptions, *const FileOptions, ?*anyopaque) callconv(.C) void = null,
user_data: ?*anyopaque = null,
};

Expand All @@ -45,14 +45,14 @@ pub const Options = extern struct {
file: FileOptions = FileOptions{},
};

pub const BufferViewType = enum (u32) {
pub const BufferViewType = enum(u32) {
invalid,
indices,
vertices,
_,
};

pub const AttributeType = enum (u32) {
pub const AttributeType = enum(u32) {
invalid,
position,
normal,
Expand All @@ -64,7 +64,7 @@ pub const AttributeType = enum (u32) {
_,
};

pub const ComponentType = enum (u32) {
pub const ComponentType = enum(u32) {
invalid,
r_8,
r_8u,
Expand All @@ -75,7 +75,7 @@ pub const ComponentType = enum (u32) {
_,
};

pub const Type = enum (u32) {
pub const Type = enum(u32) {
invalid,
scalar,
vec2,
Expand All @@ -87,7 +87,7 @@ pub const Type = enum (u32) {
_,
};

pub const PrimitiveType = enum (u32) {
pub const PrimitiveType = enum(u32) {
points,
lines,
line_loop,
Expand All @@ -98,14 +98,14 @@ pub const PrimitiveType = enum (u32) {
_,
};

pub const AlphaMode = enum (u32) {
pub const AlphaMode = enum(u32) {
opaqueMode,
mask,
blend,
_,
};

pub const AnimationPathType = enum (u32) {
pub const AnimationPathType = enum(u32) {
invalid,
translation,
rotation,
Expand All @@ -114,21 +114,21 @@ pub const AnimationPathType = enum (u32) {
_,
};

pub const InterpolationType = enum (u32) {
pub const InterpolationType = enum(u32) {
linear,
step,
cubic_spline,
_,
};

pub const CameraType = enum (u32) {
pub const CameraType = enum(u32) {
invalid,
perspective,
orthographic,
_,
};

pub const LightType = enum (u32) {
pub const LightType = enum(u32) {
invalid,
directional,
point,
Expand Down Expand Up @@ -184,24 +184,24 @@ pub const Accessor = extern struct {
is_sparse: Bool32,
sparse: AccessorSparse, // valid only if is_sparse != 0
extras: Extras,
pub fn unpackFloatsCount(self: *const Accessor) callconv(.Inline) usize {
pub inline fn unpackFloatsCount(self: *const Accessor) usize {
return cgltf_accessor_unpack_floats(self, null, 0);
}
pub fn unpackFloats(self: *const Accessor, outBuffer: []f32) callconv(.Inline) []f32 {
pub inline fn unpackFloats(self: *const Accessor, outBuffer: []f32) []f32 {
const actualCount = cgltf_accessor_unpack_floats(self, outBuffer.ptr, outBuffer.len);
return outBuffer[0..actualCount];
}
pub fn readFloat(self: *const Accessor, index: usize, outFloats: []f32) callconv(.Inline) bool {
pub inline fn readFloat(self: *const Accessor, index: usize, outFloats: []f32) bool {
assert(outFloats.len == numComponents(self.type));
const result = cgltf_accessor_read_float(self, index, outFloats.ptr, outFloats.len);
return result != 0;
}
pub fn readUint(self: *const Accessor, index: usize, outInts: []u32) callconv(.Inline) bool {
pub inline fn readUint(self: *const Accessor, index: usize, outInts: []u32) bool {
assert(outInts.len == numComponents(self.type));
const result = cgltf_accessor_read_uint(self, index, outInts.ptr, outInts.len);
return result != 0;
}
pub fn readIndex(self: *const Accessor, index: usize) callconv(.Inline) usize {
pub inline fn readIndex(self: *const Accessor, index: usize) usize {
return cgltf_accessor_read_index(self, index);
}
};
Expand Down Expand Up @@ -378,12 +378,12 @@ pub const Node = extern struct {
scale: [3]f32,
matrix: [16]f32,
extras: Extras,
pub fn transformLocal(self: *const Node) callconv(.Inline) [16]f32 {
pub inline fn transformLocal(self: *const Node) [16]f32 {
var transform: [16]f32 = undefined;
cgltf_node_transform_local(self, &transform);
return transform;
}
pub fn transformWorld(self: *const Node) callconv(.Inline) [16]f32 {
pub inline fn transformWorld(self: *const Node) [16]f32 {
var transform: [16]f32 = undefined;
cgltf_node_transform_world(self, &transform);
return transform;
Expand Down Expand Up @@ -473,7 +473,7 @@ pub const Data = extern struct {
memory: MemoryOptions,
file: FileOptions,
};
pub fn parse(options: *const Options, data: []const u8) callconv(.Inline) !*Data {
pub inline fn parse(options: *const Options, data: []const u8) !*Data {
var out_data: ?*Data = undefined;
const result = cgltf_parse(options, data.ptr, data.len, &out_data);
if (result == .success) return out_data.?;
Expand All @@ -490,7 +490,7 @@ pub fn parse(options: *const Options, data: []const u8) callconv(.Inline) !*Data
else => unreachable,
}
}
pub fn parseFile(options: Options, path: CString) callconv(.Inline) !*Data {
pub inline fn parseFile(options: Options, path: CString) !*Data {
var out_data: ?*Data = undefined;
const result = cgltf_parse_file(&options, path, &out_data);
if (result == .success) return out_data.?;
Expand All @@ -507,7 +507,7 @@ pub fn parseFile(options: Options, path: CString) callconv(.Inline) !*Data {
else => unreachable,
}
}
pub fn loadBuffers(options: Options, data: *Data, gltf_path: CString) callconv(.Inline) !void {
pub inline fn loadBuffers(options: Options, data: *Data, gltf_path: CString) !void {
const result = cgltf_load_buffers(&options, data, gltf_path);
if (result == .success) return;
switch (result) {
Expand All @@ -523,22 +523,24 @@ pub fn loadBuffers(options: Options, data: *Data, gltf_path: CString) callconv(.
else => unreachable,
}
}
pub fn loadBufferBase64(options: Options, size: usize, base64: []const u8) callconv(.Inline) ![]u8 {
pub inline fn loadBufferBase64(options: Options, size: usize, base64: []const u8) ![]u8 {
assert(base64.len >= (size * 4 + 2) / 3);
var out: ?*anyopaque = null;
const result = cgltf_load_buffer_base64(&options, size, base64.ptr, &out);
if (result == .success)
return @ptrCast([*]u8, out.?)[0..size];
if (result == .success) {
const temp: [*]u8 = @ptrCast(out.?);
return temp[0..size];
}
switch (result) {
.io_error => return error.CgltfIOError,
.out_of_memory => return error.OutOfMemory,
else => unreachable,
}
}
pub fn validate(data: *Data) callconv(.Inline) Result {
pub inline fn validate(data: *Data) Result {
return cgltf_validate(data);
}
pub fn free(data: *Data) callconv(.Inline) void {
pub inline fn free(data: *Data) void {
cgltf_free(data);
}

Expand All @@ -554,13 +556,13 @@ pub fn numComponents(inType: Type) usize {
else => 1,
};
}
pub fn copyExtrasJsonCount(data: *const Data, extras: *const Extras) callconv(.Inline) usize {
pub inline fn copyExtrasJsonCount(data: *const Data, extras: *const Extras) usize {
var size: usize = 0;
var result = cgltf_copy_extras_json(data, extras, null, &size);
assert(result == .success); // can only fail on invalid size ptr
return size;
}
pub fn copyExtrasJson(data: *const Data, extras: *const Extras, outBuffer: []u8) callconv(.Inline) []u8 {
pub inline fn copyExtrasJson(data: *const Data, extras: *const Extras, outBuffer: []u8) []u8 {
var size: usize = outBuffer.len;
var result = cgltf_copy_extras_json(data, extras, outBuffer.ptr, &size);
assert(result == .success); // can only fail on invalid size ptr
Expand Down
Loading