Skip to content

Adding backend sdl3 + opengl3 #44

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

Merged
merged 1 commit into from
May 10, 2025
Merged
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
25 changes: 25 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 => {},
}

Expand Down
16 changes: 16 additions & 0 deletions src/backend_sdl3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
52 changes: 52 additions & 0 deletions src/backend_sdl3_opengl.zig
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

I think the order of things here is wrong. But we can come back to it. See the open PR re DPI scaling.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I based this on the sdl2_opengl backend ^^. except that I do the opengl newframe before sdl3 newframe.
I seen that here:
https://github.com/ocornut/imgui/blob/f484af34c2014e98a64410e6cd81e1a5ab434bfd/examples/example_sdl3_opengl3/main.cpp#L162C12-L165C1
But tbh I think the order has no importance as the init before that is in the reverse order.

Copy link
Member

@hazeycode hazeycode May 10, 2025

Choose a reason for hiding this comment

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

Yes, fractional scaling is broken for (I think) all backends. We should fix the OpenGL backends (and others) in the same fashion as the wgpu one. See issue #3 and PRs #42 and #34.


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;
4 changes: 3 additions & 1 deletion src/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 => .{},
};
Expand Down
Loading