Skip to content

Commit 21dcc28

Browse files
authored
adding SDL3_Opengl (#44)
1 parent b1c775d commit 21dcc28

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

build.zig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ pub const Backend = enum {
1111
sdl2_opengl3,
1212
osx_metal,
1313
sdl2,
14+
sdl3,
1415
sdl3_gpu,
16+
sdl3_opengl3,
1517
};
1618

1719
pub fn build(b: *std.Build) void {
@@ -358,6 +360,29 @@ pub fn build(b: *std.Build) void {
358360
.flags = cflags,
359361
});
360362
},
363+
.sdl3_opengl3 => {
364+
if (b.lazyDependency("zsdl", .{})) |zsdl| {
365+
imgui.addIncludePath(zsdl.path("libs/sdl3/include/SDL3"));
366+
}
367+
imgui.addCSourceFiles(.{
368+
.files = &.{
369+
"libs/imgui/backends/imgui_impl_sdl3.cpp",
370+
"libs/imgui/backends/imgui_impl_opengl3.cpp",
371+
},
372+
.flags = &(cflags.* ++ .{"-DIMGUI_IMPL_OPENGL_LOADER_IMGL3W"}),
373+
});
374+
},
375+
.sdl3 => {
376+
if (b.lazyDependency("zsdl", .{})) |zsdl| {
377+
imgui.addIncludePath(zsdl.path("libs/sdl3/include/SDL3"));
378+
}
379+
imgui.addCSourceFiles(.{
380+
.files = &.{
381+
"libs/imgui/backends/imgui_impl_sdl3.cpp",
382+
},
383+
.flags = cflags,
384+
});
385+
},
361386
.no_backend => {},
362387
}
363388

src/backend_sdl3.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ pub fn initGPU(
88
}
99
}
1010

11+
pub fn initOpenGL(
12+
window: *const anyopaque, // SDL_Window
13+
context: *const anyopaque, // SDL_GL_Context
14+
) void {
15+
if (!ImGui_ImplSDL3_InitForOpenGL(window, context)) {
16+
unreachable;
17+
}
18+
}
19+
1120
pub fn processEvent(
1221
event: *const anyopaque, // SDL_Event
1322
) bool {
@@ -25,6 +34,13 @@ pub fn newFrame() void {
2534
// Those functions are defined in `imgui_impl_sdl3.cpp`
2635
// (they include few custom changes).
2736
extern fn ImGui_ImplSDL3_InitForSDLGPU(window: *const anyopaque) bool;
37+
extern fn ImGui_ImplSDL3_InitForOpenGL(window: *const anyopaque, sdl_gl_context: *const anyopaque) bool;
2838
extern fn ImGui_ImplSDL3_ProcessEvent(event: *const anyopaque) bool;
2939
extern fn ImGui_ImplSDL3_NewFrame() void;
3040
extern fn ImGui_ImplSDL3_Shutdown() void;
41+
42+
//TODO: extern fn ImGui_ImplSDL3_InitForVulkan(window: *const anyopaque) bool;
43+
//TODO: extern fn ImGui_ImplSDL3_InitForD3D(window: *const anyopaque) bool;
44+
//TODO: extern fn ImGui_ImplSDL3_InitForMetal(window: *const anyopaque) bool;
45+
//TODO: extern fn ImGui_ImplSDL3_InitForSDLRenderer(window: *const anyopaque, renderer: *const anyopaque) bool;
46+
//TODO: extern fn ImGui_ImplSDL3_InitForOther(window: *const anyopaque) bool;

src/backend_sdl3_opengl.zig

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const gui = @import("gui.zig");
2+
const backend_sdl3 = @import("backend_sdl3.zig");
3+
4+
pub fn initWithGlSlVersion(
5+
window: *const anyopaque, // SDL_Window
6+
context: *const anyopaque, // SDL_GL_Context
7+
glsl_version: ?[:0]const u8, // e.g. "#version 130"
8+
) void {
9+
backend_sdl3.initOpenGL(window, context);
10+
11+
ImGui_ImplOpenGL3_Init(@ptrCast(glsl_version));
12+
}
13+
14+
pub fn init(
15+
window: *const anyopaque, // SDL_Window
16+
context: *const anyopaque, // SDL_GL_Context
17+
) void {
18+
initWithGlSlVersion(window, context, null);
19+
}
20+
21+
pub fn processEvent(
22+
event: *const anyopaque, // SDL_Event
23+
) bool {
24+
return backend_sdl3.processEvent(event);
25+
}
26+
27+
pub fn deinit() void {
28+
ImGui_ImplOpenGL3_Shutdown();
29+
backend_sdl3.deinit();
30+
}
31+
32+
pub fn newFrame(fb_width: u32, fb_height: u32) void {
33+
ImGui_ImplOpenGL3_NewFrame();
34+
backend_sdl3.newFrame();
35+
36+
gui.io.setDisplaySize(@as(f32, @floatFromInt(fb_width)), @as(f32, @floatFromInt(fb_height)));
37+
gui.io.setDisplayFramebufferScale(1.0, 1.0);
38+
39+
gui.newFrame();
40+
}
41+
42+
pub fn draw() void {
43+
gui.render();
44+
ImGui_ImplOpenGL3_RenderDrawData(gui.getDrawData());
45+
}
46+
47+
// These functions are defined in 'imgui_impl_opengl3.cpp`
48+
// (they include few custom changes).
49+
extern fn ImGui_ImplOpenGL3_Init(glsl_version: [*c]const u8) void;
50+
extern fn ImGui_ImplOpenGL3_Shutdown() void;
51+
extern fn ImGui_ImplOpenGL3_NewFrame() void;
52+
extern fn ImGui_ImplOpenGL3_RenderDrawData(data: *const anyopaque) void;

src/gui.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ pub const backend = switch (@import("zgui_options").backend) {
1616
.glfw_vulkan => @import("backend_glfw_vulkan.zig"),
1717
.glfw => @import("backend_glfw.zig"),
1818
.win32_dx12 => @import("backend_win32_dx12.zig"),
19-
.sdl2_opengl3 => @import("backend_sdl2_opengl.zig"),
2019
.osx_metal => @import("backend_osx_metal.zig"),
2120
.sdl2 => @import("backend_sdl2.zig"),
21+
.sdl2_opengl3 => @import("backend_sdl2_opengl.zig"),
22+
.sdl3 => @import("backend_sdl3.zig"),
23+
.sdl3_opengl3 => @import("backend_sdl3_opengl.zig"),
2224
.sdl3_gpu => @import("backend_sdl3_gpu.zig"),
2325
.no_backend => .{},
2426
};

0 commit comments

Comments
 (0)