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.
This commit is contained in:
iddev5 2022-06-01 12:43:37 +05:30 committed by Stephen Gutekanst
parent 01eee68f5b
commit ed0e6f5c61
4 changed files with 27 additions and 9 deletions

View file

@ -34,10 +34,10 @@ target_desc: gpu.SwapChain.Descriptor,
internal: platform.Type, 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; var engine: Engine = undefined;
engine.allocator = allocator; engine.allocator = allocator;
engine.options = options; engine.options = structs.Options{};
engine.timer = try Timer.start(); engine.timer = try Timer.start();
engine.internal = try platform.Type.init(allocator, &engine); 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; 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 { pub fn setShouldClose(engine: *Engine, value: bool) void {
engine.internal.setShouldClose(value); engine.internal.setShouldClose(value);
} }

View file

@ -235,6 +235,11 @@ pub const Platform = struct {
platform.window.setFramebufferSizeCallback(framebuffer_size_callback); 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 { pub fn setShouldClose(platform: *Platform, value: bool) void {
platform.window.setShouldClose(value); platform.window.setShouldClose(value);
} }
@ -410,8 +415,7 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator(); const allocator = gpa.allocator();
const options = if (@hasDecl(App, "options")) App.options else structs.Options{}; var engine = try Engine.init(allocator);
var engine = try Engine.init(allocator, options);
var app: App = undefined; var app: App = undefined;
try app.init(&engine); try app.init(&engine);

View file

@ -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 setShouldClose(_: *Platform, _: bool) void {}
pub fn getFramebufferSize(platform: *Platform) structs.Size { pub fn getFramebufferSize(platform: *Platform) structs.Size {
@ -116,11 +123,7 @@ export fn wasmInit() void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator(); const allocator = gpa.allocator();
// NOTE: On wasm, vsync is double by default and cannot be changed. engine = Engine.init(allocator) catch unreachable;
// 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;
app.init(&engine) catch {}; app.init(&engine) catch {};
} }

View file

@ -12,6 +12,9 @@ pub const SizeOptional = struct {
}; };
/// Application options that can be configured at init time. /// 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 { 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 engine",