Skip to content

Commit d5d74ee

Browse files
committed
add sdl2 backend
1 parent 655a123 commit d5d74ee

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

build.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub const Backend = enum {
1010
glfw,
1111
sdl2_opengl3,
1212
osx_metal,
13+
sdl2,
1314
};
1415

1516
pub fn build(b: *std.Build) void {
@@ -385,6 +386,17 @@ pub fn build(b: *std.Build) void {
385386
.flags = objcflags,
386387
});
387388
},
389+
.sdl2 => {
390+
if (b.lazyDependency("zsdl", .{})) |zsdl| {
391+
imgui.addIncludePath(zsdl.path("libs/sdl2/include"));
392+
}
393+
imgui.addCSourceFiles(.{
394+
.files = &.{
395+
"libs/imgui/backends/imgui_impl_sdl2.cpp",
396+
},
397+
.flags = cflags,
398+
});
399+
},
388400
.no_backend => {},
389401
}
390402

libs/imgui/backends/imgui_impl_sdl2.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
138138
extern "C" {
139139

140140
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
141+
bool ImGui_ImplSDL2_InitForOther(SDL_Window* window);
141142
void ImGui_ImplSDL2_Shutdown();
142143
void ImGui_ImplSDL2_NewFrame();
143144
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event);

src/backend_sdl2.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
const gui = @import("gui.zig");
22

3+
pub fn init(
4+
window: *const anyopaque, // SDL_Window
5+
) void {
6+
if (!ImGui_ImplSDL2_InitForOther(window)) {
7+
unreachable;
8+
}
9+
}
10+
311
pub fn initOpenGL(
412
window: *const anyopaque, // SDL_Window
513
context: *const anyopaque, // SDL_GL_Context
@@ -24,6 +32,7 @@ pub fn newFrame() void {
2432
}
2533

2634
// These functions are defined in `imgui_impl_sdl2.cpp`
35+
extern fn ImGui_ImplSDL2_InitForOther(window: *const anyopaque) bool;
2736
extern fn ImGui_ImplSDL2_InitForOpenGL(window: *const anyopaque, sdl_gl_context: *const anyopaque) bool;
2837
extern fn ImGui_ImplSDL2_ProcessEvent(event: *const anyopaque) bool;
2938
extern fn ImGui_ImplSDL2_NewFrame() void;

src/gui.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub const backend = switch (@import("zgui_options").backend) {
1818
.win32_dx12 => @import("backend_win32_dx12.zig"),
1919
.sdl2_opengl3 => @import("backend_sdl2_opengl.zig"),
2020
.osx_metal => @import("backend_osx_metal.zig"),
21+
.sdl2 => @import("backend_sdl2.zig"),
2122
.no_backend => .{},
2223
};
2324
const te_enabled = @import("zgui_options").with_te;

0 commit comments

Comments
 (0)