From e79c9e075a95cdcb3187f67acdb022eff3ca954c Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 4 Jul 2022 19:46:03 -0700 Subject: [PATCH] mach: rename mach.Engine -> mach.Core Signed-off-by: Stephen Gutekanst --- src/{Engine.zig => Core.zig} | 52 +++++++++++++-------------- src/main.zig | 2 +- src/platform/common.zig | 20 +++++------ src/platform/native.zig | 70 ++++++++++++++++++------------------ src/platform/wasm.zig | 20 +++++------ src/structs.zig | 2 +- 6 files changed, 83 insertions(+), 83 deletions(-) rename src/{Engine.zig => Core.zig} (59%) diff --git a/src/Engine.zig b/src/Core.zig similarity index 59% rename from src/Engine.zig rename to src/Core.zig index 43a5fc2e..4d571dc4 100644 --- a/src/Engine.zig +++ b/src/Core.zig @@ -8,7 +8,7 @@ const structs = @import("structs.zig"); const enums = @import("enums.zig"); const Timer = @import("Timer.zig"); -const Engine = @This(); +const Core = @This(); allocator: Allocator, @@ -18,7 +18,7 @@ options: structs.Options, /// /// 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 -/// (360.0 * engine.delta_time) +/// (360.0 * core.delta_time) delta_time: f32 = 0, delta_time_ns: u64 = 0, timer: Timer, @@ -34,28 +34,28 @@ target_desc: gpu.SwapChain.Descriptor, internal: platform.Type, -pub fn init(allocator: std.mem.Allocator) !Engine { - var engine: Engine = undefined; - engine.allocator = allocator; - engine.options = structs.Options{}; - engine.timer = try Timer.start(); +pub fn init(allocator: std.mem.Allocator) !Core { + var core: Core = undefined; + core.allocator = allocator; + core.options = structs.Options{}; + 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. /// /// See mach.Options for details -pub fn setOptions(engine: *Engine, options: structs.Options) !void { - try engine.internal.setOptions(options); - engine.options = options; +pub fn setOptions(core: *Core, options: structs.Options) !void { + try core.internal.setOptions(options); + core.options = options; } // Signals mach to stop the update loop. -pub fn setShouldClose(engine: *Engine, value: bool) void { - engine.internal.setShouldClose(value); +pub fn setShouldClose(core: *Core, value: bool) void { + core.internal.setShouldClose(value); } // 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 // process scheduling. -pub fn setWaitEvent(engine: *Engine, timeout: f64) void { - engine.internal.setWaitEvent(timeout); +pub fn setWaitEvent(core: *Core, timeout: f64) void { + core.internal.setWaitEvent(timeout); } // Returns the framebuffer size, in subpixel units. // // e.g. returns 1280x960 on macOS for a window that is 640x480 -pub fn getFramebufferSize(engine: *Engine) structs.Size { - return engine.internal.getFramebufferSize(); +pub fn getFramebufferSize(core: *Core) structs.Size { + return core.internal.getFramebufferSize(); } // Returns the widow size, in pixel units. // // e.g. returns 1280x960 on macOS for a window that is 640x480 -pub fn getWindowSize(engine: *Engine) structs.Size { - return engine.internal.getWindowSize(); +pub fn getWindowSize(core: *Core) structs.Size { + return core.internal.getWindowSize(); } -pub fn setMouseCursor(engine: *Engine, cursor: enums.MouseCursor) !void { - try engine.internal.setMouseCursor(cursor); +pub fn setMouseCursor(core: *Core, cursor: enums.MouseCursor) !void { + try core.internal.setMouseCursor(cursor); } -pub fn hasEvent(engine: *Engine) bool { - return engine.internal.hasEvent(); +pub fn hasEvent(core: *Core) bool { + return core.internal.hasEvent(); } -pub fn pollEvent(engine: *Engine) ?structs.Event { - return engine.internal.pollEvent(); +pub fn pollEvent(core: *Core) ?structs.Event { + return core.internal.pollEvent(); } diff --git a/src/main.zig b/src/main.zig index 686a133d..9cd5e332 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,5 @@ pub usingnamespace @import("structs.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 ResourceManager = @import("resource/ResourceManager.zig"); diff --git a/src/platform/common.zig b/src/platform/common.zig index 3b3892f3..1fba01c0 100644 --- a/src/platform/common.zig +++ b/src/platform/common.zig @@ -1,27 +1,27 @@ -const Engine = @import("../Engine.zig"); +const Core = @import("../Core.zig"); pub fn checkApplication(comptime App: type) void { if (@hasDecl(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) - @compileError("expected 'pub fn init(app: *App, engine: *mach.Engine) !void' found '" ++ @typeName(InitFn) ++ "'"); + if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void) + @compileError("expected 'pub fn init(app: *App, core: *mach.Core) !void' found '" ++ @typeName(InitFn) ++ "'"); } 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")) { const UpdateFn = @TypeOf(@field(App, "update")); - if (UpdateFn != fn (app: *App, engine: *Engine) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void) - @compileError("expected 'pub fn update(app: *App, engine: *mach.Engine) !void' found '" ++ @typeName(UpdateFn) ++ "'"); + if (UpdateFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void) + @compileError("expected 'pub fn update(app: *App, core: *mach.Core) !void' found '" ++ @typeName(UpdateFn) ++ "'"); } 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")) { const DeinitFn = @TypeOf(@field(App, "deinit")); - if (DeinitFn != fn (app: *App, engine: *Engine) void) - @compileError("expected 'pub fn deinit(app: *App, engine: *mach.Engine) void' found '" ++ @typeName(DeinitFn) ++ "'"); + if (DeinitFn != fn (app: *App, core: *Core) void) + @compileError("expected 'pub fn deinit(app: *App, core: *mach.Core) void' found '" ++ @typeName(DeinitFn) ++ "'"); } 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'"); } } diff --git a/src/platform/native.zig b/src/platform/native.zig index f15ef64d..c91049ea 100644 --- a/src/platform/native.zig +++ b/src/platform/native.zig @@ -2,7 +2,7 @@ const std = @import("std"); const glfw = @import("glfw"); const gpu = @import("gpu"); const App = @import("app"); -const Engine = @import("../Engine.zig"); +const Core = @import("../Core.zig"); const structs = @import("../structs.zig"); const enums = @import("../enums.zig"); const util = @import("util.zig"); @@ -38,8 +38,8 @@ pub const Platform = struct { platform: *Platform, }; - pub fn init(allocator: std.mem.Allocator, engine: *Engine) !Platform { - const options = engine.options; + pub fn init(allocator: std.mem.Allocator, core: *Core) !Platform { + const options = core.options; const backend_type = try util.detectBackendType(allocator); glfw.setErrorCallback(Platform.errorCallback); @@ -174,20 +174,20 @@ pub const Platform = struct { device.setUncapturedErrorCallback(&util.printUnhandledErrorCallback); - engine.device = device; - engine.backend_type = backend_type; - engine.surface = surface; - engine.swap_chain = swap_chain; - engine.swap_chain_format = swap_chain_format; - engine.current_desc = descriptor; - engine.target_desc = descriptor; + core.device = device; + core.backend_type = backend_type; + core.surface = surface; + core.swap_chain = swap_chain; + core.swap_chain_format = swap_chain_format; + core.current_desc = descriptor; + core.target_desc = descriptor; const cursor_pos = try window.getCursorPos(); return Platform{ .window = window, .backend_type = backend_type, - .allocator = engine.allocator, + .allocator = core.allocator, .last_window_size = .{ .width = window_size.width, .height = window_size.height }, .last_framebuffer_size = .{ .width = framebuffer_size.width, .height = framebuffer_size.height }, .last_position = try window.getPos(), @@ -591,55 +591,55 @@ pub fn main() !void { defer _ = gpa.deinit(); const allocator = gpa.allocator(); - var engine = try Engine.init(allocator); - defer engine.internal.deinit(); + var core = try Core.init(allocator); + defer core.internal.deinit(); var app: App = undefined; - try app.init(&engine); - defer app.deinit(&engine); + try app.init(&core); + defer app.deinit(&core); // 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()) { - if (engine.internal.wait_event_timeout > 0.0) { - if (engine.internal.wait_event_timeout == std.math.inf(f64)) { + 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(engine.internal.wait_event_timeout); + try glfw.waitEventsTimeout(core.internal.wait_event_timeout); } } else { // Don't wait for events try glfw.pollEvents(); } - engine.delta_time_ns = engine.timer.lapPrecise(); - engine.delta_time = @intToFloat(f32, engine.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s); + 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 = engine.getFramebufferSize(); - engine.target_desc.width = framebuffer_size.width; - engine.target_desc.height = framebuffer_size.height; + var framebuffer_size = core.getFramebufferSize(); + core.target_desc.width = framebuffer_size.width; + core.target_desc.height = framebuffer_size.height; - if (engine.swap_chain == null or !engine.current_desc.equal(&engine.target_desc)) { - const use_legacy_api = engine.surface == null; + if (core.swap_chain == null or !core.current_desc.equal(&core.target_desc)) { + const use_legacy_api = core.surface == null; if (!use_legacy_api) { - engine.swap_chain = engine.device.nativeCreateSwapChain(engine.surface, &engine.target_desc); - } else engine.swap_chain.?.configure( - engine.swap_chain_format, + core.swap_chain = core.device.nativeCreateSwapChain(core.surface, &core.target_desc); + } else core.swap_chain.?.configure( + core.swap_chain_format, .{ .render_attachment = true }, - engine.target_desc.width, - engine.target_desc.height, + core.target_desc.width, + core.target_desc.height, ); 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); } } diff --git a/src/platform/wasm.zig b/src/platform/wasm.zig index 6bae74ab..afe58636 100644 --- a/src/platform/wasm.zig +++ b/src/platform/wasm.zig @@ -1,6 +1,6 @@ const std = @import("std"); const App = @import("app"); -const Engine = @import("../Engine.zig"); +const Core = @import("../Core.zig"); const structs = @import("../structs.zig"); const enums = @import("../enums.zig"); @@ -41,7 +41,7 @@ pub const Platform = struct { last_cursor_position: structs.WindowPos, 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; const id = js.machCanvasInit(&selector[0]); @@ -279,28 +279,28 @@ comptime { } var app: App = undefined; -var engine: Engine = undefined; +var core: Core = undefined; export fn wasmInit() void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - engine = Engine.init(allocator) catch unreachable; - app.init(&engine) catch {}; + core = Core.init(allocator) catch unreachable; + app.init(&core) catch {}; } export fn wasmUpdate() void { // Poll internal events, like resize - engine.internal.pollChanges(); + core.internal.pollChanges(); - engine.delta_time_ns = engine.timer.lapPrecise(); - engine.delta_time = @intToFloat(f32, engine.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s); + core.delta_time_ns = core.timer.lapPrecise(); + 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 { - app.deinit(&engine); + app.deinit(&core); } pub const log_level = if (@hasDecl(App, "log_level")) App.log_level else .info; diff --git a/src/structs.zig b/src/structs.zig index c2eb7579..2cb11fbd 100644 --- a/src/structs.zig +++ b/src/structs.zig @@ -17,7 +17,7 @@ pub const StartupOptions = struct {}; /// Application options that can be configured at run time. pub const Options = struct { /// The title of the window. - title: [*:0]const u8 = "Mach engine", + title: [*:0]const u8 = "Mach core", /// The width of the window. width: u32 = 640,