diff --git a/build/program.zig b/build/program.zig index 47d92ff..36c4bbe 100644 --- a/build/program.zig +++ b/build/program.zig @@ -201,17 +201,6 @@ pub const Program = struct { const run_exe = b.step(b.fmt("run-{s}", .{self.name}), "runs the program"); run_exe.dependOn(&run.step); - - // const runArtifact = b.addRunArtifact(exe); - // if (b.args) |args| { - // runArtifact.addArgs(args); - // } - - // // Set CWD to the build.zig directory so the exe can find zig-out/modules/ - // runArtifact.setCwd(b.path(".")); - - // const run_exe = b.step(b.fmt("run-{s}", .{self.name}), "runs the program"); - // run_exe.dependOn(&runArtifact.step); } // 2. create the actual library that the loader will try to load @@ -221,51 +210,6 @@ pub const Program = struct { // return the actual library not the loader return exe; } - - // const b = self.b; - - // const exe = self.nw_builder.addExecutable(.{ - // .name = opts.name, - // .target = self.target, - // .optimize = self.optimize, - // .root_source_file = self.nw_builder.path("engine/main.zig"), - // }); - - // b.installArtifact(exe); - // const runArtifact = b.addRunArtifact(exe); - // if (b.args) |args| { - // runArtifact.addArgs(args); - // } - // const run_exe = b.step(self.b.fmt("run-{s}", .{opts.name}), opts.desc); - // run_exe.dependOn(&runArtifact.step); - - // // main path = name/main.zig - // const mod = b.addModule(opts.name, .{ - // .target = self.target, - // .optimize = self.optimize, - // .root_source_file = opts.root_source_file, - // .imports = opts.imports, - // }); - - // exe.root_module.addImport("main", mod); - // // todo.. remove this one and see what happens - // exe.root_module.addImport("core", self.nwdep.module("core")); - // // mod.addImport("Backlog", self.nw_mod); - // exe.root_module.addOptions("BacklogOptions", self.options); - - // if (self.cookShaders) { - // const cookShadersScript = b.fmt("{s}/tools/scripts/cookShaders.py", .{self.backlogRoot}); - // const cookShadersCommand = b.addSystemCommand(&[_][]const u8{"python"}); - // cookShadersCommand.addArg(cookShadersScript); - - // run_exe.dependOn(&cookShadersCommand.step); - // } - - // b.getInstallStep().dependOn(self.nw_builder.getInstallStep()); - - // // run_exe.dependOn(b.getInstallStep()); - - // return mod; }; const std = @import("std"); diff --git a/engine/assets/src/asset_references.zig b/engine/assets/src/asset_references.zig index 189c9d4..28412e9 100644 --- a/engine/assets/src/asset_references.zig +++ b/engine/assets/src/asset_references.zig @@ -120,11 +120,6 @@ pub const AssetLoaderInterface = struct { unreachable; } - if (!@hasDecl(TargetType, "destroy")) { - @compileLog("Tried to generate AssetLoaderInterface for type ", TargetType, "but it's missing func destroy."); - unreachable; - } - const self = @This(){ .typeName = @typeName(TargetType), .typeSize = @sizeOf(TargetType), @@ -159,15 +154,15 @@ pub const AssetReferenceSys = struct { pub var NeonObjectTable = core.EngineObjectVTable.from(@This(), "AssetReference"); - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = @This(){ .loaders = .{}, .allocator = allocator, .outstandingAssetJobs = std.atomic.Value(i32).init(0), }; - - return self; } pub fn registerLoader(self: *@This(), loader: anytype) !void { @@ -210,6 +205,5 @@ pub const AssetReferenceSys = struct { // i.destroy(self.allocator); // } self.loaders.deinit(self.allocator); - self.allocator.destroy(self); } }; diff --git a/engine/audio/src/sound_engine.zig b/engine/audio/src/sound_engine.zig index cb1c544..bd421dc 100644 --- a/engine/audio/src/sound_engine.zig +++ b/engine/audio/src/sound_engine.zig @@ -73,8 +73,10 @@ pub const SoundEngine = struct { allocator: std.mem.Allocator, volume: f32 = 1.0, - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = @This(){ .engine = allocator.create(ma.ma_engine) catch unreachable, .sounds = .{}, @@ -83,8 +85,6 @@ pub const SoundEngine = struct { }; _ = ma.ma_engine_init(null, self.engine); - - return self; } pub fn shutdown(self: *@This()) void { diff --git a/engine/core/src/configVars.zig b/engine/core/src/configVars.zig index ee602d9..082d5bb 100644 --- a/engine/core/src/configVars.zig +++ b/engine/core/src/configVars.zig @@ -4,7 +4,7 @@ pub const ConfigRegistry = struct { allocator: std.mem.Allocator = undefined, configMap: ?ConfigMap = null, - pub fn create(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { if (!first) return; @@ -37,7 +37,7 @@ pub const ConfigRegistry = struct { } - pub fn destroy(self: *@This()) void { + pub fn deinit(self: *@This()) void { if (self.configMap) |*map| { map.deinit(); } diff --git a/engine/core/src/console.zig b/engine/core/src/console.zig index 7ce0066..35fbc97 100644 --- a/engine/core/src/console.zig +++ b/engine/core/src/console.zig @@ -13,7 +13,7 @@ pub const Console = struct { commandMap: std.StringHashMapUnmanaged(ConsoleCommand) = .{}, - pub fn create(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { if (!first) { return; } @@ -57,7 +57,7 @@ pub const Console = struct { } } - pub fn destroy(self: *@This()) void { + pub fn deinit(self: *@This()) void { self.arena.deinit(); self.commandMap.deinit(self.allocator); } diff --git a/engine/core/src/ecs.zig b/engine/core/src/ecs.zig index 48421cf..e6c410c 100644 --- a/engine/core/src/ecs.zig +++ b/engine/core/src/ecs.zig @@ -279,10 +279,6 @@ pub const EcsRegistry = struct { } pub fn deinit(self: *@This()) void { - _ = self; - } - - pub fn destroy(self: *@This()) void { for (self.containers.items) |ref| { ref.vtable.evictFromRegistry(ref.ptr); } diff --git a/engine/core/src/extern/externModule.zig b/engine/core/src/extern/externModule.zig index 57bd948..634a2bd 100644 --- a/engine/core/src/extern/externModule.zig +++ b/engine/core/src/extern/externModule.zig @@ -129,7 +129,7 @@ pub const ModuleLoader = struct { pub var NeonObjectTable = core.EngineObjectVTable.from(@This(), "core.ModuleLoader"); - pub fn create(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { if (!first) { return; } @@ -215,7 +215,7 @@ pub const ModuleLoader = struct { } } - pub fn destroy(self: *@This()) void { + pub fn deinit(self: *@This()) void { // std.fs.cwd().deleteTree(".modulecache") catch {}; self.arena.deinit(); } diff --git a/engine/core/src/gameObject.zig b/engine/core/src/gameObject.zig index dfa07cd..9f266d4 100644 --- a/engine/core/src/gameObject.zig +++ b/engine/core/src/gameObject.zig @@ -77,7 +77,7 @@ pub const GameObjectSystem = struct { objectSpawnEvents: std.ArrayListUnmanaged(SpawnEvent) = .{}, - pub fn create(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { if (!first) return; @@ -176,7 +176,7 @@ pub const GameObjectSystem = struct { _ = dt; } - pub fn destroy(self: *@This()) void { + pub fn deinit(self: *@This()) void { self.typesArena.deinit(); } }; diff --git a/engine/imgui/src/sgpu/backend_impl.zig b/engine/imgui/src/sgpu/backend_impl.zig index 1f375cf..f2319ca 100644 --- a/engine/imgui/src/sgpu/backend_impl.zig +++ b/engine/imgui/src/sgpu/backend_impl.zig @@ -15,14 +15,13 @@ pub const Impl = struct { }; // renderer plugin - pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; self.* = .{ .allocator = allocator, }; - - return self; } const ImguiArgs = struct { imguiIni: []const u8 = "imgui.ini" }; @@ -109,11 +108,6 @@ pub const Impl = struct { _ = ig.dockSpaceOverViewport(ig.getMainViewport(), .{ .passthru_central_node = true }, null); _ = dt; } - - // renderer plugin - pub fn destroy(self: *@This()) void { - self.allocator.destroy(self); - } }; const c = @import("cimgui").c; const ig = @import("cimgui"); diff --git a/engine/imgui/src/utils/TopBar.zig b/engine/imgui/src/utils/TopBar.zig index 094c76d..59efcc6 100644 --- a/engine/imgui/src/utils/TopBar.zig +++ b/engine/imgui/src/utils/TopBar.zig @@ -1,5 +1,4 @@ pub var NeonObjectTable = core.EngineObjectVTable.from(@This(), "game.TopBar"); -pub const Slack = core.SlackStruct(@This(), 256); allocator: std.mem.Allocator, arena: std.heap.ArenaAllocator, @@ -7,8 +6,10 @@ windowsMenu: std.ArrayListUnmanaged(*MenuEntry) = .{}, entriesByName: std.AutoHashMapUnmanaged(u32, *MenuEntry) = .{}, menuOpen: bool = false, -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try Slack.create(allocator); +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .allocator = allocator, .arena = std.heap.ArenaAllocator.init(allocator), @@ -21,8 +22,6 @@ pub fn create(allocator: std.mem.Allocator) !*@This() { .ctx = self, .windowFunction = windowOpen, }); - - return self; } pub fn setEntryOpen(self: *@This(), name: []const u8, open: ?bool) void { @@ -108,7 +107,6 @@ pub fn tick(self: *@This(), dt: f64) void { pub fn destroy(self: *@This()) void { self.arena.deinit(); - self.allocator.destroy(Slack.fromPtr(self)); } pub const MenuEntry = struct { diff --git a/engine/imgui/src/utils/consoleWindow.zig b/engine/imgui/src/utils/consoleWindow.zig index 8342109..5ee247e 100644 --- a/engine/imgui/src/utils/consoleWindow.zig +++ b/engine/imgui/src/utils/consoleWindow.zig @@ -6,16 +6,16 @@ consolePressedEnter: bool = false, pub var NeonObjectTable = core.EngineObjectVTable.from(@This(), "extras.consoleWindow"); -pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .allocator = allocator, .buffer = core.logging.LogBuffer.init(allocator), }; self.setup(); - - return self; } pub fn setup(self: *@This()) void { @@ -65,7 +65,6 @@ pub fn windowOpen(entry: *imgui.utils.MenuEntry, dt: f64) void { pub fn destroy(self: *@This()) void { self.buffer.deinit(); - self.allocator.destroy(self); } const imgui = @import("../imgui.zig"); diff --git a/engine/net/src/enet/EnetTransport.zig b/engine/net/src/enet/EnetTransport.zig index b82bc0a..72347b8 100644 --- a/engine/net/src/enet/EnetTransport.zig +++ b/engine/net/src/enet/EnetTransport.zig @@ -159,8 +159,9 @@ const ENetSessionData = struct { } }; -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); +pub fn create(self: *@This(), allocator: std.mem.Allocator, first: bool) !*@This() { + if (!first) + return; self.* = .{ .allocator = allocator, @@ -399,7 +400,6 @@ pub fn destroy(self: *@This()) void { self.deadSessions.deinit(self.allocator); enet_mod.deinitialize(); - self.allocator.destroy(self); } const core = @import("core"); diff --git a/engine/net/src/netEngine.zig b/engine/net/src/netEngine.zig index 1a1d225..7eaca13 100644 --- a/engine/net/src/netEngine.zig +++ b/engine/net/src/netEngine.zig @@ -28,15 +28,14 @@ pub const NetEngine = struct { arena: std.heap.ArenaAllocator, transport: ?TransportRef = null, - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !*@This() { + if (!first) + return; self.* = @This(){ .allocator = allocator, .arena = std.heap.ArenaAllocator.init(allocator), }; - - return self; } pub fn arenaAllocator(self: *@This()) std.mem.Allocator { diff --git a/engine/physics/src/physicsSystem.zig b/engine/physics/src/physicsSystem.zig index 4e10003..b63e162 100644 --- a/engine/physics/src/physicsSystem.zig +++ b/engine/physics/src/physicsSystem.zig @@ -139,8 +139,11 @@ pub const PhysicsRuntime = struct { try self.idToEntity.put(self.allocator, bodyId, entity); } - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) { + return; + } + try zphysics.init(std.heap.smp_allocator, .{}); //try zphysics.init(allocator, .{}); @@ -162,11 +165,6 @@ pub const PhysicsRuntime = struct { ); self.system = system; - - //try core.defineComponent(PhysicsCharacter, self.allocator); - // try core.defineComponent(PhysicsCollider, self.allocator); - - return self; } pub fn tick(self: *@This(), dt: f64) void { @@ -230,7 +228,6 @@ pub const PhysicsRuntime = struct { } pub fn deinit(self: *@This()) void { - const allocator = self.allocator; self.system.optimizeBroadPhase(); for (PhysicsCharacter.BaseContainer.list.items) |physChar| { physChar.deinit(); @@ -266,8 +263,6 @@ pub const PhysicsRuntime = struct { self.system.destroy(); zphysics.deinit(); - - allocator.destroy(self); } pub fn createShape(self: *@This(), name: *core.Name, settings: ShapeSettings) !void { diff --git a/engine/physics/src/physicsSystem_old.zig b/engine/physics/src/physicsSystem_old.zig deleted file mode 100644 index 19428c0..0000000 --- a/engine/physics/src/physicsSystem_old.zig +++ /dev/null @@ -1,308 +0,0 @@ -const std = @import("std"); -const zphysics = @import("zphysics"); -const core = @import("core"); -const zm = core.zm; - -pub const ObjectLayers = struct { - pub const non_moving: zphysics.ObjectLayer = 0; - pub const moving: zphysics.ObjectLayer = 1; - pub const len: u32 = 2; -}; - -pub const BroadPhaseLayers = struct { - pub const non_moving: zphysics.BroadPhaseLayer = 0; - pub const moving: zphysics.BroadPhaseLayer = 1; - pub const len: u32 = 2; -}; - -const BroadPhaseLayerInterface = extern struct { - usingnamespace zphysics.BroadPhaseLayerInterface.Methods(@This()); - __v: *const zphysics.BroadPhaseLayerInterface.VTable = &vtable, - - object_to_broad_phase: [ObjectLayers.len]zphysics.BroadPhaseLayer = undefined, - - const vtable = zphysics.BroadPhaseLayerInterface.VTable{ - .getNumBroadPhaseLayers = _getNumBroadPhaseLayers, - .getBroadPhaseLayer = _getBroadPhaseLayer, - }; - - fn init() BroadPhaseLayerInterface { - var layer_interface: BroadPhaseLayerInterface = .{}; - layer_interface.object_to_broad_phase[ObjectLayers.non_moving] = BroadPhaseLayers.non_moving; - layer_interface.object_to_broad_phase[ObjectLayers.moving] = BroadPhaseLayers.moving; - return layer_interface; - } - - fn _getNumBroadPhaseLayers(_: *const zphysics.BroadPhaseLayerInterface) callconv(.c) u32 { - return BroadPhaseLayers.len; - } - - fn _getBroadPhaseLayer( - interface_self: *const zphysics.BroadPhaseLayerInterface, - object_layer: zphysics.ObjectLayer, - ) callconv(.c) zphysics.BroadPhaseLayer { - const self: *const BroadPhaseLayerInterface = @ptrCast(interface_self); - return self.object_to_broad_phase[object_layer]; - } -}; - -const ObjectVsBroadPhaseLayerFilter = extern struct { - usingnamespace zphysics.ObjectVsBroadPhaseLayerFilter.Methods(@This()); - __v: *const zphysics.ObjectVsBroadPhaseLayerFilter.VTable = &vtable, - - const vtable = zphysics.ObjectVsBroadPhaseLayerFilter.VTable{ .shouldCollide = _shouldCollide }; - - fn _shouldCollide( - _: *const zphysics.ObjectVsBroadPhaseLayerFilter, - object_layer: zphysics.ObjectLayer, - broad_phase_layer: zphysics.BroadPhaseLayer, - ) callconv(.c) bool { - return switch (object_layer) { - ObjectLayers.non_moving => broad_phase_layer == BroadPhaseLayers.moving, - ObjectLayers.moving => true, - else => unreachable, - }; - } -}; - -const ObjectLayerPairFilter = extern struct { - usingnamespace zphysics.ObjectLayerPairFilter.Methods(@This()); - __v: *const zphysics.ObjectLayerPairFilter.VTable = &vtable, - - const vtable = zphysics.ObjectLayerPairFilter.VTable{ .shouldCollide = _shouldCollide }; - - fn _shouldCollide( - _: *const zphysics.ObjectLayerPairFilter, - a: zphysics.ObjectLayer, - b: zphysics.ObjectLayer, - ) callconv(.c) bool { - return switch (a) { - ObjectLayers.non_moving => b == ObjectLayers.moving, - ObjectLayers.moving => true, - else => unreachable, - }; - } -}; - -pub const PhysicsRuntime = struct { - allocator: std.mem.Allocator, - bpli: BroadPhaseLayerInterface, - ovbplf: ObjectVsBroadPhaseLayerFilter, - olpf: ObjectLayerPairFilter, - max_bodies: u32, - - system: *zphysics.PhysicsSystem = undefined, - - primBoxSettings: *zphysics.BoxShapeSettings = undefined, - primBoxShape: *zphysics.Shape = undefined, - - primSphereSettings: *zphysics.SphereShapeSettings = undefined, - primSphereShape: *zphysics.Shape = undefined, - - floorShapeSettings: *zphysics.BoxShapeSettings = undefined, - floorShape: *zphysics.Shape = undefined, - - spherePositions: std.ArrayList(core.Vectorf), - sphereRotations: std.ArrayList(core.Quat), - sphereIds: std.ArrayList(zphysics.BodyId), - - newBallTime: f64 = 5, - offset: f32 = 0, - - pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This()); - - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); - - self.* = .{ - .allocator = allocator, - .bpli = BroadPhaseLayerInterface.init(), - .ovbplf = .{}, - .olpf = .{}, - .spherePositions = std.ArrayList(core.Vectorf).init(allocator), - .sphereRotations = std.ArrayList(core.Quat).init(allocator), - .sphereIds = std.ArrayList(zphysics.BodyId).init(allocator), - .max_bodies = 8192, - }; - - const system = try zphysics.PhysicsSystem.create( - @as(*const zphysics.BroadPhaseLayerInterface, @ptrCast(&self.bpli)), - @as(*const zphysics.ObjectVsBroadPhaseLayerFilter, @ptrCast(&self.ovbplf)), - @as(*const zphysics.ObjectLayerPairFilter, @ptrCast(&self.olpf)), - .{ - .max_bodies = self.max_bodies, - .num_body_mutexes = 0, - .max_body_pairs = 8192 * 2, - .max_contact_constraints = 8192 * 2, - }, - ); - - self.system = system; - - const bodyInterface = self.system.getBodyInterfaceMut(); - - // primBoxSettings: *zphysics.BoxShapeSettings = undefined, - // primBoxShape: *zphysics.Shape = undefined, - - // primSphereSettings: *zphysics.SphereShapeSettings = undefined, - // primSphereShape: *zphysics.Shape = undefined, - - // setup primitives for settings. - self.primBoxSettings = try zphysics.BoxShapeSettings.create(.{ 2.0, 2.0, 2.0 }); - self.primBoxShape = try self.primBoxSettings.createShape(); - - self.primSphereSettings = try zphysics.SphereShapeSettings.create(0.5); - self.primSphereShape = try self.primSphereSettings.createShape(); - - self.floorShapeSettings = try zphysics.BoxShapeSettings.create(.{ 300, 1, 300 }); - self.floorShape = try self.floorShapeSettings.createShape(); - - // create floor - _ = try bodyInterface.createAndAddBody(.{ - .position = .{ 0, -1, 0, 1 }, - .rotation = .{ 0, 0, 0, 1 }, - .shape = self.floorShape, - .motion_type = .static, - .object_layer = ObjectLayers.non_moving, - }, .activate); - - const roomSize = 7; - // create up and down walls - { - const rotation = zm.quatFromMat(zm.rotationZ(core.radians(90.0))); - _ = try bodyInterface.createAndAddBody(.{ - .position = .{ roomSize, -1, 0, 1 }, - .rotation = rotation, - .shape = self.floorShape, - .motion_type = .static, - .object_layer = ObjectLayers.non_moving, - }, .activate); - - _ = try bodyInterface.createAndAddBody(.{ - .position = .{ -roomSize, -1, 0, 1 }, - .rotation = rotation, - .shape = self.floorShape, - .motion_type = .static, - .object_layer = ObjectLayers.non_moving, - }, .activate); - } - - // create left and right walls - { - const rotation = zm.quatFromMat(zm.rotationX(core.radians(90.0))); - _ = try bodyInterface.createAndAddBody(.{ - .position = .{ 0, -1, -roomSize, 1 }, - .rotation = rotation, - .shape = self.floorShape, - .motion_type = .static, - .object_layer = ObjectLayers.non_moving, - }, .activate); - - _ = try bodyInterface.createAndAddBody(.{ - .position = .{ 0, -1, roomSize, 1 }, - .rotation = rotation, - .shape = self.floorShape, - .motion_type = .static, - .object_layer = ObjectLayers.non_moving, - }, .activate); - } - - for (0..2) |i| { - _ = try bodyInterface.createAndAddBody( - .{ - .position = .{ 0, @as(f32, @floatFromInt(i)) * 1.1 + 1.0, 2, 1 }, - .rotation = .{ 0, 0, 0, 1 }, - .shape = self.primSphereShape, - .motion_type = .dynamic, - .object_layer = ObjectLayers.moving, - .restitution = 0.4, - .angular_velocity = .{ 0, 0, 0, 0 }, - }, - .activate, - ); - _ = try bodyInterface.createAndAddBody( - .{ - .position = .{ 5, @as(f32, @floatFromInt(i)) * 1.1 + 1.0, 2, 1 }, - .rotation = .{ 0, 0, 0, 1 }, - .shape = self.primSphereShape, - .motion_type = .dynamic, - .restitution = 0.4, - .object_layer = ObjectLayers.moving, - .angular_velocity = .{ 0, 0, 0, 0 }, - }, - .activate, - ); - } - - self.system.optimizeBroadPhase(); - - return self; - } - - pub fn updateSpherePositions(self: *@This()) !void { - try self.system.getBodyIds(&self.sphereIds); - try self.spherePositions.resize(self.sphereIds.items.len); - try self.sphereRotations.resize(self.sphereIds.items.len); - const lockInterface = self.system.getBodyLockInterface(); - - for (self.sphereIds.items, 0..) |bodyId, i| { - var readLock: zphysics.BodyLockRead = .{}; - readLock.lock(lockInterface, bodyId); - defer readLock.unlock(); - - if (readLock.body) |body| { - self.spherePositions.items[i] = core.Vectorf.fromArray(body.position); - self.sphereRotations.items[i] = body.rotation; - } - } - } - - // todo... schedule physics timers - pub fn tick(self: *@This(), dt: f64) void { - self.system.update(@floatCast(dt), .{}) catch unreachable; - self.updateSpherePositions() catch unreachable; - - // self.newBallTime -= dt; - - // if (self.newBallTime < 0) { - // const bodyInterface = self.system.getBodyInterfaceMut(); - // self.newBallTime = 5.0; - // self.offset += 0.01; - - // _ = bodyInterface.createAndAddBody( - // .{ - // .position = .{ 0 + self.offset, 15, 0, 1 }, - // .rotation = .{ 0, 0, 0, 1 }, - // .shape = self.primSphereShape, - // .motion_type = .dynamic, - // .object_layer = ObjectLayers.moving, - // .restitution = 0.4, - // .angular_velocity = .{ 0, 0, 0, 0 }, - // .inertia_multiplier = 30, - // }, - // .activate, - // ) catch unreachable; - - // self.system.optimizeBroadPhase(); - // } - } - - pub fn deinit(self: *@This()) void { - const allocator = self.allocator; - - self.primSphereShape.release(); - self.primSphereSettings.release(); - - self.floorShape.release(); - self.floorShapeSettings.release(); - - self.primBoxShape.release(); - self.primBoxSettings.release(); - - self.system.destroy(); - self.sphereIds.deinit(); - self.spherePositions.deinit(); - self.sphereRotations.deinit(); - allocator.destroy(self); - } -}; diff --git a/engine/platform/src/gameInput.zig b/engine/platform/src/gameInput.zig deleted file mode 100644 index 7efea15..0000000 --- a/engine/platform/src/gameInput.zig +++ /dev/null @@ -1,22 +0,0 @@ -const std = @import("std"); -const core = @import("core"); -const windowing = @import("windowing.zig"); - -// GameInput implements the RawInputListenerInterface -// This is a data driven input system where mappings are defined, -// and keys are assigned to mapping contexts - -pub const GameInputSystem = struct { - pub const RawInputListenerVTable = windowing.RawInputListenerInterface.from(@This()); - - stub: u32 = 0, - - pub fn OnIoEvent(self: *@This(), event: windowing.IOEvent) windowing.InputListenerError!void { - _ = event; - _ = self; - } - - pub fn deinit(self: *@This()) void { - _ = self; - } -}; diff --git a/engine/platform/src/windowing.zig b/engine/platform/src/windowing.zig index 4d5c857..7f3fd9d 100644 --- a/engine/platform/src/windowing.zig +++ b/engine/platform/src/windowing.zig @@ -191,8 +191,6 @@ pub const PlatformInstance = struct { self.allocator.free(self.iconPath); self.processFuncs.deinit(self.allocator); self.platformRequests.deinit(self.allocator); - self.allocator.destroy(self); - // shutdown } pub fn onExitSignal(self: *@This()) !void { @@ -204,10 +202,10 @@ pub const PlatformInstance = struct { return self.windowDestroyed; } - pub fn init( - allocator: std.mem.Allocator, - ) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .allocator = allocator, .nfdRuntime = try nfd.NFDRuntime.create(allocator, .{}), @@ -217,8 +215,6 @@ pub const PlatformInstance = struct { //.extent = .{ .x = @floatFromInt(params.extent.x), .y = @floatFromInt(params.extent.y) }, //.hasVideo = params.hasVideo, }; - - return self; } pub fn setParams(self: *@This(), params: PlatformParams) !void { diff --git a/engine/rend/src/animations/animationSystem.zig b/engine/rend/src/animations/animationSystem.zig index 9e0793b..f92add9 100644 --- a/engine/rend/src/animations/animationSystem.zig +++ b/engine/rend/src/animations/animationSystem.zig @@ -417,8 +417,10 @@ pub const AnimationSystem = struct { } } - pub fn init(alloc: std.mem.Allocator) !*@This() { - const self = try alloc.create(@This()); + pub fn init(self: *@This(), alloc: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .backingAllocator = alloc, .arena = std.heap.ArenaAllocator.init(alloc), @@ -433,7 +435,6 @@ pub const AnimationSystem = struct { Animator.allocator = alloc; core.engine_logs("Animation System initialized"); - return self; } pub fn deinit(self: *@This()) void { @@ -466,7 +467,6 @@ pub const AnimationSystem = struct { self.arena.deinit(); self.skeletons.deinit(self.backingAllocator); self.animTracks.deinit(self.backingAllocator); - self.backingAllocator.destroy(self); } }; diff --git a/engine/rend/src/animations/loaders.zig b/engine/rend/src/animations/loaders.zig index e826d96..ec57233 100644 --- a/engine/rend/src/animations/loaders.zig +++ b/engine/rend/src/animations/loaders.zig @@ -19,19 +19,14 @@ pub const AnimationLoader = struct { self.sys.newAnimTrack(assetRef.name, animation) catch return error.UnableToLoad; } - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; self.* = .{ .sys = animation_system.gAnimationSys, .allocator = allocator, }; - - return self; - } - - pub fn destroy(self: *@This()) void { - self.allocator.destroy(self); } }; @@ -56,19 +51,14 @@ pub const SkeletonLoader = struct { self.sys.newSkeleton(assetRef.name, sk) catch return error.UnableToLoad; } - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; self.* = .{ .sys = animation_system.gAnimationSys, .allocator = allocator, }; - - return self; - } - - pub fn destroy(self: *@This()) void { - self.allocator.destroy(self); } }; diff --git a/engine/rend/src/particles/particles.zig b/engine/rend/src/particles/particles.zig index 3fe122b..77a5813 100644 --- a/engine/rend/src/particles/particles.zig +++ b/engine/rend/src/particles/particles.zig @@ -410,8 +410,9 @@ pub const ParticleSystem = struct { pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This(), "rend.ParticleSystem"); - pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; self.* = .{ .particleArena = std.heap.ArenaAllocator.init(allocator), @@ -420,8 +421,6 @@ pub const ParticleSystem = struct { ParticleRandRangef.randomEngine = std.Random.DefaultPrng.init(0x1234); ParticleRandRangef.randomFunc = ParticleRandRangef.randomEngine.random(); - - return self; } pub fn tick(self: *@This(), dt: f64) void { @@ -437,7 +436,6 @@ pub const ParticleSystem = struct { pub fn destroy(self: *@This()) void { self.particleArena.deinit(); - self.allocator.destroy(self); } }; diff --git a/engine/rend/src/sgpu/DebugDrawSystem.zig b/engine/rend/src/sgpu/DebugDrawSystem.zig index 2d19170..11817e1 100644 --- a/engine/rend/src/sgpu/DebugDrawSystem.zig +++ b/engine/rend/src/sgpu/DebugDrawSystem.zig @@ -35,8 +35,10 @@ const assetReferences = [_]assets.AssetImportReference{ ), }; -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .debugDraws = try core.RingQueueU(DebugPrimitive).init(allocator, MaxObjectCount), .allocator = allocator, @@ -50,8 +52,6 @@ pub fn create(allocator: std.mem.Allocator) !*@This() { try assets.loadList(assetReferences); gDebugDrawSys = self; - - return self; } pub fn updateMeshes(self: *@This()) void { @@ -243,7 +243,6 @@ pub fn setup(self: *@This(), device: *gpu.GPUDevice) !void { pub fn destroy(self: *@This()) void { self.debugDraws.deinit(self.allocator); self.drawsThisFrame.deinit(self.allocator); - self.allocator.destroy(self); } // ==================== Primitives =================== diff --git a/engine/rend/src/sgpu/MeshAssetLoader.zig b/engine/rend/src/sgpu/MeshAssetLoader.zig index 9617e79..c8193ce 100644 --- a/engine/rend/src/sgpu/MeshAssetLoader.zig +++ b/engine/rend/src/sgpu/MeshAssetLoader.zig @@ -5,23 +5,19 @@ allocator: std.mem.Allocator, pub var LoaderInterfaceVTable = assets.AssetLoaderInterface.from("Mesh", @This()); pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This(), "rend.MeshAssetLoader"); -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .allocator = allocator, }; - - return self; } pub fn discardAll(self: *@This()) void { _ = self; } -pub fn destroy(self: *@This()) void { - self.allocator.destroy(self); -} - // unfortunately this one is blocking pub fn loadAsset(self: *@This(), assetRef: assets.AssetRef, propertiesBag: ?assets.AssetPropertiesBag) assets.AssetLoaderError!void { _ = self; diff --git a/engine/rend/src/sgpu/ParticleRenderer.zig b/engine/rend/src/sgpu/ParticleRenderer.zig index acd8416..6531c3c 100644 --- a/engine/rend/src/sgpu/ParticleRenderer.zig +++ b/engine/rend/src/sgpu/ParticleRenderer.zig @@ -15,8 +15,10 @@ const SgpuParticleRenderInfo = struct { texture: *rend.Texture, }; -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .allocator = allocator, }; @@ -39,8 +41,6 @@ pub fn create(allocator: std.mem.Allocator) !*@This() { .size = MaxParticleCount * @sizeOf(meshes_vert.Scene), .props = 0, }); - - return self; } pub fn uploadSsbos(self: *@This(), copyPass: *gpu.GPUCopyPass) !void { @@ -121,10 +121,9 @@ pub fn setup(self: *@This(), device: *gpu.GPUDevice) !void { _ = device; } -pub fn destroy(self: *@This()) void { +pub fn deinit(self: *@This()) void { self.spans.deinit(self.allocator); self.renderInfo.deinit(self.allocator); - self.allocator.destroy(self); } const assets = @import("assets"); diff --git a/engine/rend/src/sgpu/SkyboxSystem.zig b/engine/rend/src/sgpu/SkyboxSystem.zig index 3df433d..cc2f28b 100644 --- a/engine/rend/src/sgpu/SkyboxSystem.zig +++ b/engine/rend/src/sgpu/SkyboxSystem.zig @@ -16,11 +16,11 @@ brightnessFactor: f32 = 4.0, sampler: *gpu.GPUSampler = undefined, -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); - self.* = .{ .allocator = allocator }; +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; - return self; + self.* = .{ .allocator = allocator }; } pub fn setup(self: *@This(), device: *gpu.GPUDevice) !void { @@ -87,10 +87,6 @@ fn createPipeline(self: *@This()) !void { })); } -pub fn destroy(self: *@This()) void { - self.allocator.destroy(self); -} - pub fn render(self: *@This(), cmd: *gpu.GPUCommandBuffer) void { const targetInfo: [2]gpu.GPUColorTargetInfo = .{ std.mem.zeroInit(gpu.GPUColorTargetInfo, .{ diff --git a/engine/rend/src/sgpu/TextureList.zig b/engine/rend/src/sgpu/TextureList.zig index a090a18..ae1e6ad 100644 --- a/engine/rend/src/sgpu/TextureList.zig +++ b/engine/rend/src/sgpu/TextureList.zig @@ -9,19 +9,19 @@ pub var LoaderInterfaceVTable = assets.AssetLoaderInterface.from("Texture", @This()); pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This(), "rend.TextureList"); -allocator: std.mem.Allocator, +allocator: std.mem.Allocator = undefined, device: *gpu.GPUDevice = undefined, map: std.AutoHashMapUnmanaged(u32, *Texture) = .{}, requestMap: std.AutoHashMapUnmanaged(u32, bool) = .{}, -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; + self.* = .{ .allocator = allocator, }; - - return self; } pub fn setup(self: *@This(), device: *gpu.GPUDevice) !void { @@ -339,14 +339,13 @@ pub fn discardAll(self: *@This()) void { _ = self; } -pub fn destroy(self: *@This()) void { +pub fn deinit(self: *@This()) void { var iter = self.map.valueIterator(); while (iter.next()) |x| { self.allocator.destroy(x.*); } self.requestMap.deinit(self.allocator); self.map.deinit(self.allocator); - self.allocator.destroy(self); } const std = @import("std"); diff --git a/engine/rend/src/sgpu/renderer.zig b/engine/rend/src/sgpu/renderer.zig index 49a56a8..42a279a 100644 --- a/engine/rend/src/sgpu/renderer.zig +++ b/engine/rend/src/sgpu/renderer.zig @@ -99,15 +99,14 @@ pub const Renderer = struct { pub const MaxObjectCount = 50000; - pub fn init(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); - + pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; self.* = .{ .allocator = allocator, }; self.hdrTextureFormat = .textureformatR16g16b16a16Float; - return self; } pub fn imageExtents(self: @This()) core.Vectorf { @@ -1178,7 +1177,6 @@ pub const Renderer = struct { self.uploads.deinit(self.allocator); self.destroys.deinit(self.allocator); - self.allocator.destroy(self); } }; diff --git a/engine/rend/src/sgpu/ssao.zig b/engine/rend/src/sgpu/ssao.zig index f016940..2bc75c0 100644 --- a/engine/rend/src/sgpu/ssao.zig +++ b/engine/rend/src/sgpu/ssao.zig @@ -21,14 +21,13 @@ enable: bool = true, // float bias; // = 0.025; // int numSamples; // up to 64 -pub fn create(allocator: std.mem.Allocator) !*@This() { - const self = try allocator.create(@This()); +pub fn init(self: *@This(), allocator: std.mem.Allocator, first: bool) !void { + if (!first) + return; self.* = .{ .allocator = allocator, }; - - return self; } pub fn setup(self: *@This(), device: *gpu.GPUDevice) !void { @@ -216,10 +215,6 @@ pub fn createPipeline(self: *@This()) !void { self.ssaoPipeline = ctx.device.createGPUGraphicsPipeline(&pci); } -pub fn destroy(self: *@This()) void { - self.allocator.destroy(self); -} - const core = @import("core"); const rend = @import("../rend.zig"); const std = @import("std");