switch from capy to sokol + imgui

This commit is contained in:
Fabien Freling 2024-07-04 15:09:23 +02:00
parent 10f3c25f80
commit 683756900c
6 changed files with 252 additions and 44 deletions

View file

@ -1,20 +1,77 @@
const std = @import("std");
const capy = @import("capy");
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;
// This is required for your app to build to WebAssembly and other particular architectures
pub usingnamespace capy.cross_platform;
const state = struct {
var pass_action: sg.PassAction = .{};
};
pub fn main() !void {
try capy.backend.init();
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 },
});
var window = try capy.Window.init();
try window.set(
capy.label(.{ .text = "Hello, World", .alignment = .Center }),
);
window.setTitle("Hello");
window.setPreferredSize(250, 100);
window.show();
capy.runEventLoop();
// 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();
}
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 },
});
}