Backlog/engine/core/tests/tests.zig

207 lines
6.4 KiB
Zig

const core = @import("core");
const std = @import("std");
const memory = core.MemoryTracker;
const engine_log = core.engine_log;
const engine_logs = core.engine_logs;
const exampleScene = @import("exampleScene.zig");
const SampleSubsystem = @import("samplesubsystem.zig");
test "simple systems setup for core" {
std.testing.refAllDecls(core.algorithm);
memory.MTSetup(std.testing.allocator, .{ .timeline = false });
defer memory.MTShutdown();
const tracker = memory.MTGet().?;
const allocator = tracker.allocator();
std.debug.print("Starting up \n", .{});
engine_logs("systems starting");
var map = try core.createSpecVariant(.{ .name = "test" }, allocator);
defer map.deinit();
try core.start_module(&map, .{ .unitTest = true }, allocator);
defer core.shutdown_module(allocator);
const subsystem = try core.createObject(SampleSubsystem, .{});
const scene = try allocator.create(exampleScene.Scene);
defer allocator.destroy(scene);
subsystem.scene = scene;
subsystem.setPositionPtr = @ptrCast(&exampleScene.Scene.setPosition);
engine_logs("systems started, shutting down");
engine_logs("this is a new print added to the test function");
engine_logs("this is a new print added to the test function");
memory.MTPrintStatsDelta();
try test_loadingConfigs();
try test_consoleCommands();
try test_gameObjects(std.testing.allocator);
try test_loadModules();
try test_objectLoaderRegistry();
// try generateRandomSamples();
try memory.dumpTimeline("test-core-timeline.txt");
}
fn test_objectLoaderRegistry() !void {
const inputStack = core.getEngineObject(core.inputs.InputStack);
try core.assert(inputStack == core.getInputStack());
}
pub fn test_loadModules() !void {
_ = try core.loadModule("external", true);
const moduleLoader = core.getEngineObject(core.ModuleLoader).?;
std.Thread.sleep(1e9 * 0.1);
moduleLoader.tick(0.1);
}
fn generateRandomSamples() !void {
var prng = std.Random.DefaultPrng.init(blk: {
var seed: u64 = undefined;
try std.posix.getrandom(std.mem.asBytes(&seed));
break :blk seed;
});
const rand = prng.random();
std.debug.print("const float3[] = {{", .{});
for (0..64) |i| {
var vec = core.Vectorf{
.x = rand.float(f32) * 2.0 - 1.0,
.y = rand.float(f32) * 2.0 - 1.0,
.z = rand.float(f32),
};
vec = vec.normalize();
vec = vec.fmul(rand.float(f32));
var scale: f32 = @as(f32, @floatFromInt(i)) / 64.0;
scale = scale * core.lerp(0.1, 1.0, scale * scale);
vec = vec.fmul(scale);
// std.debug.print("( {d}, {d}, {d} )\n", .{ vec.x, vec.y, vec.z });
std.debug.print(" float3( {d}, {d}, {d} ),\n", .{ vec.x, vec.y, vec.z });
}
std.debug.print("}};\n", .{});
}
fn test_loadingConfigs() !void {
if (core.getConfigVar(u32, "platform.extent.width")) |width| {
core.engine_log("config: {d}", .{width});
}
if (core.getConfigVar(u64, "platform.extent.height")) |height| {
core.engine_log("config: {d}", .{height});
}
if (core.getConfigVar(f32, "platform.extent.height")) |height| {
core.engine_log("config f32: {d}", .{height});
}
if (core.getConfigVar(f64, "platform.extent.height")) |height| {
core.engine_log("config f64: {d}", .{height});
}
if (core.getConfigVar([]const u8, "platform.windowName")) |height| {
core.engine_log("config string: {s}", .{height});
}
try core.assert(core.configVar(f64, "platform.extent.height", 0) == core.getConfigVar(f64, "platform.extent.height").?);
}
fn test_consoleCommands() !void {
const ConsoleCommands = struct {
pub fn echo(args: []const u8) void {
core.engine_log("{s}", .{args});
}
pub fn foo(args: []const u8) void {
core.engine_log("foo function called with args {s}", .{args});
}
};
try core.console.addCommand("echo", ConsoleCommands.echo);
try core.console.addCommand("foo", ConsoleCommands.foo);
const T = struct {
var ScreenResolution: u32 = undefined;
pub fn F(args: []const u8) void {
core.engine_log("Setting screen resolution to = {s}", .{args});
ScreenResolution = std.fmt.parseInt(u32, args, 10) catch return;
}
};
T.ScreenResolution = core.configVar(u32, "Some.bullshit.ScreenResolution", 420);
try core.assert(T.ScreenResolution == 420);
try core.console.addCommand("ScreenResolution", T.F);
core.console.evaluate("ScreenResolution 1");
try core.assert(T.ScreenResolution == 1);
core.console.evaluate("echo lmfao");
core.console.evaluate("foo lmfao");
}
fn test_gameObjects(allocator: std.mem.Allocator) !void {
engine_logs("test: gameobjects... ");
const objList = try core.GameObjectList.create(allocator);
defer objList.destroy();
const GameObject = struct {
data: core.GameObjectData = undefined,
entity: core.Entity,
pub fn create(alloc: std.mem.Allocator) !*@This() {
const self = try alloc.create(@This());
self.entity = try core.createEntity();
_ = self.entity.addComponent(core.Scene);
return self;
}
pub fn getEntity(self: *@This()) core.Entity {
return self.entity;
}
pub fn tick(self: *@This(), dt: f64) void {
_ = dt;
engine_log("gameObject ticking.... {d} {d}", .{ self.data.index, self.entity.handle.index });
}
pub fn destroy(self: *@This()) void {
self.entity.destroy();
self.data.release(self);
}
};
const gameObject = try objList.newObject(GameObject);
const gameObject2 = try objList.newObject(GameObject);
objList.tick(0.016);
gameObject.destroy();
objList.tick(0.016);
gameObject2.destroy();
objList.tick(0.016);
const spawnFunc = core.GameObjectList.spawnObjectFunction(GameObject);
const interface = spawnFunc(objList).?;
core.engine_log("lol", .{});
core.engine_log("handle = {x}", .{interface.vtable.getEntity.?(interface.ptr).handle.index});
objList.tick(0.016);
try exampleScene.doTest();
}