From b051d0c406be7e0d72d41043f679c8fb778a8331 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sun, 1 Jan 2023 02:17:33 -0600 Subject: [PATCH 1/3] feat: add sdl2-opengl3 backend impl --- src/backend_sdl2_opengl3.zig | 42 ++++++++++++++++++++++++++++++++++++ src/main.zig | 2 ++ 2 files changed, 44 insertions(+) create mode 100644 src/backend_sdl2_opengl3.zig diff --git a/src/backend_sdl2_opengl3.zig b/src/backend_sdl2_opengl3.zig new file mode 100644 index 0000000..7eeb081 --- /dev/null +++ b/src/backend_sdl2_opengl3.zig @@ -0,0 +1,42 @@ +const zgui = @import("gui.zig"); + +const SDL_GL_Context = *anyopaque; + +pub fn init(window: *anyopaque, context: SDL_GL_Context, glsl_version: []const u8) void { + if (!ImGui_ImplSDL2_InitForOpenGL(window, context)) unreachable; + if (!ImGui_ImplOpenGL3_Init(glsl_version.ptr)) unreachable; +} + +pub fn deinit() void { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplSDL2_Shutdown(); +} + +pub fn newFrame(width: f32, height: f32) void { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplSDL2_NewFrame(); + + zgui.io.setDisplaySize(width, height); + zgui.io.setDisplayFramebufferScale(1.0, 1.0); + + zgui.newFrame(); +} + +pub fn draw() void { + zgui.render(); + ImGui_ImplOpenGL3_RenderDrawData(zgui.getDrawData()); +} + +pub fn processEvent(event: *anyopaque) bool { + return ImGui_ImplSDL2_ProcessEvent(event); +} + +extern fn ImGui_ImplSDL2_InitForOpenGL(window: *anyopaque, sdl_gl_context: SDL_GL_Context) bool; +extern fn ImGui_ImplSDL2_Shutdown() void; +extern fn ImGui_ImplSDL2_NewFrame() void; +extern fn ImGui_ImplSDL2_ProcessEvent(event: *anyopaque) bool; + +extern fn ImGui_ImplOpenGL3_Init(glsl_version: [*]const u8) bool; +extern fn ImGui_ImplOpenGL3_Shutdown() void; +extern fn ImGui_ImplOpenGL3_NewFrame() void; +extern fn ImGui_ImplOpenGL3_RenderDrawData(draw_data: *anyopaque) void; diff --git a/src/main.zig b/src/main.zig index 3c85326..9c4cc9d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -10,6 +10,8 @@ pub usingnamespace @import("gui.zig"); pub const plot = @import("plot.zig"); pub const backend = switch (@import("zgui_options").backend) { .glfw_wgpu => @import("backend_glfw_wgpu.zig"), + .glfw_opengl3 => {}, // See glfw-opengl3-backend branch + .sdl2_opengl3 => @import("backend_sdl2_opengl3.zig"), .win32_dx12 => .{}, // TODO: .no_backend => .{}, }; From 78922b64eac856e5f28c9fb27bf7b0e1c96d8f1b Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sun, 1 Jan 2023 12:54:18 -0600 Subject: [PATCH 2/3] fix: vcpkg stores SDL.h under SDL2 folder macOS and Linux seem to not, so in order to get CI working again I should make use of the preprocessor. FIXME: I think this makes vcpkg a **hard** requirement which perhaps isn't ideal if I want ZBA to be as easy to build as possible. I should try ensuring that building w/out vcpkg works as well --- libs/imgui/backends/imgui_impl_sdl.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libs/imgui/backends/imgui_impl_sdl.cpp b/libs/imgui/backends/imgui_impl_sdl.cpp index fc26bf3..d63c6e4 100644 --- a/libs/imgui/backends/imgui_impl_sdl.cpp +++ b/libs/imgui/backends/imgui_impl_sdl.cpp @@ -67,8 +67,14 @@ #include "imgui_impl_sdl.h" // SDL -#include -#include +#if defined(__WIN32__) + #include + #include +#else + #include + #include +#endif + #if defined(__APPLE__) #include #endif From 55efe08bdfef961e9081f3cf63ba9c2c163fa8c4 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 10 Mar 2023 22:04:35 -0600 Subject: [PATCH 3/3] chore: explicitly link SDL2 TODO: What happens on Windows? macOS? --- build.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/build.zig b/build.zig index 50f3e3c..3c1e15a 100644 --- a/build.zig +++ b/build.zig @@ -107,6 +107,7 @@ pub fn package( .sdl2_opengl3 => { zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_sdl.cpp", cflags); zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_opengl3.cpp", cflags); + zgui_c_cpp.linkSystemLibrary("SDL2"); }, .no_backend => {}, }