Skip to content
Flecs v4.1
modules/simple_module

  • C

    View on GitHub

    include/simple_module.h

    1#ifndef SIMPLE_MODULE_H
    2#define SIMPLE_MODULE_H
    3
    4/* This generated file contains includes for project dependencies */
    5#include "simple_module/bake_config.h"
    6
    7#ifdef __cplusplus
    8extern "C" {
    9#endif
    10
    11// Component types
    12typedef struct {
    13 double x, y;
    14} Position, Velocity;
    15
    16// Global variables that hold component ids
    17extern ECS_COMPONENT_DECLARE(Position);
    18extern ECS_COMPONENT_DECLARE(Velocity);
    19
    20// Module import function
    21void SimpleModuleImport(ecs_world_t *world);
    22
    23#ifdef __cplusplus
    24}
    25#endif
    26
    27#endif
    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_DECLARE(id)
    Forward declare a component.
    Definition flecs_c.h:65

    src/main.c

    1#include <simple_module.h>
    2#include <stdio.h>
    3
    4void PrintPosition(ecs_iter_t *it) {
    5 Position *p = ecs_field(it, Position, 0);
    6
    7 for (int i = 0; i < it->count; i ++) {
    8 printf("p = {%f, %f} (system)\n", p[i].x, p[i].y);
    9 }
    10}
    11
    12int main(int argc, char *argv[]) {
    13 ecs_world_t *world = ecs_init_w_args(argc, argv);
    14
    15 // Import module which calls the SimpleModuleImport function
    16 ECS_IMPORT(world, SimpleModule);
    17
    18 // Create system that uses component from module. Note how the component
    19 // identifier is prefixed with the module.
    20 ECS_SYSTEM(world, PrintPosition, EcsOnUpdate, simple.module.Position);
    21
    22 // Create entity with components imported from module
    23 ecs_entity_t e = ecs_insert(world, ecs_value(Position, {10, 20}));
    24 ecs_set(world, e, Velocity, {1, 2});
    25
    26 // Call progress which runs imported Move system
    27 ecs_progress(world, 0);
    28
    29 // Use component from module in operation
    30 const Position *p = ecs_get(world, e, Position);
    31 printf("p = {%f, %f} (get)\n", p->x, p->y);
    32
    33 return ecs_fini(world);
    34
    35 // Output:
    36 // p = {11.000000, 22.000000} (system)
    37 // p = {11.000000, 22.000000} (get)
    38}
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    #define ECS_IMPORT(world, id)
    Wrapper around ecs_import().
    Definition module.h:125
    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
    #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_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_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

    src/simple_module.c

    1#include <simple_module.h>
    2
    5
    6void Move(ecs_iter_t *it) {
    7 Position *p = ecs_field(it, Position, 0);
    8 Velocity *v = ecs_field(it, Velocity, 1);
    9
    10 for (int i = 0; i < it->count; i ++) {
    11 p[i].x += v[i].x;
    12 p[i].y += v[i].y;
    13 }
    14}
    15
    16void SimpleModuleImport(ecs_world_t *world) {
    17 // Create the module entity. The PascalCase module name is translated to a
    18 // lower case path for the entity name, like "simple.module".
    19 ECS_MODULE(world, SimpleModule);
    20
    21 // All contents of the module are created inside the module's namespace, so
    22 // the Position component will be created as simple.module.Position
    23
    24 ECS_COMPONENT_DEFINE(world, Position);
    25 ECS_COMPONENT_DEFINE(world, Velocity);
    26
    27 ECS_SYSTEM(world, Move, EcsOnUpdate, Position, Velocity);
    28}
    #define ECS_MODULE(world, id)
    Create a module.
    Definition module.h:113
    #define ECS_COMPONENT_DEFINE(world, id_)
    Define a forward-declared component.
    Definition flecs_c.h:75

  • C++

    View on GitHub

    include/simple_module.h

    1#ifndef SIMPLE_MODULE_H
    2#define SIMPLE_MODULE_H
    3
    4/* This generated file contains includes for project dependencies */
    5#include "simple_module/bake_config.h"
    6
    7#ifdef __cplusplus
    8extern "C" {
    9#endif
    10
    11namespace simple {
    12
    13struct Position {
    14 double x, y;
    15};
    16
    17struct Velocity {
    18 double x, y;
    19};
    20
    21struct module {
    22 module(flecs::world& world); // Ctor that loads the module
    23};
    24
    25}
    26
    27#ifdef __cplusplus
    28}
    29#endif
    30
    31#endif
    32
    flecs::entity module(const char *name=nullptr) const
    Define a module.

    src/main.cpp

    1#include <simple_module.h>
    2#include <iostream>
    3
    4int main(int, char *[]) {
    5 flecs::world ecs;
    6
    7 ecs.import<simple::module>();
    8
    9 // Create system that uses component from module
    10 ecs.system<const simple::Position>("PrintPosition")
    11 .each([](const simple::Position& p) {
    12 std::cout << "p = {" << p.x << ", " << p.y << "} (system)\n";
    13 });
    14
    15 // Create entity with imported components
    16 flecs::entity e = ecs.entity()
    17 .set<simple::Position>({10, 20})
    18 .set<simple::Velocity>({1, 1});
    19
    20 // Call progress which runs imported Move system
    21 ecs.progress();
    22
    23 // Use component from module in operation
    24 e.get([](const simple::Position& p) {
    25 std::cout << "p = {" << p.x << ", " << p.y << "} (get)\n";
    26 });
    27
    28 // Output:
    29 // p = {11.000000, 22.000000} (system)
    30 // p = {11.000000, 22.000000} (get)
    31}
    flecs::entity import()
    Import a module.
    Definition impl.hpp:150
    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

    src/simple_module.cpp

    1#include <simple_module.h>
    2
    3namespace simple {
    4
    5module::module(flecs::world& ecs) {
    6 // Register module with world. The module entity will be created with the
    7 // same hierarchy as the C++ namespaces (e.g. simple::module)
    8 ecs.module<module>();
    9
    10 // All contents of the module are created inside the module's namespace, so
    11 // the Position component will be created as simple::module::Position
    12
    13 // Component registration is optional, however by registering components
    14 // inside the module constructor, they will be created inside the scope
    15 // of the module.
    16 ecs.component<Position>();
    17 ecs.component<Velocity>();
    18
    19 ecs.system<Position, const Velocity>("Move")
    20 .each([](Position& p, const Velocity& v) {
    21 p.x += v.x;
    22 p.y += v.y;
    23 });
    24}
    25
    26}
    flecs::entity module(const char *name=nullptr) const
    Define a module.
    Definition impl.hpp:85
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11