GUI: Box drawing

This commit is contained in:
2026-05-13 05:40:31 +02:00
parent 79c62141df
commit bce62feb09
24 changed files with 747 additions and 62 deletions

View File

@@ -15,6 +15,7 @@ const Chunk = @import("assets/Chunk.zig");
const Chunks = @import("Chunks.zig");
const CommandBuffer = @import("engine/CommandBuffer.zig");
const Engine = @import("engine/Engine.zig");
const Gui = @import("Gui.zig");
const Iterator2 = math.Iterator2;
const Materials = @import("assets/Materials.zig");
const Player = @import("Player.zig");
@@ -51,6 +52,7 @@ materials: Materials,
textures: Textures,
chunks: Chunks,
skybox: Skybox,
gui: Gui,
player: Player,
@@ -164,7 +166,7 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, engine: *Engine, swapchain
},
});
errdefer engine.destroyPipelineLayout(pipeline_layout);
engine.setObjectName(pipeline_layout, "PL", .{});
engine.setObjectName(pipeline_layout, "PL Main", .{});
const vertex_shader = try engine.createShaderModule(.{ .code = &shaders.main_vert_spv });
defer engine.destroyShaderModule(vertex_shader);
@@ -403,7 +405,7 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, engine: *Engine, swapchain
.subpass = 0,
});
errdefer engine.destroyPipeline(pipeline);
engine.setObjectName(pipeline, "P", .{});
engine.setObjectName(pipeline, "P Main", .{});
const descriptor_pool = try engine.createDescriptorPool(.{
.flags = .{
@@ -430,7 +432,7 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, engine: *Engine, swapchain
},
});
errdefer engine.destroyDescriptorPool(descriptor_pool);
engine.setObjectName(descriptor_pool, "DP", .{});
engine.setObjectName(descriptor_pool, "DP Main", .{});
const global_descriptor_set = try engine.allocateDescriptorSet(.{
.descriptor_pool = descriptor_pool,
@@ -645,6 +647,14 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, engine: *Engine, swapchain
);
errdefer skybox.deinit(engine);
var gui = try Gui.init(
allocator,
engine,
global_uniforms.buffer,
swapchain.render_pass,
);
errdefer gui.deinit(engine, allocator);
return .{
.allocator = allocator,
.io = io,
@@ -674,6 +684,7 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, engine: *Engine, swapchain
.textures = textures,
.chunks = .{ .chunks = chunks },
.skybox = skybox,
.gui = gui,
.player = .init(player_position_sv, 0, 0),
};
@@ -692,6 +703,7 @@ pub fn deinit(self: *Game) void {
self.chunks.deinit(self.engine, self.descriptor_pool, self.allocator);
self.skybox.deinit(self.engine);
self.gui.deinit(self.engine, self.allocator);
self.global_uniforms.deinit(self.engine);
self.global_uniforms_staging_buffer.deinit(self.engine);
@@ -718,8 +730,36 @@ pub fn deinit(self: *Game) void {
}
pub fn update(self: *Game, dt: f32) void {
self.gui.beginFrame();
self.player.update(dt, &self.chunks);
const extent = self.swapchain.extent;
const framebuffer_size = vm.Vector2.init(
@floatFromInt(extent.width),
@floatFromInt(extent.height),
);
const crosshair_half_extent_px = 8;
const crosshair_half_width_px = 1;
self.gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_extent_px, -crosshair_half_width_px)),
.size_px = .init(2.0 * crosshair_half_extent_px, 2.0 * crosshair_half_width_px),
.border_width_px = 0,
.border_radius_px = 0,
});
self.gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_width_px, -crosshair_half_extent_px)),
.size_px = .init(2.0 * crosshair_half_width_px, 2.0 * crosshair_half_extent_px),
.border_width_px = 0,
.border_radius_px = 0,
});
self.render() catch |err| {
std.log.err("Failed to render: {s}", .{@errorName(err)});
@panic("Frame update failed");
@@ -807,11 +847,20 @@ fn render(self: *Game) !void {
);
// zig fmt: on
// zig fmt: off
const matrix_sspx_to_cs = vm.Matrix3x2.init(
2.0 / framebuffer_size.x, 0,
0, 2.0 / framebuffer_size.y,
-1, -1,
);
// zig fmt: on
const ambient_light = vm.Vector3.init(0.01, 0.01, 0.01);
const global_uniforms_data: shaders.GlobalUniforms = .{
.matrixWStoVS = matrix_ws_to_vs,
.matrixVStoCS = matrix_vs_to_cs,
.matrixSSPXtoCS = matrix_sspx_to_cs,
.ambientLight = ambient_light,
};
@@ -887,6 +936,8 @@ fn render(self: *Game) !void {
},
});
// --- SUBPASS 0 (MAIN) ---
command_buffer.bindPipeline(.graphics, self.pipeline);
try command_buffer.bindVertexBuffers(0, &.{
.{
@@ -902,8 +953,13 @@ fn render(self: *Game) !void {
chunk.draw(self.pipeline_layout, command_buffer);
}
try self.skybox.bind(command_buffer, extent);
self.skybox.draw(command_buffer);
try self.skybox.draw(command_buffer);
// --- SUBPASS 1 (GUI) ---
command_buffer.nextSubpass(.@"inline");
try self.gui.draw(self.engine, command_buffer);
}
try command_buffer.endCommandBuffer();