git.s-ol.nu zig-imgui / 05eb0d1
update for zig 0.7.0 s-ol 2 years ago
5 changed file(s) with 92 addition(s) and 90 deletion(s). Raw diff Collapse all Expand all
11 const builtin = @import("builtin");
22
33 pub fn build(b: *Builder) void {
4 const target = b.standardTargetOptions(.{});
45 const mode = b.standardReleaseOptions();
56 const windows = b.option(bool, "windows", "create windows build") orelse false;
67
78 var exe = b.addExecutable("test", "src/main.zig");
89 exe.setBuildMode(mode);
9
10 if (windows) {
11 exe.setTarget(builtin.Arch.x86_64, builtin.Os.windows, builtin.Abi.gnu);
12 }
1310
1411 exe.linkSystemLibrary("c");
1512 exe.linkSystemLibrary("glfw");
22 @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1");
33 @cInclude("cimgui.h");
44 @cInclude("GLFW/glfw3.h");
5 @cInclude("lo/lo.h");
56 });
2525
2626 pub fn Init() void {
2727 const io = c.igGetIO();
28 io.*.BackendRendererName = c"imgui_impl_gl3.zig";
28 io.*.BackendRendererName = "imgui_impl_gl3.zig";
2929 if (OpenGLHasDrawWithBaseVertex)
3030 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_RendererHasVtxOffset);
3131 // @TODO: Viewports
7373
7474 // @TODO: GLSL versions?
7575 const vertex_shader_glsl: [*]const c.GLchar =
76 c\\#version 150
77 c\\uniform mat4 ProjMtx;
78 c\\in vec2 Position;
79 c\\in vec2 UV;
80 c\\in vec4 Color;
81 c\\out vec2 Frag_UV;
82 c\\out vec4 Frag_Color;
83 c\\void main()
84 c\\{
85 c\\ Frag_UV = UV;
86 c\\ Frag_Color = Color;
87 c\\ gl_Position = ProjMtx * vec4(Position.xy, 0, 1);
88 c\\}
76 \\#version 150
77 \\uniform mat4 ProjMtx;
78 \\in vec2 Position;
79 \\in vec2 UV;
80 \\in vec4 Color;
81 \\out vec2 Frag_UV;
82 \\out vec4 Frag_Color;
83 \\void main()
84 \\{
85 \\ Frag_UV = UV;
86 \\ Frag_Color = Color;
87 \\ gl_Position = ProjMtx * vec4(Position.xy, 0, 1);
88 \\}
8989 ;
9090
9191 const fragment_shader_glsl: [*]const c.GLchar =
92 c\\#version 150
93 c\\uniform sampler2D Texture;
94 c\\in vec2 Frag_UV;
95 c\\in vec4 Frag_Color;
96 c\\out vec4 Out_Color;
97 c\\void main()
98 c\\{
99 c\\ Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
100 c\\}
92 \\#version 150
93 \\uniform sampler2D Texture;
94 \\in vec2 Frag_UV;
95 \\in vec4 Frag_Color;
96 \\out vec4 Out_Color;
97 \\void main()
98 \\{
99 \\ Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
100 \\}
101101 ;
102102
103103 // Create shaders / programs
117117 c.glLinkProgram(g_ShaderHandle);
118118 try CheckThing(.Program, g_ShaderHandle, "shader program");
119119
120 g_AttribLocationTex = c.glGetUniformLocation(g_ShaderHandle, c"Texture");
121 g_AttribLocationProjMtx = c.glGetUniformLocation(g_ShaderHandle, c"ProjMtx");
122 g_AttribLocationVtxPos = c.glGetAttribLocation(g_ShaderHandle, c"Position");
123 g_AttribLocationVtxUV = c.glGetAttribLocation(g_ShaderHandle, c"UV");
124 g_AttribLocationVtxColor = c.glGetAttribLocation(g_ShaderHandle, c"Color");
120 g_AttribLocationTex = c.glGetUniformLocation(g_ShaderHandle, "Texture");
121 g_AttribLocationProjMtx = c.glGetUniformLocation(g_ShaderHandle, "ProjMtx");
122 g_AttribLocationVtxPos = c.glGetAttribLocation(g_ShaderHandle, "Position");
123 g_AttribLocationVtxUV = c.glGetAttribLocation(g_ShaderHandle, "UV");
124 g_AttribLocationVtxColor = c.glGetAttribLocation(g_ShaderHandle, "Color");
125125
126126 // Create buffers
127127 c.glGenBuffers(1, &g_VboHandle);
154154 var buf: [1024]u8 = undefined;
155155 var length: c.GLsizei = undefined;
156156 getInfoLogFunc(handle, buf.len, &length, &buf[0]);
157 debug.warn("{}\n", buf[0..@intCast(usize, length)]);
157 debug.warn("{}\n", .{buf[0..@intCast(usize, length)]});
158158 }
159159
160160 if (@intCast(c.GLboolean, status) == c.GL_FALSE) {
161 debug.warn("ERROR: CreateDeviceObjects: failed to compile/link {}! (with GLSL '{}')\n", desc, g_GlslVersionString);
161 debug.warn("ERROR: CreateDeviceObjects: failed to compile/link {}! (with GLSL '{}')\n", .{ desc, g_GlslVersionString });
162162 return error.ShaderLinkError;
163163 }
164164 }
1414 var g_ClientApi: ClientApi = .Unknown;
1515 var g_Time: f64 = 0.0;
1616 var g_MouseJustPressed = [_]bool{false} ** 5;
17 var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT);
17 var g_MouseCursors = [_]?*c.GLFWcursor{null} ** c.ImGuiMouseCursor_COUNT;
1818 var g_WantUpdateMonitors = true;
1919
2020 // Chain GLFW callbacks for main viewport:
3030
3131 // Setup back-end capabilities flags
3232 const io = c.igGetIO();
33 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional)
34 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used)
35 if (false) io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_PlatformHasViewports); // We can create multi-viewports on the Platform side (optional)
33 io.*.BackendFlags |= c.ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
34 io.*.BackendFlags |= c.ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
35 if (false) io.*.BackendFlags |= c.ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
3636 if (false and @hasField(c, "GLFW_HAS_GLFW_HOVERED") and builtin.os == builtin.Os.windows) {
37 io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy)
38 }
39 io.*.BackendPlatformName = c"imgui_impl_glfw.zig";
37 io.*.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
38 }
39 io.*.BackendPlatformName = "imgui_impl_glfw.zig";
4040
4141 // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
42 io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB;
43 io.*.KeyMap[@enumToInt(c.ImGuiKey_LeftArrow)] = c.GLFW_KEY_LEFT;
44 io.*.KeyMap[@enumToInt(c.ImGuiKey_RightArrow)] = c.GLFW_KEY_RIGHT;
45 io.*.KeyMap[@enumToInt(c.ImGuiKey_UpArrow)] = c.GLFW_KEY_UP;
46 io.*.KeyMap[@enumToInt(c.ImGuiKey_DownArrow)] = c.GLFW_KEY_DOWN;
47 io.*.KeyMap[@enumToInt(c.ImGuiKey_PageUp)] = c.GLFW_KEY_PAGE_UP;
48 io.*.KeyMap[@enumToInt(c.ImGuiKey_PageDown)] = c.GLFW_KEY_PAGE_DOWN;
49 io.*.KeyMap[@enumToInt(c.ImGuiKey_Home)] = c.GLFW_KEY_HOME;
50 io.*.KeyMap[@enumToInt(c.ImGuiKey_End)] = c.GLFW_KEY_END;
51 io.*.KeyMap[@enumToInt(c.ImGuiKey_Insert)] = c.GLFW_KEY_INSERT;
52 io.*.KeyMap[@enumToInt(c.ImGuiKey_Delete)] = c.GLFW_KEY_DELETE;
53 io.*.KeyMap[@enumToInt(c.ImGuiKey_Backspace)] = c.GLFW_KEY_BACKSPACE;
54 io.*.KeyMap[@enumToInt(c.ImGuiKey_Space)] = c.GLFW_KEY_SPACE;
55 io.*.KeyMap[@enumToInt(c.ImGuiKey_Enter)] = c.GLFW_KEY_ENTER;
56 io.*.KeyMap[@enumToInt(c.ImGuiKey_Escape)] = c.GLFW_KEY_ESCAPE;
57 io.*.KeyMap[@enumToInt(c.ImGuiKey_KeyPadEnter)] = c.GLFW_KEY_KP_ENTER;
58 io.*.KeyMap[@enumToInt(c.ImGuiKey_A)] = c.GLFW_KEY_A;
59 io.*.KeyMap[@enumToInt(c.ImGuiKey_C)] = c.GLFW_KEY_C;
60 io.*.KeyMap[@enumToInt(c.ImGuiKey_V)] = c.GLFW_KEY_V;
61 io.*.KeyMap[@enumToInt(c.ImGuiKey_X)] = c.GLFW_KEY_X;
62 io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y;
63 io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z;
42 io.*.KeyMap[c.ImGuiKey_Tab] = c.GLFW_KEY_TAB;
43 io.*.KeyMap[c.ImGuiKey_LeftArrow] = c.GLFW_KEY_LEFT;
44 io.*.KeyMap[c.ImGuiKey_RightArrow] = c.GLFW_KEY_RIGHT;
45 io.*.KeyMap[c.ImGuiKey_UpArrow] = c.GLFW_KEY_UP;
46 io.*.KeyMap[c.ImGuiKey_DownArrow] = c.GLFW_KEY_DOWN;
47 io.*.KeyMap[c.ImGuiKey_PageUp] = c.GLFW_KEY_PAGE_UP;
48 io.*.KeyMap[c.ImGuiKey_PageDown] = c.GLFW_KEY_PAGE_DOWN;
49 io.*.KeyMap[c.ImGuiKey_Home] = c.GLFW_KEY_HOME;
50 io.*.KeyMap[c.ImGuiKey_End] = c.GLFW_KEY_END;
51 io.*.KeyMap[c.ImGuiKey_Insert] = c.GLFW_KEY_INSERT;
52 io.*.KeyMap[c.ImGuiKey_Delete] = c.GLFW_KEY_DELETE;
53 io.*.KeyMap[c.ImGuiKey_Backspace] = c.GLFW_KEY_BACKSPACE;
54 io.*.KeyMap[c.ImGuiKey_Space] = c.GLFW_KEY_SPACE;
55 io.*.KeyMap[c.ImGuiKey_Enter] = c.GLFW_KEY_ENTER;
56 io.*.KeyMap[c.ImGuiKey_Escape] = c.GLFW_KEY_ESCAPE;
57 io.*.KeyMap[c.ImGuiKey_KeyPadEnter] = c.GLFW_KEY_KP_ENTER;
58 io.*.KeyMap[c.ImGuiKey_A] = c.GLFW_KEY_A;
59 io.*.KeyMap[c.ImGuiKey_C] = c.GLFW_KEY_C;
60 io.*.KeyMap[c.ImGuiKey_V] = c.GLFW_KEY_V;
61 io.*.KeyMap[c.ImGuiKey_X] = c.GLFW_KEY_X;
62 io.*.KeyMap[c.ImGuiKey_Y] = c.GLFW_KEY_Y;
63 io.*.KeyMap[c.ImGuiKey_Z] = c.GLFW_KEY_Z;
6464
6565 // @TODO: Clipboard
6666 // io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
6767 // io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
6868 io.*.ClipboardUserData = g_Window;
6969
70 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Arrow)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR);
71 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_TextInput)] = c.glfwCreateStandardCursor(c.GLFW_IBEAM_CURSOR);
72 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeAll)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
73 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNS)] = c.glfwCreateStandardCursor(c.GLFW_VRESIZE_CURSOR);
74 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeEW)] = c.glfwCreateStandardCursor(c.GLFW_HRESIZE_CURSOR);
75 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNESW)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
76 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNWSE)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
77 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Hand)] = c.glfwCreateStandardCursor(c.GLFW_HAND_CURSOR);
70 g_MouseCursors[c.ImGuiMouseCursor_Arrow] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR);
71 g_MouseCursors[c.ImGuiMouseCursor_TextInput] = c.glfwCreateStandardCursor(c.GLFW_IBEAM_CURSOR);
72 g_MouseCursors[c.ImGuiMouseCursor_ResizeAll] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
73 g_MouseCursors[c.ImGuiMouseCursor_ResizeNS] = c.glfwCreateStandardCursor(c.GLFW_VRESIZE_CURSOR);
74 g_MouseCursors[c.ImGuiMouseCursor_ResizeEW] = c.glfwCreateStandardCursor(c.GLFW_HRESIZE_CURSOR);
75 g_MouseCursors[c.ImGuiMouseCursor_ResizeNESW] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
76 g_MouseCursors[c.ImGuiMouseCursor_ResizeNWSE] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
77 g_MouseCursors[c.ImGuiMouseCursor_Hand] = c.glfwCreateStandardCursor(c.GLFW_HAND_CURSOR);
7878
7979 // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
8080 g_PrevUserCallbackMousebutton = null;
9191 // Our mouse update function expect PlatformHandle to be filled for the main viewport
9292 const main_viewport = c.igGetMainViewport();
9393 main_viewport.*.PlatformHandle = g_Window;
94 if (builtin.os == builtin.Os.windows)
95 main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
94 // if (builtin.os == builtin.Os.windows)
95 // main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
9696
9797 // @TODO: Platform Interface (Viewport)
98 if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0)
98 if (io.*.ConfigFlags & c.ImGuiConfigFlags_ViewportsEnable != 0)
9999 unreachable;
100100 // ImGui_ImplGlfw_InitPlatformInterface();
101101
206206 var mouse_x: f64 = undefined;
207207 var mouse_y: f64 = undefined;
208208 c.glfwGetCursorPos(window, &mouse_x, &mouse_y);
209 if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0) {
209 if (io.*.ConfigFlags & c.ImGuiConfigFlags_ViewportsEnable != 0) {
210210 // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
211211 var window_x: c_int = undefined;
212212 var window_y: c_int = undefined;
229229
230230 fn UpdateMouseCursor() void {
231231 const io = c.igGetIO();
232 if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_NoMouseCursorChange) != 0 or c.glfwGetInputMode(g_Window, c.GLFW_CURSOR) == c.GLFW_CURSOR_DISABLED)
232 if (io.*.ConfigFlags & c.ImGuiConfigFlags_NoMouseCursorChange != 0 or c.glfwGetInputMode(g_Window, c.GLFW_CURSOR) == c.GLFW_CURSOR_DISABLED)
233233 return;
234234
235235 const imgui_cursor = c.igGetMouseCursor();
237237 var n: usize = 0;
238238 while (n < @intCast(usize, platform_io.*.Viewports.Size)) : (n += 1) {
239239 const window = @ptrCast(*c.GLFWwindow, platform_io.*.Viewports.Data[n].*.PlatformHandle);
240 if (imgui_cursor == @enumToInt(c.ImGuiMouseCursor_None) or io.*.MouseDrawCursor) {
240 if (imgui_cursor == c.ImGuiMouseCursor_None or io.*.MouseDrawCursor) {
241241 // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
242242 c.glfwSetInputMode(window, c.GLFW_CURSOR, c.GLFW_CURSOR_HIDDEN);
243243 } else {
244244 // Show OS mouse cursor
245245 // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
246 c.glfwSetCursor(window, if (g_MouseCursors[@intCast(usize, imgui_cursor)]) |cursor| cursor else g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Arrow)]);
246 c.glfwSetCursor(window, if (g_MouseCursors[@intCast(usize, imgui_cursor)]) |cursor| cursor else g_MouseCursors[c.ImGuiMouseCursor_Arrow]);
247247 c.glfwSetInputMode(window, c.GLFW_CURSOR, c.GLFW_CURSOR_NORMAL);
248248 }
249249 }
254254 }
255255
256256 // GLFW Callbacks
257 extern fn Callback_MouseButton(window: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) void {
257 fn Callback_MouseButton(window: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) callconv(.C) void {
258258 if (g_PrevUserCallbackMousebutton) |prev| {
259259 prev(window, button, action, mods);
260260 }
267267 g_MouseJustPressed[button_u] = true;
268268 }
269269
270 extern fn Callback_Scroll(window: ?*c.GLFWwindow, dx: f64, dy: f64) void {
270 fn Callback_Scroll(window: ?*c.GLFWwindow, dx: f64, dy: f64) callconv(.C) void {
271271 if (g_PrevUserCallbackScroll) |prev| {
272272 prev(window, dx, dy);
273273 }
277277 io.*.MouseWheel += @floatCast(f32, dy);
278278 }
279279
280 extern fn Callback_Key(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, modifiers: c_int) void {
280 fn Callback_Key(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, modifiers: c_int) callconv(.C) void {
281281 if (g_PrevUserCallbackKey) |prev| {
282282 prev(window, key, scancode, action, modifiers);
283283 }
300300 io.*.KeySuper = io.*.KeysDown[c.GLFW_KEY_LEFT_SUPER] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SUPER];
301301 }
302302
303 extern fn Callback_Char(window: ?*c.GLFWwindow, char: c_uint) void {
303 fn Callback_Char(window: ?*c.GLFWwindow, char: c_uint) callconv(.C) void {
304304 if (g_PrevUserCallbackChar) |prev| {
305305 prev(window, char);
306306 }
55 const glfw_impl = @import("glfw_impl.zig");
66 const gl3_impl = @import("gl3_impl.zig");
77
8 extern fn errorCallback(err: c_int, description: [*c]const u8) void {
9 panic("Error: {}\n", description);
8 fn errorCallback(err: c_int, description: [*c]const u8) callconv(.C) void {
9 panic("Error: {}\n", .{ description });
1010 }
1111
1212 var window: *c.GLFWwindow = undefined;
13
14 fn handleStuff(path: [*c]const u8, x: i32, y: i32) callconv(.C) bool {
15 return true;
16 }
1317
1418 pub fn main() !void {
1519 _ = c.glfwSetErrorCallback(errorCallback);
1620
1721 if (c.glfwInit() == c.GL_FALSE) {
18 panic("GLFW init failure\n");
22 panic("GLFW init failure\n", .{});
1923 }
2024 defer c.glfwTerminate();
2125
3034
3135 const window_width = 640;
3236 const window_height = 480;
33 window = c.glfwCreateWindow(window_width, window_height, c"ImGUI Test", null, null) orelse {
34 panic("unable to create window\n");
37 window = c.glfwCreateWindow(window_width, window_height, "ImGUI Test", null, null) orelse {
38 panic("unable to create window\n", .{});
3539 };
3640 defer c.glfwDestroyWindow(window);
3741
4246 defer c.igDestroyContext(context);
4347
4448 const io = c.igGetIO();
45 io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_NavEnableKeyboard);
46 // io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_DockingEnable);
47 // io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_ViewportsEnable);
49 io.*.ConfigFlags |= c.ImGuiConfigFlags_NavEnableKeyboard;
50 io.*.ConfigFlags |= c.ImGuiConfigFlags_DockingEnable;
51 // io.*.ConfigFlags |= c.ImGuiConfigFlags_ViewportsEnable;
4852
4953 const style = c.igGetStyle();
5054 c.igStyleColorsDark(style);