Skip to content
Flecs v4.2
queries/sorting

  • C

    View on GitHub

    1#include <sorting.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position;
    7
    8// Order by x member of Position
    9int compare_position(
    10 ecs_entity_t e1,
    11 const void *ptr1,
    12 ecs_entity_t e2,
    13 const void *ptr2)
    14{
    15 const Position *p1 = ptr1;
    16 const Position *p2 = ptr2;
    17 (void)e1;
    18 (void)e2;
    19 return (p1->x > p2->x) - (p1->x < p2->x);
    20}
    21
    22void print_position(ecs_iter_t *it) {
    23 Position *p = ecs_field(it, Position, 0);
    24 for (int i = 0; i < it->count; i ++) {
    25 printf("{%.1f, %.1f}\n", p[i].x, p[i].y);
    26 }
    27}
    28
    29// Iterate query, printed values will be ordered
    30void print_query(ecs_world_t *ecs, ecs_query_t *q) {
    31 ecs_iter_t it = ecs_query_iter(ecs, q);
    32 while (ecs_query_next(&it)) {
    33 print_position(&it);
    34 }
    35}
    36
    37int main(int argc, char *argv[]) {
    38 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    39
    40 ECS_COMPONENT(ecs, Position);
    41
    42 ecs_entity_t e = ecs_insert(ecs, ecs_value(Position, {1, 0}));
    43 ecs_insert(ecs, ecs_value(Position, {6, 0}));
    44 ecs_insert(ecs, ecs_value(Position, {2, 0}));
    45 ecs_insert(ecs, ecs_value(Position, {5, 0}));
    46 ecs_insert(ecs, ecs_value(Position, {4, 0}));
    47
    48 // Create a sorted system
    49 ecs_entity_t sys = ecs_system(ecs, {
    50 .query = {
    51 .terms = {{ .id = ecs_id(Position) }},
    52 .order_by_callback = compare_position,
    53 .order_by = ecs_id(Position)
    54 },
    55 .callback = print_position
    56 });
    57
    58 // Create sorted query
    59 ecs_query_t *q = ecs_query(ecs, {
    60 .terms = {{ .id = ecs_id(Position) }},
    61 .order_by_callback = compare_position,
    62 .order_by = ecs_id(Position)
    63 });
    64
    65 // Iterate query, print values of Position
    66 printf("-- First iteration\n");
    67 print_query(ecs, q);
    68
    69 // Change the value of one entity, invalidating the order
    70 ecs_set(ecs, e, Position, {7, 0});
    71
    72 // Iterate query again, printed values are still ordered
    73 printf("\n-- Second iteration\n");
    74 print_query(ecs, q);
    75
    76 // Create new entity to show that data is also sorted for system
    77 ecs_insert(ecs, ecs_value(Position, {3, 0}));
    78
    79 printf("\n-- System iteration\n");
    80 ecs_run(ecs, sys, 0, NULL);
    81
    82 // Output
    83 // -- First iteration
    84 // {1.0, 0.0}
    85 // {2.0, 0.0}
    86 // {4.0, 0.0}
    87 // {5.0, 0.0}
    88 // {6.0, 0.0}
    89 //
    90 // -- Second iteration
    91 // {2.0, 0.0}
    92 // {4.0, 0.0}
    93 // {5.0, 0.0}
    94 // {6.0, 0.0}
    95 // {7.0, 0.0}
    96 //
    97 // -- System iteration
    98 // {2.0, 0.0}
    99 // {3.0, 0.0}
    100 // {4.0, 0.0}
    101 // {5.0, 0.0}
    102 // {6.0, 0.0}
    103 // {7.0, 0.0}
    104
    105 return ecs_fini(ecs);
    106}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    #define ecs_system(world,...)
    Shorthand for creating a system with ecs_system_init().
    Definition system.h:288
    FLECS_API ecs_entity_t ecs_run(ecs_world_t *world, ecs_entity_t system, ecs_ftime_t delta_time, void *param)
    Run a specific system manually.
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:396
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:440
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:99
    #define ecs_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:218
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:313
    #define ecs_insert(world,...)
    Insert a new entity with a list of component values.
    Definition flecs_c.h:305
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:695
    #define ecs_value(T,...)
    Convenience macro for creating a compound literal value literal.
    Definition flecs_c.h:758
    bool ecs_query_next(ecs_iter_t *it)
    Progress a query iterator.
    ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
    Create a query iterator.
    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:1184
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1190
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:859

  • C++

    View on GitHub

    1#include <sorting.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8// Order by x member of Position
    9int compare_position(
    11 const Position *p1,
    13 const Position *p2)
    14{
    15 (void)e1;
    16 (void)e2;
    17 return (p1->x > p2->x) - (p1->x < p2->x);
    18}
    19
    20// Iterate query, printed values will be ordered
    21void print_query(flecs::query<const Position>& q) {
    22 q.each([](flecs::entity, const Position& p) {
    23 std::cout << "{" << p.x << "," << p.y << "}" << std::endl;
    24 });
    25}
    26
    27int main(int argc, char *argv[]) {
    28 flecs::world ecs(argc, argv);
    29
    30 // Create entities, set Position in random order
    31 flecs::entity e = ecs.entity().set<Position>({1, 0});
    32 ecs.entity().set<Position>({6, 0});
    33 ecs.entity().set<Position>({2, 0});
    34 ecs.entity().set<Position>({5, 0});
    35 ecs.entity().set<Position>({4, 0});
    36
    37 // Create a sorted system
    38 flecs::system sys = ecs.system<const Position>()
    39 .order_by(compare_position)
    40 .each([](const Position &p) {
    41 std::cout << "{" << p.x << "," << p.y << "}" << std::endl;
    42 });
    43
    44 // Create a sorted query
    45 flecs::query<const Position> q = ecs.query_builder<const Position>()
    46 .order_by(compare_position)
    47 .build();
    48
    49 // Iterate query, print values of Position
    50 std::cout << "-- First iteration\n";
    51 print_query(q);
    52
    53 // Change the value of one entity, invalidating the order
    54 e.set<Position>({7, 0});
    55
    56 // Iterate query again, printed values are still ordered
    57 std::cout << "\n-- Second iteration";
    58 print_query(q);
    59
    60 // Create new entity to show that data is also sorted for system
    61 ecs.entity().set<Position>({3, 0});
    62
    63 // Run system, output will be sorted
    64 std::cout << "\n-- System iteration";
    65 sys.run();
    66
    67 // Output
    68 // -- First iteration
    69 // {1,0}
    70 // {2,0}
    71 // {4,0}
    72 // {5,0}
    73 // {6,0}
    74 //
    75 // -- Second iteration{2,0}
    76 // {4,0}
    77 // {5,0}
    78 // {6,0}
    79 // {7,0}
    80 //
    81 // -- System iteration{2,0}
    82 // {3,0}
    83 // {4,0}
    84 // {5,0}
    85 // {6,0}
    86 // {7,0}
    87
    88}
    ecs_entity_t entity_t
    Entity type.
    Definition c_types.hpp:21
    Entity.
    Definition entity.hpp:30
    entity()
    Default constructor.
    Definition entity.hpp:32
    void each(Func &&func) const
    Each iterator.
    Definition iterable.hpp:37
    Typed query.
    Definition impl.hpp:271
    System.
    Definition impl.hpp:78
    system()
    Default constructor.
    Definition impl.hpp:82
    The world.
    Definition world.hpp:109