diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2019-10-18 14:26:20 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2019-10-18 14:26:32 +0000 |
| commit | f9bd966065755233e9bed0cf79ff4a45fa052521 (patch) | |
| tree | a8dcd6dcc4ba38a04240ae5b27ccb69468b8e904 /src | |
| download | zig-imgui-f9bd966065755233e9bed0cf79ff4a45fa052521.tar.gz zig-imgui-f9bd966065755233e9bed0cf79ff4a45fa052521.zip | |
initial commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/c.zig | 5 | ||||
| -rw-r--r-- | src/glfw_impl.zig | 107 | ||||
| -rw-r--r-- | src/main.zig | 77 |
3 files changed, 189 insertions, 0 deletions
diff --git a/src/c.zig b/src/c.zig new file mode 100644 index 0000000..eeea35f --- /dev/null +++ b/src/c.zig @@ -0,0 +1,5 @@ +pub usingnamespace @cImport({ + @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1"); + @cInclude("cimgui.h"); + @cInclude("GLFW/glfw3.h"); +}); diff --git a/src/glfw_impl.zig b/src/glfw_impl.zig new file mode 100644 index 0000000..7ef2f63 --- /dev/null +++ b/src/glfw_impl.zig @@ -0,0 +1,107 @@ +const c = @import("c.zig"); +const builtin = @import("builtin"); +pub const ClientApi = enum { + Unknown, + OpenGL, + Vulkan, +}; + +// Data +var g_Window: ?*c.GLFWwindow = null; +var g_ClientApi: ClientApi = .Unknown; +var g_Time: f32 = 0.0; +var g_MouseJustPressed = [5]bool; +var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT); +var g_WantUpdateMonitors = true; + +// Chain GLFW callbacks for main viewport: +// our callbacks will call the user's previously installed callbacks, if any. +var g_PrevUserCallbackMousebutton: ?*c.GLFWmousebuttonfun = null; +var g_PrevUserCallbackScroll: ?*c.GLFWscrollfun = null; +var g_PrevUserCallbackKey: ?*c.GLFWkeyfun = null; +var g_PrevUserCallbackChar: ?*c.GLFWcharfun = null; + +pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientApi) void { + g_Window = window; + g_Time = 0.0; + + // Setup back-end capabilities flags + const io = c.igGetIO(); + io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional) + io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used) + io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_PlatformHasViewports); // We can create multi-viewports on the Platform side (optional) + if (false and c.GLFW_HAS_GLFW_HOVERED and builtin.os == builtin.Os.windows) { + io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy) + } + io.*.BackendPlatformName = c"imgui_impl_glfw"; + + // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array. + io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB; + io.*.KeyMap[@enumToInt(c.ImGuiKey_LeftArrow)] = c.GLFW_KEY_LEFT; + io.*.KeyMap[@enumToInt(c.ImGuiKey_RightArrow)] = c.GLFW_KEY_RIGHT; + io.*.KeyMap[@enumToInt(c.ImGuiKey_UpArrow)] = c.GLFW_KEY_UP; + io.*.KeyMap[@enumToInt(c.ImGuiKey_DownArrow)] = c.GLFW_KEY_DOWN; + io.*.KeyMap[@enumToInt(c.ImGuiKey_PageUp)] = c.GLFW_KEY_PAGE_UP; + io.*.KeyMap[@enumToInt(c.ImGuiKey_PageDown)] = c.GLFW_KEY_PAGE_DOWN; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Home)] = c.GLFW_KEY_HOME; + io.*.KeyMap[@enumToInt(c.ImGuiKey_End)] = c.GLFW_KEY_END; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Insert)] = c.GLFW_KEY_INSERT; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Delete)] = c.GLFW_KEY_DELETE; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Backspace)] = c.GLFW_KEY_BACKSPACE; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Space)] = c.GLFW_KEY_SPACE; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Enter)] = c.GLFW_KEY_ENTER; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Escape)] = c.GLFW_KEY_ESCAPE; + io.*.KeyMap[@enumToInt(c.ImGuiKey_KeyPadEnter)] = c.GLFW_KEY_KP_ENTER; + io.*.KeyMap[@enumToInt(c.ImGuiKey_A)] = c.GLFW_KEY_A; + io.*.KeyMap[@enumToInt(c.ImGuiKey_C)] = c.GLFW_KEY_C; + io.*.KeyMap[@enumToInt(c.ImGuiKey_V)] = c.GLFW_KEY_V; + io.*.KeyMap[@enumToInt(c.ImGuiKey_X)] = c.GLFW_KEY_X; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y; + io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z; + + // io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; + // io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; + // io.ClipboardUserData = g_Window; + + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Arrow)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_TextInput)] = c.glfwCreateStandardCursor(c.GLFW_IBEAM_CURSOR); + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeAll)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNS)] = c.glfwCreateStandardCursor(c.GLFW_VRESIZE_CURSOR); + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeEW)] = c.glfwCreateStandardCursor(c.GLFW_HRESIZE_CURSOR); + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNESW)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNWSE)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. + g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Hand)] = c.glfwCreateStandardCursor(c.GLFW_HAND_CURSOR); + + // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. + g_PrevUserCallbackMousebutton = null; + g_PrevUserCallbackScroll = null; + g_PrevUserCallbackKey = null; + g_PrevUserCallbackChar = null; + if (install_callbacks and false) { + // g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); + // g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); + // g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); + // g_PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); + } + + // Our mouse update function expect PlatformHandle to be filled for the main viewport + const main_viewport = c.igGetMainViewport(); + main_viewport.*.PlatformHandle = g_Window; + if (builtin.os == builtin.Os.windows) { + main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window); + } + + // if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable != 0) + // ImGui_ImplGlfw_InitPlatformInterface(); + + g_ClientApi = client_api; +} + +pub fn Shutdown() void { + // ImGui_ImplGlfw_ShutdownPlatformInterface(); + for (g_MouseCursors) |*cursor| { + c.glfwDestroyCursor(cursor.*); + cursor.* = null; + } + g_ClientApi = .Unknown; +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..42fa152 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,77 @@ +const c = @import("c.zig"); +const std = @import("std"); +const panic = std.debug.panic; +const glfw_impl = @import("glfw_impl.zig"); + +extern fn errorCallback(err: c_int, description: [*c]const u8) void { + panic("Error: {}\n", description); +} + +var window: *c.GLFWwindow = undefined; + +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_PROFILE, c.GLFW_OPENGL_CORE_PROFILE); + + // c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on); + // 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, c"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 |= @enumToInt(c.ImGuiConfigFlags_NavEnableKeyboard); + io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_DockingEnable); + io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_ViewportsEnable); + + const style = c.igGetStyle(); + c.igStyleColorsDark(style); + + if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0) { + style.*.WindowRounding = 0.0; + style.*.Colors[@enumToInt(c.ImGuiCol_WindowBg)].w = 1.0; + } + + glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL); + // ImGui_InitForOpenGL(window, true); + // ImGui_ImplOpenGL3_Init(glsl_version); + + const start_time = c.glfwGetTime(); + var prev_time = start_time; + + while (c.glfwWindowShouldClose(window) == c.GL_FALSE) { + // c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT | c.GL_STENCIL_BUFFER_BIT); + + 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.glfwPollEvents(); + } +} |
