git.s-ol.nu zig-imgui / f9bd966
initial commit s-ol a month ago
7 changed file(s) with 220 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1 zig-cache
1 [submodule "cimgui"]
2 path = cimgui
3 url = https://github.com/cimgui/cimgui.git
1 const Builder = @import("std").build.Builder;
2 const builtin = @import("builtin");
3
4 pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6 const windows = b.option(bool, "windows", "create windows build") orelse false;
7
8 var exe = b.addExecutable("test", "src/main.zig");
9 exe.setBuildMode(mode);
10
11 if (windows) {
12 exe.setTarget(builtin.Arch.x86_64, builtin.Os.windows, builtin.Abi.gnu);
13 }
14
15 exe.linkSystemLibrary("c");
16 exe.linkSystemLibrary("glfw");
17 exe.linkSystemLibrary("epoxy");
18 exe.addIncludeDir("cimgui");
19 exe.linkSystemLibraryName("cimgui/cimgui.so");
20 exe.install();
21
22 const play = b.step("play", "Play the game");
23 const run = exe.run();
24 run.step.dependOn(b.getInstallStep());
25 play.dependOn(&run.step);
26 }
1 Submodule commit 75f2167111696e569289d323c5b85ad5ad5a3534
1 pub usingnamespace @cImport({
2 @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1");
3 @cInclude("cimgui.h");
4 @cInclude("GLFW/glfw3.h");
5 });
1 const c = @import("c.zig");
2 const builtin = @import("builtin");
3 pub const ClientApi = enum {
4 Unknown,
5 OpenGL,
6 Vulkan,
7 };
8
9 // Data
10 var g_Window: ?*c.GLFWwindow = null;
11 var g_ClientApi: ClientApi = .Unknown;
12 var g_Time: f32 = 0.0;
13 var g_MouseJustPressed = [5]bool;
14 var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT);
15 var g_WantUpdateMonitors = true;
16
17 // Chain GLFW callbacks for main viewport:
18 // 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;
23
24 pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientApi) void {
25 g_Window = window;
26 g_Time = 0.0;
27
28 // Setup back-end capabilities flags
29 const io = c.igGetIO();
30 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional)
31 io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used)
32 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) {
34 io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy)
35 }
36 io.*.BackendPlatformName = c"imgui_impl_glfw";
37
38 // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
39 io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB;
40 io.*.KeyMap[@enumToInt(c.ImGuiKey_LeftArrow)] = c.GLFW_KEY_LEFT;
41 io.*.KeyMap[@enumToInt(c.ImGuiKey_RightArrow)] = c.GLFW_KEY_RIGHT;
42 io.*.KeyMap[@enumToInt(c.ImGuiKey_UpArrow)] = c.GLFW_KEY_UP;
43 io.*.KeyMap[@enumToInt(c.ImGuiKey_DownArrow)] = c.GLFW_KEY_DOWN;
44 io.*.KeyMap[@enumToInt(c.ImGuiKey_PageUp)] = c.GLFW_KEY_PAGE_UP;
45 io.*.KeyMap[@enumToInt(c.ImGuiKey_PageDown)] = c.GLFW_KEY_PAGE_DOWN;
46 io.*.KeyMap[@enumToInt(c.ImGuiKey_Home)] = c.GLFW_KEY_HOME;
47 io.*.KeyMap[@enumToInt(c.ImGuiKey_End)] = c.GLFW_KEY_END;
48 io.*.KeyMap[@enumToInt(c.ImGuiKey_Insert)] = c.GLFW_KEY_INSERT;
49 io.*.KeyMap[@enumToInt(c.ImGuiKey_Delete)] = c.GLFW_KEY_DELETE;
50 io.*.KeyMap[@enumToInt(c.ImGuiKey_Backspace)] = c.GLFW_KEY_BACKSPACE;
51 io.*.KeyMap[@enumToInt(c.ImGuiKey_Space)] = c.GLFW_KEY_SPACE;
52 io.*.KeyMap[@enumToInt(c.ImGuiKey_Enter)] = c.GLFW_KEY_ENTER;
53 io.*.KeyMap[@enumToInt(c.ImGuiKey_Escape)] = c.GLFW_KEY_ESCAPE;
54 io.*.KeyMap[@enumToInt(c.ImGuiKey_KeyPadEnter)] = c.GLFW_KEY_KP_ENTER;
55 io.*.KeyMap[@enumToInt(c.ImGuiKey_A)] = c.GLFW_KEY_A;
56 io.*.KeyMap[@enumToInt(c.ImGuiKey_C)] = c.GLFW_KEY_C;
57 io.*.KeyMap[@enumToInt(c.ImGuiKey_V)] = c.GLFW_KEY_V;
58 io.*.KeyMap[@enumToInt(c.ImGuiKey_X)] = c.GLFW_KEY_X;
59 io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y;
60 io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z;
61
62 // io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
63 // io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
64 // io.ClipboardUserData = g_Window;
65
66 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Arrow)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR);
67 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_TextInput)] = c.glfwCreateStandardCursor(c.GLFW_IBEAM_CURSOR);
68 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeAll)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
69 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNS)] = c.glfwCreateStandardCursor(c.GLFW_VRESIZE_CURSOR);
70 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeEW)] = c.glfwCreateStandardCursor(c.GLFW_HRESIZE_CURSOR);
71 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNESW)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
72 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNWSE)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
73 g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Hand)] = c.glfwCreateStandardCursor(c.GLFW_HAND_CURSOR);
74
75 // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
76 g_PrevUserCallbackMousebutton = null;
77 g_PrevUserCallbackScroll = null;
78 g_PrevUserCallbackKey = null;
79 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);
85 }
86
87 // Our mouse update function expect PlatformHandle to be filled for the main viewport
88 const main_viewport = c.igGetMainViewport();
89 main_viewport.*.PlatformHandle = g_Window;
90 if (builtin.os == builtin.Os.windows) {
91 main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
92 }
93
94 // if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable != 0)
95 // ImGui_ImplGlfw_InitPlatformInterface();
96
97 g_ClientApi = client_api;
98 }
99
100 pub fn Shutdown() void {
101 // ImGui_ImplGlfw_ShutdownPlatformInterface();
102 for (g_MouseCursors) |*cursor| {
103 c.glfwDestroyCursor(cursor.*);
104 cursor.* = null;
105 }
106 g_ClientApi = .Unknown;
107 }
1 const c = @import("c.zig");
2 const std = @import("std");
3 const panic = std.debug.panic;
4 const glfw_impl = @import("glfw_impl.zig");
5
6 extern fn errorCallback(err: c_int, description: [*c]const u8) void {
7 panic("Error: {}\n", description);
8 }
9
10 var window: *c.GLFWwindow = undefined;
11
12 pub fn main() void {
13 _ = c.glfwSetErrorCallback(errorCallback);
14
15 if (c.glfwInit() == c.GL_FALSE) {
16 panic("GLFW init failure\n");
17 }
18 defer c.glfwTerminate();
19
20 c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3);
21 c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2);
22 c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE);
23 c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE);
24
25 // c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on);
26 // c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0);
27 // c.glfwWindowHint(c.GLFW_STENCIL_BITS, 8);
28 c.glfwWindowHint(c.GLFW_RESIZABLE, c.GL_TRUE);
29
30 const window_width = 640;
31 const window_height = 480;
32 window = c.glfwCreateWindow(window_width, window_height, c"ImGUI Test", null, null) orelse {
33 panic("unable to create window\n");
34 };
35 defer c.glfwDestroyWindow(window);
36
37 c.glfwMakeContextCurrent(window);
38 c.glfwSwapInterval(1);
39
40 const context = c.igCreateContext(null);
41 defer c.igDestroyContext(context);
42
43 const io = c.igGetIO();
44 io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_NavEnableKeyboard);
45 io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_DockingEnable);
46 io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_ViewportsEnable);
47
48 const style = c.igGetStyle();
49 c.igStyleColorsDark(style);
50
51 if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0) {
52 style.*.WindowRounding = 0.0;
53 style.*.Colors[@enumToInt(c.ImGuiCol_WindowBg)].w = 1.0;
54 }
55
56 glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL);
57 // ImGui_InitForOpenGL(window, true);
58 // ImGui_ImplOpenGL3_Init(glsl_version);
59
60 const start_time = c.glfwGetTime();
61 var prev_time = start_time;
62
63 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
66 const now_time = c.glfwGetTime();
67 const elapsed = now_time - prev_time;
68 prev_time = now_time;
69
70 // nextFrame(t, elapsed);
71
72 // draw(t, @This());
73 c.glfwSwapBuffers(window);
74
75 c.glfwPollEvents();
76 }
77 }