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