pub export fn startup(p_allocator: *anyopaque, p_a: ?*anyopaque) bool { const allocator = core.modulePreamble(p_allocator, p_a) catch return false; _ = allocator; imgui.setupFromModule(); platform.setupFromModule(); sys.setupFromModule(); rend.setupFromModule(); backlog.physics.setupFromModule(); // TODO backlog.setupFromModule core.engine_logs("creating externgame"); start_module(core.startup_getArgs(p_a.?)) catch return false; return true; } pub export fn add(a: i32, b: i32) i32 { return a + b; } pub export fn subtract(a: i32, b: i32) i32 { return a + b; } var gAllocator: std.mem.Allocator = undefined; pub fn start_module(args: core.ModuleLoaderArgs) !void { if (args.firstLoad) { _ = core.createObject(ExternGameObject, .{}) catch {}; core.engine_logs("creating externgame"); return; } core.logDisplay("externGame", "accessing old object at {x} same object? {d}", .{ @intFromPtr(core.EngineObject(ExternGameObject).get()), @sizeOf(ExternGameObject) }); // getting rid of dumb comment //log("gonna try something dumb- lets patch the vtable of that old object with my vtable's values", .{}); const ref = core.getEngineObjectRef(ExternGameObject).?; const vtable: *core.EngineObjectVTable = @constCast(ref.vtable); vtable.* = ExternGameObject.NeonObjectTable; core.PatchStruct(ExternGameObject, @ptrCast(@alignCast(ref.ptr)), ref.vtable.fieldList.?); } pub const BoxObject = struct { playing: bool = false, pub fn create(allocator: std.mem.Allocator, entity: core.Entity, params: core.SpawnParameters) !*@This() { const position = params.posRot.position; const box2 = entity; const scene = box2.addComponent(core.Scene).?; const mesh = box2.addComponent(rend.MeshComponent).?; mesh.setMesh("m_crate"); mesh.setTexture("t_crate"); scene.setPosition(position); scene.setScaleV(params.posRot.scale); scene.setMobility(.moveable); const collider = box2.addComponent(physics.PhysicsCollider).?; try collider.setupByShapeName(core.MakeName("ph_small_box"), .{ .motion_type = .dynamic, .object_layer = physics.ObjectLayers.moving, .friction = 0.8, .mass_properties_override = .{ .mass = 12 }, .override_mass_properties = .calc_inertia, }); return try allocator.create(@This()); } pub fn createSmoky(allocator: std.mem.Allocator, entity: core.Entity, params: core.SpawnParameters) !*@This() { const rv = try create(allocator, entity, params); const particle = entity.addComponent(rend.ParticleEmitter).?; particle.start(); particle.gravity = .{ .y = 1.0 }; particle.particleVelocity = .{ .min = 0.6, .max = 1.2 }; particle.particleLife = .{ .min = 5, .max = 6 }; var name = core.MakeName("t_pixelSmoke"); particle.texture = rend.getTexture(&name).?; return rv; } pub fn destroy(self: *@This(), alloc: std.mem.Allocator) void { alloc.destroy(self); } }; pub const ExternGameObject = struct { pub var NeonObjectTable = core.EngineObjectVTable.from(@This(), "game.ExternGameObject"); pub const Slack = core.SlackStruct(@This(), 512); allocator: std.mem.Allocator = undefined, consoleWindow: imgui.utils.ConsoleWindow = undefined, tbMap: ?*bsp.maploader.TBMap = null, physicsObjectsWindow: *extras.PhysicsObjectList = undefined, fireTraceFilter: physics.IgnoreFixedBodiesFilter = .{}, particleDebugger: extras.ParticleDebugger.ParticleDebugger = undefined, volume: f32 = 100, //lmao: bool = false, //lmao2: bool = true, // lib: bool = false, //s: u32 = 0x42, // firstTick: bool = true, fireInput: ?*core.ActionBinding = null, altFireInput: ?*core.ActionBinding = null, a: core.Vector2f = .{ .x = 1, .y = 4 }, b: core.Vector2f = .{ .x = 4, .y = 1 }, Anormal: core.Vector2f = .{ .x = -1, .y = 3 }, Bnormal: core.Vector2f = .{ .x = -1, .y = -3 }, spawnedEntities: std.ArrayListUnmanaged(core.Entity) = .{}, displayVectorsTest: bool = false, pub fn beginPlay(p: *anyopaque) void { const self = core.cast(*@This(), p); self._beginPlay() catch { core.engine_errs("unable to beginplay"); }; } pub fn _beginPlay(self: *@This()) !void { if (self.fireInput == null) { self.fireInput = try core.ActionBinding.create(core.MakeName("fire")); self.fireInput.?.addKey(.Mouse1, .keyDown); _ = self.fireInput.?.data.addListener(self, onFire); } self.fireInput.?.activate(); if (self.altFireInput == null) { self.altFireInput = try core.ActionBinding.create(core.MakeName("altFire")); self.altFireInput.?.addKey(.Mouse3, .keyDown); _ = self.altFireInput.?.data.addListener(self, onAltFire); } self.altFireInput.?.activate(); } pub fn endPlay(p: *anyopaque) void { const self = core.cast(*@This(), p); self.fireInput.?.deactivate(); self.altFireInput.?.deactivate(); } pub fn loadMap2(self: *@This()) !void { if (self.tbMap) |tbMap| { core.engine_log("killing the map", .{}); tbMap.destroy(); } self.tbMap = try bsp.maploader.LoadTrenchbroomMap(self.allocator, .{ .rotation = core.Rotation.eulerX(core.radians(-90.0)), .mapName = "bsp/testmap.map", }); core.engine_log("map loaded", .{}); } pub fn create(allocator: std.mem.Allocator) !*@This() { const self = try Slack.create(allocator); self.* = .{ .allocator = allocator, .consoleWindow = imgui.utils.ConsoleWindow.init(allocator), .particleDebugger = extras.ParticleDebugger.ParticleDebugger.init(), }; imgui.utils.addMenuFunc(self, "Input Stack Viewer", extras.inputDebugger.windowOpen); self.physicsObjectsWindow = try extras.PhysicsObjectList.create(self.allocator); self.consoleWindow.setup(); try core.registerObject(BoxObject, "Box"); try core.registerObjectAdvanced(BoxObject, "BoxSmoky", "createSmoky"); const settings: physics.ShapeSettings = .{ .box = try physics.BoxShapeSettings.create(.{ 1.0, 1.0, 1.0 }) }; defer settings.release(); try physics.addShape("ph_small_box", settings); if (true) { const ctx = ui.getScreen(); const panel2 = try ctx.addPanel(.{}); ctx.drawDebug = true; { ctx.getPanel(panel2).hasTitle = true; //ctx.get(panel2).anchor = .TopRight; // ctx.get(panel2).fill = .FillY; todo, this is a bug. why. ctx.get(panel2).pos = .{ .x = 900, .y = 200 }; ctx.get(panel2).size = .{ .x = 300, .y = 500 }; ctx.get(panel2).style.backgroundColor = ui.ModernStyle.GreyDark; ctx.getPanel(panel2).titleColor = ui.ModernStyle.GreyDark; ctx.getPanel(panel2).layoutMode = .Vertical; // add some text to this panel const text = try ctx.addText(panel2, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); ctx.get(text).pos = .{ .x = 2, .y = 2 }; ctx.get(text).size = .{ .x = 296, .y = 300 }; const btn = try ctx.addButton(panel2, "select file..."); _ = btn; } } try extras.games.addGame("ExternGame", self, @This()); extras.beginGame("ExternGame"); return self; } fn editVector2(comptime Name: []const u8, v: *core.Vector2f) void { const ItemWidth = 120; ig.textFmt(Name, .{}) catch {}; ig.sameLine(0, 10); ig.setNextItemWidth(ItemWidth); _ = ig.inputFloat("X##particleEd" ++ Name, &v.x, 1.0, 5.0, null, .{}); ig.sameLine(0, 10); ig.setNextItemWidth(ItemWidth); _ = ig.inputFloat("Y##particleEd" ++ Name, &v.y, 1.0, 5.0, null, .{}); } fn v2ToV3(v: core.Vector2f) core.Vectorf { return core.Vectorf{ .x = v.x, .z = v.y, .y = 0 }; } pub fn intersectLine(p1: core.Vector2f, v1: core.Vector2f, p2: core.Vector2f, v2: core.Vector2f) ?core.Vector2f { const denom = v1.x * v2.y - v1.y * v2.x; if (@abs(denom) < 0.00001) { return null; } const t = ((p2.x - p1.x) * v2.y - (p2.y - p1.y) * v2.x) / denom; return .{ .x = p1.x + t * v1.x, .y = p1.y + t * v1.y, }; } pub fn drawVector2Test(self: *@This()) void { if (ig.begin("vector2Test", null, .{})) { editVector2("a", &self.a); editVector2("Anormal", &self.Anormal); ig.separator(); editVector2("b", &self.b); editVector2("Bnormal", &self.Bnormal); } ig.end(); const a = core.Vectorf{ .x = self.a.x, .z = self.a.y, .y = 0 }; const b = core.Vectorf{ .x = self.b.x, .z = self.b.y, .y = 0 }; const A = core.Vectorf{ .x = self.Anormal.x, .z = self.Anormal.y, .y = 0 }; const B = core.Vectorf{ .x = self.Bnormal.x, .z = self.Bnormal.y, .y = 0 }; core.debugSphere(a, 0.1, .{}); core.debugSphere(b, 0.1, .{}); core.debugLine(a, a.add(A), .{ .color = .{ .y = 0, .x = 1.0 } }); core.debugLine(b, b.add(B), .{ .color = .{ .y = 0, .x = 1.0 } }); const Vab = self.b.sub(self.a); const Vba = Vab.negate(); core.debugLine(a, a.add(v2ToV3(Vab)), .{ .color = .{ .x = 1.0, .z = 1.0 } }); // const ADwall = Vab.dot(self.Anormal.removeComponent(Vab.normalize()).normalize()); // const BDwall = Vba.dot(self.Anormal.removeComponent(Vba.normalize()).normalize()); //const Awall = Vab.removeComponent(self.Anormal.normalize()).normalize().fmul(ADwall).normalize(); const Awall = Vab.removeComponent(self.Anormal.normalize()).normalize(); const Bwall = Vba.removeComponent(self.Bnormal.normalize()).normalize(); //ig.textFmt("Awall: {d} {d}", .{ Awall.x, Awall.y }) catch {}; core.debugLine(a.sub(v2ToV3(Awall).fmul(10)), a.add(v2ToV3(Awall).fmul(10)), .{ .color = .{ .y = 1.0, .z = 1.0 } }); // ig.textFmt("Bwall: {d} {d}", .{ Bwall.x, Bwall.y }) catch {}; core.debugLine(b.sub(v2ToV3(Bwall).fmul(10)), b.add(v2ToV3(Bwall).fmul(10)), .{ .color = .{ .y = 1.0, .z = 1.0 } }); const vp = intersectLine(self.a, Awall.normalize(), self.b, Bwall.normalize()) orelse { return; }; // ig.textFmt("p, y: {d} {d}", .{ vp.x, vp.y }) catch {}; core.debugSphere(v2ToV3(vp), 0.1, .{ .color = .{ .y = 1.0, .z = 1.0, .x = 1.0 } }); } fn onAltFire(ctx: ?*anyopaque, _: core.ActionEvent) void { const self: *@This() = @ptrCast(@alignCast(ctx)); if (platform.context().isCursorEnabled()) { core.engine_logs("moving light"); const ray = rend.context().activeCamera.?.getNormalRayFromScreen(platform.getCursorPosition()); if (physics.traceLine(ray.start, ray.dir.fmul(10000), .{ .bodyFilter = &self.fireTraceFilter, .getNormalsSlow = true })) |r| { if (r.normal) |n| { const p = r.point.add(n.fmul(0.2)); core.debugSphere(p, 0.2, .{ .color = .{ .y = 1.0 }, .duration = 10.0 }); rend.context().lightPosition = p; } // core.debugLine(ray.start, ray.start.add(ray.dir.fmul(1000)), .{ .duration = 2.5 }); // physics.applyForce(r.body, ray.dir.fmul(400), r.point); } } } fn onFire(ctx: ?*anyopaque, _: core.ActionEvent) void { const self: *@This() = @ptrCast(@alignCast(ctx)); if (platform.context().isCursorEnabled()) { core.engine_logs("moving light"); const ray = rend.context().activeCamera.?.getNormalRayFromScreen(platform.getCursorPosition()); if (physics.traceLine(ray.start, ray.dir.fmul(10000), .{ .bodyFilter = &self.fireTraceFilter, .getNormalsSlow = true })) |r| { if (r.normal) |n| { const p: core.Vectorf = r.point.add(n.fmul(0.2)); const posRot = core.ScenePosRot{ .position = p, }; self.spawnedEntities.append(self.allocator, core.get(extras.ObjectSystemSpawner).spawnObjectAt(posRot) orelse return) catch unreachable; // addBox(p.add(n.fmul(1.0))) catch unreachable; } } } } pub fn tick(self: *@This(), dt: f64) void { const rctx = rend.context(); _ = rctx; _ = dt; if (core.getEngineObject(imgui.utils.TopBar)) |topbar| { topbar.menuOpen = platform.context().isCursorEnabled(); } if (self.displayVectorsTest) { self.drawVector2Test(); } if (self.firstTick) { self.loadMap2() catch unreachable; self.firstTick = false; } if (platform.context().isCursorEnabled()) { if (ig.begin("meh", null, .{})) { // if (ig.checkbox("move lights ", null)) {} ig.textFmt("info: - WASD to move, mouse to look,\n- Q and E to go up and down lol", .{}) catch return; ig.textFmt("- shift to slow down camera speed", .{}) catch return; ig.textFmt("- T to enable/disable mouse cursor", .{}) catch return; ig.textFmt("- f2 to route all inputs to the doom player", .{}) catch return; ig.textFmt("- movingLight checkbox makes the light stop moving with you", .{}) catch return; if (ig.smallButton("reload map")) { self.loadMap2() catch unreachable; } if (ig.smallButton("destroy map")) { if (self.tbMap) |tbMap| { core.engine_log("killing the map", .{}); tbMap.destroy(); self.tbMap = null; } } if (ig.smallButton("dumpTimeline")) { core.MemoryTracker.dumpTimeline("timeline.txt") catch unreachable; } if (ig.checkbox("vectors test", &self.displayVectorsTest)) {} if (ig.checkbox("showdebug", &ui.context().screenContext.drawDebug)) {} if (ig.smallButton("click for fun")) { // var soundname = core.MakeName("s_macintosh"); // audio.context().playSound(&soundname) catch {}; } if (ig.sliderFloat("sliderFloat", &self.volume, 0, 100, null, .{})) { audio.context().setVolume(self.volume / 100); } } ig.end(); if (platform.context().isCursorEnabled()) { if (rend.context().activeCamera) |cam| { const ray = cam.getNormalRayFromScreen(platform.getCursorPosition()); if (physics.traceLine(ray.start, ray.dir.fmul(10000), .{ .bodyFilter = &self.fireTraceFilter, .getNormalsSlow = false })) |r| { core.debugSphere(r.point, 0.2, .{ .color = .{ .x = 1.0 }, .duration = 0 }); } } } self.particleDebugger.particleDebug(); } } pub fn destroy(self: *@This()) void { if (self.tbMap) |map| { map.destroy(); } for (self.spawnedEntities.items) |*entity| { entity.destroy(); } self.spawnedEntities.deinit(self.allocator); self.consoleWindow.deinit(); self.physicsObjectsWindow.destroy(); self.allocator.destroy(Slack.fromPtr(self)); } }; pub export fn shutdown() void {} const std = @import("std"); const backlog = @import("backlog"); const core = backlog.core; const imgui = backlog.imgui; const physics = backlog.physics; const rend = backlog.rend; const ig = imgui.api; const sys = backlog.sys; const ui = backlog.ui; const audio = backlog.audio; const platform = backlog.platform; const extras = @import("gameExtras"); const bsp = @import("bsp");