Skip to content
Flecs v4.1
systems/pipeline

  • C

    View on GitHub

    1#include <pipeline.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position, Velocity;
    7
    8void Move(ecs_iter_t *it) {
    9 Position *p = ecs_field(it, Position, 0);
    10 const Velocity *v = ecs_field(it, const Velocity, 1);
    11
    12 for (int i = 0; i < it->count; i ++) {
    13 p[i].x += v[i].x;
    14 p[i].y += v[i].y;
    15 }
    16}
    17
    18void PrintPosition(ecs_iter_t *it) {
    19 const Position *p = ecs_field(it, const Position, 0);
    20
    21 for (int i = 0; i < it->count; i ++) {
    22 printf("%s: {%f, %f}\n", ecs_get_name(it->stage, it->entities[i]),
    23 p[i].x, p[i].y);
    24 }
    25}
    26
    27int main(int argc, char *argv[]) {
    28 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    29
    30 ECS_COMPONENT(ecs, Position);
    31 ECS_COMPONENT(ecs, Velocity);
    32
    33 // Create system for moving an entity in the EcsOnUpdate phase
    34 ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, [in] Velocity);
    35
    36 // Create system for printing a Position in the EcsPostUpdate phase
    37 ECS_SYSTEM(ecs, PrintPosition, EcsPostUpdate, [in] Position);
    38
    39 // Create a few test entities for a Position, Velocity query
    40 ecs_entity_t e1 = ecs_entity(ecs, { .name = "e1" });
    41 ecs_set(ecs, e1, Position, {10, 20});
    42 ecs_set(ecs, e1, Velocity, {1, 2});
    43
    44 ecs_entity_t e2 = ecs_entity(ecs, { .name = "e2" });
    45 ecs_set(ecs, e2, Position, {10, 20});
    46 ecs_set(ecs, e2, Velocity, {3, 4});
    47
    48 // Run the default pipeline. This will run all systems ordered by their
    49 // phase. Systems within the same phase are ran in declaration order. This
    50 // function is usually called in a loop.
    51 ecs_progress(ecs, 0.0f);
    52
    53 return ecs_fini(ecs);
    54}
    const ecs_entity_t EcsPostUpdate
    PostUpdate pipeline phase.
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
    Progress a world.
    #define ECS_SYSTEM(world, id, phase,...)
    Declare and define a system.
    Definition system.h:263
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    Pipeline module.
    Iterator.
    Definition flecs.h:1183
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1189
    const ecs_entity_t * entities
    Entity identifiers.
    Definition flecs.h:1190

  • C++

    View on GitHub

    1#include <pipeline.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Velocity {
    9 double x, y;
    10};
    11
    12int main(int, char *[]) {
    13 flecs::world ecs;
    14
    15 // Create a system for moving an entity
    16 ecs.system<Position, const Velocity>()
    17 .kind(flecs::OnUpdate) // A phase orders a system in a pipeline
    18 .each([](Position& p, const Velocity& v) {
    19 p.x += v.x;
    20 p.y += v.y;
    21 });
    22
    23 // Create a system for printing the entity position
    24 ecs.system<const Position>()
    25 .kind(flecs::PostUpdate)
    26 .each([](flecs::entity e, const Position& p) {
    27 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    28 });
    29
    30 // Create a few test entities for a Position, Velocity query
    31 ecs.entity("e1")
    32 .set<Position>({10, 20})
    33 .set<Velocity>({1, 2});
    34
    35 ecs.entity("e2")
    36 .set<Position>({10, 20})
    37 .set<Velocity>({3, 4});
    38
    39 // Run the default pipeline. This will run all systems ordered by their
    40 // phase. Systems within the same phase are ran in declaration order. This
    41 // function is usually called in a loop.
    42 ecs.progress();
    43}
    flecs::system system(flecs::entity e) const
    Upcast an entity to a system.
    Definition impl.hpp:181
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    bool progress(ecs_ftime_t delta_time=0.0) const
    Progress the world one tick.
    Definition impl.hpp:47
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    system & each(Func &&func)
    Replace the system's each callback.
    Definition impl.hpp:119
    The world.
    Definition world.hpp:129