Skip to content
Flecs v4.1
reflection/basics_bitmask

  • C

    View on GitHub

    1#include <basics_bitmask.h>
    2#include <stdio.h>
    3
    4typedef uint32_t Toppings;
    5static const uint32_t Bacon = 0x1;
    6static const uint32_t Lettuce = 0x2;
    7static const uint32_t Tomato = 0x4;
    8
    9typedef struct {
    10 Toppings toppings;
    11} Sandwich;
    12
    13int main(int argc, char *argv[]) {
    14 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    15
    16 // Register component as usual
    17 ECS_COMPONENT(ecs, Toppings);
    18 ECS_COMPONENT(ecs, Sandwich);
    19
    20 // Add reflection data to components
    21 ecs_bitmask(ecs,{
    22 .entity = ecs_id(Toppings), // Make sure to use existing id
    23 .constants = {
    24 { .name = "Bacon", .value = Bacon },
    25 { .name = "Lettuce", .value = Lettuce },
    26 { .name = "Tomato", .value = Tomato }
    27 }
    28 });
    29
    30 ecs_struct(ecs, {
    31 .entity = ecs_id(Sandwich), // Make sure to use existing id
    32 .members = {
    33 { .name = "toppings", .type = ecs_id(Toppings) }
    34 }
    35 });
    36
    37 // Create entity with Sandwich as usual
    38 ecs_entity_t ent = ecs_new(ecs);
    39 ecs_set(ecs, ent, Sandwich, {Bacon | Lettuce});
    40
    41 // Convert Sandwich component to flecs expression string
    42 const Sandwich *ptr = ecs_get(ecs, ent, Sandwich);
    43 char *str = ecs_ptr_to_expr(ecs, ecs_id(Sandwich), ptr);
    44 printf("%s\n", str); // {toppings: Bacon|Lettuce}
    45 ecs_os_free(str);
    46
    47 ecs_fini(ecs);
    48}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    #define ecs_bitmask(world,...)
    Create a bitmask type.
    Definition meta.h:1757
    #define ecs_struct(world,...)
    Create a struct type.
    Definition meta.h:1777
    FLECS_API char * ecs_ptr_to_expr(const ecs_world_t *world, ecs_entity_t type, const void *data)
    Serialize value into expression string.
    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
    ecs_entity_t ecs_new(ecs_world_t *world)
    Create new entity ID.
    #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_get(world, entity, T)
    Get a component.
    Definition flecs_c.h:339
    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.

  • C++

    View on GitHub

    1#include <basics_bitmask.h>
    2#include <iostream>
    3
    4struct Toppings : flecs::bitmask {
    5 static constexpr uint32_t Bacon = 0x1;
    6 static constexpr uint32_t Lettuce = 0x2;
    7 static constexpr uint32_t Tomato = 0x4;
    8};
    9
    10struct Sandwich {
    11 uint32_t toppings;
    12};
    13
    14int main(int, char *[]) {
    15 flecs::world ecs;
    16
    17 // Register components with reflection data
    18 ecs.component<Toppings>()
    19 .bit("Bacon", Toppings::Bacon)
    20 .bit("Lettuce", Toppings::Lettuce)
    21 .bit("Tomato", Toppings::Tomato);
    22
    23 ecs.component<Sandwich>()
    24 .member<Toppings>("toppings");
    25
    26 // Create entity with Sandwich as usual
    27 flecs::entity e = ecs.entity()
    28 .set<Sandwich>({Toppings::Bacon | Toppings::Lettuce});
    29
    30 // Convert Sandwich component to flecs expression string
    31 const Sandwich& c = e.get<Sandwich>();
    32 std::cout << ecs.to_expr(&c).c_str() << "\n"; // {toppings: Lettuce|Bacon}
    33}
    untyped_component & bit(const char *name, T value)
    Add a bitmask constant.
    flecs::string to_expr(flecs::entity_t tid, const void *value)
    Convert a value to a string.
    Definition world.hpp:81
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    Base type for bitmasks.
    Definition decl.hpp:53
    Entity.
    Definition entity.hpp:30
    const char * c_str() const
    Return the C string.
    Definition string.hpp:109
    The world.
    Definition world.hpp:129