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