make the ecs subsystem be a separate module

This commit is contained in:
Lorenzo Torres 2025-03-23 19:38:34 +01:00
parent 874134f3ff
commit 1d720c790d
4 changed files with 40 additions and 27 deletions

View file

@ -61,6 +61,12 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
const ecs = b.addModule("ecs", .{
.root_source_file = b.path("src/ecs/ecs.zig"),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.root_source_file = b.path("src/main.zig"), .root_source_file = b.path("src/main.zig"),
.target = target, .target = target,
@ -68,9 +74,9 @@ pub fn build(b: *std.Build) void {
.name = "sideros", .name = "sideros",
}); });
exe.root_module.addImport("mods", mods); exe.root_module.addImport("mods", mods);
exe.root_module.addImport("ecs", ecs);
exe.addIncludePath(b.path("ext/glfw/include")); exe.addIncludePath(b.path("ext/glfw/include"));
exe.linkSystemLibrary("vulkan"); exe.linkSystemLibrary("vulkan");
compileAllShaders(b, exe); compileAllShaders(b, exe);
exe.linkLibrary(glfw); exe.linkLibrary(glfw);

6
src/ecs/ecs.zig Normal file
View file

@ -0,0 +1,6 @@
pub const components = @import("components.zig");
const entities = @import("entities.zig");
pub const Pool = entities.Pool;
pub const System = *const fn (*Pool) void;
pub const SystemGroup = []const System;

View file

@ -6,14 +6,12 @@ const config = @import("config");
const Renderer = @import("rendering/renderer_vulkan.zig"); const Renderer = @import("rendering/renderer_vulkan.zig");
const math = @import("math.zig"); const math = @import("math.zig");
const mods = @import("mods"); const mods = @import("mods");
const ecs = @import("ecs");
const components = @import("ecs/components.zig"); fn testSystem2(pool: *ecs.Pool) void {
const entities = @import("ecs/entities.zig"); for (pool.getQuery(ecs.components.Position), 0..) |position, i| {
const entity = pool.getEntity(i, ecs.components.Position);
fn testSystem2(pool: *entities.Pool) void { if (pool.getComponent(entity, ecs.components.Speed)) |speed| {
for (pool.getQuery(components.Position), 0..) |position, i| {
const entity = pool.getEntity(i, components.Position);
if (pool.getComponent(entity, components.Speed)) |speed| {
std.debug.print("entity{d}: {any},{any},{any} {any}\n", .{ i, position.x, position.y, position.z, speed.speed }); std.debug.print("entity{d}: {any},{any},{any} {any}\n", .{ i, position.x, position.y, position.z, speed.speed });
} }
} }
@ -46,20 +44,20 @@ pub fn main() !void {
const w = try window.Window.create(800, 600, "sideros"); const w = try window.Window.create(800, 600, "sideros");
defer w.destroy(); defer w.destroy();
var pool = try entities.Pool.init(allocator); var pool = try ecs.Pool.init(allocator);
defer pool.deinit(allocator); defer pool.deinit(allocator);
//try pool.addSystemGroup(&[_]entities.System{ //try pool.addSystemGroup(&[_]entities.System{
// testSystem, // testSystem,
//}); //});
try pool.addSystemGroup(&[_]entities.System{ try pool.addSystemGroup(&[_]ecs.System{
testSystem2, testSystem2,
}); });
for (0..1000) |_| { for (0..1000) |_| {
const entity = try pool.createEntity(); const entity = try pool.createEntity();
try pool.addComponent(entity, components.Position{ .x = 1.0, .y = 0.5, .z = 3.0 }); try pool.addComponent(entity, ecs.components.Position{ .x = 1.0, .y = 0.5, .z = 3.0 });
try pool.addComponent(entity, components.Speed{ .speed = 5.0 }); try pool.addComponent(entity, ecs.components.Speed{ .speed = 5.0 });
} }
// TODO(luccie-cmd): Renderer.create shouldn't return an error // TODO(luccie-cmd): Renderer.create shouldn't return an error

3
src/rendering/gltf.zig Normal file
View file

@ -0,0 +1,3 @@
const std = @import("std");
const mesh = @import("mesh.zig");
const Allocator = std.mem.Allocator;