doggo/src/gui/main.zig
Fabien Freling d12af291e1 add tui
2024-07-17 11:14:41 +02:00

93 lines
2.6 KiB
Zig

const ig = @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 state = struct {
var pass_action: sg.PassAction = .{};
};
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 },
});
// 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(),
});
//=== UI CODE STARTS HERE
var menu_bar_size: ig.ImVec2 = undefined;
if (ig.igBeginMainMenuBar()) {
ig.igGetWindowSize(&menu_bar_size);
defer ig.igEndMainMenuBar();
if (ig.igBeginMenu("File", true)) {
defer ig.igEndMenu();
if (ig.igMenuItem_Bool("Open", "o", false, true)) {
// Do something
}
}
}
const io = ig.igGetIO();
ig.igSetNextWindowPos(.{ .x = 0, .y = menu_bar_size.y }, ig.ImGuiCond_Always, .{ .x = 0, .y = 0 });
var content_size: ig.ImVec2 = io.*.DisplaySize;
content_size.y -= menu_bar_size.y;
ig.igSetNextWindowSize(content_size, ig.ImGuiCond_Always);
_ = ig.igBegin("Hello Dear ImGui!", 0, ig.ImGuiWindowFlags_NoMove | ig.ImGuiWindowFlags_NoResize | ig.ImGuiWindowFlags_NoTitleBar | ig.ImGuiWindowFlags_NoCollapse);
_ = ig.igColorEdit3("Background", &state.pass_action.colors[0].clear_value.r, ig.ImGuiColorEditFlags_None);
ig.igEnd();
//=== UI CODE ENDS HERE
// call simgui.render() inside a sokol-gfx pass
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
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 = "Doggo",
.width = 800,
.height = 600,
.icon = .{ .sokol_default = true },
.logger = .{ .func = slog.func },
});
}