const use_docking = @import("build_options").docking; const ig = if (use_docking) @import("cimgui_docking") else @import("cimgui"); const sokol = @import("sokol"); const slog = sokol.log; const sg = sokol.gfx; const sapp = sokol.app; const sglue = sokol.glue; const simgui = sokol.imgui; const shd = @import("shader/quad.glsl.zig"); const state = struct { var bind: sg.Bindings = .{}; var pip: sg.Pipeline = .{}; var pass_action: sg.PassAction = .{}; var show_first_window: bool = true; var show_second_window: bool = true; }; export fn init() void { // initialize sokol-gfx sg.setup(.{ .environment = sglue.environment(), .logger = .{ .func = slog.func }, }); // initialize sokol-imgui simgui.setup(.{ .logger = .{ .func = slog.func }, }); if (use_docking) { ig.igGetIO().*.ConfigFlags |= ig.ImGuiConfigFlags_DockingEnable; } // a vertex buffer state.bind.vertex_buffers[0] = sg.makeBuffer(.{ .data = sg.asRange(&[_]f32{ // positions colors -0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, -0.5, -0.5, 0.5, 1.0, 1.0, 0.0, 1.0, }), }); // an index buffer state.bind.index_buffer = sg.makeBuffer(.{ .usage = .{ .index_buffer = true }, .data = sg.asRange(&[_]u16{ 0, 1, 2, 0, 2, 3 }), }); // a shader and pipeline state object state.pip = sg.makePipeline(.{ .shader = sg.makeShader(shd.quadShaderDesc(sg.queryBackend())), .layout = init: { var l = sg.VertexLayoutState{}; l.attrs[shd.ATTR_quad_position].format = .FLOAT3; l.attrs[shd.ATTR_quad_color0].format = .FLOAT4; break :init l; }, .index_type = .UINT16, }); // initial clear color state.pass_action.colors[0] = .{ .load_action = .CLEAR, .clear_value = .{ .r = 0.0, .g = 0.5, .b = 1.0, .a = 1.0 }, }; } export fn frame() void { // call simgui.newFrame() before any ImGui calls simgui.newFrame(.{ .width = sapp.width(), .height = sapp.height(), .delta_time = sapp.frameDuration(), .dpi_scale = sapp.dpiScale(), }); if (ig.igBegin("Hello Dear ImGui!", &state.show_first_window, ig.ImGuiWindowFlags_None)) { _ = ig.igColorEdit3("Background", &state.pass_action.colors[0].clear_value.r, ig.ImGuiColorEditFlags_None); _ = ig.igText("Dear ImGui Version: %s", ig.IMGUI_VERSION); ig.igEnd(); } // call simgui.render() inside a sokol-gfx pass sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() }); sg.applyPipeline(state.pip); sg.applyBindings(state.bind); sg.draw(0, 6, 1); simgui.render(); sg.endPass(); sg.commit(); } export fn cleanup() void { simgui.shutdown(); sg.shutdown(); } export fn event(ev: [*c]const sapp.Event) void { // forward input events to sokol-imgui _ = simgui.handleEvent(ev.*); } pub fn main() void { sapp.run(.{ .init_cb = init, .frame_cb = frame, .cleanup_cb = cleanup, .event_cb = event, .window_title = "sokol-zig + Dear Imgui", .width = 800, .height = 600, .icon = .{ .sokol_default = true }, .logger = .{ .func = slog.func }, }); }