diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 145 |
1 files changed, 78 insertions, 67 deletions
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); } -} +}; |
