Skip to content
Flecs v4.1
hello_world

  • C

    View on GitHub

    1#include <hello_world.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x;
    6 double y;
    7} Position, Velocity;
    8
    9/* Move system implementation. System callbacks may be called multiple times,
    10 * as entities are grouped in tables by which components they have, and each
    11 * table has its own set of component arrays. */
    12void Move(ecs_iter_t *it) {
    13 Position *p = ecs_field(it, Position, 0);
    14 Velocity *v = ecs_field(it, Velocity, 1);
    15
    16 /* Print the set of components for the iterated over entities */
    17 char *type_str = ecs_table_str(it->stage, it->table);
    18 printf("Move entities with [%s]\n", type_str);
    19 ecs_os_free(type_str);
    20
    21 /* Iterate entities for the current table */
    22 for (int i = 0; i < it->count; i ++) {
    23 p[i].x += v[i].x;
    24 p[i].y += v[i].y;
    25 }
    26}
    27
    28int main(int argc, char *argv[]) {
    29 /* Create the world */
    30 ecs_world_t *world = ecs_init_w_args(argc, argv);
    31
    32 /* Register components */
    33 ECS_COMPONENT(world, Position);
    34 ECS_COMPONENT(world, Velocity);
    35
    36 /* Register system */
    37 ECS_SYSTEM(world, Move, EcsOnUpdate, Position, Velocity);
    38
    39 /* Register tags (components without a size) */
    40 ECS_TAG(world, Eats);
    41 ECS_TAG(world, Apples);
    42 ECS_TAG(world, Pears);
    43
    44 /* Create an entity with name Bob, add Position and food preference */
    45 ecs_entity_t Bob = ecs_entity(world, { .name = "Bob" });
    46 ecs_set(world, Bob, Position, {0, 0});
    47 ecs_set(world, Bob, Velocity, {1, 2});
    48 ecs_add_pair(world, Bob, Eats, Apples);
    49
    50 /* Run systems twice. Usually this function is called once per frame */
    51 ecs_progress(world, 0);
    52 ecs_progress(world, 0);
    53
    54 /* See if Bob has moved (he has) */
    55 const Position *p = ecs_get(world, Bob, Position);
    56 printf("Bob's position is {%f, %f}\n", p->x, p->y);
    57
    58 return ecs_fini(world);
    59
    60 // Output
    61 // Move entities with [Position, Velocity, (Identifier,Name), (Eats,Apples)]
    62 // Move entities with [Position, Velocity, (Identifier,Name), (Eats,Apples)]
    63 // Bob's position is {2.000000, 4.000000}
    64}
    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
    char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
    Convert a table to a string.
    #define ecs_add_pair(world, subject, first, second)
    Add a pair to an entity.
    Definition flecs_c.h:275
    #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_TAG(world, id)
    Declare and define a tag.
    Definition flecs_c.h:59
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_get(world, entity, T)
    Get a component.
    Definition flecs_c.h:339
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    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.
    Iterator.
    Definition flecs.h:1183
    ecs_table_t * table
    Current table.
    Definition flecs.h:1195
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1189

  • C++

    View on GitHub

    1#include <hello_world.h>
    2#include <iostream>
    3
    4// Component types
    5struct Position {
    6 double x;
    7 double y;
    8};
    9
    10struct Velocity {
    11 double x;
    12 double y;
    13};
    14
    15// Tag types
    16struct Eats { };
    17struct Apples { };
    18
    19int main(int, char *[]) {
    20 // Create the world
    21 flecs::world ecs;
    22
    23 // Register system
    24 ecs.system<Position, Velocity>()
    25 .each([](Position& p, Velocity& v) {
    26 p.x += v.x;
    27 p.y += v.y;
    28 });
    29
    30 // Create an entity with name Bob, add Position and food preference
    31 flecs::entity Bob = ecs.entity("Bob")
    32 .set(Position{0, 0})
    33 .set(Velocity{1, 2})
    34 .add<Eats, Apples>();
    35
    36 // Show us what you got
    37 std::cout << Bob.name() << "'s got [" << Bob.type().str() << "]\n";
    38
    39 // Run systems twice. Usually this function is called once per frame
    40 ecs.progress();
    41 ecs.progress();
    42
    43 // See if Bob has moved (he has)
    44 const Position& p = Bob.get<Position>();
    45 std::cout << Bob.name() << "'s position is {" << p.x << ", " << p.y << "}\n";
    46
    47 // Output
    48 // Bob's got [Position, Velocity, (Identifier,Name), (Eats,Apples)]
    49 // Bob's position is {2, 4}
    50}
    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
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.