Vulkan port
This commit is contained in:
226
src/main.zig
226
src/main.zig
@@ -1,215 +1,63 @@
|
||||
const std = @import("std");
|
||||
|
||||
const ig = @import("cimgui");
|
||||
const sokol = @import("sokol");
|
||||
|
||||
const sapp = sokol.app;
|
||||
const sg = sokol.gfx;
|
||||
const sglue = sokol.glue;
|
||||
const simgui = sokol.imgui;
|
||||
const glfw = @import("zglfw");
|
||||
const stbi = @import("zstbi");
|
||||
const vk = @import("vulkan");
|
||||
|
||||
const c = @import("const.zig");
|
||||
const engine = @import("engine.zig");
|
||||
const game = @import("game.zig");
|
||||
const Log = @import("Log.zig");
|
||||
|
||||
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";
|
||||
|
||||
pub var allocator: std.mem.Allocator = undefined;
|
||||
pub var temp_allocator: std.mem.Allocator = undefined;
|
||||
|
||||
pub var logs: std.ArrayListUnmanaged(Log) = .empty;
|
||||
|
||||
pub const std_options: std.Options = .{
|
||||
.logFn = logFn,
|
||||
};
|
||||
|
||||
const imgui_action = blk: {
|
||||
var ret: sg.PassAction = .{};
|
||||
|
||||
ret.colors[0] = .{
|
||||
.load_action = .LOAD,
|
||||
};
|
||||
|
||||
break :blk ret;
|
||||
};
|
||||
|
||||
fn init() callconv(.c) void {
|
||||
sg.setup(.{
|
||||
.environment = sglue.environment(),
|
||||
.logger = .{ .func = sokolLogFn },
|
||||
});
|
||||
|
||||
simgui.setup(.{
|
||||
.logger = .{ .func = sokolLogFn },
|
||||
});
|
||||
|
||||
sapp.lockMouse(true);
|
||||
|
||||
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 = dt,
|
||||
.dpi_scale = sapp.dpiScale(),
|
||||
});
|
||||
|
||||
game.update(@floatCast(dt));
|
||||
|
||||
// --- BEGIN IMGUI PASS ---
|
||||
|
||||
sg.beginPass(.{
|
||||
.action = imgui_action,
|
||||
.swapchain = sglue.swapchain(),
|
||||
});
|
||||
simgui.render();
|
||||
sg.endPass();
|
||||
|
||||
// --- END IMGUI PASS ---
|
||||
|
||||
sg.commit();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
if (ev.type == .KEY_UP and !ev.key_repeat) {
|
||||
game.onKeyUp(ev.key_code, ev.modifiers);
|
||||
}
|
||||
}
|
||||
|
||||
if (ev.type == .MOUSE_MOVE) {
|
||||
game.onMouseMove(ev.mouse_dx, ev.mouse_dy);
|
||||
}
|
||||
}
|
||||
pub var window: *glfw.Window = undefined;
|
||||
|
||||
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;
|
||||
pub var buffer: [c.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);
|
||||
}
|
||||
stbi.init(allocator);
|
||||
defer stbi.deinit();
|
||||
|
||||
sapp.run(.{
|
||||
.init_cb = &init,
|
||||
.cleanup_cb = &deinit,
|
||||
.frame_cb = &frame,
|
||||
.event_cb = &event,
|
||||
.window_title = "Voxel Game",
|
||||
.width = 1280,
|
||||
.height = 720,
|
||||
.icon = .{ .sokol_default = true },
|
||||
.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,
|
||||
glfw.init() catch |err| {
|
||||
std.log.err("Could not initialize GLFW", .{});
|
||||
return err;
|
||||
};
|
||||
defer glfw.terminate();
|
||||
|
||||
const full_message = allocator.dupe(u8, writer.buffered()) catch return;
|
||||
if (!glfw.isVulkanSupported()) {
|
||||
std.log.err("Vulkan is not supported by this system", .{});
|
||||
return error.NoVulkan;
|
||||
}
|
||||
|
||||
std.debug.print("{s}", .{full_message});
|
||||
|
||||
logs.append(allocator, .init(level, full_message)) catch {
|
||||
allocator.free(full_message);
|
||||
return;
|
||||
glfw.windowHint(.client_api, .no_api);
|
||||
window = glfw.Window.create(c.default_window_width, c.default_window_height, c.window_title, null) catch |err| {
|
||||
std.log.err("Could not create window", .{});
|
||||
return err;
|
||||
};
|
||||
}
|
||||
defer window.destroy();
|
||||
|
||||
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;
|
||||
window.setSizeLimits(c.min_window_width, c.min_window_height, -1, -1);
|
||||
|
||||
engine.init() catch |err| {
|
||||
std.log.err("Could not initialize engine", .{});
|
||||
return err;
|
||||
};
|
||||
defer engine.deinit();
|
||||
|
||||
//game.init();
|
||||
//defer game.deinit();
|
||||
|
||||
while (!window.shouldClose()) {
|
||||
glfw.pollEvents();
|
||||
//game.update(dt);
|
||||
window.swapBuffers();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user