diff options
| author | s-ol <s+removethis@s-ol.nu> | 2021-10-01 10:03:28 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2021-10-01 10:28:50 +0000 |
| commit | 531c1e8079aac9fb7a12e8fe9ebd78f795870972 (patch) | |
| tree | 3e4c538942be56b0cd810fb3252778cc9cb79e72 /src | |
| parent | update for zig 0.7.0 (diff) | |
| download | zig-imgui-531c1e8079aac9fb7a12e8fe9ebd78f795870972.tar.gz zig-imgui-531c1e8079aac9fb7a12e8fe9ebd78f795870972.zip | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/debug_gl.zig | 4 | ||||
| -rw-r--r-- | src/demo.zig | 25 | ||||
| -rw-r--r-- | src/gl3_impl.zig | 10 | ||||
| -rw-r--r-- | src/glfw_impl.zig | 6 | ||||
| -rw-r--r-- | src/main.zig | 145 |
5 files changed, 113 insertions, 77 deletions
diff --git a/src/debug_gl.zig b/src/debug_gl.zig index debf51d..1567e7c 100644 --- a/src/debug_gl.zig +++ b/src/debug_gl.zig @@ -4,10 +4,10 @@ const os = std.os; const panic = std.debug.panic; const builtin = @import("builtin"); -pub const is_on = if (builtin.mode == builtin.Mode.ReleaseFast) c.GL_FALSE else c.GL_TRUE; +pub const is_on = if (builtin.mode == .ReleaseFast) c.GL_FALSE else c.GL_TRUE; pub fn assertNoError() void { - if (builtin.mode != builtin.Mode.ReleaseFast) { + if (builtin.mode != .ReleaseFast) { const err = c.glGetError(); if (err != c.GL_NO_ERROR) { panic("GL error: {}\n", err); diff --git a/src/demo.zig b/src/demo.zig new file mode 100644 index 0000000..cf27a2c --- /dev/null +++ b/src/demo.zig @@ -0,0 +1,25 @@ +const ig = @import("main.zig"); + +pub fn main() !void { + const window = ig.Window.init(); + + // const start_time = c.glfwGetTime(); + // var prev_time = start_time; + var show_demo = false; + + while (!window.shouldClose()) { + try window.beginFrame(); + + // main part + if (show_demo) + ig.igShowDemoWindow(null); + + if (ig.igBegin("yolozwek", null, 0)) { + ig.igText("Hello World :)"); + _ = ig.igCheckbox("Demo window", &show_demo); + ig.igEnd(); + } + + window.endFrame(); + } +} diff --git a/src/gl3_impl.zig b/src/gl3_impl.zig index 27212ff..8f84d8b 100644 --- a/src/gl3_impl.zig +++ b/src/gl3_impl.zig @@ -155,11 +155,11 @@ fn CheckThing(comptime thingType: CheckableThing, handle: c.GLuint, desc: []cons var buf: [1024]u8 = undefined; var length: c.GLsizei = undefined; getInfoLogFunc(handle, buf.len, &length, &buf[0]); - debug.warn("{}\n", .{buf[0..@intCast(usize, length)]}); + debug.warn("{s}\n", .{buf[0..@intCast(usize, length)]}); } if (@intCast(c.GLboolean, status) == c.GL_FALSE) { - debug.warn("ERROR: CreateDeviceObjects: failed to compile/link {}! (with GLSL '{}')\n", .{ desc, g_GlslVersionString }); + debug.warn("ERROR: CreateDeviceObjects: failed to compile/link {s}! (with GLSL '{s}')\n", .{ desc, g_GlslVersionString }); return error.ShaderLinkError; } } @@ -443,7 +443,7 @@ fn SetupRenderState(draw_data: *c.ImDrawData, fb_width: i32, fb_height: i32, ver c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxPos)); c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxUV)); c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxColor)); - c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxPos), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @byteOffsetOf(c.ImDrawVert, "pos"))); - c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxUV), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @byteOffsetOf(c.ImDrawVert, "uv"))); - c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxColor), 4, c.GL_UNSIGNED_BYTE, c.GL_TRUE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @byteOffsetOf(c.ImDrawVert, "col"))); + c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxPos), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @offsetOf(c.ImDrawVert, "pos"))); + c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxUV), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @offsetOf(c.ImDrawVert, "uv"))); + c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxColor), 4, c.GL_UNSIGNED_BYTE, c.GL_TRUE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @offsetOf(c.ImDrawVert, "col"))); } diff --git a/src/glfw_impl.zig b/src/glfw_impl.zig index 7d28e08..439c749 100644 --- a/src/glfw_impl.zig +++ b/src/glfw_impl.zig @@ -35,7 +35,7 @@ pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientAp io.*.BackendFlags |= c.ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) if (false) io.*.BackendFlags |= c.ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) if (false and @hasField(c, "GLFW_HAS_GLFW_HOVERED") and builtin.os == builtin.Os.windows) { - io.*.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy) + io.*.BackendFlags |= c.ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy) } io.*.BackendPlatformName = "imgui_impl_glfw.zig"; @@ -166,8 +166,8 @@ fn UpdateMonitors() void { var w: c_int = undefined; var h: c_int = undefined; c.glfwGetMonitorWorkarea(glfw_monitor, &x, &y, &w, &h); - monitor.*.WorkPos = ImVec2{ .x = @intToFloat(f32, x), .y = @intToFloat(f32, y) }; - monitor.*.WorkSize = ImVec2{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) }; + monitor.*.WorkPos = c.ImVec2{ .x = @intToFloat(f32, x), .y = @intToFloat(f32, y) }; + monitor.*.WorkSize = c.ImVec2{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) }; } if (false and c.GLFW_HAS_PER_MONITOR_DPI) { // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, diff --git a/src/main.zig b/src/main.zig index ba1c92e..e3eb3dd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -7,92 +7,103 @@ const glfw_impl = @import("glfw_impl.zig"); const gl3_impl = @import("gl3_impl.zig"); fn errorCallback(err: c_int, description: [*c]const u8) callconv(.C) void { - panic("Error: {}\n", .{ description }); + _ = err; + panic("Error: {s}\n", .{description}); } -var window: *c.GLFWwindow = undefined; - -fn handleStuff(path: [*c]const u8, x: i32, y: i32) callconv(.C) bool { - return true; -} - -pub fn main() !void { - _ = c.glfwSetErrorCallback(errorCallback); - - if (c.glfwInit() == c.GL_FALSE) { - panic("GLFW init failure\n", .{}); - } - defer c.glfwTerminate(); - - c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3); - c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2); - c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE); - c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on); - c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE); - // c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0); - // c.glfwWindowHint(c.GLFW_STENCIL_BITS, 8); - c.glfwWindowHint(c.GLFW_RESIZABLE, c.GL_TRUE); - - const window_width = 640; - const window_height = 480; - window = c.glfwCreateWindow(window_width, window_height, "ImGUI Test", null, null) orelse { - panic("unable to create window\n", .{}); - }; - defer c.glfwDestroyWindow(window); - - c.glfwMakeContextCurrent(window); - c.glfwSwapInterval(1); - - const context = c.igCreateContext(null); - defer c.igDestroyContext(context); - - const io = c.igGetIO(); - io.*.ConfigFlags |= c.ImGuiConfigFlags_NavEnableKeyboard; - io.*.ConfigFlags |= c.ImGuiConfigFlags_DockingEnable; - // io.*.ConfigFlags |= c.ImGuiConfigFlags_ViewportsEnable; - - const style = c.igGetStyle(); - c.igStyleColorsDark(style); - - if (false and io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0) { - style.*.WindowRounding = 0.0; - style.*.Colors[@enumToInt(c.ImGuiCol_WindowBg)].w = 1.0; +pub usingnamespace c; + +pub const Window = struct { + window: *c.GLFWwindow, + context: *c.ImGuiContext, + + pub fn init() Window { + _ = c.glfwSetErrorCallback(errorCallback); + + if (c.glfwInit() == c.GL_FALSE) { + panic("GLFW init failure\n", .{}); + } + errdefer c.glfwTerminate(); + + c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3); + c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2); + c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE); + c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on); + c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE); + // c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0); + // c.glfwWindowHint(c.GLFW_STENCIL_BITS, 8); + c.glfwWindowHint(c.GLFW_RESIZABLE, c.GL_TRUE); + + const window_width = 640; + const window_height = 480; + const window = c.glfwCreateWindow(window_width, window_height, "ImGUI Test", null, null) orelse { + panic("unable to create window\n", .{}); + }; + errdefer c.glfwDestroyWindow(window); + + c.glfwMakeContextCurrent(window); + c.glfwSwapInterval(1); + + const context = c.igCreateContext(null) orelse panic("ImGui init failure\n", .{}); + errdefer c.igDestroyContext(context); + + const io = c.igGetIO(); + io.*.ConfigFlags |= c.ImGuiConfigFlags_NavEnableKeyboard; + io.*.ConfigFlags |= c.ImGuiConfigFlags_DockingEnable; + // io.*.ConfigFlags |= c.ImGuiConfigFlags_ViewportsEnable; + + const style = c.igGetStyle(); + c.igStyleColorsDark(style); + + if (io.*.ConfigFlags & c.ImGuiConfigFlags_ViewportsEnable != 0) { + style.*.WindowRounding = 0.0; + style.*.Colors[c.ImGuiCol_WindowBg].w = 1.0; + } + + glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL); + errdefer glfw_impl.Shutdown(); + + gl3_impl.Init(); + errdefer gl3_impl.Shutdown(); + + return .{ + .window = window, + .context = context, + }; } - glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL); - defer glfw_impl.Shutdown(); + pub fn deinit(self: *const Window) void { + gl3_impl.Shutdown(); + glfw_impl.Shutdown(); + c.igDestroyContext(self.context); + c.glfwDestroyWindow(self.window); + c.glfwTerminate(); + } - gl3_impl.Init(); // #version 150 - defer gl3_impl.Shutdown(); + pub fn shouldClose(self: *const Window) bool { + return c.glfwWindowShouldClose(self.window) == c.GL_TRUE; + } - const start_time = c.glfwGetTime(); - var prev_time = start_time; + pub fn beginFrame(self: *const Window) !void { + _ = self; - while (c.glfwWindowShouldClose(window) == c.GL_FALSE) { c.glfwPollEvents(); try gl3_impl.NewFrame(); glfw_impl.NewFrame(); c.igNewFrame(); + } - // main part - c.igShowDemoWindow(null); - + pub fn endFrame(self: *const Window) void { c.igRender(); var w: c_int = undefined; var h: c_int = undefined; - c.glfwGetFramebufferSize(window, &w, &h); + c.glfwGetFramebufferSize(self.window, &w, &h); c.glViewport(0, 0, w, h); c.glClearColor(0.0, 0.0, 0.0, 0.0); c.glClear(c.GL_COLOR_BUFFER_BIT); gl3_impl.RenderDrawData(c.igGetDrawData()); - // const now_time = c.glfwGetTime(); - // const elapsed = now_time - prev_time; - // prev_time = now_time; - // nextFrame(t, elapsed); - // draw(t, @This()); - - c.glfwSwapBuffers(window); + c.glfwSwapBuffers(self.window); } -} +}; |
