diff --git a/build.zig b/build.zig index aaa3ca7..60b099b 100644 --- a/build.zig +++ b/build.zig @@ -11,7 +11,9 @@ pub const Backend = enum { sdl2_opengl3, osx_metal, sdl2, + sdl3, sdl3_gpu, + sdl3_opengl3, }; pub fn build(b: *std.Build) void { @@ -358,6 +360,29 @@ pub fn build(b: *std.Build) void { .flags = cflags, }); }, + .sdl3_opengl3 => { + if (b.lazyDependency("zsdl", .{})) |zsdl| { + imgui.addIncludePath(zsdl.path("libs/sdl3/include/SDL3")); + } + imgui.addCSourceFiles(.{ + .files = &.{ + "libs/imgui/backends/imgui_impl_sdl3.cpp", + "libs/imgui/backends/imgui_impl_opengl3.cpp", + }, + .flags = &(cflags.* ++ .{"-DIMGUI_IMPL_OPENGL_LOADER_IMGL3W"}), + }); + }, + .sdl3 => { + if (b.lazyDependency("zsdl", .{})) |zsdl| { + imgui.addIncludePath(zsdl.path("libs/sdl3/include/SDL3")); + } + imgui.addCSourceFiles(.{ + .files = &.{ + "libs/imgui/backends/imgui_impl_sdl3.cpp", + }, + .flags = cflags, + }); + }, .no_backend => {}, } diff --git a/src/backend_sdl3.zig b/src/backend_sdl3.zig index 4e15c99..ff8692f 100644 --- a/src/backend_sdl3.zig +++ b/src/backend_sdl3.zig @@ -8,6 +8,15 @@ pub fn initGPU( } } +pub fn initOpenGL( + window: *const anyopaque, // SDL_Window + context: *const anyopaque, // SDL_GL_Context +) void { + if (!ImGui_ImplSDL3_InitForOpenGL(window, context)) { + unreachable; + } +} + pub fn processEvent( event: *const anyopaque, // SDL_Event ) bool { @@ -25,6 +34,13 @@ pub fn newFrame() void { // Those functions are defined in `imgui_impl_sdl3.cpp` // (they include few custom changes). extern fn ImGui_ImplSDL3_InitForSDLGPU(window: *const anyopaque) bool; +extern fn ImGui_ImplSDL3_InitForOpenGL(window: *const anyopaque, sdl_gl_context: *const anyopaque) bool; extern fn ImGui_ImplSDL3_ProcessEvent(event: *const anyopaque) bool; extern fn ImGui_ImplSDL3_NewFrame() void; extern fn ImGui_ImplSDL3_Shutdown() void; + +//TODO: extern fn ImGui_ImplSDL3_InitForVulkan(window: *const anyopaque) bool; +//TODO: extern fn ImGui_ImplSDL3_InitForD3D(window: *const anyopaque) bool; +//TODO: extern fn ImGui_ImplSDL3_InitForMetal(window: *const anyopaque) bool; +//TODO: extern fn ImGui_ImplSDL3_InitForSDLRenderer(window: *const anyopaque, renderer: *const anyopaque) bool; +//TODO: extern fn ImGui_ImplSDL3_InitForOther(window: *const anyopaque) bool; diff --git a/src/backend_sdl3_opengl.zig b/src/backend_sdl3_opengl.zig new file mode 100644 index 0000000..11acdda --- /dev/null +++ b/src/backend_sdl3_opengl.zig @@ -0,0 +1,52 @@ +const gui = @import("gui.zig"); +const backend_sdl3 = @import("backend_sdl3.zig"); + +pub fn initWithGlSlVersion( + window: *const anyopaque, // SDL_Window + context: *const anyopaque, // SDL_GL_Context + glsl_version: ?[:0]const u8, // e.g. "#version 130" +) void { + backend_sdl3.initOpenGL(window, context); + + ImGui_ImplOpenGL3_Init(@ptrCast(glsl_version)); +} + +pub fn init( + window: *const anyopaque, // SDL_Window + context: *const anyopaque, // SDL_GL_Context +) void { + initWithGlSlVersion(window, context, null); +} + +pub fn processEvent( + event: *const anyopaque, // SDL_Event +) bool { + return backend_sdl3.processEvent(event); +} + +pub fn deinit() void { + ImGui_ImplOpenGL3_Shutdown(); + backend_sdl3.deinit(); +} + +pub fn newFrame(fb_width: u32, fb_height: u32) void { + ImGui_ImplOpenGL3_NewFrame(); + backend_sdl3.newFrame(); + + gui.io.setDisplaySize(@as(f32, @floatFromInt(fb_width)), @as(f32, @floatFromInt(fb_height))); + gui.io.setDisplayFramebufferScale(1.0, 1.0); + + gui.newFrame(); +} + +pub fn draw() void { + gui.render(); + ImGui_ImplOpenGL3_RenderDrawData(gui.getDrawData()); +} + +// These functions are defined in 'imgui_impl_opengl3.cpp` +// (they include few custom changes). +extern fn ImGui_ImplOpenGL3_Init(glsl_version: [*c]const u8) void; +extern fn ImGui_ImplOpenGL3_Shutdown() void; +extern fn ImGui_ImplOpenGL3_NewFrame() void; +extern fn ImGui_ImplOpenGL3_RenderDrawData(data: *const anyopaque) void; diff --git a/src/gui.zig b/src/gui.zig index ccd7983..eace0de 100644 --- a/src/gui.zig +++ b/src/gui.zig @@ -16,9 +16,11 @@ pub const backend = switch (@import("zgui_options").backend) { .glfw_vulkan => @import("backend_glfw_vulkan.zig"), .glfw => @import("backend_glfw.zig"), .win32_dx12 => @import("backend_win32_dx12.zig"), - .sdl2_opengl3 => @import("backend_sdl2_opengl.zig"), .osx_metal => @import("backend_osx_metal.zig"), .sdl2 => @import("backend_sdl2.zig"), + .sdl2_opengl3 => @import("backend_sdl2_opengl.zig"), + .sdl3 => @import("backend_sdl3.zig"), + .sdl3_opengl3 => @import("backend_sdl3_opengl.zig"), .sdl3_gpu => @import("backend_sdl3_gpu.zig"), .no_backend => .{}, };