C++ ECS example

A complete C++ ECS example.

Use ordinary structs, infer read and write access from a callback, and run the system through one world.

Copy the program

The program uses the public siecs.h header and the SIECS C runtime compiled beside the application.

#include <siecs.h>

struct Position {
    float x;
    float y;
};

struct Velocity {
    float x;
    float y;
};

int main() {
    ecs::init();

    ecs::entity::create("player").set(
        Position{ .x = 0.0f, .y = 0.0f },
        Velocity{ .x = 1.0f, .y = 1.0f }
    );

    ecs::system("Move")
        .phase(EcsOnUpdate)
        .each([](
            Position &position,
            const Velocity &velocity
        ) {
            position.x += velocity.x;
            position.y += velocity.y;
        });

    ecs::progress();
    ecs::fini();
}

Use ordinary structs

Position and velocity are regular structures. Typed operations associate each type with the active world.

The application code stays close to the domain types, while the runtime handles matching and iteration.

Create the entity

ecs::entity::create() creates a typed handle. The multi-value set() call attaches both component values in one expression.

Let the callback describe access

Position& requests writable data. The const Velocity& parameter requests read-only data.

SIECS stores the query and callback in the system. ecs::progress() runs it over matching batches.

Compile and run

cc -std=c23 -I. -c siecs.c -o siecs.o
c++ -std=c++23 -I. main.cpp siecs.o -pthread -o ecs_example

Continue with the C++ API guide for resources, relations, observers, inheritance, and reusable modules.