From 1f42a3a6c75b6de6d46ba979e94a0b3b3adb7154 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sat, 31 Dec 2022 00:47:48 -0600 Subject: [PATCH] feat: add backend impl for glfw+opengl3 --- src/backend_glfw_opengl3.zig | 38 ++++++++++++++++++++++++++++++++++++ src/main.zig | 1 + 2 files changed, 39 insertions(+) create mode 100644 src/backend_glfw_opengl3.zig diff --git a/src/backend_glfw_opengl3.zig b/src/backend_glfw_opengl3.zig new file mode 100644 index 0000000..785ed0b --- /dev/null +++ b/src/backend_glfw_opengl3.zig @@ -0,0 +1,38 @@ +const zgui = @import("gui.zig"); + +pub fn init(window: *anyopaque, glsl_version: []const u8) void { + if (!ImGui_ImplGlfw_InitForOpenGL(window, true)) unreachable; + if (!ImGui_ImplOpenGL3_Init(glsl_version.ptr)) unreachable; +} + +pub fn deinit() void { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); +} + +pub fn newFrame(width: f32, height: f32) void { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + + zgui.io.setDisplaySize(width, height); + zgui.io.setDisplayFramebufferScale(1.0, 1.0); + + zgui.newFrame(); +} + +pub fn render() void { + zgui.render(); +} + +pub fn draw() void { + ImGui_ImplOpenGL3_RenderDrawData(zgui.getDrawData()); +} + +extern fn ImGui_ImplGlfw_InitForOpenGL(window: *anyopaque, install_callbacks: bool) bool; +extern fn ImGui_ImplGlfw_Shutdown() void; +extern fn ImGui_ImplGlfw_NewFrame() void; + +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; // ImDrawData diff --git a/src/main.zig b/src/main.zig index 3c85326..cc9e9e6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -10,6 +10,7 @@ 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 => @import("backend_glfw_opengl3.zig"), .win32_dx12 => .{}, // TODO: .no_backend => .{}, };