diff --git a/src/Engine.zig b/src/Engine.zig index 11de2bde..c31f04c5 100644 --- a/src/Engine.zig +++ b/src/Engine.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Allocator = std.mem.Allocator; +const builtin = @import("builtin"); const glfw = @import("glfw"); const gpu = @import("gpu"); const structs = @import("structs.zig"); @@ -88,9 +89,11 @@ pub fn init(allocator: std.mem.Allocator, options: structs.Options) !Engine { } fn GetCoreInternalType() type { + if (builtin.cpu.arch == .wasm32) return @import("wasm.zig").CoreWasm; return @import("native.zig").CoreGlfw; } fn GetGpuDriverInternalType() type { + if (builtin.cpu.arch == .wasm32) return @import("wasm.zig").GpuDriverWeb; return @import("native.zig").GpuDriverNative; } diff --git a/src/Timer.zig b/src/Timer.zig index c02fc6d9..dc68fd0d 100644 --- a/src/Timer.zig +++ b/src/Timer.zig @@ -6,7 +6,29 @@ const Timer = @This(); backing_timer: BackingTimerType = undefined, // TODO: verify declarations and its signatures -const BackingTimerType = if (builtin.cpu.arch == .wasm32) void else std.time.Timer; +const BackingTimerType = if (builtin.cpu.arch == .wasm32) struct { + pad0: u8 = 0, + + const WasmTimer = @This(); + + fn start() !WasmTimer { + return WasmTimer{}; + } + + fn read(_: *WasmTimer) u64 { + return 0; + } + + fn reset(_: *WasmTimer) void {} + + fn lap(_: *WasmTimer) u64 { + return 0; + } + + fn timeToNs(_: f64) u64 { + return 0; + } +} else std.time.Timer; /// Initialize the timer. pub fn start() !Timer { diff --git a/src/wasm.zig b/src/wasm.zig new file mode 100644 index 00000000..842ee549 --- /dev/null +++ b/src/wasm.zig @@ -0,0 +1,52 @@ +const std = @import("std"); +const App = @import("app"); +const Engine = @import("Engine.zig"); +const structs = @import("structs.zig"); +const enums = @import("enums.zig"); + +const js = struct {}; + +pub const CoreWasm = struct { + pub fn init(_: std.mem.Allocator, _: *Engine) !CoreWasm { + return CoreWasm{}; + } + + pub fn setShouldClose(_: *CoreWasm, _: bool) void {} + + pub fn getFramebufferSize(_: *CoreWasm) !structs.Size { + return structs.Size{ .width = 0, .height = 0 }; + } + + pub fn setSizeLimits(_: *CoreWasm, _: structs.SizeOptional, _: structs.SizeOptional) !void {} + + pub fn pollEvent(_: *CoreWasm) ?structs.Event { + return null; + } +}; + +pub const GpuDriverWeb = struct { + pub fn init(_: std.mem.Allocator, _: *Engine) !GpuDriverWeb { + return GpuDriverWeb{}; + } +}; + +var app: App = undefined; +var engine: Engine = undefined; + +export fn wasmInit() void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); + + const options = if (@hasDecl(App, "options")) App.options else structs.Options{}; + engine = Engine.init(allocator, options) catch unreachable; + + app.init(&engine) catch {}; +} + +export fn wasmUpdate() bool { + return app.update(&engine) catch false; +} + +export fn wasmDeinit() void { + app.deinit(&engine); +}