revert bad entrypoint API design changes

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-08-23 23:03:54 -07:00
parent ee4d3886e5
commit bfa3b069f7
10 changed files with 65 additions and 87 deletions

View file

@ -8,11 +8,9 @@ pub const modules = .{
}; };
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -8,11 +8,9 @@ pub const modules = .{
}; };
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -10,11 +10,9 @@ pub const modules = .{
}; };
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -11,11 +11,9 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead // TODO(important): use standard entrypoint instead
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -12,11 +12,9 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead // TODO(important): use standard entrypoint instead
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -10,11 +10,9 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead // TODO(important): use standard entrypoint instead
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -10,11 +10,9 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead // TODO(important): use standard entrypoint instead
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -10,11 +10,9 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead // TODO(important): use standard entrypoint instead
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -10,11 +10,9 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead // TODO(important): use standard entrypoint instead
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Initialize mach.Core
defer _ = gpa.deinit(); try mach.core.initModule();
const allocator = gpa.allocator();
var app = try mach.App.init(allocator, .app); // Main loop
defer app.deinit(allocator); while (try mach.core.tick()) {}
try app.run(.{ .allocator = allocator });
} }

View file

@ -45,54 +45,50 @@ pub const Entities = @import("module/main.zig").Entities;
pub const is_debug = builtin.mode == .Debug; pub const is_debug = builtin.mode == .Debug;
pub const App = struct { pub const core = struct {
mods: *Modules, var mods: Modules = undefined;
comptime main_mod: ModuleName = .app,
pub fn init(allocator: std.mem.Allocator, comptime main_mod: ModuleName) !App {
var mods: *Modules = try allocator.create(Modules);
try mods.init(allocator);
return .{
.mods = mods,
.main_mod = main_mod,
};
}
pub fn deinit(app: *App, allocator: std.mem.Allocator) void {
app.mods.deinit(allocator);
allocator.destroy(app.mods);
}
pub fn run(app: *App, core_options: Core.InitOptions) !void {
var stack_space: [8 * 1024 * 1024]u8 = undefined; var stack_space: [8 * 1024 * 1024]u8 = undefined;
app.mods.mod.mach_core.init(undefined); // TODO pub fn initModule() !void {
app.mods.scheduleWithArgs(.mach_core, .init, .{core_options}); try mods.init(std.heap.c_allocator); // TODO: allocator
app.mods.schedule(app.main_mod, .init);
// Main loop // TODO: this is a hack
mods.mod.mach_core.init(undefined);
mods.scheduleWithArgs(.mach_core, .init, .{.{ .allocator = std.heap.c_allocator }});
mods.schedule(.app, .init);
}
pub fn tick() !bool {
if (comptime builtin.target.isDarwin()) { if (comptime builtin.target.isDarwin()) {
Core.Platform.run(on_each_update, .{app, &stack_space}); // TODO: tick() should never block, but we should have a way to block for other platforms.
Core.Platform.run(on_each_update, .{});
} else { } else {
while (try app.on_each_update(&stack_space)) {} return try on_each_update();
}
} }
fn on_each_update(app: *App, stack_space: []u8) !bool { return false;
if (app.mods.mod.mach_core.state().should_close) { }
// TODO: support deinitialization
// pub fn deinit() void {
// mods.deinit(std.heap.c_allocator); // TODO: allocator
// }
fn on_each_update() !bool {
// TODO: this should not exist here
if (mods.mod.mach_core.state().should_close) {
// Final Dispatch to deinitalize resources // Final Dispatch to deinitalize resources
app.mods.schedule(app.main_mod, .deinit); mods.schedule(.app, .deinit);
try app.mods.dispatch(stack_space, .{}); try mods.dispatch(&stack_space, .{});
app.mods.schedule(.mach_core, .deinit); mods.schedule(.mach_core, .deinit);
try app.mods.dispatch(stack_space, .{}); try mods.dispatch(&stack_space, .{});
return false; return false;
} }
// Dispatch events until queue is empty // Dispatch events until queue is empty
try app.mods.dispatch(stack_space, .{}); try mods.dispatch(&stack_space, .{});
// Run `update` when `init` and all other systems are executed // Run `update` when `init` and all other systems are executed
app.mods.schedule(app.main_mod, .update); mods.schedule(.app, .update);
return true; return true;
} }
}; };