mach: wasm: Add initial framework bones

This commit is contained in:
iddev5 2022-05-21 23:50:37 +05:30 committed by Stephen Gutekanst
parent 66cc8281e5
commit ccb1063e3f
3 changed files with 78 additions and 1 deletions

View file

@ -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;
}

View file

@ -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 {

52
src/wasm.zig Normal file
View file

@ -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);
}