diff --git a/README.md b/README.md index f2c24f2..9cbcfaa 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,9 @@ The static library for Dear ImGui, and supported backends, are provided as artif ## How to use the backends -Right now, this package only exports the Vulkan backend: `dear_imgui_vulkan`. +Right now, this package only exports the following backends: +* `dear_imgui_vulkan` +* `dear_imgui_sdl3` Contributions are welcome if you'd like to add others. The process should be fairly straightforward. diff --git a/build.zig b/build.zig index 4567d8a..2ddb0fa 100644 --- a/build.zig +++ b/build.zig @@ -69,6 +69,26 @@ pub fn build(b: *std.Build) void { dear_imgui_vulkan_lib.installHeadersDirectory(vulkan_headers.path("include"), "", .{}); b.installArtifact(dear_imgui_vulkan_lib); + // Compile the SDL3 backend as a static library + const dear_imgui_sdl3_lib = b.addLibrary(.{ + .name = "dear_imgui_vulkan", + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize_external, + }), + }); + dear_imgui_sdl3_lib.linkLibrary(dear_imgui_lib); + dear_imgui_sdl3_lib.addCSourceFile(.{ .file = upstream.path("backends/imgui_impl_sdl3.cpp"), .flags = flags }); + dear_imgui_sdl3_lib.addCSourceFile(.{ .file = b.path("src/cached/dcimgui_impl_sdl3.cpp"), .flags = flags }); + dear_imgui_sdl3_lib.addIncludePath(upstream.path("")); + dear_imgui_sdl3_lib.addIncludePath(upstream.path("backends")); + const sdl = b.dependency("sdl", .{}); + dear_imgui_sdl3_lib.addIncludePath(sdl.path("include")); + // dear_imgui_sdl3_lib.root_module.addCMacro("IMGUI_IMPL_VULKAN_NO_PROTOTYPES", "1"); // Assumed in generator // XXX: ... + dear_imgui_sdl3_lib.installHeadersDirectory(upstream.path("backends"), "", .{}); + // dear_imgui_sdl3_lib.installHeadersDirectory(vulkan_headers.path("include"), "", .{}); // XXX: ... + b.installArtifact(dear_imgui_sdl3_lib); + // Compile the generator const generate_exe = b.addExecutable(.{ .name = "generate", @@ -114,4 +134,17 @@ pub fn build(b: *std.Build) void { }); dear_imgui_vulkan_zig_module.linkLibrary(dear_imgui_vulkan_lib); dear_imgui_vulkan_zig_module.addImport("dear_imgui", dear_imgui_zig_module); + + // Generate Zig bindings for the SDL3 backend + const generate_sdl3 = b.addRunArtifact(generate_exe); + generate_sdl3.addFileArg(b.path("src/cached/dcimgui_impl_sdl3.json")); + const dear_imgui_sdl3_zig = generate_sdl3.addOutputFileArg("dear_imgui_impl_sdl3.zig"); + generate_sdl3.addFileArg(b.path("src/templates/impl_sdl3_prefix.zig.template")); + generate_sdl3.addFileArg(b.path("src/templates/impl_sdl3_postfix.zig.template")); + const dear_imgui_sdl3_zig_module = b.addModule("dear_imgui_sdl3", .{ + .root_source_file = dear_imgui_sdl3_zig, + .target = target, + .optimize = optimize, + }); + dear_imgui_sdl3_zig_module.linkLibrary(dear_imgui_sdl3_lib); } diff --git a/build.zig.zon b/build.zig.zon index 13ee44c..6997f3d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -12,6 +12,11 @@ .url = "https://github.com/KhronosGroup/Vulkan-Headers/archive/refs/tags/v1.4.313.tar.gz", .hash = "N-V-__8AAPLT7gH4vgN7Xo6yZlOMrzllGbdvVxxhrwUvgI9r", }, + .sdl = .{ + .url = "https://github.com/libsdl-org/SDL/archive/refs/tags/release-3.2.14.tar.gz", + .hash = "N-V-__8AAHRriwPslozjEw46IksOzPlNxJRXM087krt8ynXh", + .version = "3.2.14", + }, }, .paths = .{ diff --git a/src/generate.zig b/src/generate.zig index f1d3f21..ee098d3 100644 --- a/src/generate.zig +++ b/src/generate.zig @@ -97,6 +97,7 @@ const Header = struct { const Type = struct { type_details: ?Details = null, description: Description, + declaration: ?[]const u8 = null, const Details = struct { flavour: enum { function_pointer }, @@ -300,7 +301,11 @@ fn writeTypedefs(writer: anytype, header: *const Header, declarations: *const De // Skip duplicate declarations (e.g. naming enums as ints in C) if (declarations.contains(typedef.name)) continue; - // Write the typedef + // Skip redundant typedefs (comes up with `SDL_Event`) + if (typedef.type.declaration != null and + std.mem.eql(u8, typedef.name, typedef.type.declaration.?)) continue; + + // Write the typedef prefix try writer.writeAll("const "); try writeTypeName(writer, typedef.name); try writer.writeAll(" = "); @@ -893,7 +898,7 @@ fn writeTypeName(writer: anytype, raw: []const u8) !void { // Backend prefixes { - const prefixes: []const []const u8 = &.{ "_ImplVulkanH", "_ImplVulkan" }; + const prefixes: []const []const u8 = &.{ "_ImplVulkanH", "_ImplVulkan", "_ImplSDL3" }; for (prefixes) |prefix| { if (std.mem.startsWith(u8, name, prefix)) { name = name[prefix.len..]; @@ -956,7 +961,7 @@ fn writeFunctionName(writer: anytype, raw: []const u8) !void { // Imgui prefixes { - const prefixes: []const []const u8 = &.{ "cImGui_ImplVulkan", "ImGui", "Im" }; + const prefixes: []const []const u8 = &.{ "cImGui_ImplVulkan", "cImGui_ImplSDL3", "ImGui", "Im" }; for (prefixes) |prefix| { if (std.mem.startsWith(u8, name, prefix)) { name = name[prefix.len..]; diff --git a/src/templates/impl_sdl3_postfix.zig.template b/src/templates/impl_sdl3_postfix.zig.template new file mode 100644 index 0000000..8e30c4f --- /dev/null +++ b/src/templates/impl_sdl3_postfix.zig.template @@ -0,0 +1,2 @@ + }; +} diff --git a/src/templates/impl_sdl3_prefix.zig.template b/src/templates/impl_sdl3_prefix.zig.template new file mode 100644 index 0000000..36f5208 --- /dev/null +++ b/src/templates/impl_sdl3_prefix.zig.template @@ -0,0 +1,8 @@ +const Options = struct { + SDLEvent: type, +}; + +pub fn get(options: Options) type { + const SDLEvent = options.SDLEvent; + + return struct {