Convert Figma logo to code with AI

SanderMertens logoflecs

A fast entity component system (ECS) for C & C++

6,226
440
6,226
26

Top Related Projects

9,998

Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more

Quick Overview

Flecs is a high-performance, open-source Entity-Component-System (ECS) framework written in C. It is designed to be lightweight, flexible, and easy to integrate into existing projects. Flecs provides a powerful set of tools for building complex, data-oriented applications, with a focus on performance and scalability.

Pros

  • High Performance: Flecs is designed to be highly efficient, with a focus on minimizing memory usage and maximizing CPU utilization.
  • Flexibility: Flecs is highly customizable and can be easily integrated into a wide range of projects, from game engines to simulations.
  • Ease of Use: Flecs has a simple and intuitive API, making it easy for developers to get started and build complex applications.
  • Cross-Platform: Flecs is designed to be cross-platform, with support for a variety of operating systems and architectures.

Cons

  • Steep Learning Curve: While the API is relatively simple, the ECS paradigm can be challenging for developers who are new to the concept.
  • Limited Documentation: The project's documentation, while improving, could be more comprehensive and user-friendly.
  • Lack of Tooling: Flecs does not currently have a robust ecosystem of tools and libraries, which can make it more difficult to integrate into larger projects.
  • Niche Use Case: Flecs is primarily targeted at game development and simulation applications, which may limit its appeal to developers working on other types of projects.

Code Examples

Here are a few examples of how to use Flecs:

  1. Creating an Entity and Adding Components:
ecs_world_t *world = ecs_init();
ecs_entity_t e = ecs_new(world, 0);
ecs_set(world, e, Position, {10, 20});
ecs_set(world, e, Velocity, {5, 10});
  1. Iterating over Entities with Matching Components:
ecs_query_t *q = ecs_query_new(world, "Position, Velocity");
ecs_iter_t it = ecs_query_iter(world, q);
while (ecs_query_next(&it)) {
    Position *p = ecs_get_ptr(world, it.entities[0], Position);
    Velocity *v = ecs_get_ptr(world, it.entities[0], Velocity);
    printf("Entity %d has position (%f, %f) and velocity (%f, %f)\n",
           it.entities[0], p->x, p->y, v->x, v->y);
}
  1. Defining a System:
void MovementSystem(ecs_iter_t *it) {
    Position *p = ecs_get_ptr(it, 0, Position);
    Velocity *v = ecs_get_ptr(it, 0, Velocity);
    for (int i = 0; i < it->count; i++) {
        p[i].x += v[i].x;
        p[i].y += v[i].y;
    }
}

ecs_system_init(world, &(ecs_system_desc_t) {
    .entity.name = "MovementSystem",
    .callback = MovementSystem,
    .query.filter.terms = {
        {.id = ecs_id(Position)},
        {.id = ecs_id(Velocity)}
    }
});
  1. Registering Custom Components:
typedef struct {
    float x, y;
} Position;

ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DEFINE(Position);

Getting Started

To get started with Flecs, follow these steps:

  1. Install the Flecs library:

    • On Linux/macOS, you can install Flecs using a package manager like apt-get or brew.
    • On Windows, you can download the pre-built binaries from the project's GitHub repository.
  2. Create a new C project and include the Flecs header file:

    #include <flecs.h>
    

Competitor Comparisons

9,998

Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more

Pros of EnTT

  • Header-only library, making it easy to integrate into projects
  • Highly performant, with benchmarks showing excellent runtime efficiency
  • Extensive compile-time optimizations and template metaprogramming

Cons of EnTT

  • Steeper learning curve due to heavy use of modern C++ features
  • Less built-in functionality compared to Flecs (e.g., no built-in systems or modules)

Code Comparison

EnTT:

entt::registry registry;
auto entity = registry.create();
registry.emplace<Position>(entity, 0, 0);
registry.emplace<Velocity>(entity, 1, 1);

Flecs:

ecs_world_t *world = ecs_init();
ecs_entity_t entity = ecs_new(world, 0);
ecs_set(world, entity, Position, {0, 0});
ecs_set(world, entity, Velocity, {1, 1});

Both libraries provide efficient entity creation and component assignment, but EnTT uses a more modern C++ approach with templates, while Flecs offers a C-style API that's also usable in C++.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

flecs

Version MIT Documentation actions Discord Chat

Flecs is a fast and lightweight Entity Component System that lets you build games and simulations with millions of entities (join the Discord!). Here are some of the framework's highlights:

Flecs Explorer

To support the project, give it a star 🌟 !

What is an Entity Component System?

ECS is a way of organizing code and data that lets you build games that are larger, more complex and are easier to extend. Something is called an ECS when it:

  • Has entities that uniquely identify objects in a game
  • Has components which are datatypes that can be added to entities
  • Has systems which are functions that run for all entities matching a component query

For more information, check the ECS FAQ!

Show me the code!

C99 example:

typedef struct {
  float x, y;
} Position, Velocity;

void Move(ecs_iter_t *it) {
  Position *p = ecs_field(it, Position, 0);
  Velocity *v = ecs_field(it, Velocity, 1);

  for (int i = 0; i < it->count; i++) {
    p[i].x += v[i].x;
    p[i].y += v[i].y;
  }
}

int main(int argc, char *argv[]) {
  ecs_world_t *ecs = ecs_init();

  ECS_COMPONENT(ecs, Position);
  ECS_COMPONENT(ecs, Velocity);

  ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, Velocity);

  ecs_entity_t e = ecs_insert(ecs, 
    ecs_value(Position, {10, 20}),
    ecs_value(Velocity, {1, 2}));

  while (ecs_progress(ecs, 0)) { }
}

Same example in C++11:

struct Position {
  float x, y;
};

struct Velocity {
  float x, y;
};

int main(int argc, char *argv[]) {
  flecs::world ecs;

  ecs.system<Position, const Velocity>()
    .each([](Position& p, const Velocity& v) {
      p.x += v.x;
      p.y += v.y;
    });

  auto e = ecs.entity()
    .insert([](Position& p, Velocity& v) {
      p = {10, 20};
      v = {1, 2};
    });

  while (ecs.progress()) { }
}

Projects using Flecs

If you have a project you'd like to share, let me know on Discord!

Hytale

Hytale

We knew that we wanted to build Hytale around an Entity-Component-System (ECS). When we analyzed the options, FLECS rose to the top. FLECS provides the backbone of the Hytale Game Engine. Its flexibility has allowed us to build highly varied gameplay while supporting our vision for empowering Creators.

-- Dann Webster, Hypixel studios

Tempest Rising

Tempest Rising

Territory Control 2

image

The Forge

image

Extermination Shock

image

Tome Tumble Tournament

image

Sol Survivor

image

Equilibrium Engine

image

After Sun

image

Flecs Demo's

https://github.com/SanderMertens/tower_defense Tower Defense

https://github.com/flecs-hub/city City

Flecs Hub

Flecs Hub is a collection of repositories that show how Flecs can be used to build game systems like input handling, hierarchical transforms and rendering.

ModuleDescription
flecs.components.cglmComponent registration for cglm (math) types
flecs.components.inputComponents that describe keyboard and mouse input
flecs.components.transformComponents that describe position, rotation and scale
flecs.components.physicsComponents that describe physics and movement
flecs.components.geometryComponents that describe geometry
flecs.components.graphicsComponents used for computer graphics
flecs.components.guiComponents used to describe GUI components
flecs.systems.transformHierarchical transforms for scene graphs
flecs.systems.physicsSystems for moving objects and collision detection
flecs.systems.sokolSokol-based renderer
flecs.gameGeneric game systems, like a camera controller

Language bindings

The following language bindings have been developed with Flecs! Note that these are projects built and maintained by helpful community members, and may not always be up to date with the latest commit from master!