zig 0.9.0; make usable as lib
s-ol
2 years ago
1 | 1 | const builtin = @import("builtin"); |
2 | 2 | |
3 | 3 | pub fn build(b: *Builder) void { |
4 | const target = b.standardTargetOptions(.{}); | |
5 | const mode = b.standardReleaseOptions(); | |
6 | const windows = b.option(bool, "windows", "create windows build") orelse false; | |
4 | const mode = b.standardReleaseOptions(); | |
7 | 5 | |
8 | var exe = b.addExecutable("test", "src/main.zig"); | |
9 | exe.setBuildMode(mode); | |
6 | const lib = b.addStaticLibrary("imgui", "src/main.zig"); | |
7 | lib.setBuildMode(mode); | |
8 | lib.linkSystemLibrary("c"); | |
9 | lib.linkSystemLibrary("glfw"); | |
10 | lib.linkSystemLibrary("epoxy"); | |
11 | lib.addIncludeDir("cimgui"); | |
12 | lib.linkSystemLibraryName("cimgui/cimgui.so"); | |
13 | lib.install(); | |
10 | 14 | |
11 | exe.linkSystemLibrary("c"); | |
12 | exe.linkSystemLibrary("glfw"); | |
13 | exe.linkSystemLibrary("epoxy"); | |
14 | exe.addIncludeDir("cimgui"); | |
15 | exe.linkSystemLibraryName("cimgui/cimgui.so"); | |
16 | exe.install(); | |
15 | var exe = b.addExecutable("demo", "src/demo.zig"); | |
16 | exe.setBuildMode(mode); | |
17 | 17 | |
18 | const play = b.step("play", "Play the game"); | |
19 | const run = exe.run(); | |
20 | run.step.dependOn(b.getInstallStep()); | |
21 | play.dependOn(&run.step); | |
18 | exe.linkSystemLibrary("c"); | |
19 | exe.linkSystemLibrary("glfw"); | |
20 | exe.linkSystemLibrary("epoxy"); | |
21 | exe.addIncludeDir("cimgui"); | |
22 | exe.linkSystemLibraryName("cimgui/cimgui.so"); | |
23 | exe.install(); | |
24 | ||
25 | const demo = b.step("demo", "Run the demo"); | |
26 | const run = exe.run(); | |
27 | run.step.dependOn(b.getInstallStep()); | |
28 | demo.dependOn(&run.step); | |
22 | 29 | } |
3 | 3 | const panic = std.debug.panic; |
4 | 4 | const builtin = @import("builtin"); |
5 | 5 | |
6 | pub const is_on = if (builtin.mode == builtin.Mode.ReleaseFast) c.GL_FALSE else c.GL_TRUE; | |
6 | pub const is_on = if (builtin.mode == .ReleaseFast) c.GL_FALSE else c.GL_TRUE; | |
7 | 7 | |
8 | 8 | pub fn assertNoError() void { |
9 | if (builtin.mode != builtin.Mode.ReleaseFast) { | |
9 | if (builtin.mode != .ReleaseFast) { | |
10 | 10 | const err = c.glGetError(); |
11 | 11 | if (err != c.GL_NO_ERROR) { |
12 | 12 | panic("GL error: {}\n", err); |
0 | const ig = @import("main.zig"); | |
1 | ||
2 | pub fn main() !void { | |
3 | const window = ig.Window.init(); | |
4 | ||
5 | // const start_time = c.glfwGetTime(); | |
6 | // var prev_time = start_time; | |
7 | var show_demo = false; | |
8 | ||
9 | while (!window.shouldClose()) { | |
10 | try window.beginFrame(); | |
11 | ||
12 | // main part | |
13 | if (show_demo) | |
14 | ig.igShowDemoWindow(null); | |
15 | ||
16 | if (ig.igBegin("yolozwek", null, 0)) { | |
17 | ig.igText("Hello World :)"); | |
18 | _ = ig.igCheckbox("Demo window", &show_demo); | |
19 | ig.igEnd(); | |
20 | } | |
21 | ||
22 | window.endFrame(); | |
23 | } | |
24 | } |
154 | 154 | var buf: [1024]u8 = undefined; |
155 | 155 | var length: c.GLsizei = undefined; |
156 | 156 | getInfoLogFunc(handle, buf.len, &length, &buf[0]); |
157 | debug.warn("{}\n", .{buf[0..@intCast(usize, length)]}); | |
157 | debug.warn("{s}\n", .{buf[0..@intCast(usize, length)]}); | |
158 | 158 | } |
159 | 159 | |
160 | 160 | 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 {s}! (with GLSL '{s}')\n", .{ desc, g_GlslVersionString }); | |
162 | 162 | return error.ShaderLinkError; |
163 | 163 | } |
164 | 164 | } |
442 | 442 | c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxPos)); |
443 | 443 | c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxUV)); |
444 | 444 | c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxColor)); |
445 | c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxPos), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @byteOffsetOf(c.ImDrawVert, "pos"))); | |
446 | c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxUV), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @byteOffsetOf(c.ImDrawVert, "uv"))); | |
447 | c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxColor), 4, c.GL_UNSIGNED_BYTE, c.GL_TRUE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @byteOffsetOf(c.ImDrawVert, "col"))); | |
448 | } | |
445 | c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxPos), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @offsetOf(c.ImDrawVert, "pos"))); | |
446 | c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxUV), 2, c.GL_FLOAT, c.GL_FALSE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @offsetOf(c.ImDrawVert, "uv"))); | |
447 | c.glVertexAttribPointer(@intCast(c.GLuint, g_AttribLocationVtxColor), 4, c.GL_UNSIGNED_BYTE, c.GL_TRUE, @sizeOf(c.ImDrawVert), @intToPtr(?*c.GLvoid, @offsetOf(c.ImDrawVert, "col"))); | |
448 | } |
34 | 34 | io.*.BackendFlags |= c.ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) |
35 | 35 | if (false) io.*.BackendFlags |= c.ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) |
36 | 36 | if (false and @hasField(c, "GLFW_HAS_GLFW_HOVERED") and builtin.os == builtin.Os.windows) { |
37 | io.*.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy) | |
37 | io.*.BackendFlags |= c.ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy) | |
38 | 38 | } |
39 | 39 | io.*.BackendPlatformName = "imgui_impl_glfw.zig"; |
40 | 40 | |
165 | 165 | var w: c_int = undefined; |
166 | 166 | var h: c_int = undefined; |
167 | 167 | c.glfwGetMonitorWorkarea(glfw_monitor, &x, &y, &w, &h); |
168 | monitor.*.WorkPos = ImVec2{ .x = @intToFloat(f32, x), .y = @intToFloat(f32, y) }; | |
169 | monitor.*.WorkSize = ImVec2{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) }; | |
168 | monitor.*.WorkPos = c.ImVec2{ .x = @intToFloat(f32, x), .y = @intToFloat(f32, y) }; | |
169 | monitor.*.WorkSize = c.ImVec2{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) }; | |
170 | 170 | } |
171 | 171 | if (false and c.GLFW_HAS_PER_MONITOR_DPI) { |
172 | 172 | // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, |
6 | 6 | const gl3_impl = @import("gl3_impl.zig"); |
7 | 7 | |
8 | 8 | fn errorCallback(err: c_int, description: [*c]const u8) callconv(.C) void { |
9 | panic("Error: {}\n", .{ description }); | |
9 | _ = err; | |
10 | panic("Error: {s}\n", .{description}); | |
10 | 11 | } |
11 | 12 | |
12 | var window: *c.GLFWwindow = undefined; | |
13 | pub usingnamespace c; | |
13 | 14 | |
14 | fn handleStuff(path: [*c]const u8, x: i32, y: i32) callconv(.C) bool { | |
15 | return true; | |
16 | } | |
15 | pub const Window = struct { | |
16 | window: *c.GLFWwindow, | |
17 | context: *c.ImGuiContext, | |
17 | 18 | |
18 | pub fn main() !void { | |
19 | _ = c.glfwSetErrorCallback(errorCallback); | |
19 | pub fn init() Window { | |
20 | _ = c.glfwSetErrorCallback(errorCallback); | |
20 | 21 | |
21 | if (c.glfwInit() == c.GL_FALSE) { | |
22 | panic("GLFW init failure\n", .{}); | |
23 | } | |
24 | defer c.glfwTerminate(); | |
22 | if (c.glfwInit() == c.GL_FALSE) { | |
23 | panic("GLFW init failure\n", .{}); | |
24 | } | |
25 | errdefer c.glfwTerminate(); | |
25 | 26 | |
26 | c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3); | |
27 | c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2); | |
28 | c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE); | |
29 | c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on); | |
30 | c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE); | |
31 | // c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0); | |
32 | // c.glfwWindowHint(c.GLFW_STENCIL_BITS, 8); | |
33 | c.glfwWindowHint(c.GLFW_RESIZABLE, c.GL_TRUE); | |
27 | c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3); | |
28 | c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2); | |
29 | c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE); | |
30 | c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on); | |
31 | c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE); | |
32 | // c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0); | |
33 | // c.glfwWindowHint(c.GLFW_STENCIL_BITS, 8); | |
34 | c.glfwWindowHint(c.GLFW_RESIZABLE, c.GL_TRUE); | |
34 | 35 | |
35 | const window_width = 640; | |
36 | const window_height = 480; | |
37 | window = c.glfwCreateWindow(window_width, window_height, "ImGUI Test", null, null) orelse { | |
38 | panic("unable to create window\n", .{}); | |
39 | }; | |
40 | defer c.glfwDestroyWindow(window); | |
36 | const window_width = 640; | |
37 | const window_height = 480; | |
38 | const window = c.glfwCreateWindow(window_width, window_height, "ImGUI Test", null, null) orelse { | |
39 | panic("unable to create window\n", .{}); | |
40 | }; | |
41 | errdefer c.glfwDestroyWindow(window); | |
41 | 42 | |
42 | c.glfwMakeContextCurrent(window); | |
43 | c.glfwSwapInterval(1); | |
43 | c.glfwMakeContextCurrent(window); | |
44 | c.glfwSwapInterval(1); | |
44 | 45 | |
45 | const context = c.igCreateContext(null); | |
46 | defer c.igDestroyContext(context); | |
46 | const context = c.igCreateContext(null) orelse panic("ImGui init failure\n", .{}); | |
47 | errdefer c.igDestroyContext(context); | |
47 | 48 | |
48 | const io = c.igGetIO(); | |
49 | io.*.ConfigFlags |= c.ImGuiConfigFlags_NavEnableKeyboard; | |
50 | io.*.ConfigFlags |= c.ImGuiConfigFlags_DockingEnable; | |
51 | // io.*.ConfigFlags |= c.ImGuiConfigFlags_ViewportsEnable; | |
49 | const io = c.igGetIO(); | |
50 | io.*.ConfigFlags |= c.ImGuiConfigFlags_NavEnableKeyboard; | |
51 | io.*.ConfigFlags |= c.ImGuiConfigFlags_DockingEnable; | |
52 | // io.*.ConfigFlags |= c.ImGuiConfigFlags_ViewportsEnable; | |
52 | 53 | |
53 | const style = c.igGetStyle(); | |
54 | c.igStyleColorsDark(style); | |
54 | const style = c.igGetStyle(); | |
55 | c.igStyleColorsDark(style); | |
55 | 56 | |
56 | if (false and io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0) { | |
57 | style.*.WindowRounding = 0.0; | |
58 | style.*.Colors[@enumToInt(c.ImGuiCol_WindowBg)].w = 1.0; | |
57 | if (io.*.ConfigFlags & c.ImGuiConfigFlags_ViewportsEnable != 0) { | |
58 | style.*.WindowRounding = 0.0; | |
59 | style.*.Colors[c.ImGuiCol_WindowBg].w = 1.0; | |
60 | } | |
61 | ||
62 | glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL); | |
63 | errdefer glfw_impl.Shutdown(); | |
64 | ||
65 | gl3_impl.Init(); | |
66 | errdefer gl3_impl.Shutdown(); | |
67 | ||
68 | return .{ | |
69 | .window = window, | |
70 | .context = context, | |
71 | }; | |
59 | 72 | } |
60 | 73 | |
61 | glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL); | |
62 | defer glfw_impl.Shutdown(); | |
74 | pub fn deinit(self: *const Window) void { | |
75 | gl3_impl.Shutdown(); | |
76 | glfw_impl.Shutdown(); | |
77 | c.igDestroyContext(self.context); | |
78 | c.glfwDestroyWindow(self.window); | |
79 | c.glfwTerminate(); | |
80 | } | |
63 | 81 | |
64 | gl3_impl.Init(); // #version 150 | |
65 | defer gl3_impl.Shutdown(); | |
82 | pub fn shouldClose(self: *const Window) bool { | |
83 | return c.glfwWindowShouldClose(self.window) == c.GL_TRUE; | |
84 | } | |
66 | 85 | |
67 | const start_time = c.glfwGetTime(); | |
68 | var prev_time = start_time; | |
86 | pub fn beginFrame(self: *const Window) !void { | |
87 | _ = self; | |
69 | 88 | |
70 | while (c.glfwWindowShouldClose(window) == c.GL_FALSE) { | |
71 | 89 | c.glfwPollEvents(); |
72 | 90 | |
73 | 91 | try gl3_impl.NewFrame(); |
74 | 92 | glfw_impl.NewFrame(); |
75 | 93 | c.igNewFrame(); |
94 | } | |
76 | 95 | |
77 | // main part | |
78 | c.igShowDemoWindow(null); | |
79 | ||
96 | pub fn endFrame(self: *const Window) void { | |
80 | 97 | c.igRender(); |
81 | 98 | var w: c_int = undefined; |
82 | 99 | var h: c_int = undefined; |
83 | c.glfwGetFramebufferSize(window, &w, &h); | |
100 | c.glfwGetFramebufferSize(self.window, &w, &h); | |
84 | 101 | c.glViewport(0, 0, w, h); |
85 | 102 | c.glClearColor(0.0, 0.0, 0.0, 0.0); |
86 | 103 | c.glClear(c.GL_COLOR_BUFFER_BIT); |
87 | 104 | gl3_impl.RenderDrawData(c.igGetDrawData()); |
88 | 105 | |
89 | // const now_time = c.glfwGetTime(); | |
90 | // const elapsed = now_time - prev_time; | |
91 | // prev_time = now_time; | |
92 | // nextFrame(t, elapsed); | |
93 | // draw(t, @This()); | |
94 | ||
95 | c.glfwSwapBuffers(window); | |
106 | c.glfwSwapBuffers(self.window); | |
96 | 107 | } |
97 | } | |
108 | }; |