Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flecs identifiers #536

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/zflecs/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
b.installArtifact(flecs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's going on with the diff, but installArtifact(flecs) is already being called on main branch, albeit not on this exact line. Can you try rebasing this branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will do.

flecs.linkLibC();
flecs.addIncludePath(.{ .path = "libs/flecs" });
flecs.addCSourceFile(.{
Expand Down
134 changes: 115 additions & 19 deletions libs/zflecs/src/zflecs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ fn make_error() error{FlecsError} {
pub const ID_FLAGS_MASK: u64 = @as(u64, 0xFF) << 60;
pub const COMPONENT_MASK: u64 = ~ID_FLAGS_MASK;

pub fn ecs_id(comptime name: []const u8) *entity_t {
return @extern(*entity_t, .{
.name = "FLECS_ID" ++ name ++ "ID_",
});
}

extern const EcsFlecs: entity_t;
extern const EcsFlecsCore: entity_t;
extern const EcsWorld: entity_t;
extern const EcsSystem: entity_t;

extern const EcsWildcard: entity_t;
extern const EcsAny: entity_t;
extern const EcsTransitive: entity_t;
Expand All @@ -44,6 +55,8 @@ extern const EcsUnion: entity_t;
extern const EcsAlias: entity_t;
extern const EcsChildOf: entity_t;
extern const EcsSlotOf: entity_t;
extern const EcsModule: entity_t;
extern const EcsPrivate: entity_t;
extern const EcsPrefab: entity_t;
extern const EcsDisabled: entity_t;

Expand Down Expand Up @@ -86,6 +99,15 @@ extern const EcsPredLookup: entity_t;
extern const EcsIsA: entity_t;
extern const EcsDependsOn: entity_t;

pub var Component: entity_t = undefined;
pub var Identifier: entity_t = undefined;
pub var Description: entity_t = undefined;

pub var Flecs: entity_t = undefined;
pub var FlecsCore: entity_t = undefined;
pub var World: entity_t = undefined;
pub var System: entity_t = undefined;

pub var Wildcard: entity_t = undefined;
pub var Any: entity_t = undefined;
pub var Transitive: entity_t = undefined;
Expand All @@ -106,6 +128,9 @@ pub var ChildOf: entity_t = undefined;
pub var DependsOn: entity_t = undefined;
pub var SlotOf: entity_t = undefined;

pub var Module: entity_t = undefined;
pub var Private: entity_t = undefined;

pub var AlwaysOverride: entity_t = undefined;
pub var Alias: entity_t = undefined;
pub var Prefab: entity_t = undefined;
Expand Down Expand Up @@ -889,8 +914,7 @@ const EcsAllocator = struct {

const Alignment = 16;

var gpa: ?std.heap.GeneralPurposeAllocator(.{}) = null;
var allocator: ?std.mem.Allocator = null;
var allocator: std.mem.Allocator = undefined;

fn alloc(size: i32) callconv(.C) ?*anyopaque {
if (size < 0) {
Expand All @@ -899,7 +923,7 @@ const EcsAllocator = struct {

const allocation_size = Alignment + @as(usize, @intCast(size));

const data = allocator.?.alignedAlloc(u8, Alignment, allocation_size) catch {
const data = allocator.alignedAlloc(u8, Alignment, allocation_size) catch {
return null;
};

Expand All @@ -923,7 +947,7 @@ const EcsAllocator = struct {
@ptrCast(@alignCast(ptr_unwrapped)),
);

allocator.?.free(
allocator.free(
@as([]align(Alignment) u8, @alignCast(ptr_unwrapped[0..allocation_header.size])),
);
}
Expand All @@ -945,7 +969,7 @@ const EcsAllocator = struct {
const old_slice_aligned = @as([]align(Alignment) u8, @alignCast(old_slice));

const new_allocation_size = Alignment + @as(usize, @intCast(size));
const new_data = allocator.?.realloc(old_slice_aligned, new_allocation_size) catch {
const new_data = allocator.realloc(old_slice_aligned, new_allocation_size) catch {
return null;
};

Expand Down Expand Up @@ -976,27 +1000,59 @@ fn flecs_abort() callconv(.C) noreturn {
// Creation & Deletion
//
//--------------------------------------------------------------------------------------------------
pub fn init() *world_t {
pub fn log_callback(level: i32, filename: [*c]const u8, line: i32, msg: [*c]const u8) callconv(.C) void {
_ = filename;
_ = line;

const level_name = switch (level) {
-4 => "fatal",
-3 => "error",
-2 => "warning",
-1 => "warning",
0 => "trace",
1 => "debug",
2 => "debug",
3 => "debug",
else => "unknown",
};
std.debug.print("flecs.{s: <8}| {s}\n", .{ level_name, msg });
}

pub fn init(allocator: std.mem.Allocator) *world_t {
os_init();
var api = os_get_api();
if (builtin.os.tag == .windows) {
os.ecs_os_api.abort_ = flecs_abort;
api.abort_ = flecs_abort;
}

assert(num_worlds == 0);

if (num_worlds == 0) {
EcsAllocator.gpa = .{};
EcsAllocator.allocator = EcsAllocator.gpa.?.allocator();
EcsAllocator.allocator = allocator;

os.ecs_os_api.malloc_ = &EcsAllocator.alloc;
os.ecs_os_api.free_ = &EcsAllocator.free;
os.ecs_os_api.realloc_ = &EcsAllocator.realloc;
os.ecs_os_api.calloc_ = &EcsAllocator.calloc;
api.malloc_ = &EcsAllocator.alloc;
api.free_ = &EcsAllocator.free;
api.realloc_ = &EcsAllocator.realloc;
api.calloc_ = &EcsAllocator.calloc;
}

api.log_ = log_callback;

os_set_api(&api);

num_worlds += 1;
component_ids_hm.ensureTotalCapacity(32) catch @panic("OOM");
const world = ecs_init();

Component = ecs_id("EcsComponent").*;
Identifier = ecs_id("EcsIdentifier").*;
Description = ecs_id("EcsDocDescription").*;

Flecs = EcsFlecs;
FlecsCore = EcsFlecsCore;
World = EcsWorld;
System = EcsSystem;

Wildcard = EcsWildcard;
Any = EcsAny;
Transitive = EcsTransitive;
Expand All @@ -1015,6 +1071,9 @@ pub fn init() *world_t {
DependsOn = EcsDependsOn;
SlotOf = EcsSlotOf;

Module = EcsModule;
Private = EcsPrivate;

OnDelete = EcsOnDelete;
OnDeleteTarget = EcsOnDeleteTarget;
Remove = EcsRemove;
Expand Down Expand Up @@ -1072,9 +1131,7 @@ pub fn fini(world: *world_t) i32 {
component_ids_hm.clearRetainingCapacity();

if (num_worlds == 0) {
_ = EcsAllocator.gpa.?.deinit();
EcsAllocator.gpa = null;
EcsAllocator.allocator = null;
EcsAllocator.allocator = undefined;
}

return fini_result;
Expand Down Expand Up @@ -1917,6 +1974,23 @@ extern fn ecs_query_get_ctx(query: *const query_t) ?*anyopaque;
pub const query_get_binding_ctx = ecs_query_get_binding_ctx;
extern fn ecs_query_get_binding_ctx(query: *const query_t) ?*anyopaque;

pub const identifier_t = extern struct {
value: [*]u8,
length: size_t,
hash: u64,
index_hash: u64,
index: *opaque {},
};

pub const component_t = extern struct {
size: size_t,
alignment: size_t,
};

pub const description_t = extern struct {
value: [*:0]const u8,
};

//--------------------------------------------------------------------------------------------------
//
// Functions for working with events and observers.
Expand Down Expand Up @@ -2290,6 +2364,15 @@ var num_worlds: u32 = 0;
var component_ids_hm = std.AutoHashMap(*id_t, u0).init(std.heap.page_allocator);

pub fn COMPONENT(world: *world_t, comptime T: type) void {
componentWithOptions(world, T, .{});
}

pub const ComponentOptions = struct {
name: ?[:0]const u8 = null,
symbol: ?[:0]const u8 = null,
};

pub fn componentWithOptions(world: *world_t, comptime T: type, options: ComponentOptions) void {
if (@sizeOf(T) == 0)
@compileError("Size of the type must be greater than zero");

Expand All @@ -2299,11 +2382,14 @@ pub fn COMPONENT(world: *world_t, comptime T: type) void {

component_ids_hm.put(type_id_ptr, 0) catch @panic("OOM");

const name = options.name orelse typeName(T);
const symbol = options.symbol orelse typeName(T);

type_id_ptr.* = ecs_component_init(world, &.{
.entity = ecs_entity_init(world, &.{
.use_low_id = true,
.name = typeName(T),
.symbol = typeName(T),
.name = name,
.symbol = symbol,
}),
.type = .{
.alignment = @alignOf(T),
Expand All @@ -2313,6 +2399,14 @@ pub fn COMPONENT(world: *world_t, comptime T: type) void {
}

pub fn TAG(world: *world_t, comptime T: type) void {
tagWithOptions(world, T, .{});
}

pub const TagOptions = struct {
name: ?[:0]const u8 = null,
};

pub fn tagWithOptions(world: *world_t, comptime T: type, options: TagOptions) void {
if (@sizeOf(T) != 0)
@compileError("Size of the type must be zero");

Expand All @@ -2322,7 +2416,9 @@ pub fn TAG(world: *world_t, comptime T: type) void {

component_ids_hm.put(type_id_ptr, 0) catch @panic("OOM");

type_id_ptr.* = ecs_entity_init(world, &.{ .name = typeName(T) });
const name = options.name orelse typeName(T);

type_id_ptr.* = ecs_entity_init(world, &.{ .name = name });
}

pub fn SYSTEM(
Expand Down
6 changes: 6 additions & 0 deletions libs/zglfw/src/zglfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,12 @@ pub const Window = opaque {
pub const swapBuffers = glfwSwapBuffers;
extern fn glfwSwapBuffers(window: *Window) void;

pub fn setMonitor (window: *Window, monitor: ?*Monitor, xpos: i32, ypos: i32, width: i32, height: i32, refreshRate: i32) void
{
glfwSetWindowMonitor (window, monitor, xpos, ypos, width, height, refreshRate);
}
extern fn glfwSetWindowMonitor(window: *Window, monitor: ?*Monitor, xpos: i32, ypos: i32, width: i32, height: i32, refreshRate: i32) void;

pub fn create(
width: i32,
height: i32,
Expand Down
3 changes: 2 additions & 1 deletion libs/ztracy/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ pub fn build(b: *std.Build) void {

const options_module = options_step.createModule();

_ = b.addModule("root", .{
const ztracy = b.addModule("root", .{
.root_source_file = .{ .path = "src/ztracy.zig" },
.imports = &.{
.{ .name = "ztracy_options", .module = options_module },
},
});
ztracy.addIncludePath(.{ .path = "libs/tracy/tracy" });
Comment on lines +27 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise here (changes present on main not showing in diff)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange... I'll have a look


const tracy = b.addStaticLibrary(.{
.name = "tracy",
Expand Down
13 changes: 10 additions & 3 deletions libs/ztracy/src/ztracy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ const tracy_full = struct {
// struct.
const static = struct {
var loc: c.___tracy_source_location_data = undefined;

// Ensure that a unique struct type is generated for each unique `src`. See
// https://github.com/ziglang/zig/issues/18816
comptime {
// https://github.com/ziglang/zig/issues/19274
_ = @sizeOf (@TypeOf (src));
hazeycode marked this conversation as resolved.
Show resolved Hide resolved
}
};
static.loc = .{
.name = name,
Expand Down Expand Up @@ -595,12 +602,12 @@ const tracy_full = struct {
log2_ptr_align: u8,
new_len: usize,
ra: usize,
) ?[*]u8 {
) bool {
const self: *TracyAllocator = @ptrCast(@alignCast(ctx));
const result = self.child_allocator.rawResize(buf, log2_ptr_align, new_len, ra);
if (result) |addr| {
if (result) {
Free(buf.ptr);
Alloc(addr, new_len);
Alloc(buf.ptr, new_len);
} else {
var buffer: [128]u8 = undefined;
const msg = std.fmt.bufPrint(&buffer, "resize failed requesting {d} -> {d}", .{ buf.len, new_len }) catch return result;
Expand Down
Loading