doggo/src/main.zig

78 lines
2.1 KiB
Zig
Raw Normal View History

2024-07-04 15:09:23 +02:00
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;
2022-01-21 15:12:40 +01:00
2024-07-04 15:09:23 +02:00
const state = struct {
var pass_action: sg.PassAction = .{};
};
2022-01-22 14:42:40 +01:00
2024-07-04 15:09:23 +02:00
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 },
});
2022-04-14 14:05:54 +02:00
2024-07-04 15:09:23 +02:00
// 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
ig.igSetNextWindowPos(.{ .x = 10, .y = 10 }, ig.ImGuiCond_Once, .{ .x = 0, .y = 0 });
ig.igSetNextWindowSize(.{ .x = 400, .y = 100 }, ig.ImGuiCond_Once);
_ = ig.igBegin("Hello Dear ImGui!", 0, ig.ImGuiWindowFlags_None);
_ = 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();
}
2022-04-14 14:05:54 +02:00
2024-07-04 15:09:23 +02:00
export fn event(ev: [*c]const sapp.Event) void {
// forward input events to sokol-imgui
_ = simgui.handleEvent(ev.*);
}
2022-01-22 14:42:40 +01:00
2024-07-04 15:09:23 +02:00
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 },
});
2022-01-21 15:12:40 +01:00
}