# freakmangd/zentig_ecs architecture

> Architecture diagram of the GitHub repository freakmangd/zentig_ecs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/freakmangd/zentig_ecs
- Repository: https://github.com/freakmangd/zentig_ecs

## Overview

#### 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: [`src/world.zig`](https://github.com/freakmangd/zentig_ecs/blob/main/src/world.zig) (Generated World), [`src/worldbuilder.zig`](https://github.com/freakmangd/zentig_ecs/blob/main/src/worldbuilder.zig) (WorldBuilder), [`src/system_order.zig`](https://github.com/freakmangd/zentig_ecs/blob/main/src/system_order.zig) (System ordering), [`src/init.zig`](https://github.com/freakmangd/zentig_ecs/blob/main/src/init.zig) (ztg public API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_build["Build &amp; integration"]
  node_build_zig["Zig build module<br/>[build.zig]"]
  node_public_api["ztg public API<br/>library entry<br/>[init.zig]"]
  node_host_app["Host application loop<br/>consumer runtime"]
  node_examples["Embedding examples<br/>example executables<br/>[hello_world.zig]"]
end

subgraph group_core["ECS core"]
  node_world_builder{{"WorldBuilder<br/>compile-time extension boundary<br/>[worldbuilder.zig]"}}
  node_world["Generated World<br/>runtime ECS world<br/>[world.zig]"]
  node_entities["Entity lifecycle<br/>entity IDs and handles<br/>[entity.zig]"]
  node_component_storage[("Component storage<br/>component-oriented arrays")]
  node_entity_arrays["Entity array bookkeeping<br/>entity-oriented storage<br/>[entity_array.zig]"]
  node_sparse_packed["Packed sparse utilities<br/>lookup utility<br/>[packed_sparse.zig]"]
end

subgraph group_execution["Execution model"]
  node_query["Typed Query<br/>system data access<br/>[query.zig]"]
  node_stages["Named stages<br/>stage execution API<br/>[stages.zig]"]
  node_scheduler["System ordering<br/>scheduler<br/>[system_order.zig]"]
  node_commands["Deferred commands<br/>structural mutation boundary<br/>[commands.zig]"]
  node_events["Events<br/>event facility<br/>[events.zig]"]
end

subgraph group_features["Composable features"]
  node_base_module["Base module<br/>built-in feature module<br/>[init.zig]"]
  node_input_module["Input module<br/>optional feature module<br/>[input.zig]"]
  node_zmath["zmath<br/>vendored math dependency<br/>[root.zig]"]
end

node_build_zig -->|"builds module"| node_public_api
node_host_app -->|"imports ztg"| node_public_api
node_examples -.->|"illustrate"| node_host_app
node_public_api -->|"exports"| node_world_builder
node_world_builder -->|"Build&#40;&#41; generates"| node_world
node_world_builder -->|"includes"| node_base_module
node_world_builder -.->|"optionally includes"| node_input_module
node_host_app -->|"invokes"| node_stages
node_stages -->|"runs systems through"| node_scheduler
node_scheduler -->|"executes against"| node_world
node_world -->|"manages"| node_entities
node_world -->|"owns"| node_component_storage
node_component_storage -->|"uses"| node_entity_arrays
node_entity_arrays -->|"uses"| node_sparse_packed
node_query -->|"accesses"| node_component_storage
node_scheduler -->|"systems declare"| node_query
node_scheduler -->|"systems enqueue"| node_commands
node_commands -->|"applies mutations"| node_world
node_world -->|"provides"| node_events
node_base_module -->|"uses"| node_zmath

click node_build_zig "https://github.com/freakmangd/zentig_ecs/blob/main/build.zig"
click node_public_api "https://github.com/freakmangd/zentig_ecs/blob/main/src/init.zig"
click node_examples "https://github.com/freakmangd/zentig_ecs/blob/main/examples/hello_world.zig"
click node_world_builder "https://github.com/freakmangd/zentig_ecs/blob/main/src/worldbuilder.zig"
click node_world "https://github.com/freakmangd/zentig_ecs/blob/main/src/world.zig"
click node_entities "https://github.com/freakmangd/zentig_ecs/blob/main/src/entity.zig"
click node_component_storage "https://github.com/freakmangd/zentig_ecs/blob/main/src/component_array.zig"
click node_entity_arrays "https://github.com/freakmangd/zentig_ecs/blob/main/src/entity_array.zig"
click node_sparse_packed "https://github.com/freakmangd/zentig_ecs/blob/main/src/etc/packed_sparse.zig"
click node_query "https://github.com/freakmangd/zentig_ecs/blob/main/src/query.zig"
click node_stages "https://github.com/freakmangd/zentig_ecs/blob/main/src/stages.zig"
click node_scheduler "https://github.com/freakmangd/zentig_ecs/blob/main/src/system_order.zig"
click node_commands "https://github.com/freakmangd/zentig_ecs/blob/main/src/commands.zig"
click node_events "https://github.com/freakmangd/zentig_ecs/blob/main/src/events.zig"
click node_base_module "https://github.com/freakmangd/zentig_ecs/blob/main/src/mods/base/init.zig"
click node_input_module "https://github.com/freakmangd/zentig_ecs/blob/main/src/mods/input.zig"
click node_zmath "https://github.com/freakmangd/zentig_ecs/blob/main/deps/zmath/src/root.zig"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_build_zig,node_public_api,node_host_app,node_examples toneBlue
class node_world_builder,node_world,node_entities,node_component_storage,node_entity_arrays,node_sparse_packed toneAmber
class node_query,node_stages,node_scheduler,node_commands,node_events toneMint
class node_base_module,node_input_module,node_zmath toneRose
```

## Components

### Build & integration

- [Zig build module](https://github.com/freakmangd/zentig_ecs/blob/main/build.zig): build · Exposes Zentig to consumer builds. · `build.zig`
- [ztg public API](https://github.com/freakmangd/zentig_ecs/blob/main/src/init.zig): library entry · Public aggregation point imported by consumers. · `src/init.zig`
- **Host application loop**: consumer runtime · Owns the main loop and invokes world stages.
- [Embedding examples](https://github.com/freakmangd/zentig_ecs/blob/main/examples/hello_world.zig): example executables · Demonstrate ECS use in application loops. · `examples/hello_world.zig`

### ECS core

- [WorldBuilder](https://github.com/freakmangd/zentig_ecs/blob/main/src/worldbuilder.zig): compile-time extension boundary · Registers components, systems, stages, and modules. · `src/worldbuilder.zig`
- [Generated World](https://github.com/freakmangd/zentig_ecs/blob/main/src/world.zig): runtime ECS world · Owns storage, allocators, lifecycle, and system execution. · `src/world.zig`
- [Entity lifecycle](https://github.com/freakmangd/zentig_ecs/blob/main/src/entity.zig): entity IDs and handles · `src/entity.zig`
- [Component storage](https://github.com/freakmangd/zentig_ecs/blob/main/src/component_array.zig): component-oriented arrays · Stores registered components and membership data. · `src/component_array.zig`
- [Entity array bookkeeping](https://github.com/freakmangd/zentig_ecs/blob/main/src/entity_array.zig): entity-oriented storage · `src/entity_array.zig`
- [Packed sparse utilities](https://github.com/freakmangd/zentig_ecs/blob/main/src/etc/packed_sparse.zig): lookup utility · `src/etc/packed_sparse.zig`

### Execution model

- [Typed Query](https://github.com/freakmangd/zentig_ecs/blob/main/src/query.zig): system data access · Matches typed component sets against runtime storage. · `src/query.zig`
- [Named stages](https://github.com/freakmangd/zentig_ecs/blob/main/src/stages.zig): stage execution API · Supports load, update, draw, and custom stage lists. · `src/stages.zig`
- [System ordering](https://github.com/freakmangd/zentig_ecs/blob/main/src/system_order.zig): scheduler · `src/system_order.zig`
- [Deferred commands](https://github.com/freakmangd/zentig_ecs/blob/main/src/commands.zig): structural mutation boundary · Defers ECS mutations until iteration is safe. · `src/commands.zig`
- [Events](https://github.com/freakmangd/zentig_ecs/blob/main/src/events.zig): event facility · `src/events.zig`

### Composable features

- [Base module](https://github.com/freakmangd/zentig_ecs/blob/main/src/mods/base/init.zig): built-in feature module · Provides transforms, time, and lifetime features. · `src/mods/base/init.zig`
- [Input module](https://github.com/freakmangd/zentig_ecs/blob/main/src/mods/input.zig): optional feature module · `src/mods/input.zig`
- [zmath](https://github.com/freakmangd/zentig_ecs/blob/main/deps/zmath/src/root.zig): vendored math dependency · `deps/zmath/src/root.zig`

## Connections

- 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
