Port old pipeline logic
This commit is contained in:
192
src/main.zig
192
src/main.zig
@@ -1,3 +1,5 @@
|
||||
const std = @import("std");
|
||||
|
||||
const ig = @import("cimgui");
|
||||
const shader = @import("shader");
|
||||
const sokol = @import("sokol");
|
||||
@@ -6,19 +8,24 @@ const sapp = sokol.app;
|
||||
const sg = sokol.gfx;
|
||||
const sglue = sokol.glue;
|
||||
const simgui = sokol.imgui;
|
||||
const slog = sokol.log;
|
||||
|
||||
var show_demo_window = true;
|
||||
const game = @import("game.zig");
|
||||
const Log = @import("Log.zig");
|
||||
|
||||
const main_action = blk: {
|
||||
var ret: sg.PassAction = .{};
|
||||
pub const min_framerate = 30.0;
|
||||
pub const min_frametime = 1.0 / min_framerate;
|
||||
pub const min_window_height = 360;
|
||||
pub const min_window_width = 640;
|
||||
pub const temp_allocator_capacity = 16 * 1024 * 1024;
|
||||
pub const window_title = "voxel-game";
|
||||
|
||||
ret.colors[0] = .{
|
||||
.load_action = .CLEAR,
|
||||
.clear_value = .{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0 },
|
||||
};
|
||||
pub var allocator: std.mem.Allocator = undefined;
|
||||
pub var temp_allocator: std.mem.Allocator = undefined;
|
||||
|
||||
break :blk ret;
|
||||
pub var logs: std.ArrayListUnmanaged(Log) = .empty;
|
||||
|
||||
pub const std_options: std.Options = .{
|
||||
.logFn = logFn,
|
||||
};
|
||||
|
||||
const imgui_action = blk: {
|
||||
@@ -31,72 +38,38 @@ const imgui_action = blk: {
|
||||
break :blk ret;
|
||||
};
|
||||
|
||||
var bindings: sg.Bindings = .{};
|
||||
var pipeline: sg.Pipeline = .{};
|
||||
|
||||
fn init() callconv(.c) void {
|
||||
sg.setup(.{
|
||||
.environment = sglue.environment(),
|
||||
.logger = .{ .func = slog.func },
|
||||
.logger = .{ .func = sokolLogFn },
|
||||
});
|
||||
|
||||
simgui.setup(.{
|
||||
.logger = .{ .func = slog.func },
|
||||
.logger = .{ .func = sokolLogFn },
|
||||
});
|
||||
|
||||
bindings.vertex_buffers[0] = sg.makeBuffer(.{
|
||||
.data = sg.asRange(&[_]f32{
|
||||
// positionOS texCoord normalOS tangentOS
|
||||
0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0,
|
||||
0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0,
|
||||
-0.5, -0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0,
|
||||
}),
|
||||
});
|
||||
|
||||
pipeline = sg.makePipeline(.{
|
||||
.shader = sg.makeShader(shader.programShaderDesc(sg.queryBackend())),
|
||||
.layout = blk: {
|
||||
var ret: sg.VertexLayoutState = .{};
|
||||
ret.attrs[shader.ATTR_program_positionOS].format = .FLOAT3;
|
||||
ret.attrs[shader.ATTR_program_texCoord].format = .FLOAT2;
|
||||
ret.attrs[shader.ATTR_program_normalOS].format = .FLOAT3;
|
||||
ret.attrs[shader.ATTR_program_tangentOS].format = .FLOAT4;
|
||||
break :blk ret;
|
||||
},
|
||||
});
|
||||
game.init();
|
||||
}
|
||||
|
||||
fn deinit() callconv(.c) void {
|
||||
game.deinit();
|
||||
simgui.shutdown();
|
||||
sg.shutdown();
|
||||
}
|
||||
|
||||
fn frame() callconv(.c) void {
|
||||
const dt = sapp.frameDuration();
|
||||
|
||||
simgui.newFrame(.{
|
||||
.width = sapp.width(),
|
||||
.height = sapp.height(),
|
||||
.delta_time = sapp.frameDuration(),
|
||||
.delta_time = dt,
|
||||
.dpi_scale = sapp.dpiScale(),
|
||||
});
|
||||
|
||||
// --- UPDATE ---
|
||||
game.update(dt);
|
||||
|
||||
if (show_demo_window) {
|
||||
ig.igShowDemoWindow(&show_demo_window);
|
||||
}
|
||||
|
||||
// --- MAIN PASS ---
|
||||
|
||||
sg.beginPass(.{
|
||||
.action = main_action,
|
||||
.swapchain = sglue.swapchain(),
|
||||
});
|
||||
sg.applyPipeline(pipeline);
|
||||
sg.applyBindings(bindings);
|
||||
sg.draw(0, 3, 1);
|
||||
sg.endPass();
|
||||
|
||||
// --- IMGUI PASS ---
|
||||
// --- BEGIN IMGUI PASS ---
|
||||
|
||||
sg.beginPass(.{
|
||||
.action = imgui_action,
|
||||
@@ -105,16 +78,40 @@ fn frame() callconv(.c) void {
|
||||
simgui.render();
|
||||
sg.endPass();
|
||||
|
||||
// ---
|
||||
// --- END IMGUI PASS ---
|
||||
|
||||
sg.commit();
|
||||
}
|
||||
|
||||
fn event(ev: [*c]const sapp.Event) callconv(.c) void {
|
||||
_ = simgui.handleEvent(ev.*);
|
||||
fn event(_ev: [*c]const sapp.Event) callconv(.c) void {
|
||||
const ev: *const sapp.Event = @ptrCast(_ev);
|
||||
const ig_capture_keyboard = simgui.handleEvent(ev.*);
|
||||
|
||||
if (!ig_capture_keyboard) {
|
||||
if (ev.type == .KEY_DOWN and !ev.key_repeat) {
|
||||
game.onKeyDown(ev.key_code, ev.modifiers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
pub fn main() !void {
|
||||
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
var fba: std.heap.FixedBufferAllocator = .init(&struct {
|
||||
pub var buffer: [temp_allocator_capacity]u8 = undefined;
|
||||
}.buffer);
|
||||
|
||||
allocator = gpa.allocator();
|
||||
temp_allocator = fba.threadSafeAllocator();
|
||||
|
||||
defer {
|
||||
for (logs.items) |log| {
|
||||
allocator.free(log.message);
|
||||
}
|
||||
logs.deinit(allocator);
|
||||
}
|
||||
|
||||
sapp.run(.{
|
||||
.init_cb = &init,
|
||||
.cleanup_cb = &deinit,
|
||||
@@ -124,6 +121,87 @@ pub fn main() void {
|
||||
.width = 1280,
|
||||
.height = 720,
|
||||
.icon = .{ .sokol_default = true },
|
||||
.logger = .{ .func = slog.func },
|
||||
.logger = .{ .func = sokolLogFn },
|
||||
});
|
||||
}
|
||||
|
||||
fn sokolLogFn(
|
||||
maybe_tag: ?[*:0]const u8,
|
||||
log_level: u32,
|
||||
log_item: u32,
|
||||
maybe_message: ?[*:0]const u8,
|
||||
line_nr: u32,
|
||||
maybe_filename: ?[*:0]const u8,
|
||||
user_data: ?*anyopaque,
|
||||
) callconv(.c) void {
|
||||
defer {
|
||||
if (log_level == 0) {
|
||||
const message = std.mem.span(maybe_message) orelse "";
|
||||
@panic(message);
|
||||
}
|
||||
}
|
||||
|
||||
const _SLOG_LINE_LENGTH = 512;
|
||||
|
||||
_ = user_data;
|
||||
|
||||
var line_buf: [_SLOG_LINE_LENGTH]u8 = undefined;
|
||||
var writer = std.Io.Writer.fixed(&line_buf);
|
||||
|
||||
if (maybe_tag) |tag| {
|
||||
writer.print("[{s}]", .{tag}) catch return;
|
||||
}
|
||||
|
||||
writer.print("[{s}]", .{switch (log_level) {
|
||||
0 => "panic",
|
||||
1 => "error",
|
||||
2 => "warning",
|
||||
else => "info",
|
||||
}}) catch return;
|
||||
|
||||
writer.print("[id:{d}]", .{log_item}) catch return;
|
||||
|
||||
if (maybe_filename) |filename| {
|
||||
writer.print(" {s}:{d}:0: ", .{ filename, line_nr }) catch return;
|
||||
} else {
|
||||
writer.print("[line:{d}]", .{line_nr}) catch return;
|
||||
}
|
||||
|
||||
if (maybe_message) |message| {
|
||||
writer.print("\n\t{s}", .{message}) catch return;
|
||||
}
|
||||
|
||||
writer.print("\n\n", .{}) catch return;
|
||||
|
||||
if (log_level == 0) {
|
||||
writer.print("ABORTING because of [panic]\n", .{}) catch return;
|
||||
}
|
||||
|
||||
const level: std.log.Level = switch (log_level) {
|
||||
0, 1 => .err,
|
||||
2 => .warn,
|
||||
else => .info,
|
||||
};
|
||||
|
||||
const full_message = allocator.dupe(u8, writer.buffered()) catch return;
|
||||
|
||||
std.debug.print("{s}", .{full_message});
|
||||
|
||||
logs.append(allocator, .init(level, full_message)) catch {
|
||||
allocator.free(full_message);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
fn logFn(
|
||||
comptime level: std.log.Level,
|
||||
comptime _: @Type(.enum_literal),
|
||||
comptime fmt: []const u8,
|
||||
args: anytype,
|
||||
) void {
|
||||
const message = std.fmt.allocPrint(allocator, fmt, args) catch return;
|
||||
logs.append(allocator, .init(level, message)) catch {
|
||||
allocator.free(message);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user