Skip to content

Commit 3af85d9

Browse files
authored
Merge pull request #20 from vkensou/feature/add_sdl2_backend
add sdl2 backend
2 parents e808643 + 139e40d commit 3af85d9

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
sdl3_gpu,
1415
};
1516

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

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
.sdl3_gpu => @import("backend_sdl3_gpu.zig"),
2223
.no_backend => .{},
2324
};

0 commit comments

Comments
 (0)