libmach: update API again, factors out init/update/deinit from native.zig main function
This commit is contained in:
parent
ce21694d75
commit
5d86314fbb
5 changed files with 121 additions and 147 deletions
|
|
@ -3,6 +3,7 @@ const Core = @import("../Core.zig");
|
|||
const gpu = @import("gpu");
|
||||
const ecs = @import("ecs");
|
||||
const glfw = @import("glfw");
|
||||
const native = @import("native.zig");
|
||||
|
||||
pub const App = @This();
|
||||
|
||||
|
|
@ -19,89 +20,42 @@ pub fn update(_: *App, _: *Core) !void { }
|
|||
// 2. Core might need to expose more state so more API functions can be exposed (for example, the WebGPU API)
|
||||
// 3. Be very careful about arguments, types, memory, etc - any mismatch will result in undefined behavior
|
||||
|
||||
pub export fn mach_set_should_close(core: *Core) void {
|
||||
pub export fn mach_core_set_should_close(core: *Core) void {
|
||||
core.setShouldClose(true);
|
||||
}
|
||||
|
||||
pub export fn mach_delta_time(core: *Core) f32 {
|
||||
pub export fn mach_core_delta_time(core: *Core) f32 {
|
||||
return core.delta_time;
|
||||
}
|
||||
|
||||
pub export fn mach_core_window_should_close(core: *Core) bool {
|
||||
return core.internal.window.shouldClose();
|
||||
}
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// Initializes and returns Mach's core structure
|
||||
// Uses an optional type so it is valid to return 0 (on an error)
|
||||
// TODO: come up with a better error reporting system
|
||||
pub export fn mach_init_core() ?*Core {
|
||||
const core: *Core = allocator.create(Core) catch {
|
||||
return @intToPtr(?*Core, 0); // on error, return null pointer
|
||||
// Returns a pointer to a newly allocated Core
|
||||
// Will return a null pointer if an error occurred while initializing Core
|
||||
pub export fn mach_core_init() ?*Core {
|
||||
const core = native.core_init(allocator) catch {
|
||||
return @intToPtr(?*Core, 0);
|
||||
};
|
||||
core.* = Core.init(allocator) catch {
|
||||
return @intToPtr(?*Core, 0); // on error, return null pointer
|
||||
};
|
||||
|
||||
// // Glfw specific: initialize the user pointer used in callbacks
|
||||
core.*.internal.initCallback();
|
||||
|
||||
return core;
|
||||
}
|
||||
|
||||
// Deinitializes mach core structure
|
||||
pub export fn mach_deinit(core: *Core) void {
|
||||
core.internal.deinit();
|
||||
allocator.destroy(core);
|
||||
pub export fn mach_core_deinit(core: *Core) void {
|
||||
native.core_deinit(core);
|
||||
}
|
||||
|
||||
pub export fn mach_window_should_close(core: *Core) bool {
|
||||
return core.internal.window.shouldClose();
|
||||
pub export fn mach_core_update(core: *Core, resize: ?native.CoreResizeCallback) MachReturn {
|
||||
native.core_update(core, resize) catch {
|
||||
return MachReturn.Failure;
|
||||
};
|
||||
return MachReturn.Success;
|
||||
}
|
||||
|
||||
pub const CoreCallback = fn (*Core, u32, u32) callconv(.C) void;
|
||||
|
||||
// Adapted from native.zig
|
||||
pub export fn mach_update(core: *Core, resize_fn: ?CoreCallback) i32 {
|
||||
if (core.internal.wait_event_timeout > 0.0) {
|
||||
if (core.internal.wait_event_timeout == std.math.inf(f64)) {
|
||||
// Wait for an event
|
||||
glfw.waitEvents() catch {
|
||||
return 0;
|
||||
};
|
||||
} else {
|
||||
// Wait for an event with a timeout
|
||||
glfw.waitEventsTimeout(core.internal.wait_event_timeout) catch {
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// Don't wait for events
|
||||
glfw.pollEvents() catch {
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
core.delta_time_ns = core.timer.lapPrecise();
|
||||
core.delta_time = @intToFloat(f32, core.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s);
|
||||
|
||||
var framebuffer_size = core.getFramebufferSize();
|
||||
core.target_desc.width = framebuffer_size.width;
|
||||
core.target_desc.height = framebuffer_size.height;
|
||||
|
||||
if (core.swap_chain == null or !core.current_desc.equal(&core.target_desc)) {
|
||||
const use_legacy_api = core.surface == null;
|
||||
if (!use_legacy_api) {
|
||||
core.swap_chain = core.device.nativeCreateSwapChain(core.surface, &core.target_desc);
|
||||
} else core.swap_chain.?.configure(
|
||||
core.swap_chain_format,
|
||||
.{ .render_attachment = true },
|
||||
core.target_desc.width,
|
||||
core.target_desc.height,
|
||||
);
|
||||
|
||||
if (resize_fn != null) {
|
||||
resize_fn.?(core, core.target_desc.width, core.target_desc.height);
|
||||
}
|
||||
core.current_desc = core.target_desc;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
const MachReturn = enum(c_int) {
|
||||
Failure,
|
||||
Success,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -587,60 +587,81 @@ pub const Platform = struct {
|
|||
|
||||
pub const BackingTimer = std.time.Timer;
|
||||
|
||||
var app: App = undefined;
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var core = try Core.init(allocator);
|
||||
defer core.internal.deinit();
|
||||
var app: App = undefined;
|
||||
var core = try core_init(allocator);
|
||||
defer core_deinit(core);
|
||||
|
||||
try app.init(&core);
|
||||
defer app.deinit(&core);
|
||||
try app.init(core);
|
||||
defer app.deinit(core);
|
||||
|
||||
// Glfw specific: initialize the user pointer used in callbacks
|
||||
core.internal.initCallback();
|
||||
while (!core.internal.window.shouldClose()) {
|
||||
try core_update(core, null);
|
||||
|
||||
const window = core.internal.window;
|
||||
while (!window.shouldClose()) {
|
||||
if (core.internal.wait_event_timeout > 0.0) {
|
||||
if (core.internal.wait_event_timeout == std.math.inf(f64)) {
|
||||
// Wait for an event
|
||||
try glfw.waitEvents();
|
||||
} else {
|
||||
// Wait for an event with a timeout
|
||||
try glfw.waitEventsTimeout(core.internal.wait_event_timeout);
|
||||
}
|
||||
} else {
|
||||
// Don't wait for events
|
||||
try glfw.pollEvents();
|
||||
}
|
||||
|
||||
core.delta_time_ns = core.timer.lapPrecise();
|
||||
core.delta_time = @intToFloat(f32, core.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s);
|
||||
|
||||
var framebuffer_size = core.getFramebufferSize();
|
||||
core.target_desc.width = framebuffer_size.width;
|
||||
core.target_desc.height = framebuffer_size.height;
|
||||
|
||||
if (core.swap_chain == null or !core.current_desc.equal(&core.target_desc)) {
|
||||
const use_legacy_api = core.surface == null;
|
||||
if (!use_legacy_api) {
|
||||
core.swap_chain = core.device.nativeCreateSwapChain(core.surface, &core.target_desc);
|
||||
} else core.swap_chain.?.configure(
|
||||
core.swap_chain_format,
|
||||
.{ .render_attachment = true },
|
||||
core.target_desc.width,
|
||||
core.target_desc.height,
|
||||
);
|
||||
|
||||
if (@hasDecl(App, "resize")) {
|
||||
try app.resize(&core, core.target_desc.width, core.target_desc.height);
|
||||
}
|
||||
core.current_desc = core.target_desc;
|
||||
}
|
||||
|
||||
try app.update(&core);
|
||||
try app.update(core);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn core_init(allocator: std.mem.Allocator) !*Core {
|
||||
const core: *Core = try allocator.create(Core);
|
||||
core.* = try Core.init(allocator);
|
||||
|
||||
// // Glfw specific: initialize the user pointer used in callbacks
|
||||
core.*.internal.initCallback();
|
||||
|
||||
return core;
|
||||
}
|
||||
|
||||
pub export fn core_deinit(core: *Core) void {
|
||||
core.internal.deinit();
|
||||
// assumes that core.allocator is the same allocator used to allocate core
|
||||
core.allocator.destroy(core); // "I used the core to destroy the core"
|
||||
}
|
||||
|
||||
pub const CoreResizeCallback = fn (*Core, u32, u32) callconv(.C) void;
|
||||
|
||||
pub fn core_update(core: *Core, resize: ?CoreResizeCallback) !void {
|
||||
if (core.internal.wait_event_timeout > 0.0) {
|
||||
if (core.internal.wait_event_timeout == std.math.inf(f64)) {
|
||||
// Wait for an event
|
||||
try glfw.waitEvents();
|
||||
} else {
|
||||
// Wait for an event with a timeout
|
||||
try glfw.waitEventsTimeout(core.internal.wait_event_timeout);
|
||||
}
|
||||
} else {
|
||||
// Don't wait for events
|
||||
try glfw.pollEvents();
|
||||
}
|
||||
|
||||
core.delta_time_ns = core.timer.lapPrecise();
|
||||
core.delta_time = @intToFloat(f32, core.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s);
|
||||
|
||||
var framebuffer_size = core.getFramebufferSize();
|
||||
core.target_desc.width = framebuffer_size.width;
|
||||
core.target_desc.height = framebuffer_size.height;
|
||||
|
||||
if (core.swap_chain == null or !core.current_desc.equal(&core.target_desc)) {
|
||||
const use_legacy_api = core.surface == null;
|
||||
if (!use_legacy_api) {
|
||||
core.swap_chain = core.device.nativeCreateSwapChain(core.surface, &core.target_desc);
|
||||
} else core.swap_chain.?.configure(
|
||||
core.swap_chain_format,
|
||||
.{ .render_attachment = true },
|
||||
core.target_desc.width,
|
||||
core.target_desc.height,
|
||||
);
|
||||
|
||||
if (@hasDecl(App, "resize")) {
|
||||
try app.resize(core, core.target_desc.width, core.target_desc.height);
|
||||
} else if (resize != null) {
|
||||
resize.?(core, core.target_desc.width, core.target_desc.height);
|
||||
}
|
||||
core.current_desc = core.target_desc;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue