Skip to content
Flecs v4.1
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 Position *p1,
    12 ecs_entity_t e2,
    13 const Position *p2)
    14{
    15 (void)e1;
    16 (void)e2;
    17 return (p1->x > p2->x) - (p1->x < p2->x);
    18}
    19
    20void print_position(ecs_iter_t *it) {
    21 Position *p = ecs_field(it, Position, 0);
    22 for (int i = 0; i < it->count; i ++) {
    23 printf("{%.1f, %.1f}\n", p[i].x, p[i].y);
    24 }
    25}
    26
    27// Iterate query, printed values will be ordered
    28void print_query(ecs_world_t *ecs, ecs_query_t *q) {
    29 ecs_iter_t it = ecs_query_iter(ecs, q);
    30 while (ecs_query_next(&it)) {
    31 print_position(&it);
    32 }
    33}
    34
    35int main(int argc, char *argv[]) {
    36 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    37
    38 ECS_COMPONENT(ecs, Position);
    39
    40 ecs_entity_t e = ecs_insert(ecs, ecs_value(Position, {1, 0}));
    41 ecs_insert(ecs, ecs_value(Position, {6, 0}));
    42 ecs_insert(ecs, ecs_value(Position, {2, 0}));
    43 ecs_insert(ecs, ecs_value(Position, {5, 0}));
    44 ecs_insert(ecs, ecs_value(Position, {4, 0}));
    45
    46 // Create a sorted system
    47 ecs_entity_t sys = ecs_system(ecs, {
    48 .query = {
    49 .terms = {{ .id = ecs_id(Position) }},
    50 .order_by_callback = (ecs_order_by_action_t)compare_position,
    51 .order_by = ecs_id(Position)
    52 },
    53 .callback = print_position
    54 });
    55
    56 // Create sorted query
    57 ecs_query_t *q = ecs_query(ecs, {
    58 .terms = {{ .id = ecs_id(Position) }},
    59 .order_by_callback = (ecs_order_by_action_t)compare_position,
    60 .order_by = ecs_id(Position)
    61 });
    62
    63 // Iterate query, print values of Position
    64 printf("-- First iteration\n");
    65 print_query(ecs, q);
    66
    67 // Change the value of one entity, invalidating the order
    68 ecs_set(ecs, e, Position, {7, 0});
    69
    70 // Iterate query again, printed values are still ordered
    71 printf("\n-- Second iteration\n");
    72 print_query(ecs, q);
    73
    74 // Create new entity to show that data is also sorted for system
    75 ecs_insert(ecs, ecs_value(Position, {3, 0}));
    76
    77 printf("\n-- System iteration\n");
    78 ecs_run(ecs, sys, 0, NULL);
    79
    80 // Output
    81 // -- First iteration
    82 // {1.0, 0.0}
    83 // {2.0, 0.0}
    84 // {4.0, 0.0}
    85 // {5.0, 0.0}
    86 // {6.0, 0.0}
    87 //
    88 // -- Second iteration
    89 // {2.0, 0.0}
    90 // {4.0, 0.0}
    91 // {5.0, 0.0}
    92 // {6.0, 0.0}
    93 // {7.0, 0.0}
    94 //
    95 // -- System iteration
    96 // {2.0, 0.0}
    97 // {3.0, 0.0}
    98 // {4.0, 0.0}
    99 // {5.0, 0.0}
    100 // {6.0, 0.0}
    101 // {7.0, 0.0}
    102
    103 return ecs_fini(ecs);
    104}
    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:285
    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: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_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:215
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_insert(world,...)
    Insert a new entity with a list of component values.
    Definition flecs_c.h:302
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    #define ecs_value(T,...)
    Convenience macro for creating a compound literal value literal.
    Definition flecs_c.h:783
    int(*) ecs_order_by_action_t(ecs_entity_t e1, const void *ptr1, ecs_entity_t e2, const void *ptr2)
    Callback used for comparing components.
    Definition flecs.h:613
    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:1183
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1189
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:858

  • 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:129