hypatia/main.zig
2024-05-14 00:48:50 +09:00

100 lines
3.1 KiB
Zig

const std = @import("std");
const zglfw = @import("zglfw");
const zgpu = @import("zgpu");
const wgpu = zgpu.wgpu;
const zgui = @import("zgui");
pub fn main() !void {
try zglfw.init();
defer zglfw.terminate();
zglfw.windowHintTyped(.client_api, .no_api);
const window = try zglfw.Window.create(800, 500, "HELLOWORLD", null);
defer window.destroy();
window.setSizeLimits(400, 400, -1, -1);
var gpa_state = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa_state.deinit();
const gpa = gpa_state.allocator();
const gctx = try zgpu.GraphicsContext.create(
gpa,
.{
.window = window,
.fn_getTime = @ptrCast(&zglfw.getTime),
.fn_getFramebufferSize = @ptrCast(&zglfw.Window.getFramebufferSize),
.fn_getWin32Window = @ptrCast(&zglfw.getWin32Window),
.fn_getX11Display = @ptrCast(&zglfw.getX11Display),
.fn_getX11Window = @ptrCast(&zglfw.getX11Window),
.fn_getWaylandDisplay = @ptrCast(&zglfw.getWaylandDisplay),
.fn_getWaylandSurface = @ptrCast(&zglfw.getWaylandWindow),
.fn_getCocoaWindow = @ptrCast(&zglfw.getCocoaWindow),
},
.{},
);
defer gctx.destroy(gpa);
zgui.init(gpa);
defer zgui.deinit();
zgui.backend.init(
window,
gctx.device,
@intFromEnum(zgpu.GraphicsContext.swapchain_format),
@intFromEnum(wgpu.TextureFormat.undef),
);
defer zgui.backend.deinit();
var prev_scale: f32 = 1.0;
while (!window.shouldClose() and window.getKey(.escape) != .press) {
zglfw.pollEvents();
const scale_factor = scale_factor: {
const scale = window.getContentScale();
break :scale_factor @max(scale[0], scale[1]);
};
std.debug.print("{}", .{scale_factor / prev_scale});
zgui.getStyle().scaleAllSizes(scale_factor / prev_scale);
prev_scale = scale_factor;
zgui.backend.newFrame(
gctx.swapchain_descriptor.width,
gctx.swapchain_descriptor.height,
);
// Set the starting window position and size to custom values
zgui.setNextWindowPos(.{ .x = 20.0, .y = 20.0, .cond = .first_use_ever });
zgui.setNextWindowSize(.{ .w = -1.0, .h = -1.0, .cond = .first_use_ever });
if (zgui.begin("My window", .{})) {
if (zgui.button("Press me!", .{ .w = 200.0 })) {
std.debug.print("Button pressed\n", .{});
}
}
zgui.end();
const swapchain_texv = gctx.swapchain.getCurrentTextureView();
defer swapchain_texv.release();
const commands = commands: {
const encoder = gctx.device.createCommandEncoder(null);
defer encoder.release();
// GUI pass
{
const pass = zgpu.beginRenderPassSimple(encoder, .load, swapchain_texv, null, null, null);
defer zgpu.endReleasePass(pass);
zgui.backend.draw(pass);
}
break :commands encoder.finish(null);
};
defer commands.release();
gctx.submit(&.{commands});
_ = gctx.present();
}
}