Top Related Projects
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:
- 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});
- 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);
}
- 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)}
}
});
- 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:
-
Install the Flecs library:
- On Linux/macOS, you can install Flecs using a package manager like
apt-get
orbrew
. - On Windows, you can download the pre-built binaries from the project's GitHub repository.
- On Linux/macOS, you can install Flecs using a package manager like
-
Create a new C project and include the Flecs header file:
#include <flecs.h>
Competitor Comparisons
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 designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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:
- Fast and portable zero dependency C99 API
- Modern type-safe C++11 API that doesn't use STL containers
- First open source ECS with full support for Entity Relationships!
- Fast native support for hierarchies and prefabs
- Code base that builds in less than 5 seconds
- Runs in the browser without modifications with emscripten
- Cache friendly archetype/SoA storage that can process millions of entities every frame
- Automatic component registration that works out of the box across shared libraries/DLLs
- Write free functions with queries or run code automatically in systems
- Run games on multiple CPU cores with a fast lockless scheduler
- Verified on all major compilers and platforms with CI running more than 8000 tests
- Integrated reflection framework with JSON serializer and support for runtime components
- Unit annotations for components
- Powerful query language with support for joins and inheritance
- Statistics addon for profiling ECS performance
- A web-based UI for monitoring & controlling your apps:
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
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
Territory Control 2
The Forge
Extermination Shock
Tome Tumble Tournament
Sol Survivor
Equilibrium Engine
After Sun
Flecs Demo's
https://github.com/SanderMertens/tower_defense
https://github.com/flecs-hub/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.
Module | Description |
---|---|
flecs.components.cglm | Component registration for cglm (math) types |
flecs.components.input | Components that describe keyboard and mouse input |
flecs.components.transform | Components that describe position, rotation and scale |
flecs.components.physics | Components that describe physics and movement |
flecs.components.geometry | Components that describe geometry |
flecs.components.graphics | Components used for computer graphics |
flecs.components.gui | Components used to describe GUI components |
flecs.systems.transform | Hierarchical transforms for scene graphs |
flecs.systems.physics | Systems for moving objects and collision detection |
flecs.systems.sokol | Sokol-based renderer |
flecs.game | Generic 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!
Top Related Projects
Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot