1 const use_docking = @import("build_options").docking;
2 const ig = if (use_docking) @import("cimgui_docking") else @import("cimgui");
3 const sokol = @import("sokol");
4 const slog = sokol.log;
6 const sapp = sokol.app;
7 const sglue = sokol.glue;
8 const simgui = sokol.imgui;
10 const shd = @import("shader/quad.glsl.zig");
12 const state = struct {
13 var bind: sg.Bindings = .{};
14 var pip: sg.Pipeline = .{};
15 var pass_action: sg.PassAction = .{};
16 var show_first_window: bool = true;
17 var show_second_window: bool = true;
20 export fn init() void {
21 // initialize sokol-gfx
23 .environment = sglue.environment(),
24 .logger = .{ .func = slog.func },
27 // initialize sokol-imgui
29 .logger = .{ .func = slog.func },
32 ig.igGetIO().*.ConfigFlags |= ig.ImGuiConfigFlags_DockingEnable;
36 state.bind.vertex_buffers[0] = sg.makeBuffer(.{
37 .data = sg.asRange(&[_]f32{
39 -0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0,
40 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0,
41 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0,
42 -0.5, -0.5, 0.5, 1.0, 1.0, 0.0, 1.0,
47 state.bind.index_buffer = sg.makeBuffer(.{
48 .usage = .{ .index_buffer = true },
49 .data = sg.asRange(&[_]u16{ 0, 1, 2, 0, 2, 3 }),
52 // a shader and pipeline state object
53 state.pip = sg.makePipeline(.{
54 .shader = sg.makeShader(shd.quadShaderDesc(sg.queryBackend())),
56 var l = sg.VertexLayoutState{};
57 l.attrs[shd.ATTR_quad_position].format = .FLOAT3;
58 l.attrs[shd.ATTR_quad_color0].format = .FLOAT4;
61 .index_type = .UINT16,
64 // initial clear color
65 state.pass_action.colors[0] = .{
66 .load_action = .CLEAR,
67 .clear_value = .{ .r = 0.0, .g = 0.5, .b = 1.0, .a = 1.0 },
71 export fn frame() void {
72 // call simgui.newFrame() before any ImGui calls
74 .width = sapp.width(),
75 .height = sapp.height(),
76 .delta_time = sapp.frameDuration(),
77 .dpi_scale = sapp.dpiScale(),
80 if (ig.igBegin("Hello Dear ImGui!", &state.show_first_window, ig.ImGuiWindowFlags_None)) {
81 _ = ig.igColorEdit3("Background", &state.pass_action.colors[0].clear_value.r, ig.ImGuiColorEditFlags_None);
82 _ = ig.igText("Dear ImGui Version: %s", ig.IMGUI_VERSION);
87 // call simgui.render() inside a sokol-gfx pass
88 sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
89 sg.applyPipeline(state.pip);
90 sg.applyBindings(state.bind);
97 export fn cleanup() void {
102 export fn event(ev: [*c]const sapp.Event) void {
103 // forward input events to sokol-imgui
104 _ = simgui.handleEvent(ev.*);
111 .cleanup_cb = cleanup,
113 .window_title = "sokol-zig + Dear Imgui",
116 .icon = .{ .sokol_default = true },
117 .logger = .{ .func = slog.func },