Globalize textures, materials and gui
This commit is contained in:
83
src/Game.zig
83
src/Game.zig
@@ -16,7 +16,6 @@ const Chunk = @import("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("engine/Materials.zig");
|
||||
const Player = @import("Player.zig");
|
||||
@@ -44,14 +43,9 @@ directional_lights: shaders.DirectionalLightBuffer,
|
||||
sampler: vk.Sampler,
|
||||
deferred_command_buffers: std.ArrayList(CommandBuffer),
|
||||
|
||||
stbi: media.stbi,
|
||||
blocks: Blocks,
|
||||
materials: Materials,
|
||||
textures: Textures,
|
||||
chunks: Chunks,
|
||||
skybox: Skybox,
|
||||
gui: Gui,
|
||||
|
||||
player: Player,
|
||||
|
||||
const max_textures = 1024;
|
||||
@@ -64,18 +58,11 @@ const camera_near_plane = 0.1;
|
||||
pub fn init() !Game {
|
||||
const allocator_general = ctx.allocator_general;
|
||||
const allocator_frame = ctx.allocator_frame;
|
||||
const io = ctx.io;
|
||||
const engine = ctx.engine;
|
||||
const swapchain = ctx.swapchain;
|
||||
|
||||
var stbi = media.stbi.init(allocator_general, io);
|
||||
errdefer stbi.deinit();
|
||||
|
||||
var materials = try Materials.init();
|
||||
errdefer materials.deinit();
|
||||
|
||||
var textures = try Textures.init();
|
||||
errdefer textures.deinit();
|
||||
const textures = ctx.textures;
|
||||
const materials = ctx.materials;
|
||||
const gui = ctx.gui;
|
||||
|
||||
var blocks = try Blocks.init();
|
||||
errdefer blocks.deinit();
|
||||
@@ -83,8 +70,7 @@ pub fn init() !Game {
|
||||
// JANK HACK When this line is removed, capturing a frame with RenderDoc
|
||||
// will crash the game with segfault reading address 0x140 (presumably
|
||||
// within librenderdoc.so).
|
||||
|
||||
blocks.loadAll(&materials, &textures, &stbi);
|
||||
blocks.loadAll();
|
||||
|
||||
const sampler = try engine.createSampler(.{
|
||||
.mag_filter = .linear,
|
||||
@@ -447,10 +433,10 @@ pub fn init() !Game {
|
||||
});
|
||||
engine.setObjectName(global_descriptor_set, "DS Global", .{});
|
||||
|
||||
const block_grass = try blocks.getOrLoad(&materials, &textures, &stbi, "Grass.json");
|
||||
const block_dirt = try blocks.getOrLoad(&materials, &textures, &stbi, "Dirt.json");
|
||||
const block_stone = try blocks.getOrLoad(&materials, &textures, &stbi, "Stone.json");
|
||||
const block_bedrock = try blocks.getOrLoad(&materials, &textures, &stbi, "Bedrock.json");
|
||||
const block_grass = try blocks.getOrLoad("Grass.json");
|
||||
const block_dirt = try blocks.getOrLoad("Dirt.json");
|
||||
const block_stone = try blocks.getOrLoad("Stone.json");
|
||||
const block_bedrock = try blocks.getOrLoad("Bedrock.json");
|
||||
|
||||
// VOLATILE Load all assets before this point
|
||||
|
||||
@@ -640,18 +626,33 @@ pub fn init() !Game {
|
||||
.elements = directional_lights_data,
|
||||
});
|
||||
|
||||
var skybox = try Skybox.load(
|
||||
"skybox.hdr",
|
||||
&stbi,
|
||||
512,
|
||||
global_uniforms.buffer,
|
||||
);
|
||||
var skybox = try Skybox.load("skybox.hdr", 512, global_uniforms.buffer);
|
||||
errdefer skybox.deinit();
|
||||
|
||||
var gui = try Gui.init(
|
||||
global_uniforms.buffer,
|
||||
);
|
||||
errdefer gui.deinit();
|
||||
// TODO This coupling is not elegant - the game must provide the GUI with
|
||||
// uniform buffer for globals. Maybe GUI should just use their own global
|
||||
// uniform buffer not for any other reason than decoupling. After all, GUI
|
||||
// only needs a single 3×2 matrix and does not need to depend on game's
|
||||
// rendering pipeline.
|
||||
try engine.updateDescriptorSets(.{
|
||||
.writes = &.{
|
||||
.{
|
||||
.dst_set = gui.box_descriptor_set,
|
||||
.dst_binding = 0,
|
||||
.dst_array_element = 0,
|
||||
.descriptor_type = .uniform_buffer,
|
||||
.descriptor_infos = .{
|
||||
.buffer = &.{
|
||||
.{
|
||||
.buffer = global_uniforms.buffer,
|
||||
.offset = 0,
|
||||
.range = vk.WHOLE_SIZE,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return .{
|
||||
.global_descriptor_set_layout = global_descriptor_set_layout,
|
||||
@@ -672,14 +673,9 @@ pub fn init() !Game {
|
||||
.sampler = sampler,
|
||||
.deferred_command_buffers = try .initCapacity(allocator_general, 4),
|
||||
|
||||
.stbi = stbi,
|
||||
.blocks = blocks,
|
||||
.materials = materials,
|
||||
.textures = textures,
|
||||
.chunks = .{ .chunks = chunks },
|
||||
.skybox = skybox,
|
||||
.gui = gui,
|
||||
|
||||
.player = .init(player_position_sv, 0, 0),
|
||||
};
|
||||
}
|
||||
@@ -700,7 +696,6 @@ pub fn deinit(self: *Game) void {
|
||||
|
||||
self.chunks.deinit(self.descriptor_pool);
|
||||
self.skybox.deinit();
|
||||
self.gui.deinit();
|
||||
|
||||
self.global_uniforms.deinit();
|
||||
self.global_uniforms_staging_buffer.deinit();
|
||||
@@ -719,17 +714,14 @@ pub fn deinit(self: *Game) void {
|
||||
engine.destroyDescriptorSetLayout(self.per_batch_descriptor_set_layout);
|
||||
engine.destroyDescriptorSetLayout(self.global_descriptor_set_layout);
|
||||
|
||||
self.textures.deinit();
|
||||
self.materials.deinit();
|
||||
self.blocks.deinit();
|
||||
self.stbi.deinit();
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn update(self: *Game, dt: f32) void {
|
||||
const swapchain = ctx.swapchain;
|
||||
const gui = ctx.gui;
|
||||
|
||||
self.gui.beginFrame();
|
||||
self.player.update(dt, &self.chunks);
|
||||
|
||||
const extent = swapchain.extent;
|
||||
@@ -741,7 +733,7 @@ pub fn update(self: *Game, dt: f32) void {
|
||||
const crosshair_half_extent_px = 8;
|
||||
const crosshair_half_width_px = 1;
|
||||
|
||||
self.gui.pushBox(.{
|
||||
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)),
|
||||
@@ -750,7 +742,7 @@ pub fn update(self: *Game, dt: f32) void {
|
||||
.border_radius_px = 0,
|
||||
});
|
||||
|
||||
self.gui.pushBox(.{
|
||||
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)),
|
||||
@@ -790,6 +782,7 @@ fn render(self: *Game) !void {
|
||||
const allocator_general = ctx.allocator_general;
|
||||
const window = ctx.window;
|
||||
const swapchain = ctx.swapchain;
|
||||
const gui = ctx.gui;
|
||||
|
||||
const framebuffer_width, const framebuffer_height = blk: {
|
||||
const w, const h = window.getFramebufferSize();
|
||||
@@ -1020,7 +1013,7 @@ fn render(self: *Game) !void {
|
||||
|
||||
// --- RENDER GUI ---
|
||||
|
||||
try self.gui.draw(command_buffer);
|
||||
try gui.draw(command_buffer);
|
||||
|
||||
// --- FINALIZE ---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user