How freakmangd/zentig_ecs works
Purpose and Public Entry Point
Zentig is a framework-agnostic Zig entity-component-system (ECS) library. Consumers add it as the zentig build module and import ztg; the library’s public aggregation point is src/init.zig. Example executable entry points demonstrate embedding the ECS in an application loop: examples/hello_world.zig, examples/input_example.zig.
Compile-Time World Construction
WorldBuilder defines a world’s static shape at compile time: registered component types, systems, stages, and included feature namespaces. Its Build() output produces the concrete world type used at runtime. This is the principal extension boundary for games and third-party modules.
• src/worldbuilder.zig
• src/init.zig
• src/mods/base/init.zig
Runtime World and Entity Lifecycle
The generated world owns ECS storage, allocators/frame cleanup behavior, system execution, and entity management. Entities are usize identifiers, with handle and array abstractions supporting lifecycle and storage bookkeeping.
• src/world.zig
• src/entity.zig
• src/entity_handle.zig
Component Storage Model
Components are registered by type and stored in component-oriented arrays; entity-oriented arrays and sparse/packed utilities support lookup and membership relationships. This is the core data boundary systems access through queries rather than directly coupling to world internals.
• src/component_array.zig
• src/entity_array.zig
• src/etc/packed_sparse.zig
Query-Based System Access
Systems declare typed Query parameters, allowing iteration over matching component sets. Query construction and filtering bridge statically registered component types to runtime component storage, isolating gameplay systems from storage implementation.
• src/query.zig
• src/component_array.zig
• src/world.zig
Stages, Scheduling, and Commands
Applications invoke named stages such as load, update, and draw; custom stage lists are also supported. Systems registered into stages are ordered through scheduling/order logic. Deferred commands provide a structural-change boundary so systems can request ECS mutations without directly destabilizing iteration.
• src/stages.zig
• src/system_order.zig
• src/commands.zig
Events and Built-In Modules
The library provides event handling plus composable modules for common ECS concerns. The base module contributes transform, global-transform, time, and lifetime functionality; input and animation are optional feature modules included through the builder.
• src/events.zig
• src/mods/base/init.zig
• src/mods/input.zig
Runtime and External Boundaries
The project is Zig-native, built through build.zig, with zmath vendored as a dependency under deps/zmath. Zentig intentionally contains no renderer or application framework integration; host applications own the main loop and call world stages. Raylib integration is referenced externally but is not part of this repository.
• build.zig
• build.zig.zon
• deps/zmath/src/root.zig
Main entry points: world.zig Generated World · worldbuilder.zig WorldBuilder · system_order.zig System ordering · init.zig ztg public API
Components 18
Build & integration
- Zig build module · Exposes Zentig to consumer builds.
build.zig - ztg public API · Public aggregation point imported by consumers.
src/init.zig - Host application loop · Owns the main loop and invokes world stages.
- Embedding examples · Demonstrate ECS use in application loops.
examples/hello_world.zig
ECS core
- WorldBuilder · Registers components, systems, stages, and modules.
src/worldbuilder.zig - Generated World · Owns storage, allocators, lifecycle, and system execution.
src/world.zig - Entity lifecycle
src/entity.zig - Component storage · Stores registered components and membership data.
src/component_array.zig - Entity array bookkeeping
src/entity_array.zig - Packed sparse utilities
src/etc/packed_sparse.zig
Execution model
- Typed Query · Matches typed component sets against runtime storage.
src/query.zig - Named stages · Supports load, update, draw, and custom stage lists.
src/stages.zig - System ordering
src/system_order.zig - Deferred commands · Defers ECS mutations until iteration is safe.
src/commands.zig - Events
src/events.zig
Composable features
- Base module · Provides transforms, time, and lifetime features.
src/mods/base/init.zig - Input module
src/mods/input.zig - zmath
deps/zmath/src/root.zig
Connections 20
- Zig build module → ztg public API: builds module
- Host application loop → ztg public API: imports ztg
- Embedding examples → Host application loop: illustrate
- ztg public API → WorldBuilder: exports
- WorldBuilder → Generated World: Build() generates
- WorldBuilder → Base module: includes
- WorldBuilder → Input module: optionally includes
- Host application loop → Named stages: invokes
- Named stages → System ordering: runs systems through
- System ordering → Generated World: executes against
- Generated World → Entity lifecycle: manages
- Generated World → Component storage: owns
- Component storage → Entity array bookkeeping: uses
- Entity array bookkeeping → Packed sparse utilities: uses
- Typed Query → Component storage: accesses
- System ordering → Typed Query: systems declare
- System ordering → Deferred commands: systems enqueue
- Deferred commands → Generated World: applies mutations
- Generated World → Events: provides
- Base module → zmath: uses