git.s-ol.nu zig-imgui / master
zig 0.9.0; make usable as lib s-ol 1 year, 11 months ago
7 changed file(s) with 127 addition(s) and 83 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(.{});
5 const mode = b.standardReleaseOptions();
6 const windows = b.option(bool, "windows", "create windows build") orelse false;
4 const mode = b.standardReleaseOptions();
75
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();
1014
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);
1717
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);
2229 }
0 pub usingnamespace @import("./src/main.zig");
33 const panic = std.debug.panic;
44 const builtin = @import("builtin");
55
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;
77
88 pub fn assertNoError() void {
9 if (builtin.mode != builtin.Mode.ReleaseFast) {
9 if (builtin.mode != .ReleaseFast) {
1010 const err = c.glGetError();
1111 if (err != c.GL_NO_ERROR) {
1212 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 }
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("{s}\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 {s}! (with GLSL '{s}')\n", .{ desc, g_GlslVersionString });
162162 return error.ShaderLinkError;
163163 }
164164 }
442442 c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxPos));
443443 c.glEnableVertexAttribArray(@intCast(c.GLuint, g_AttribLocationVtxUV));
444444 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 }
3434 io.*.BackendFlags |= c.ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
3535 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 |= 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)
3838 }
3939 io.*.BackendPlatformName = "imgui_impl_glfw.zig";
4040
165165 var w: c_int = undefined;
166166 var h: c_int = undefined;
167167 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) };
170170 }
171171 if (false and c.GLFW_HAS_PER_MONITOR_DPI) {
172172 // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings,
66 const gl3_impl = @import("gl3_impl.zig");
77
88 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});
1011 }
1112
12 var window: *c.GLFWwindow = undefined;
13 pub usingnamespace c;
1314
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,
1718
18 pub fn main() !void {
19 _ = c.glfwSetErrorCallback(errorCallback);
19 pub fn init() Window {
20 _ = c.glfwSetErrorCallback(errorCallback);
2021
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();
2526
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);
3435
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);
4142
42 c.glfwMakeContextCurrent(window);
43 c.glfwSwapInterval(1);
43 c.glfwMakeContextCurrent(window);
44 c.glfwSwapInterval(1);
4445
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);
4748
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;
5253
53 const style = c.igGetStyle();
54 c.igStyleColorsDark(style);
54 const style = c.igGetStyle();
55 c.igStyleColorsDark(style);
5556
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 };
5972 }
6073
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 }
6381
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 }
6685
67 const start_time = c.glfwGetTime();
68 var prev_time = start_time;
86 pub fn beginFrame(self: *const Window) !void {
87 _ = self;
6988
70 while (c.glfwWindowShouldClose(window) == c.GL_FALSE) {
7189 c.glfwPollEvents();
7290
7391 try gl3_impl.NewFrame();
7492 glfw_impl.NewFrame();
7593 c.igNewFrame();
94 }
7695
77 // main part
78 c.igShowDemoWindow(null);
79
96 pub fn endFrame(self: *const Window) void {
8097 c.igRender();
8198 var w: c_int = undefined;
8299 var h: c_int = undefined;
83 c.glfwGetFramebufferSize(window, &w, &h);
100 c.glfwGetFramebufferSize(self.window, &w, &h);
84101 c.glViewport(0, 0, w, h);
85102 c.glClearColor(0.0, 0.0, 0.0, 0.0);
86103 c.glClear(c.GL_COLOR_BUFFER_BIT);
87104 gl3_impl.RenderDrawData(c.igGetDrawData());
88105
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);
96107 }
97 }
108 };