From ed0e6f5c61944decef79becefc4ccdda7a1c56f0 Mon Sep 17 00:00:00 2001 From: iddev5 Date: Wed, 1 Jun 2022 12:43:37 +0530 Subject: [PATCH] mach: Added runtime application options Reused mach.Options for run time options. It is set with Engine.setOptions function. ``pub const options`` on top level App has no effect and will be ignored completely. Added a blank struct StartupOptions which would be used for startup time options in future. Currently they aren't used for anything. --- src/Engine.zig | 12 ++++++++++-- src/platform/native.zig | 8 ++++++-- src/platform/wasm.zig | 13 ++++++++----- src/structs.zig | 3 +++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Engine.zig b/src/Engine.zig index 4b77fbe1..1ef4c4b5 100644 --- a/src/Engine.zig +++ b/src/Engine.zig @@ -34,10 +34,10 @@ target_desc: gpu.SwapChain.Descriptor, internal: platform.Type, -pub fn init(allocator: std.mem.Allocator, options: structs.Options) !Engine { +pub fn init(allocator: std.mem.Allocator) !Engine { var engine: Engine = undefined; engine.allocator = allocator; - engine.options = options; + engine.options = structs.Options{}; engine.timer = try Timer.start(); engine.internal = try platform.Type.init(allocator, &engine); @@ -45,6 +45,14 @@ pub fn init(allocator: std.mem.Allocator, options: structs.Options) !Engine { return engine; } +/// 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 setShouldClose(engine: *Engine, value: bool) void { engine.internal.setShouldClose(value); } diff --git a/src/platform/native.zig b/src/platform/native.zig index c248a3a0..3c2fccb8 100644 --- a/src/platform/native.zig +++ b/src/platform/native.zig @@ -235,6 +235,11 @@ pub const Platform = struct { platform.window.setFramebufferSizeCallback(framebuffer_size_callback); } + pub fn setOptions(platform: *Platform, options: structs.Options) !void { + try platform.window.setSize(.{ .width = options.width, .height = options.height }); + try platform.window.setTitle(options.title); + } + pub fn setShouldClose(platform: *Platform, value: bool) void { platform.window.setShouldClose(value); } @@ -410,8 +415,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - const options = if (@hasDecl(App, "options")) App.options else structs.Options{}; - var engine = try Engine.init(allocator, options); + var engine = try Engine.init(allocator); var app: App = undefined; try app.init(&engine); diff --git a/src/platform/wasm.zig b/src/platform/wasm.zig index 9449ec1e..aeda74fc 100644 --- a/src/platform/wasm.zig +++ b/src/platform/wasm.zig @@ -42,6 +42,13 @@ pub const Platform = struct { }; } + pub fn setOptions(platform: *Platform, options: structs.Options) !void { + js.machCanvasSetSize(platform.id, options.width, options.height); + + const title = std.mem.span(options.title); + js.machCanvasSetTitle(platform.id, title.ptr, title.len); + } + pub fn setShouldClose(_: *Platform, _: bool) void {} pub fn getFramebufferSize(platform: *Platform) structs.Size { @@ -116,11 +123,7 @@ export fn wasmInit() void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - // NOTE: On wasm, vsync is double by default and cannot be changed. - // Hence options.vsync is not used anywhere. - const options = if (@hasDecl(App, "options")) App.options else structs.Options{}; - engine = Engine.init(allocator, options) catch unreachable; - + engine = Engine.init(allocator) catch unreachable; app.init(&engine) catch {}; } diff --git a/src/structs.zig b/src/structs.zig index e0e3182a..83b101c4 100644 --- a/src/structs.zig +++ b/src/structs.zig @@ -12,6 +12,9 @@ pub const SizeOptional = struct { }; /// Application options that can be configured at init time. +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",