mach: rename mach.Engine -> mach.Core

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-07-04 19:46:03 -07:00 committed by Stephen Gutekanst
parent 6f7f17c5e8
commit e79c9e075a
6 changed files with 83 additions and 83 deletions

View file

@ -8,7 +8,7 @@ const structs = @import("structs.zig");
const enums = @import("enums.zig"); const enums = @import("enums.zig");
const Timer = @import("Timer.zig"); const Timer = @import("Timer.zig");
const Engine = @This(); const Core = @This();
allocator: Allocator, allocator: Allocator,
@ -18,7 +18,7 @@ options: structs.Options,
/// ///
/// For example, if you are animating a cube which should rotate 360 degrees every second, /// For example, if you are animating a cube which should rotate 360 degrees every second,
/// instead of writing (360.0 / 60.0) and assuming the frame rate is 60hz, write /// instead of writing (360.0 / 60.0) and assuming the frame rate is 60hz, write
/// (360.0 * engine.delta_time) /// (360.0 * core.delta_time)
delta_time: f32 = 0, delta_time: f32 = 0,
delta_time_ns: u64 = 0, delta_time_ns: u64 = 0,
timer: Timer, timer: Timer,
@ -34,28 +34,28 @@ target_desc: gpu.SwapChain.Descriptor,
internal: platform.Type, internal: platform.Type,
pub fn init(allocator: std.mem.Allocator) !Engine { pub fn init(allocator: std.mem.Allocator) !Core {
var engine: Engine = undefined; var core: Core = undefined;
engine.allocator = allocator; core.allocator = allocator;
engine.options = structs.Options{}; core.options = structs.Options{};
engine.timer = try Timer.start(); core.timer = try Timer.start();
engine.internal = try platform.Type.init(allocator, &engine); core.internal = try platform.Type.init(allocator, &core);
return engine; return core;
} }
/// Set runtime options for application, like title, window size etc. /// Set runtime options for application, like title, window size etc.
/// ///
/// See mach.Options for details /// See mach.Options for details
pub fn setOptions(engine: *Engine, options: structs.Options) !void { pub fn setOptions(core: *Core, options: structs.Options) !void {
try engine.internal.setOptions(options); try core.internal.setOptions(options);
engine.options = options; core.options = options;
} }
// Signals mach to stop the update loop. // Signals mach to stop the update loop.
pub fn setShouldClose(engine: *Engine, value: bool) void { pub fn setShouldClose(core: *Core, value: bool) void {
engine.internal.setShouldClose(value); core.internal.setShouldClose(value);
} }
// Sets seconds to wait for an event with timeout before calling update() // Sets seconds to wait for an event with timeout before calling update()
@ -69,32 +69,32 @@ pub fn setShouldClose(engine: *Engine, value: bool) void {
// //
// update() can be called a bit later than timeout due to timer precision and // update() can be called a bit later than timeout due to timer precision and
// process scheduling. // process scheduling.
pub fn setWaitEvent(engine: *Engine, timeout: f64) void { pub fn setWaitEvent(core: *Core, timeout: f64) void {
engine.internal.setWaitEvent(timeout); core.internal.setWaitEvent(timeout);
} }
// Returns the framebuffer size, in subpixel units. // Returns the framebuffer size, in subpixel units.
// //
// e.g. returns 1280x960 on macOS for a window that is 640x480 // e.g. returns 1280x960 on macOS for a window that is 640x480
pub fn getFramebufferSize(engine: *Engine) structs.Size { pub fn getFramebufferSize(core: *Core) structs.Size {
return engine.internal.getFramebufferSize(); return core.internal.getFramebufferSize();
} }
// Returns the widow size, in pixel units. // Returns the widow size, in pixel units.
// //
// e.g. returns 1280x960 on macOS for a window that is 640x480 // e.g. returns 1280x960 on macOS for a window that is 640x480
pub fn getWindowSize(engine: *Engine) structs.Size { pub fn getWindowSize(core: *Core) structs.Size {
return engine.internal.getWindowSize(); return core.internal.getWindowSize();
} }
pub fn setMouseCursor(engine: *Engine, cursor: enums.MouseCursor) !void { pub fn setMouseCursor(core: *Core, cursor: enums.MouseCursor) !void {
try engine.internal.setMouseCursor(cursor); try core.internal.setMouseCursor(cursor);
} }
pub fn hasEvent(engine: *Engine) bool { pub fn hasEvent(core: *Core) bool {
return engine.internal.hasEvent(); return core.internal.hasEvent();
} }
pub fn pollEvent(engine: *Engine) ?structs.Event { pub fn pollEvent(core: *Core) ?structs.Event {
return engine.internal.pollEvent(); return core.internal.pollEvent();
} }

View file

@ -1,5 +1,5 @@
pub usingnamespace @import("structs.zig"); pub usingnamespace @import("structs.zig");
pub usingnamespace @import("enums.zig"); pub usingnamespace @import("enums.zig");
pub const Engine = @import("Engine.zig"); pub const Core = @import("Core.zig");
pub const Timer = @import("Timer.zig"); pub const Timer = @import("Timer.zig");
pub const ResourceManager = @import("resource/ResourceManager.zig"); pub const ResourceManager = @import("resource/ResourceManager.zig");

View file

@ -1,27 +1,27 @@
const Engine = @import("../Engine.zig"); const Core = @import("../Core.zig");
pub fn checkApplication(comptime App: type) void { pub fn checkApplication(comptime App: type) void {
if (@hasDecl(App, "init")) { if (@hasDecl(App, "init")) {
const InitFn = @TypeOf(@field(App, "init")); const InitFn = @TypeOf(@field(App, "init"));
if (InitFn != fn (app: *App, engine: *Engine) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void) if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void)
@compileError("expected 'pub fn init(app: *App, engine: *mach.Engine) !void' found '" ++ @typeName(InitFn) ++ "'"); @compileError("expected 'pub fn init(app: *App, core: *mach.Core) !void' found '" ++ @typeName(InitFn) ++ "'");
} else { } else {
@compileError("App must export 'pub fn init(app: *App, engine: *mach.Engine) !void'"); @compileError("App must export 'pub fn init(app: *App, core: *mach.Core) !void'");
} }
if (@hasDecl(App, "update")) { if (@hasDecl(App, "update")) {
const UpdateFn = @TypeOf(@field(App, "update")); const UpdateFn = @TypeOf(@field(App, "update"));
if (UpdateFn != fn (app: *App, engine: *Engine) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void) if (UpdateFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void)
@compileError("expected 'pub fn update(app: *App, engine: *mach.Engine) !void' found '" ++ @typeName(UpdateFn) ++ "'"); @compileError("expected 'pub fn update(app: *App, core: *mach.Core) !void' found '" ++ @typeName(UpdateFn) ++ "'");
} else { } else {
@compileError("App must export 'pub fn update(app: *App, engine: *mach.Engine) !void'"); @compileError("App must export 'pub fn update(app: *App, core: *mach.Core) !void'");
} }
if (@hasDecl(App, "deinit")) { if (@hasDecl(App, "deinit")) {
const DeinitFn = @TypeOf(@field(App, "deinit")); const DeinitFn = @TypeOf(@field(App, "deinit"));
if (DeinitFn != fn (app: *App, engine: *Engine) void) if (DeinitFn != fn (app: *App, core: *Core) void)
@compileError("expected 'pub fn deinit(app: *App, engine: *mach.Engine) void' found '" ++ @typeName(DeinitFn) ++ "'"); @compileError("expected 'pub fn deinit(app: *App, core: *mach.Core) void' found '" ++ @typeName(DeinitFn) ++ "'");
} else { } else {
@compileError("App must export 'pub fn deinit(app: *App, engine: *mach.Engine) void'"); @compileError("App must export 'pub fn deinit(app: *App, core: *mach.Core) void'");
} }
} }

View file

@ -2,7 +2,7 @@ const std = @import("std");
const glfw = @import("glfw"); const glfw = @import("glfw");
const gpu = @import("gpu"); const gpu = @import("gpu");
const App = @import("app"); const App = @import("app");
const Engine = @import("../Engine.zig"); const Core = @import("../Core.zig");
const structs = @import("../structs.zig"); const structs = @import("../structs.zig");
const enums = @import("../enums.zig"); const enums = @import("../enums.zig");
const util = @import("util.zig"); const util = @import("util.zig");
@ -38,8 +38,8 @@ pub const Platform = struct {
platform: *Platform, platform: *Platform,
}; };
pub fn init(allocator: std.mem.Allocator, engine: *Engine) !Platform { pub fn init(allocator: std.mem.Allocator, core: *Core) !Platform {
const options = engine.options; const options = core.options;
const backend_type = try util.detectBackendType(allocator); const backend_type = try util.detectBackendType(allocator);
glfw.setErrorCallback(Platform.errorCallback); glfw.setErrorCallback(Platform.errorCallback);
@ -174,20 +174,20 @@ pub const Platform = struct {
device.setUncapturedErrorCallback(&util.printUnhandledErrorCallback); device.setUncapturedErrorCallback(&util.printUnhandledErrorCallback);
engine.device = device; core.device = device;
engine.backend_type = backend_type; core.backend_type = backend_type;
engine.surface = surface; core.surface = surface;
engine.swap_chain = swap_chain; core.swap_chain = swap_chain;
engine.swap_chain_format = swap_chain_format; core.swap_chain_format = swap_chain_format;
engine.current_desc = descriptor; core.current_desc = descriptor;
engine.target_desc = descriptor; core.target_desc = descriptor;
const cursor_pos = try window.getCursorPos(); const cursor_pos = try window.getCursorPos();
return Platform{ return Platform{
.window = window, .window = window,
.backend_type = backend_type, .backend_type = backend_type,
.allocator = engine.allocator, .allocator = core.allocator,
.last_window_size = .{ .width = window_size.width, .height = window_size.height }, .last_window_size = .{ .width = window_size.width, .height = window_size.height },
.last_framebuffer_size = .{ .width = framebuffer_size.width, .height = framebuffer_size.height }, .last_framebuffer_size = .{ .width = framebuffer_size.width, .height = framebuffer_size.height },
.last_position = try window.getPos(), .last_position = try window.getPos(),
@ -591,55 +591,55 @@ pub fn main() !void {
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var engine = try Engine.init(allocator); var core = try Core.init(allocator);
defer engine.internal.deinit(); defer core.internal.deinit();
var app: App = undefined; var app: App = undefined;
try app.init(&engine); try app.init(&core);
defer app.deinit(&engine); defer app.deinit(&core);
// Glfw specific: initialize the user pointer used in callbacks // Glfw specific: initialize the user pointer used in callbacks
engine.internal.initCallback(); core.internal.initCallback();
const window = engine.internal.window; const window = core.internal.window;
while (!window.shouldClose()) { while (!window.shouldClose()) {
if (engine.internal.wait_event_timeout > 0.0) { if (core.internal.wait_event_timeout > 0.0) {
if (engine.internal.wait_event_timeout == std.math.inf(f64)) { if (core.internal.wait_event_timeout == std.math.inf(f64)) {
// Wait for an event // Wait for an event
try glfw.waitEvents(); try glfw.waitEvents();
} else { } else {
// Wait for an event with a timeout // Wait for an event with a timeout
try glfw.waitEventsTimeout(engine.internal.wait_event_timeout); try glfw.waitEventsTimeout(core.internal.wait_event_timeout);
} }
} else { } else {
// Don't wait for events // Don't wait for events
try glfw.pollEvents(); try glfw.pollEvents();
} }
engine.delta_time_ns = engine.timer.lapPrecise(); core.delta_time_ns = core.timer.lapPrecise();
engine.delta_time = @intToFloat(f32, engine.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s); core.delta_time = @intToFloat(f32, core.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s);
var framebuffer_size = engine.getFramebufferSize(); var framebuffer_size = core.getFramebufferSize();
engine.target_desc.width = framebuffer_size.width; core.target_desc.width = framebuffer_size.width;
engine.target_desc.height = framebuffer_size.height; core.target_desc.height = framebuffer_size.height;
if (engine.swap_chain == null or !engine.current_desc.equal(&engine.target_desc)) { if (core.swap_chain == null or !core.current_desc.equal(&core.target_desc)) {
const use_legacy_api = engine.surface == null; const use_legacy_api = core.surface == null;
if (!use_legacy_api) { if (!use_legacy_api) {
engine.swap_chain = engine.device.nativeCreateSwapChain(engine.surface, &engine.target_desc); core.swap_chain = core.device.nativeCreateSwapChain(core.surface, &core.target_desc);
} else engine.swap_chain.?.configure( } else core.swap_chain.?.configure(
engine.swap_chain_format, core.swap_chain_format,
.{ .render_attachment = true }, .{ .render_attachment = true },
engine.target_desc.width, core.target_desc.width,
engine.target_desc.height, core.target_desc.height,
); );
if (@hasDecl(App, "resize")) { if (@hasDecl(App, "resize")) {
try app.resize(&engine, engine.target_desc.width, engine.target_desc.height); try app.resize(&core, core.target_desc.width, core.target_desc.height);
} }
engine.current_desc = engine.target_desc; core.current_desc = core.target_desc;
} }
try app.update(&engine); try app.update(&core);
} }
} }

View file

@ -1,6 +1,6 @@
const std = @import("std"); const std = @import("std");
const App = @import("app"); const App = @import("app");
const Engine = @import("../Engine.zig"); const Core = @import("../Core.zig");
const structs = @import("../structs.zig"); const structs = @import("../structs.zig");
const enums = @import("../enums.zig"); const enums = @import("../enums.zig");
@ -41,7 +41,7 @@ pub const Platform = struct {
last_cursor_position: structs.WindowPos, last_cursor_position: structs.WindowPos,
last_key_mods: structs.KeyMods, last_key_mods: structs.KeyMods,
pub fn init(allocator: std.mem.Allocator, eng: *Engine) !Platform { pub fn init(allocator: std.mem.Allocator, eng: *Core) !Platform {
var selector = [1]u8{0} ** 15; var selector = [1]u8{0} ** 15;
const id = js.machCanvasInit(&selector[0]); const id = js.machCanvasInit(&selector[0]);
@ -279,28 +279,28 @@ comptime {
} }
var app: App = undefined; var app: App = undefined;
var engine: Engine = undefined; var core: Core = undefined;
export fn wasmInit() void { export fn wasmInit() void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator(); const allocator = gpa.allocator();
engine = Engine.init(allocator) catch unreachable; core = Core.init(allocator) catch unreachable;
app.init(&engine) catch {}; app.init(&core) catch {};
} }
export fn wasmUpdate() void { export fn wasmUpdate() void {
// Poll internal events, like resize // Poll internal events, like resize
engine.internal.pollChanges(); core.internal.pollChanges();
engine.delta_time_ns = engine.timer.lapPrecise(); core.delta_time_ns = core.timer.lapPrecise();
engine.delta_time = @intToFloat(f32, engine.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s); core.delta_time = @intToFloat(f32, core.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s);
app.update(&engine) catch engine.setShouldClose(true); app.update(&core) catch core.setShouldClose(true);
} }
export fn wasmDeinit() void { export fn wasmDeinit() void {
app.deinit(&engine); app.deinit(&core);
} }
pub const log_level = if (@hasDecl(App, "log_level")) App.log_level else .info; pub const log_level = if (@hasDecl(App, "log_level")) App.log_level else .info;

View file

@ -17,7 +17,7 @@ pub const StartupOptions = struct {};
/// Application options that can be configured at run time. /// Application options that can be configured at run time.
pub const Options = struct { pub const Options = struct {
/// The title of the window. /// The title of the window.
title: [*:0]const u8 = "Mach engine", title: [*:0]const u8 = "Mach core",
/// The width of the window. /// The width of the window.
width: u32 = 640, width: u32 = 640,