git.s-ol.nu zig-imgui / 883688f
finish GLFW impl s-ol 30 days ago
3 changed file(s) with 107 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
11 pub usingnamespace @cImport({
2 @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1");
3 @cInclude("cimgui.h");
4 @cInclude("GLFW/glfw3.h");
2 @cInclude("epoxy/gl.h");
3 @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1");
4 @cInclude("cimgui.h");
5 @cInclude("GLFW/glfw3.h");
56 });
1010 var g_Window: ?*c.GLFWwindow = null;
1111 var g_ClientApi: ClientApi = .Unknown;
1212 var g_Time: f32 = 0.0;
13 var g_MouseJustPressed = [5]bool;
14 var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT);
13 var g_MouseJustPressed = [_]bool{ false } ** 5;
14 var g_MouseCursors = [_]?*c.GLFWcursor{ null } ** @enumToInt(c.ImGuiMouseCursor_COUNT);
1515 var g_WantUpdateMonitors = true;
1616
1717 // Chain GLFW callbacks for main viewport:
1818 // our callbacks will call the user's previously installed callbacks, if any.
19 var g_PrevUserCallbackMousebutton: ?*c.GLFWmousebuttonfun = null;
20 var g_PrevUserCallbackScroll: ?*c.GLFWscrollfun = null;
21 var g_PrevUserCallbackKey: ?*c.GLFWkeyfun = null;
22 var g_PrevUserCallbackChar: ?*c.GLFWcharfun = null;
19 var g_PrevUserCallbackMousebutton: ?c.GLFWmousebuttonfun = null;
20 var g_PrevUserCallbackScroll: ?c.GLFWscrollfun = null;
21 var g_PrevUserCallbackKey: ?c.GLFWkeyfun = null;
22 var g_PrevUserCallbackChar: ?c.GLFWcharfun = null;
2323
2424 pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientApi) void {
2525 g_Window = window;
3030 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional)
3131 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used)
3232 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_PlatformHasViewports); // We can create multi-viewports on the Platform side (optional)
33 if (false and c.GLFW_HAS_GLFW_HOVERED and builtin.os == builtin.Os.windows) {
33 if (@hasField(c, "GLFW_HAS_GLFW_HOVERED") and builtin.os == builtin.Os.windows) {
3434 io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy)
3535 }
36 io.*.BackendPlatformName = c"imgui_impl_glfw";
36 io.*.BackendPlatformName = c"imgui_impl_glfw.zig";
3737
3838 // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
3939 io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB;
5959 io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y;
6060 io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z;
6161
62 // @TODO: Clipboard
6263 // io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
6364 // io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
6465 // io.ClipboardUserData = g_Window;
7778 g_PrevUserCallbackScroll = null;
7879 g_PrevUserCallbackKey = null;
7980 g_PrevUserCallbackChar = null;
80 if (install_callbacks and false) {
81 // g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
82 // g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
83 // g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
84 // g_PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
81 if (install_callbacks) {
82 g_PrevUserCallbackMousebutton = c.glfwSetMouseButtonCallback(window, Callback_MouseButton);
83 g_PrevUserCallbackScroll = c.glfwSetScrollCallback(window, Callback_Scroll);
84 g_PrevUserCallbackKey = c.glfwSetKeyCallback(window, Callback_Key);
85 g_PrevUserCallbackChar = c.glfwSetCharCallback(window, Callback_Char);
8586 }
8687
8788 // Our mouse update function expect PlatformHandle to be filled for the main viewport
9192 main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
9293 }
9394
95 // @TODO: Platform Interface (Viewport)
9496 // if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable != 0)
9597 // ImGui_ImplGlfw_InitPlatformInterface();
9698
98100 }
99101
100102 pub fn Shutdown() void {
103 // @TODO: Platform Interface (Viewport)
101104 // ImGui_ImplGlfw_ShutdownPlatformInterface();
105
102106 for (g_MouseCursors) |*cursor| {
103107 c.glfwDestroyCursor(cursor.*);
104108 cursor.* = null;
105109 }
106110 g_ClientApi = .Unknown;
107111 }
112
113 pub fn NewFrame() void {
114 const io = c.igGetIO();
115 // @TODO: assert font atlas
116
117 var w : c_int = undefined;
118 var h : c_int = undefined;
119 var display_w : c_int = undefined;
120 var display_h : c_int = undefined;
121 c.glfwGetWindowSize(g_Window, &w, &h);
122 c.glfwGetFramebufferSize(g_Window, &display_w, &display_h);
123 io.*.DisplaySize = c.ImVec2{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) };
124 }
125
126 // GLFW Callbacks
127 extern fn Callback_MouseButton(window: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) void {
128 // @TODO: delegate up
129
130 if (button < 0)
131 return;
132
133 const button_u = @intCast(usize, button);
134 if (action == c.GLFW_PRESS and button_u < g_MouseJustPressed.len)
135 g_MouseJustPressed[button_u] = true;
136 }
137
138 extern fn Callback_Scroll(window: ?*c.GLFWwindow, dx: f64, dy: f64) void {
139 // @TODO: delegate up
140
141 const io = c.igGetIO();
142 io.*.MouseWheelH += @floatCast(f32, dx);
143 io.*.MouseWheel += @floatCast(f32, dy);
144 }
145
146 extern fn Callback_Key(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, modifiers: c_int) void {
147 // @TODO: delegate up
148
149 if (key < 0)
150 unreachable;
151
152 const key_u = @intCast(usize, key);
153
154 const io = c.igGetIO();
155 if (action == c.GLFW_PRESS)
156 io.*.KeysDown[key_u] = true;
157 if (action == c.GLFW_RELEASE)
158 io.*.KeysDown[key_u] = false;
159
160 // Modifiers are not reliable across systems
161 io.*.KeyCtrl = io.*.KeysDown[c.GLFW_KEY_LEFT_CONTROL] or io.*.KeysDown[c.GLFW_KEY_RIGHT_CONTROL];
162 io.*.KeyShift = io.*.KeysDown[c.GLFW_KEY_LEFT_SHIFT] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SHIFT];
163 io.*.KeyAlt = io.*.KeysDown[c.GLFW_KEY_LEFT_ALT] or io.*.KeysDown[c.GLFW_KEY_RIGHT_ALT];
164 io.*.KeySuper = io.*.KeysDown[c.GLFW_KEY_LEFT_SUPER] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SUPER];
165 }
166
167 extern fn Callback_Char(window: ?*c.GLFWwindow, char: c_uint) void {
168 // @TODO: delegate up
169
170 const io = c.igGetIO();
171 c.ImGuiIO_AddInputCharacter(io, char);
172 }
44 const glfw_impl = @import("glfw_impl.zig");
55
66 extern fn errorCallback(err: c_int, description: [*c]const u8) void {
7 panic("Error: {}\n", description);
7 panic("Error: {}\n", description);
88 }
99
1010 var window: *c.GLFWwindow = undefined;
1313 _ = c.glfwSetErrorCallback(errorCallback);
1414
1515 if (c.glfwInit() == c.GL_FALSE) {
16 panic("GLFW init failure\n");
16 panic("GLFW init failure\n");
1717 }
1818 defer c.glfwTerminate();
1919
3030 const window_width = 640;
3131 const window_height = 480;
3232 window = c.glfwCreateWindow(window_width, window_height, c"ImGUI Test", null, null) orelse {
33 panic("unable to create window\n");
33 panic("unable to create window\n");
3434 };
3535 defer c.glfwDestroyWindow(window);
3636
5454 }
5555
5656 glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL);
57 // ImGui_InitForOpenGL(window, true);
57 defer glfw_impl.Shutdown();
58
5859 // ImGui_ImplOpenGL3_Init(glsl_version);
5960
6061 const start_time = c.glfwGetTime();
6162 var prev_time = start_time;
6263
6364 while (c.glfwWindowShouldClose(window) == c.GL_FALSE) {
64 // c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT | c.GL_STENCIL_BUFFER_BIT);
65 c.glfwPollEvents();
6566
66 const now_time = c.glfwGetTime();
67 const elapsed = now_time - prev_time;
68 prev_time = now_time;
67 glfw_impl.NewFrame();
68 // gl3_impl.NewFrame();
69 c.igNewFrame();
6970
71 // main part
72 c.igShowDemoWindow(null);
73
74 c.igRender();
75 var w: c_int = undefined;
76 var h: c_int = undefined;
77 c.glfwGetFramebufferSize(window, &w, &h);
78 c.glViewport(0, 0, w, h);
79 c.glClearColor(0.0, 0.0, 0.0, 0.0);
80 c.glClear(c.GL_COLOR_BUFFER_BIT);
81 // gl3_impl.RenderDrawData(c.igGetDrawData());
82
83 // const now_time = c.glfwGetTime();
84 // const elapsed = now_time - prev_time;
85 // prev_time = now_time;
7086 // nextFrame(t, elapsed);
71
7287 // draw(t, @This());
88
7389 c.glfwSwapBuffers(window);
74
75 c.glfwPollEvents();
7690 }
7791 }