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

Proposal: Make zgui a module for emscripten #22

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ pub fn build(b: *std.Build) void {
.with_implot = true,
});
exe.root_module.addImport("zgui", zgui.module("root"));
exe.linkLibrary(zgui.artifact("imgui"));


{ // Needed for glfw/wgpu rendering backend
const zglfw = b.dependency("zglfw", .{});
exe.root_module.addImport("zglfw", zglfw.module("root"));
Expand Down
101 changes: 41 additions & 60 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn build(b: *std.Build) void {

const options_module = options_step.createModule();

_ = b.addModule("root", .{
const imgui = b.addModule("root", .{
.root_source_file = b.path("src/gui.zig"),
.imports = &.{
.{ .name = "zgui_options", .module = options_module },
Expand All @@ -87,49 +87,24 @@ pub fn build(b: *std.Build) void {
"-Wno-availability",
};

const imgui = if (options.shared) blk: {
const lib = b.addSharedLibrary(.{
.name = "imgui",
.target = target,
.optimize = optimize,
});

if (target.result.os.tag == .windows) {
lib.root_module.addCMacro("IMGUI_API", "__declspec(dllexport)");
lib.root_module.addCMacro("IMPLOT_API", "__declspec(dllexport)");
lib.root_module.addCMacro("ZGUI_API", "__declspec(dllexport)");
}

if (target.result.os.tag == .macos) {
lib.linker_allow_shlib_undefined = true;
}

break :blk lib;
} else b.addStaticLibrary(.{
.name = "imgui",
.target = target,
.optimize = optimize,
});

b.installArtifact(imgui);

const emscripten = target.result.os.tag == .emscripten;
if (emscripten) {
imgui.root_module.addCMacro("__EMSCRIPTEN__", "");
// TODO: read from enviroment or `emcc --version`
imgui.root_module.addCMacro("__EMSCRIPTEN_major__", "3");
imgui.root_module.addCMacro("__EMSCRIPTEN_minor__", "1");
imgui.root_module.stack_protector = false;
//imgui.root_module.disable_stack_probing = true;
}
//Shared Library details
//if (target.result.os.tag == .macos) {
// lib.linker_allow_shlib_undefined = true;
//}
//if (target.result.os.tag == .windows) {
// imgui.addCMacro("IMGUI_API", "__declspec(dllexport)");
// imgui.addCMacro("IMPLOT_API", "__declspec(dllexport)");
// imgui.addCMacro("ZGUI_API", "__declspec(dllexport)");
//}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: Maybe this should go in the readme.


imgui.addIncludePath(b.path("libs"));
imgui.addIncludePath(b.path("libs/imgui"));

const emscripten = target.result.os.tag == .emscripten;
if (!emscripten) {
imgui.linkLibC();
imgui.link_libc = true;
if (target.result.abi != .msvc)
imgui.linkLibCpp();
imgui.link_libcpp = true;
}

imgui.addCSourceFile(.{
Expand All @@ -156,11 +131,11 @@ pub fn build(b: *std.Build) void {
.file = b.path("libs/imgui/misc/freetype/imgui_freetype.cpp"),
.flags = cflags,
});
imgui.root_module.addCMacro("IMGUI_ENABLE_FREETYPE", "1");
imgui.addCMacro("IMGUI_ENABLE_FREETYPE", "1");
}

if (options.use_wchar32) {
imgui.root_module.addCMacro("IMGUI_USE_WCHAR32", "1");
imgui.addCMacro("IMGUI_USE_WCHAR32", "1");
}

if (options.with_implot) {
Expand Down Expand Up @@ -215,8 +190,8 @@ pub fn build(b: *std.Build) void {
.flags = cflags,
});

imgui.root_module.addCMacro("IMGUI_ENABLE_TEST_ENGINE", "");
imgui.root_module.addCMacro("IMGUI_TEST_ENGINE_ENABLE_COROUTINE_STDTHREAD_IMPL", "1");
imgui.addCMacro("IMGUI_ENABLE_TEST_ENGINE", "");
imgui.addCMacro("IMGUI_TEST_ENGINE_ENABLE_COROUTINE_STDTHREAD_IMPL", "1");

imgui.addIncludePath(b.path("libs/imgui_test_engine/"));

Expand Down Expand Up @@ -267,7 +242,6 @@ pub fn build(b: *std.Build) void {
winpthreads.addIncludePath(b.path("libs/winpthreads/include"));
winpthreads.addIncludePath(b.path("libs/winpthreads/src"));
winpthreads.linkLibC();
b.installArtifact(winpthreads);
imgui.linkLibrary(winpthreads);
imgui.addSystemIncludePath(b.path("libs/winpthreads/include"));
}
Expand All @@ -279,21 +253,28 @@ pub fn build(b: *std.Build) void {
imgui.addSystemIncludePath(.{
.cwd_relative = b.pathJoin(&.{ b.sysroot.?, "include" }),
});
imgui.addCSourceFiles(.{
.files = &.{
"libs/imgui/backends/imgui_impl_glfw.cpp",
"libs/imgui/backends/imgui_impl_wgpu.cpp",
},
.flags = cflags,
});
} else {
if (b.lazyDependency("zglfw", .{})) |zglfw| {
imgui.addIncludePath(zglfw.path("libs/glfw/include"));
}
if (b.lazyDependency("zgpu", .{})) |zgpu| {
imgui.addIncludePath(zgpu.path("libs/dawn/include"));
}
imgui.addCSourceFiles(.{
.files = &.{
"libs/imgui/backends/imgui_impl_glfw.cpp",
"libs/imgui/backends/imgui_impl_wgpu.cpp",
},
.flags = &(cflags.* ++ .{"-DIMGUI_IMPL_WEBGPU_BACKEND_WGPU"}),
});
}
imgui.addCSourceFiles(.{
.files = &.{
"libs/imgui/backends/imgui_impl_glfw.cpp",
"libs/imgui/backends/imgui_impl_wgpu.cpp",
},
.flags = cflags,
});
},
.glfw_opengl3 => {
if (b.lazyDependency("zglfw", .{})) |zglfw| {
Expand All @@ -318,7 +299,7 @@ pub fn build(b: *std.Build) void {
},
.flags = cflags,
});
imgui.linkSystemLibrary("d3dcompiler_47");
imgui.linkSystemLibrary("d3dcompiler_47", .{});
},
.win32_dx12 => {
imgui.addCSourceFiles(.{
Expand All @@ -328,11 +309,11 @@ pub fn build(b: *std.Build) void {
},
.flags = cflags,
});
imgui.linkSystemLibrary("d3dcompiler_47");
imgui.linkSystemLibrary("dwmapi");
imgui.linkSystemLibrary("d3dcompiler_47", .{});
imgui.linkSystemLibrary("dwmapi", .{});
switch (target.result.abi) {
.msvc => imgui.linkSystemLibrary("Gdi32"),
.gnu => imgui.linkSystemLibrary("gdi32"),
.msvc => imgui.linkSystemLibrary("Gdi32", .{}),
.gnu => imgui.linkSystemLibrary("gdi32", .{}),
else => {},
}
},
Expand Down Expand Up @@ -373,10 +354,10 @@ pub fn build(b: *std.Build) void {
});
},
.osx_metal => {
imgui.linkFramework("Foundation");
imgui.linkFramework("Metal");
imgui.linkFramework("Cocoa");
imgui.linkFramework("QuartzCore");
imgui.linkFramework("Foundation", .{});
imgui.linkFramework("Metal", .{});
imgui.linkFramework("Cocoa", .{});
imgui.linkFramework("QuartzCore", .{});
imgui.addCSourceFiles(.{
.files = &.{
"libs/imgui/backends/imgui_impl_osx.mm",
Expand Down Expand Up @@ -406,7 +387,7 @@ pub fn build(b: *std.Build) void {
b.installArtifact(tests);

tests.root_module.addImport("zgui_options", options_module);
tests.linkLibrary(imgui);
tests.root_module.addImport("zgui", imgui);

test_step.dependOn(&b.addRunArtifact(tests).step);
}
5 changes: 0 additions & 5 deletions libs/imgui/backends/imgui_impl_wgpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
// When targeting native platforms (i.e. NOT emscripten), one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN
// or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.

// FIX(zig-gamedev)
#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU

#ifndef __EMSCRIPTEN__
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
#error exactly one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be defined!
Expand Down Expand Up @@ -836,8 +833,6 @@ bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info)
io.BackendRendererName = "imgui_impl_webgpu_emscripten";
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
io.BackendRendererName = "imgui_impl_webgpu_dawn";
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
io.BackendRendererName = "imgui_impl_webgpu_wgpu";
#else
io.BackendRendererName = "imgui_impl_webgpu";
#endif
Expand Down
3 changes: 0 additions & 3 deletions libs/imgui/backends/imgui_impl_wgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
// This requirement will be removed once WebGPU stabilizes and backends converge on a unified interface.
//#define IMGUI_IMPL_WEBGPU_BACKEND_DAWN

// FIX(zig-gamedev)
#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU

// Implemented features:
// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
Expand Down
4 changes: 2 additions & 2 deletions src/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ const ImageButton = struct {
pub fn imageButton(str_id: [:0]const u8, user_texture_id: TextureIdent, args: ImageButton) bool {
return zguiImageButton(
str_id,
user_texture_id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be here?

@intFromPtr(user_texture_id),
args.w,
args.h,
&args.uv0,
Expand All @@ -1689,7 +1689,7 @@ pub fn imageButton(str_id: [:0]const u8, user_texture_id: TextureIdent, args: Im
}
extern fn zguiImageButton(
str_id: [*:0]const u8,
user_texture_id: TextureIdent,
Copy link
Member

@hazeycode hazeycode Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this?

user_texture_id: u64,
w: f32,
h: f32,
uv0: *const [2]f32,
Expand Down
Loading