Project template
This commit is contained in:
136
src/main.zig
Normal file
136
src/main.zig
Normal file
@@ -0,0 +1,136 @@
|
||||
const std = @import("std");
|
||||
|
||||
const zaudio = @import("zaudio");
|
||||
const zglfw = @import("zglfw");
|
||||
const zgpu = @import("zgpu");
|
||||
const zgui = @import("zgui");
|
||||
const ztracy = @import("ztracy");
|
||||
|
||||
pub var allocator: std.mem.Allocator = undefined;
|
||||
pub var audio_engine: *zaudio.Engine = undefined;
|
||||
pub var gctx: *zgpu.GraphicsContext = undefined;
|
||||
pub var temp_allocator: std.mem.Allocator = undefined;
|
||||
pub var window: *zglfw.Window = undefined;
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
var ta = ztracy.TracyAllocator.init(gpa.allocator());
|
||||
|
||||
var fba = std.heap.FixedBufferAllocator.init(&struct {
|
||||
pub var buffer: [16 * 1024 * 1024]u8 = undefined;
|
||||
}.buffer);
|
||||
|
||||
allocator = ta.allocator();
|
||||
temp_allocator = fba.threadSafeAllocator();
|
||||
|
||||
// --- zaudio --------------------------------------------------------------
|
||||
|
||||
const zone_init_zaudio = ztracy.ZoneN(@src(), "Init zaudio");
|
||||
|
||||
zaudio.init(allocator);
|
||||
defer zaudio.deinit();
|
||||
|
||||
audio_engine = try zaudio.Engine.create(null);
|
||||
defer audio_engine.destroy();
|
||||
|
||||
zone_init_zaudio.End();
|
||||
|
||||
// --- zglfw ---------------------------------------------------------------
|
||||
|
||||
const zone_init_zglfw = ztracy.ZoneN(@src(), "Init zglfw");
|
||||
|
||||
try zglfw.init();
|
||||
defer zglfw.terminate();
|
||||
|
||||
zglfw.windowHint(.client_api, .no_api);
|
||||
window = try zglfw.Window.create(1280, 720, "voxel-game", null);
|
||||
window.setSizeLimits(640, 360, -1, -1);
|
||||
defer window.destroy();
|
||||
|
||||
zone_init_zglfw.End();
|
||||
|
||||
// --- zgpu ----------------------------------------------------------------
|
||||
|
||||
const zone_init_zgpu = ztracy.ZoneN(@src(), "Init zgpu");
|
||||
|
||||
gctx = try zgpu.GraphicsContext.create(allocator, .{
|
||||
.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(allocator);
|
||||
|
||||
zone_init_zgpu.End();
|
||||
|
||||
// --- zgui ----------------------------------------------------------------
|
||||
|
||||
const zone_init_zgui = ztracy.ZoneN(@src(), "Init zgui");
|
||||
|
||||
zgui.init(allocator);
|
||||
defer zgui.deinit();
|
||||
|
||||
zgui.backend.init(
|
||||
window,
|
||||
gctx.device,
|
||||
@intFromEnum(zgpu.GraphicsContext.swapchain_format),
|
||||
@intFromEnum(zgpu.wgpu.TextureFormat.depth32_float),
|
||||
);
|
||||
defer zgui.backend.deinit();
|
||||
|
||||
zgui.io.setIniFilename(null);
|
||||
|
||||
zone_init_zgui.End();
|
||||
|
||||
// --- main loop -----------------------------------------------------------
|
||||
|
||||
const zone_main_loop = ztracy.ZoneN(@src(), "Main loop");
|
||||
|
||||
while (!window.shouldClose()) {
|
||||
ztracy.FrameMark();
|
||||
|
||||
fba.reset();
|
||||
|
||||
zglfw.pollEvents();
|
||||
zgui.backend.newFrame(
|
||||
gctx.swapchain_descriptor.width,
|
||||
gctx.swapchain_descriptor.height,
|
||||
);
|
||||
|
||||
const swapchain_texture_view = gctx.swapchain.getCurrentTextureView();
|
||||
defer swapchain_texture_view.release();
|
||||
|
||||
const commands = commands: {
|
||||
const encoder = gctx.device.createCommandEncoder(null);
|
||||
defer encoder.release();
|
||||
|
||||
{
|
||||
const pass = zgpu.beginRenderPassSimple(
|
||||
encoder,
|
||||
.load,
|
||||
swapchain_texture_view,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
);
|
||||
defer zgpu.endReleasePass(pass);
|
||||
zgui.backend.draw(pass);
|
||||
}
|
||||
|
||||
break :commands encoder.finish(null);
|
||||
};
|
||||
defer commands.release();
|
||||
|
||||
gctx.submit(&.{commands});
|
||||
_ = gctx.present();
|
||||
}
|
||||
|
||||
zone_main_loop.End();
|
||||
}
|
||||
Reference in New Issue
Block a user