110 lines
3.0 KiB
Zig
110 lines
3.0 KiB
Zig
const std = @import("std");
|
|
const builtin = @import("builtin");
|
|
const Engine = @import("engine");
|
|
const CLI = Engine.CLI;
|
|
const build_options = Engine.Lib.build_options;
|
|
|
|
pub const std_options: std.Options = .{
|
|
// TODO: Override .logFn for logging to a file
|
|
};
|
|
|
|
var engine: Engine = undefined;
|
|
|
|
pub fn main() !void {
|
|
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
|
|
defer _ = debug_allocator.deinit();
|
|
|
|
const isWasm = builtin.cpu.arch.isWasm();
|
|
|
|
// TODO: Use tracy TracingAllocator
|
|
var allocator: std.mem.Allocator = undefined;
|
|
if (builtin.cpu.arch.isWasm()) {
|
|
allocator = std.heap.wasm_allocator;
|
|
} else if (builtin.mode == .Debug) {
|
|
allocator = debug_allocator.allocator();
|
|
} else {
|
|
allocator = std.heap.smp_allocator;
|
|
}
|
|
|
|
var game_library_path: ?[]const u8 = null;
|
|
defer if (game_library_path) |str| allocator.free(str);
|
|
|
|
var asset_loading: Engine.RunOptions.AssetLoading = undefined;
|
|
if (!build_options.asset_hot_reload) {
|
|
asset_loading = .{
|
|
.bundle_bytes = @embedFile("asset_bundle")
|
|
};
|
|
} else {
|
|
asset_loading = .{
|
|
.dir_path = build_options.asset_dir
|
|
};
|
|
}
|
|
|
|
if (!isWasm) {
|
|
var cli: CLI = undefined;
|
|
cli.init(allocator);
|
|
defer cli.deinit();
|
|
|
|
const opt_help = try cli.addOption(.{
|
|
.long_name = "help",
|
|
.description = "Show this message"
|
|
});
|
|
|
|
var opt_game_lib_path: ?CLI.Option.Id = null;
|
|
if (build_options.code_hot_reload) {
|
|
opt_game_lib_path = try cli.addOption(.{
|
|
.long_name = "game-lib-path",
|
|
.description = "Load game code from dynamic library",
|
|
.kind = .single_argument
|
|
});
|
|
}
|
|
|
|
const cmd_version = try cli.addCommand(.{
|
|
.name = "version",
|
|
.description = "Show semantic version and build options"
|
|
});
|
|
|
|
const cmd_launch = try cli.addCommand(.{
|
|
.name = "launch",
|
|
});
|
|
|
|
const args = try std.process.argsAlloc(allocator);
|
|
defer std.process.argsFree(allocator, args);
|
|
|
|
var parsed = try cli.parse(allocator, args);
|
|
defer parsed.deinit();
|
|
|
|
if (parsed.isSet(opt_help)) {
|
|
cli.showUsage(args[0]);
|
|
std.process.exit(0);
|
|
}
|
|
|
|
if (opt_game_lib_path) |opt| {
|
|
if (parsed.getOptionArgument(opt)) |arg| {
|
|
game_library_path = try allocator.dupe(u8, arg);
|
|
}
|
|
}
|
|
|
|
const command = parsed.command orelse cmd_launch;
|
|
if (command == cmd_version) {
|
|
cli.stdout.write("Version: {s}\n", .{ "TODO:" });
|
|
cli.stdout.flush();
|
|
|
|
std.process.exit(0);
|
|
|
|
} else if (command == cmd_launch) {
|
|
// Nothing to be done
|
|
|
|
} else {
|
|
unreachable;
|
|
}
|
|
}
|
|
|
|
try engine.run(.{
|
|
.allocator = allocator,
|
|
.game_library_path = game_library_path,
|
|
.asset_loading = asset_loading
|
|
});
|
|
}
|
|
|